Browse Source

Begin homegrown algorithm implementation

tags/v0.1
Starbeamrainbowlabs 3 years ago
commit
57b89d3c59

+ 256
- 0
.gitignore View File

@@ -0,0 +1,256 @@
1
+
2
+# Created by https://www.gitignore.io/api/visualstudio
3
+
4
+### VisualStudio ###
5
+## Ignore Visual Studio temporary files, build results, and
6
+## files generated by popular Visual Studio add-ons.
7
+
8
+# User-specific files
9
+*.suo
10
+*.user
11
+*.userosscache
12
+*.sln.docstates
13
+
14
+# User-specific files (MonoDevelop/Xamarin Studio)
15
+*.userprefs
16
+
17
+# Build results
18
+[Dd]ebug/
19
+[Dd]ebugPublic/
20
+[Rr]elease/
21
+[Rr]eleases/
22
+x64/
23
+x86/
24
+bld/
25
+[Bb]in/
26
+[Oo]bj/
27
+[Ll]og/
28
+
29
+# Visual Studio 2015 cache/options directory
30
+.vs/
31
+# Uncomment if you have tasks that create the project's static files in wwwroot
32
+#wwwroot/
33
+
34
+# MSTest test Results
35
+[Tt]est[Rr]esult*/
36
+[Bb]uild[Ll]og.*
37
+
38
+# NUNIT
39
+*.VisualState.xml
40
+TestResult.xml
41
+
42
+# Build Results of an ATL Project
43
+[Dd]ebugPS/
44
+[Rr]eleasePS/
45
+dlldata.c
46
+
47
+# DNX
48
+project.lock.json
49
+artifacts/
50
+
51
+*_i.c
52
+*_p.c
53
+*_i.h
54
+*.ilk
55
+*.meta
56
+*.obj
57
+*.pch
58
+*.pdb
59
+*.pgc
60
+*.pgd
61
+*.rsp
62
+*.sbr
63
+*.tlb
64
+*.tli
65
+*.tlh
66
+*.tmp
67
+*.tmp_proj
68
+*.log
69
+*.vspscc
70
+*.vssscc
71
+.builds
72
+*.pidb
73
+*.svclog
74
+*.scc
75
+
76
+# Chutzpah Test files
77
+_Chutzpah*
78
+
79
+# Visual C++ cache files
80
+ipch/
81
+*.aps
82
+*.ncb
83
+*.opendb
84
+*.opensdf
85
+*.sdf
86
+*.cachefile
87
+*.VC.db
88
+*.VC.VC.opendb
89
+
90
+# Visual Studio profiler
91
+*.psess
92
+*.vsp
93
+*.vspx
94
+*.sap
95
+
96
+# TFS 2012 Local Workspace
97
+$tf/
98
+
99
+# Guidance Automation Toolkit
100
+*.gpState
101
+
102
+# ReSharper is a .NET coding add-in
103
+_ReSharper*/
104
+*.[Rr]e[Ss]harper
105
+*.DotSettings.user
106
+
107
+# JustCode is a .NET coding add-in
108
+.JustCode
109
+
110
+# TeamCity is a build add-in
111
+_TeamCity*
112
+
113
+# DotCover is a Code Coverage Tool
114
+*.dotCover
115
+
116
+# NCrunch
117
+_NCrunch_*
118
+.*crunch*.local.xml
119
+nCrunchTemp_*
120
+
121
+# MightyMoose
122
+*.mm.*
123
+AutoTest.Net/
124
+
125
+# Web workbench (sass)
126
+.sass-cache/
127
+
128
+# Installshield output folder
129
+[Ee]xpress/
130
+
131
+# DocProject is a documentation generator add-in
132
+DocProject/buildhelp/
133
+DocProject/Help/*.HxT
134
+DocProject/Help/*.HxC
135
+DocProject/Help/*.hhc
136
+DocProject/Help/*.hhk
137
+DocProject/Help/*.hhp
138
+DocProject/Help/Html2
139
+DocProject/Help/html
140
+
141
+# Click-Once directory
142
+publish/
143
+
144
+# Publish Web Output
145
+*.[Pp]ublish.xml
146
+*.azurePubxml
147
+# TODO: Comment the next line if you want to checkin your web deploy settings
148
+# but database connection strings (with potential passwords) will be unencrypted
149
+*.pubxml
150
+*.publishproj
151
+
152
+# Microsoft Azure Web App publish settings. Comment the next line if you want to
153
+# checkin your Azure Web App publish settings, but sensitive information contained
154
+# in these scripts will be unencrypted
155
+PublishScripts/
156
+
157
+# NuGet Packages
158
+*.nupkg
159
+# The packages folder can be ignored because of Package Restore
160
+**/packages/*
161
+# except build/, which is used as an MSBuild target.
162
+!**/packages/build/
163
+# Uncomment if necessary however generally it will be regenerated when needed
164
+#!**/packages/repositories.config
165
+# NuGet v3's project.json files produces more ignoreable files
166
+*.nuget.props
167
+*.nuget.targets
168
+
169
+# Microsoft Azure Build Output
170
+csx/
171
+*.build.csdef
172
+
173
+# Microsoft Azure Emulator
174
+ecf/
175
+rcf/
176
+
177
+# Windows Store app package directories and files
178
+AppPackages/
179
+BundleArtifacts/
180
+Package.StoreAssociation.xml
181
+_pkginfo.txt
182
+
183
+# Visual Studio cache files
184
+# files ending in .cache can be ignored
185
+*.[Cc]ache
186
+# but keep track of directories ending in .cache
187
+!*.[Cc]ache/
188
+
189
+# Others
190
+ClientBin/
191
+~$*
192
+*~
193
+*.dbmdl
194
+*.dbproj.schemaview
195
+*.pfx
196
+*.publishsettings
197
+node_modules/
198
+orleans.codegen.cs
199
+
200
+# Since there are multiple workflows, uncomment next line to ignore bower_components
201
+# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
202
+#bower_components/
203
+
204
+# RIA/Silverlight projects
205
+Generated_Code/
206
+
207
+# Backup & report files from converting an old project file
208
+# to a newer Visual Studio version. Backup files are not needed,
209
+# because we have git ;-)
210
+_UpgradeReport_Files/
211
+Backup*/
212
+UpgradeLog*.XML
213
+UpgradeLog*.htm
214
+
215
+# SQL Server files
216
+*.mdf
217
+*.ldf
218
+
219
+# Business Intelligence projects
220
+*.rdl.data
221
+*.bim.layout
222
+*.bim_*.settings
223
+
224
+# Microsoft Fakes
225
+FakesAssemblies/
226
+
227
+# GhostDoc plugin setting file
228
+*.GhostDoc.xml
229
+
230
+# Node.js Tools for Visual Studio
231
+.ntvs_analysis.dat
232
+
233
+# Visual Studio 6 build log
234
+*.plg
235
+
236
+# Visual Studio 6 workspace options file
237
+*.opt
238
+
239
+# Visual Studio LightSwitch build output
240
+**/*.HTMLClient/GeneratedArtifacts
241
+**/*.DesktopClient/GeneratedArtifacts
242
+**/*.DesktopClient/ModelManifest.xml
243
+**/*.Server/GeneratedArtifacts
244
+**/*.Server/ModelManifest.xml
245
+_Pvt_Extensions
246
+
247
+# Paket dependency manager
248
+.paket/paket.exe
249
+paket-files/
250
+
251
+# FAKE - F# Make
252
+.fake/
253
+
254
+# JetBrains Rider
255
+.idea/
256
+*.sln.iml

+ 17
- 0
SpritePacker.sln View File

@@ -0,0 +1,17 @@
1
+
2
+Microsoft Visual Studio Solution File, Format Version 12.00
3
+# Visual Studio 2012
4
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpritePacker", "SpritePacker\SpritePacker.csproj", "{6EF47B64-1920-4827-BEEF-B262D5A2D214}"
5
+EndProject
6
+Global
7
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
8
+		Debug|x86 = Debug|x86
9
+		Release|x86 = Release|x86
10
+	EndGlobalSection
11
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
12
+		{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Debug|x86.ActiveCfg = Debug|x86
13
+		{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Debug|x86.Build.0 = Debug|x86
14
+		{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Release|x86.ActiveCfg = Release|x86
15
+		{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Release|x86.Build.0 = Release|x86
16
+	EndGlobalSection
17
+EndGlobal

+ 238
- 0
SpritePacker/Packer.cs View File

@@ -0,0 +1,238 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Drawing;
4
+using System.Security.Cryptography;
5
+using System.IO;
6
+using System.Configuration;
7
+using System.Drawing.Text;
8
+
9
+namespace SpritePacker
10
+{
11
+	public class Packer
12
+	{
13
+		private List<Sprite> sprites = new List<Sprite>();
14
+
15
+		public Packer()
16
+		{
17
+			
18
+		}
19
+
20
+		public void Add(Sprite sprite)
21
+		{
22
+			sprites.Add(sprite);
23
+		}
24
+		public bool Remove(Sprite sprite)
25
+		{
26
+			return sprites.Remove(sprite);
27
+		}
28
+
29
+		public void Arrange()
30
+		{
31
+			List<Sprite> arrangedSprites = new List<Sprite>();
32
+			foreach(Sprite cspr in sprites)
33
+			{
34
+				Point scanLines = Point.Empty;
35
+				Point nextScanLines = scanLines;
36
+				while(true)
37
+				{
38
+					if (!cspr.IntersectsWith(arrangedSprites))
39
+						break;
40
+
41
+					// Scan along the X axis
42
+					cspr.Area.X = 0;
43
+					cspr.Area.Y = scanLines.Y;
44
+
45
+					bool foundPosition = false;
46
+					while(cspr.Area.X < scanLines.X)
47
+					{
48
+						if (!cspr.IntersectsWith(arrangedSprites))
49
+						{
50
+							foundPosition = true;
51
+							break;
52
+						}
53
+
54
+						// Get the edge furthest to the right
55
+						List<Sprite> problems = cspr.GetIntersectors(arrangedSprites);
56
+						Sprite rightProblem = problems[0];
57
+						foreach (Sprite probSpr in problems)
58
+							if (probSpr.Area.Right > rightProblem.Area.Right)
59
+								rightProblem = probSpr;
60
+
61
+						// Move up to the position furthest to the right
62
+						// NOTE: We may need to add one here.
63
+						cspr.Area.X = furthestRightPos + 1;
64
+
65
+					}
66
+
67
+					if (!foundPosition)
68
+					{
69
+						// We didn't find anything along the x axis - let's scan the y axis next
70
+						cspr.Area.X = scanLines.X;
71
+						cspr.Area.Y = 0;
72
+						while (cspr.Area.Y < scanLines.Y)
73
+						{
74
+							if (!cspr.IntersectsWith(arrangedSprites))
75
+							{
76
+								foundPosition = true;
77
+								break;
78
+							}
79
+
80
+							// Get the edge furthest downwards
81
+							List<Sprite> problems = cspr.GetIntersectors(arrangedSprites);
82
+							Sprite downProblem = problems[0];
83
+							foreach (Sprite probSpr in problems)
84
+								if (probSpr.Area.Bottom > downProblem.Area.Bottom)
85
+									downProblem = probSpr;
86
+
87
+							// Move up to the position furthest downwards
88
+							cspr.Area.Y = downProblem.Area.Bottom + 1;
89
+						}
90
+					}
91
+
92
+					// If we found a new position, then we don't need to move the scan lines up and try again
93
+					if (foundPosition)
94
+						break;
95
+
96
+
97
+				}
98
+				if (cspr.IntersectsWith(arrangedSprites))
99
+				{
100
+					// We have a conflict! Let's move it to try and sort this out.
101
+					List<Sprite> problemSprites = cspr.GetIntersectors(arrangedSprites);
102
+					// Find the problem sprite closest to (0, 0)
103
+					Sprite mainProblem = problemSprites[0];
104
+					foreach (Sprite pSpr in problemSprites) {
105
+						if ((pSpr.Area.X < mainProblem.Area.X && pSpr.Area.Y <= mainProblem.Area.Y) ||
106
+						    (pSpr.Area.Y < mainProblem.Area.Y && pSpr.Area.X <= mainProblem.Area.X))
107
+							mainProblem = pSpr;
108
+					}
109
+
110
+					Point scanBox = new Point(mainProblem.Area.X, mainProblem.Area.Y);
111
+
112
+
113
+				}
114
+				else
115
+				{
116
+					arrangedSprites.Add(cspr);
117
+				}
118
+			}
119
+		}
120
+	}
121
+
122
+	public class Sprite 
123
+	{
124
+		private Rectangle area;
125
+		private string filename;
126
+
127
+		public Rectangle Area
128
+		{
129
+			get { return area; }
130
+			set { area = value; }
131
+		}
132
+		public string Filename
133
+		{
134
+			get { return filename; }
135
+			set { filename = value; }
136
+		}
137
+
138
+		public Sprite(string inFilename)
139
+		{
140
+			Filename = inFilename;
141
+			// TODO: Fill in the area automagically based on the given image
142
+			throw new NotImplementedException("Todo: Fill in the area automagically based on the given image");
143
+		}
144
+
145
+		public static int GetLargestSize(List<Sprite> sprList)
146
+		{
147
+			int largestSoFar = 0;
148
+			foreach(Sprite spr in sprList)
149
+			{
150
+				if (spr.Area.Width > largestSoFar)
151
+					largestSoFar = spr.Area.Width;
152
+			}
153
+		}
154
+
155
+		public List<Sprite> GetIntersectors(List<Sprite> spriteList)
156
+		{
157
+			List<Sprite> result = new List<Sprite>();
158
+			foreach(Sprite spr in spriteList)
159
+			{
160
+				if (spr.IntersectsWith(this))
161
+					result.Add(spr);
162
+			}
163
+			return result;
164
+		}
165
+		public List<Sprite> GetIntersectorsX(List<Sprite> spriteList)
166
+		{
167
+			List<Sprite> result = new List<Sprite>();
168
+			foreach(Sprite spr in spriteList)
169
+			{
170
+				if (spr.IntersectsWithX(this))
171
+					result.Add(spr);
172
+			}
173
+			return result;
174
+		}
175
+		public List<Sprite> GetIntersectorsY(List<Sprite> spriteList)
176
+		{
177
+			List<Sprite> result = new List<Sprite>();
178
+			foreach(Sprite spr in spriteList)
179
+			{
180
+				if (spr.IntersectsWithY(this))
181
+					result.Add(spr);
182
+			}
183
+			return result;
184
+		}
185
+
186
+		public bool IntersectsWith(List<Sprite> otherSprites)
187
+		{
188
+			foreach (Sprite spr in otherSprites)
189
+			{
190
+				if (IntersectsWith(spr))
191
+					return true;
192
+			}
193
+			return false;
194
+		}
195
+		public bool IntersectsWith(Sprite otherSprite)
196
+		{
197
+			return otherSprite.Area.IntersectsWith(Area);
198
+		}
199
+
200
+		public bool IntersectsWithX(List<Sprite> otherSprites)
201
+		{
202
+			foreach (Sprite spr in otherSprites)
203
+			{
204
+				if (IntersectsWithX(spr))
205
+					return true;
206
+			}
207
+			return false;
208
+		}
209
+		public bool IntersectsWithY(List<Sprite> otherSprites)
210
+		{
211
+			foreach (Sprite spr in otherSprites)
212
+			{
213
+				if (IntersectsWithY(spr))
214
+					return true;
215
+			}
216
+			return false;
217
+		}
218
+
219
+		public bool IntersectsWithX(Sprite otherSprite)
220
+		{
221
+			if(Area.Right > otherSprite.Area.X &&
222
+				Area.X < otherSprite.Area.Right)
223
+				return true;
224
+			else
225
+				return false;
226
+		}
227
+		public bool IntersectsWithY(Sprite otherSprite)
228
+		{
229
+			if(Area.Bottom > otherSprite.Area.Y &&
230
+				Area.Y < otherSprite.Area.Bottom)
231
+				return true;
232
+			else
233
+				return false;
234
+		}
235
+
236
+	}
237
+}
238
+

+ 12
- 0
SpritePacker/Program.cs View File

@@ -0,0 +1,12 @@
1
+using System;
2
+
3
+namespace SpritePacker
4
+{
5
+	class Program
6
+	{
7
+		public static void Main(string[] args)
8
+		{
9
+			Console.WriteLine("Hello World!");
10
+		}
11
+	}
12
+}

+ 27
- 0
SpritePacker/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,27 @@
1
+using System.Reflection;
2
+using System.Runtime.CompilerServices;
3
+
4
+// Information about this assembly is defined by the following attributes.
5
+// Change them to the values specific to your project.
6
+
7
+[assembly: AssemblyTitle("SpritePacker")]
8
+[assembly: AssemblyDescription("")]
9
+[assembly: AssemblyConfiguration("")]
10
+[assembly: AssemblyCompany("")]
11
+[assembly: AssemblyProduct("")]
12
+[assembly: AssemblyCopyright("Starbeamrainbowlabs")]
13
+[assembly: AssemblyTrademark("")]
14
+[assembly: AssemblyCulture("")]
15
+
16
+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17
+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
18
+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
19
+
20
+[assembly: AssemblyVersion("1.0.*")]
21
+
22
+// The following attributes are used to specify the signing key for the assembly,
23
+// if desired. See the Mono documentation for more information about signing.
24
+
25
+//[assembly: AssemblyDelaySign(false)]
26
+//[assembly: AssemblyKeyFile("")]
27
+

+ 42
- 0
SpritePacker/SpritePacker.csproj View File

@@ -0,0 +1,42 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3
+  <PropertyGroup>
4
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6
+    <ProjectGuid>{6EF47B64-1920-4827-BEEF-B262D5A2D214}</ProjectGuid>
7
+    <OutputType>Exe</OutputType>
8
+    <RootNamespace>SpritePacker</RootNamespace>
9
+    <AssemblyName>SpritePacker</AssemblyName>
10
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
11
+  </PropertyGroup>
12
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
13
+    <DebugSymbols>true</DebugSymbols>
14
+    <DebugType>full</DebugType>
15
+    <Optimize>false</Optimize>
16
+    <OutputPath>bin\Debug</OutputPath>
17
+    <DefineConstants>DEBUG;</DefineConstants>
18
+    <ErrorReport>prompt</ErrorReport>
19
+    <WarningLevel>4</WarningLevel>
20
+    <Externalconsole>true</Externalconsole>
21
+    <PlatformTarget>x86</PlatformTarget>
22
+  </PropertyGroup>
23
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
24
+    <DebugType>full</DebugType>
25
+    <Optimize>true</Optimize>
26
+    <OutputPath>bin\Release</OutputPath>
27
+    <ErrorReport>prompt</ErrorReport>
28
+    <WarningLevel>4</WarningLevel>
29
+    <Externalconsole>true</Externalconsole>
30
+    <PlatformTarget>x86</PlatformTarget>
31
+  </PropertyGroup>
32
+  <ItemGroup>
33
+    <Reference Include="System" />
34
+    <Reference Include="System.Drawing" />
35
+  </ItemGroup>
36
+  <ItemGroup>
37
+    <Compile Include="Program.cs" />
38
+    <Compile Include="Properties\AssemblyInfo.cs" />
39
+    <Compile Include="Packer.cs" />
40
+  </ItemGroup>
41
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
42
+</Project>

Loading…
Cancel
Save