/
Engine.cs
84 lines (74 loc) · 2.54 KB
/
Engine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Sprite Sheet Packer Engine
* @author John McWatters
* An engine to calculate optimal positioning of rectangles in a power of 2 square
* The heavy lifting is all done by CygonRectanglePacker,
* written by Markus Ewald (cygon at nuclex.org)
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using Nuclex.Game.Packing;
namespace spriteSheetPacker
{
/*
* Engine class
*/
public class Engine
{
/// <summary>
/// The packer
/// </summary>
CygonRectanglePacker packer;
/// <summary>
/// The size of the square, side length
/// </summary>
public int size;
/*
* Constructor for Engine class
* @param side, an int of the lenght of a side. Only one as it works with a square
* @param images, a collection of ImageInfo objects
* Takes a collection of images and trys to fit them to a square,
* increasing the size of square if they don't fit
*/
public Engine(int side, IEnumerable<ImageInfo> images)
{
/// Collection of images not fitting
List<ImageInfo> dontFit = new List<ImageInfo>();
/// A check of whether all images are mapped
bool check = true;
while (check)
{
/// Initalise a packer object
packer = new CygonRectanglePacker(side, side);
/// Creating a Point object, for positioning images
Point placement;
/// Iterates through images, and trys to place it on the square
foreach (ImageInfo image in images)
{
/// Checking to see if it fits, else it is added to dontFit
if (packer.TryPack(image.Width, image.Height, out placement))
{
image.Position = placement;
}//end if
else
{
dontFit.Add(image);
}//end else
}// end foreach
/// If images didn't fit, the loop will go again, otherwise it will finsih
if(dontFit.Count == 0)
{
check = false;
}// end if
else
{
side = side * 2;
dontFit.Clear();
}// end else
}
/// Initalising size with final square side lenght
size = side;
}
}
}