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

View file

@ -2,9 +2,12 @@
Built at {build-date} Built at {build-date}
Usage: 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. --help Shows this help message.
--version Shows the version information. --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 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.Configuration;
using System.Drawing.Text; using System.Drawing.Text;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Text;
namespace SpritePacker namespace SpritePacker
{ {
@ -29,7 +30,7 @@ namespace SpritePacker
/// <param name="sprite">Sprite.</param> /// <param name="sprite">Sprite.</param>
public void Add(Sprite sprite) public void Add(Sprite sprite)
{ {
Console.WriteLine("Adding {0}.", sprite); if(Verbose) Console.WriteLine("Adding {0}.", sprite);
sprites.Add(sprite); sprites.Add(sprite);
} }
/// <summary> /// <summary>
@ -97,12 +98,12 @@ namespace SpritePacker
// move the next scan line up a bit. // 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. // 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) 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); if(Verbose) Console.WriteLine("Found rightmost problem: {0}", rightProblem);
// Move up to the position furthest to the right // Move up to the position furthest to the right
cspr.X = rightProblem.Right + 1; cspr.X = rightProblem.Right; // NOTE: Add one here?
} }
if (!foundPosition) if (!foundPosition)
@ -132,12 +133,12 @@ namespace SpritePacker
// move the enxt scan line up to meet it. // 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. // 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) 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); if(Verbose) Console.WriteLine("Found downProblem {0}", downProblem);
// Move up to the position furthest downwards // 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(); 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> /// <summary>
/// Sorts the sprites by size. /// Sorts the sprites by size.
/// </summary> /// </summary>