Added CSV output format.

This commit is contained in:
Starbeamrainbowlabs 2016-05-21 14:35:24 +01:00
parent 1fb42d55d5
commit 345dd1651c
3 changed files with 47 additions and 8 deletions

View file

@ -11,7 +11,13 @@ namespace SpritePacker
Normal,
DisplayHelpText,
DisplayVersionText
};
}
enum OutputFormat
{
Text,
CSV
}
class Program
{
@ -25,6 +31,7 @@ namespace SpritePacker
private static SpritePacker spritePacker;
private static OutputFormat outputFormat = OutputFormat.Text;
private static string outputFilename = "spritesheet.png";
private static List<string> values = new List<string>();
@ -48,6 +55,9 @@ namespace SpritePacker
case "--verbose":
Verbose = true;
break;
case "--csv":
outputFormat = OutputFormat.CSV;
break;
default:
if(args[i].StartsWith("-"))
{
@ -98,8 +108,19 @@ namespace SpritePacker
spritePacker = new SpritePacker(Verbose);
addSprites(values);
spritePacker.Arrange();
Console.WriteLine(spritePacker.ToString());
spritePacker.Output(outputFilename);
switch(outputFormat)
{
case OutputFormat.CSV:
Console.WriteLine(spritePacker.GetSpritePositionsCSV().Trim());
break;
case OutputFormat.Text:
Console.WriteLine(spritePacker.ToString());
break;
default:
Console.Error.WriteLine("Error: Invalid output format!");
break;
}
}
private static void addSprites(List<string> filenames)

View file

@ -2,9 +2,12 @@
Built at {build-date}
Usage:
mono ./SpritePacker.exe [flags] /path/to/output/file.png [filenames]
[mono ]./SpritePacker.exe [flags] /path/to/output/file.png [filenames]
Option Meaning
-------------------------------------------------------------------------------
--help Shows this help message.
--version Shows the version information.
--csv Outputs the final sprite positions in CSV.
More information can be found at https://git.starbeamrainbowlabs.com/sbrl/SpritePacker

View file

@ -6,6 +6,7 @@ using System.IO;
using System.Configuration;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Text;
namespace SpritePacker
{
@ -29,7 +30,7 @@ namespace SpritePacker
/// <param name="sprite">Sprite.</param>
public void Add(Sprite sprite)
{
Console.WriteLine("Adding {0}.", sprite);
if(Verbose) Console.WriteLine("Adding {0}.", sprite);
sprites.Add(sprite);
}
/// <summary>
@ -97,12 +98,12 @@ namespace SpritePacker
// move the next scan line up a bit.
// Also make sure that the next scan line and the current scan line don't touch or cross.
if (probSpr.Bottom < nextScanLines.Y && probSpr.Bottom > scanLines.Y)
nextScanLines.Y = probSpr.Bottom + 1;
nextScanLines.Y = probSpr.Bottom; // NOTE: Add one here?
}
if(Verbose) Console.WriteLine("Found rightmost problem: {0}", rightProblem);
// Move up to the position furthest to the right
cspr.X = rightProblem.Right + 1;
cspr.X = rightProblem.Right; // NOTE: Add one here?
}
if (!foundPosition)
@ -132,12 +133,12 @@ namespace SpritePacker
// move the enxt scan line up to meet it.
// Also make sure that the next scan line and the current scan line don't touch or cross.
if (probSpr.Right < nextScanLines.X && probSpr.Right > scanLines.X)
nextScanLines.X = probSpr.Right + 1;
nextScanLines.X = probSpr.Right; // NOTE: Add one here?
}
if(Verbose) Console.WriteLine("Found downProblem {0}", downProblem);
// Move up to the position furthest downwards
cspr.Y = downProblem.Bottom + 1;
cspr.Y = downProblem.Bottom; // NOTE: Add one here?
}
}
@ -200,6 +201,20 @@ namespace SpritePacker
finalImage.Dispose();
}
public string GetSpritePositionsCSV(bool header = true)
{
StringWriter result = new StringWriter();
if (header)
result.WriteLine("index,filename,x,y,width,height");
int i = 0;
foreach(Sprite spr in sprites)
{
result.WriteLine("{0},{1},{2},{3},{4},{5}", new object[] { i, spr.Filename, spr.X, spr.Y, spr.Width, spr.Height });
i++;
}
return result.ToString();
}
/// <summary>
/// Sorts the sprites by size.
/// </summary>