Compare commits

...

2 commits

6 changed files with 337 additions and 18 deletions

19
.gitignore vendored
View file

@ -2,18 +2,15 @@
# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
[Bb]in/
[Oo]bj/
# mstest test results
TestResults
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.userprefs
*.sln.docstates
# Build results
[Dd]ebug/
[Rr]elease/
@ -37,32 +34,25 @@ x64/
*.vspscc
*.vssscc
.builds
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
# Visual Studio profiler
*.psess
*.vsp
*.vspx
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*
# NCrunch
*.ncrunch*
.*crunch*.local.xml
# Installshield output folder
[Ee]xpress
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
@ -72,23 +62,17 @@ DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish
# Publish Web Output
*.Publish.xml
# NuGet Packages Directory
packages
# Windows Azure Build Output
csx
*.build.csdef
# Windows Store app package directory
AppPackages/
# Others
[Bb]in
[Oo]bj
@ -101,7 +85,6 @@ ClientBin
~$*
*.dbmdl
Generated_Code #added for RIA/Silverlight projects
# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/

17
SpritePacker.sln Normal file
View file

@ -0,0 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpritePacker", "SpritePacker\SpritePacker.csproj", "{6EF47B64-1920-4827-BEEF-B262D5A2D214}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Debug|x86.ActiveCfg = Debug|x86
{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Debug|x86.Build.0 = Debug|x86
{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Release|x86.ActiveCfg = Release|x86
{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Release|x86.Build.0 = Release|x86
EndGlobalSection
EndGlobal

238
SpritePacker/Packer.cs Normal file
View file

@ -0,0 +1,238 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Security.Cryptography;
using System.IO;
using System.Configuration;
using System.Drawing.Text;
namespace SpritePacker
{
public class Packer
{
private List<Sprite> sprites = new List<Sprite>();
public Packer()
{
}
public void Add(Sprite sprite)
{
sprites.Add(sprite);
}
public bool Remove(Sprite sprite)
{
return sprites.Remove(sprite);
}
public void Arrange()
{
List<Sprite> arrangedSprites = new List<Sprite>();
foreach(Sprite cspr in sprites)
{
Point scanLines = Point.Empty;
Point nextScanLines = scanLines;
while(true)
{
if (!cspr.IntersectsWith(arrangedSprites))
break;
// Scan along the X axis
cspr.Area.X = 0;
cspr.Area.Y = scanLines.Y;
bool foundPosition = false;
while(cspr.Area.X < scanLines.X)
{
if (!cspr.IntersectsWith(arrangedSprites))
{
foundPosition = true;
break;
}
// Get the edge furthest to the right
List<Sprite> problems = cspr.GetIntersectors(arrangedSprites);
Sprite rightProblem = problems[0];
foreach (Sprite probSpr in problems)
if (probSpr.Area.Right > rightProblem.Area.Right)
rightProblem = probSpr;
// Move up to the position furthest to the right
// NOTE: We may need to add one here.
cspr.Area.X = furthestRightPos + 1;
}
if (!foundPosition)
{
// We didn't find anything along the x axis - let's scan the y axis next
cspr.Area.X = scanLines.X;
cspr.Area.Y = 0;
while (cspr.Area.Y < scanLines.Y)
{
if (!cspr.IntersectsWith(arrangedSprites))
{
foundPosition = true;
break;
}
// Get the edge furthest downwards
List<Sprite> problems = cspr.GetIntersectors(arrangedSprites);
Sprite downProblem = problems[0];
foreach (Sprite probSpr in problems)
if (probSpr.Area.Bottom > downProblem.Area.Bottom)
downProblem = probSpr;
// Move up to the position furthest downwards
cspr.Area.Y = downProblem.Area.Bottom + 1;
}
}
// If we found a new position, then we don't need to move the scan lines up and try again
if (foundPosition)
break;
}
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);
}
}
}
}
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;
}
}
}

12
SpritePacker/Program.cs Normal file
View file

@ -0,0 +1,12 @@
using System;
namespace SpritePacker
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

View file

@ -0,0 +1,27 @@
using System.Reflection;
using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("SpritePacker")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("Starbeamrainbowlabs")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProjectGuid>{6EF47B64-1920-4827-BEEF-B262D5A2D214}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>SpritePacker</RootNamespace>
<AssemblyName>SpritePacker</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Drawing" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Packer.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>