Finish implementing packer, and write simple interface.
Unfortunately we will have to re-implement the Rectangle struct because we can't modify a value type apparently.
This commit is contained in:
parent
7e4f7c1a61
commit
d19df5b1b9
4 changed files with 189 additions and 132 deletions
|
@ -32,7 +32,7 @@ namespace SpritePacker
|
|||
foreach(Sprite cspr in sprites)
|
||||
{
|
||||
Point scanLines = Point.Empty;
|
||||
Point nextScanLines = scanLines;
|
||||
Point nextScanLines = new Point(int.MaxValue, int.MaxValue);
|
||||
while(true)
|
||||
{
|
||||
if (!cspr.IntersectsWith(arrangedSprites))
|
||||
|
@ -55,12 +55,16 @@ namespace SpritePacker
|
|||
List<Sprite> problems = cspr.GetIntersectors(arrangedSprites);
|
||||
Sprite rightProblem = problems[0];
|
||||
foreach (Sprite probSpr in problems)
|
||||
{
|
||||
if (probSpr.Area.Right > rightProblem.Area.Right)
|
||||
rightProblem = probSpr;
|
||||
if (probSpr.Area.Top < nextScanLines.Y)
|
||||
nextScanLines.Y = probSpr.Area.Top + 1;
|
||||
}
|
||||
|
||||
// Move up to the position furthest to the right
|
||||
// NOTE: We may need to add one here.
|
||||
cspr.Area.X = furthestRightPos + 1;
|
||||
cspr.Area.X = rightProblem.Area.Right + 1;
|
||||
|
||||
}
|
||||
|
||||
|
@ -81,9 +85,12 @@ namespace SpritePacker
|
|||
List<Sprite> problems = cspr.GetIntersectors(arrangedSprites);
|
||||
Sprite downProblem = problems[0];
|
||||
foreach (Sprite probSpr in problems)
|
||||
{
|
||||
if (probSpr.Area.Bottom > downProblem.Area.Bottom)
|
||||
downProblem = probSpr;
|
||||
|
||||
if (probSpr.Area.Left < nextScanLines.X)
|
||||
nextScanLines.X = probSpr.Area.Left + 1;
|
||||
}
|
||||
// Move up to the position furthest downwards
|
||||
cspr.Area.Y = downProblem.Area.Bottom + 1;
|
||||
}
|
||||
|
@ -93,146 +100,38 @@ namespace SpritePacker
|
|||
if (foundPosition)
|
||||
break;
|
||||
|
||||
// Make sure that the next scan lines are sane
|
||||
if (nextScanLines.X == int.MaxValue)
|
||||
nextScanLines.X = scanLines.X;
|
||||
if (nextScanLines.Y == int.MaxValue)
|
||||
nextScanLines.Y = scanLines.Y;
|
||||
|
||||
// If the next scan lines and the current scan lines are identical,
|
||||
// then something is very wrong
|
||||
if(nextScanLines.Equals(scanLines))
|
||||
throw new Exception("Failed to find the next set of lines to scan!");
|
||||
|
||||
// Move the scan lines up to the next nearest ones we've found
|
||||
scanLines = nextScanLines;
|
||||
nextScanLines = new Point(int.MaxValue, int.MaxValue);
|
||||
}
|
||||
if (cspr.IntersectsWith(arrangedSprites))
|
||||
{
|
||||
// We have a conflict! Let's move it to try and sort this out.
|
||||
List<Sprite> problemSprites = cspr.GetIntersectors(arrangedSprites);
|
||||
// Find the problem sprite closest to (0, 0)
|
||||
Sprite mainProblem = problemSprites[0];
|
||||
foreach (Sprite pSpr in problemSprites) {
|
||||
if ((pSpr.Area.X < mainProblem.Area.X && pSpr.Area.Y <= mainProblem.Area.Y) ||
|
||||
(pSpr.Area.Y < mainProblem.Area.Y && pSpr.Area.X <= mainProblem.Area.X))
|
||||
mainProblem = pSpr;
|
||||
}
|
||||
|
||||
Point scanBox = new Point(mainProblem.Area.X, mainProblem.Area.Y);
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
arrangedSprites.Add(cspr);
|
||||
}
|
||||
arrangedSprites.Add(cspr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Sprite
|
||||
{
|
||||
private Rectangle area;
|
||||
private string filename;
|
||||
|
||||
public Rectangle Area
|
||||
{
|
||||
get { return area; }
|
||||
set { area = value; }
|
||||
}
|
||||
public string Filename
|
||||
{
|
||||
get { return filename; }
|
||||
set { filename = value; }
|
||||
// We don't need to copy the list of arranged sprites across to the main list here
|
||||
// because Sprite is a class and classes are passed by _reference_.
|
||||
}
|
||||
|
||||
public Sprite(string inFilename)
|
||||
public override string ToString()
|
||||
{
|
||||
Filename = inFilename;
|
||||
// TODO: Fill in the area automagically based on the given image
|
||||
throw new NotImplementedException("Todo: Fill in the area automagically based on the given image");
|
||||
}
|
||||
string result = string.Format("SpritePacker:");
|
||||
|
||||
public static int GetLargestSize(List<Sprite> sprList)
|
||||
{
|
||||
int largestSoFar = 0;
|
||||
foreach(Sprite spr in sprList)
|
||||
{
|
||||
if (spr.Area.Width > largestSoFar)
|
||||
largestSoFar = spr.Area.Width;
|
||||
}
|
||||
}
|
||||
foreach (Sprite spr in sprites)
|
||||
result += string.Format("\t{0}\n", spr);
|
||||
|
||||
public List<Sprite> GetIntersectors(List<Sprite> spriteList)
|
||||
{
|
||||
List<Sprite> result = new List<Sprite>();
|
||||
foreach(Sprite spr in spriteList)
|
||||
{
|
||||
if (spr.IntersectsWith(this))
|
||||
result.Add(spr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public List<Sprite> GetIntersectorsX(List<Sprite> spriteList)
|
||||
{
|
||||
List<Sprite> result = new List<Sprite>();
|
||||
foreach(Sprite spr in spriteList)
|
||||
{
|
||||
if (spr.IntersectsWithX(this))
|
||||
result.Add(spr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public List<Sprite> GetIntersectorsY(List<Sprite> spriteList)
|
||||
{
|
||||
List<Sprite> result = new List<Sprite>();
|
||||
foreach(Sprite spr in spriteList)
|
||||
{
|
||||
if (spr.IntersectsWithY(this))
|
||||
result.Add(spr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool IntersectsWith(List<Sprite> otherSprites)
|
||||
{
|
||||
foreach (Sprite spr in otherSprites)
|
||||
{
|
||||
if (IntersectsWith(spr))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public bool IntersectsWith(Sprite otherSprite)
|
||||
{
|
||||
return otherSprite.Area.IntersectsWith(Area);
|
||||
}
|
||||
|
||||
public bool IntersectsWithX(List<Sprite> otherSprites)
|
||||
{
|
||||
foreach (Sprite spr in otherSprites)
|
||||
{
|
||||
if (IntersectsWithX(spr))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public bool IntersectsWithY(List<Sprite> otherSprites)
|
||||
{
|
||||
foreach (Sprite spr in otherSprites)
|
||||
{
|
||||
if (IntersectsWithY(spr))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IntersectsWithX(Sprite otherSprite)
|
||||
{
|
||||
if(Area.Right > otherSprite.Area.X &&
|
||||
Area.X < otherSprite.Area.Right)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
public bool IntersectsWithY(Sprite otherSprite)
|
||||
{
|
||||
if(Area.Bottom > otherSprite.Area.Y &&
|
||||
Area.Y < otherSprite.Area.Bottom)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Markup;
|
||||
using System.IO;
|
||||
|
||||
namespace SpritePacker
|
||||
{
|
||||
|
@ -6,7 +9,34 @@ namespace SpritePacker
|
|||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello World!");
|
||||
List<string> values = new List<string>();
|
||||
for(int i = 0; i < args.Length; i++)
|
||||
{
|
||||
switch(args[i])
|
||||
{
|
||||
case "--help":
|
||||
Console.WriteLine("Help text coming soon!");
|
||||
return;
|
||||
break;
|
||||
case "--version":
|
||||
Console.WriteLine("Version text coming soon!");
|
||||
break;
|
||||
default:
|
||||
values.Add(args[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Packer spritePacker = new Packer();
|
||||
foreach(string filename in values)
|
||||
{
|
||||
if (File.Exists(filename))
|
||||
spritePacker.Add(new Sprite(filename));
|
||||
else
|
||||
Console.Error.WriteLine("Warning: Ignoring non-existent file '{0}'.", filename);
|
||||
}
|
||||
spritePacker.Arrange();
|
||||
Console.WriteLine(spritePacker.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
127
SpritePacker/Sprite.cs
Normal file
127
SpritePacker/Sprite.cs
Normal file
|
@ -0,0 +1,127 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
|
||||
namespace SpritePacker
|
||||
{
|
||||
public class Sprite
|
||||
{
|
||||
private Rectangle area;
|
||||
private string filename;
|
||||
|
||||
public Rectangle Area
|
||||
{
|
||||
get { return area; }
|
||||
set { area = value; }
|
||||
}
|
||||
public string Filename
|
||||
{
|
||||
get { return filename; }
|
||||
set { filename = value; }
|
||||
}
|
||||
|
||||
public Sprite(string inFilename)
|
||||
{
|
||||
Filename = inFilename;
|
||||
// TODO: Fill in the area automagically based on the given image
|
||||
throw new NotImplementedException("Todo: Fill in the area automagically based on the given image");
|
||||
}
|
||||
|
||||
public static int GetLargestSize(List<Sprite> sprList)
|
||||
{
|
||||
int largestSoFar = 0;
|
||||
foreach(Sprite spr in sprList)
|
||||
{
|
||||
if (spr.Area.Width > largestSoFar)
|
||||
largestSoFar = spr.Area.Width;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Sprite> GetIntersectors(List<Sprite> spriteList)
|
||||
{
|
||||
List<Sprite> result = new List<Sprite>();
|
||||
foreach(Sprite spr in spriteList)
|
||||
{
|
||||
if (spr.IntersectsWith(this))
|
||||
result.Add(spr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public List<Sprite> GetIntersectorsX(List<Sprite> spriteList)
|
||||
{
|
||||
List<Sprite> result = new List<Sprite>();
|
||||
foreach(Sprite spr in spriteList)
|
||||
{
|
||||
if (spr.IntersectsWithX(this))
|
||||
result.Add(spr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public List<Sprite> GetIntersectorsY(List<Sprite> spriteList)
|
||||
{
|
||||
List<Sprite> result = new List<Sprite>();
|
||||
foreach(Sprite spr in spriteList)
|
||||
{
|
||||
if (spr.IntersectsWithY(this))
|
||||
result.Add(spr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool IntersectsWith(List<Sprite> otherSprites)
|
||||
{
|
||||
foreach (Sprite spr in otherSprites)
|
||||
{
|
||||
if (IntersectsWith(spr))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public bool IntersectsWith(Sprite otherSprite)
|
||||
{
|
||||
return otherSprite.Area.IntersectsWith(Area);
|
||||
}
|
||||
|
||||
public bool IntersectsWithX(List<Sprite> otherSprites)
|
||||
{
|
||||
foreach (Sprite spr in otherSprites)
|
||||
{
|
||||
if (IntersectsWithX(spr))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public bool IntersectsWithY(List<Sprite> otherSprites)
|
||||
{
|
||||
foreach (Sprite spr in otherSprites)
|
||||
{
|
||||
if (IntersectsWithY(spr))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IntersectsWithX(Sprite otherSprite)
|
||||
{
|
||||
if(Area.Right > otherSprite.Area.X &&
|
||||
Area.X < otherSprite.Area.Right)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
public bool IntersectsWithY(Sprite otherSprite)
|
||||
{
|
||||
if(Area.Bottom > otherSprite.Area.Y &&
|
||||
Area.Y < otherSprite.Area.Bottom)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("Sprite {0} at {1}", Filename, Area);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -37,6 +37,7 @@
|
|||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Packer.cs" />
|
||||
<Compile Include="Sprite.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
Loading…
Reference in a new issue