From 345dd1651c971a374ee3bb16a85061b91b6ab59e Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 21 May 2016 14:35:24 +0100 Subject: [PATCH] Added CSV output format. --- SpritePacker/Program.cs | 25 +++++++++++++++++++++++-- SpritePacker/Resources/HelpText.txt | 5 ++++- SpritePacker/SpritePacker.cs | 25 ++++++++++++++++++++----- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/SpritePacker/Program.cs b/SpritePacker/Program.cs index 61da4c2..12feee4 100644 --- a/SpritePacker/Program.cs +++ b/SpritePacker/Program.cs @@ -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 values = new List(); @@ -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 filenames) diff --git a/SpritePacker/Resources/HelpText.txt b/SpritePacker/Resources/HelpText.txt index 221f5ba..997acec 100644 --- a/SpritePacker/Resources/HelpText.txt +++ b/SpritePacker/Resources/HelpText.txt @@ -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 diff --git a/SpritePacker/SpritePacker.cs b/SpritePacker/SpritePacker.cs index 91e9593..0ca2085 100644 --- a/SpritePacker/SpritePacker.cs +++ b/SpritePacker/SpritePacker.cs @@ -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 /// Sprite. public void Add(Sprite sprite) { - Console.WriteLine("Adding {0}.", sprite); + if(Verbose) Console.WriteLine("Adding {0}.", sprite); sprites.Add(sprite); } /// @@ -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(); + } + /// /// Sorts the sprites by size. ///