-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTileset.cs
180 lines (154 loc) · 4.99 KB
/
Tileset.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using Capricorn.IO;
namespace Capricorn.Drawing;
/// <summary>
/// Tileset Class
/// </summary>
public class Tileset
{
public const int TileWidth = 56;
public const int TileHeight = 27;
public const int TileSize = TileWidth * TileHeight;
private string name;
private string filename;
private int tileCount;
private List<byte[]> tiles = new List<byte[]>();
/// <summary>
/// Gets or sets a tile's bitmap image.
/// </summary>
/// <param name="index">Zero-based index of the tile.</param>
/// <returns></returns>
public byte[] this[int index]
{
get { return tiles[index]; }
set { tiles[index] = value; }
}
/// <summary>
/// Gets the file name of the tileset.
/// </summary>
public string FileName
{
get { return filename; }
}
/// <summary>
/// Gets or sets the name of the tileset.
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// Gets the tiles loaded into the tileset.
/// </summary>
public byte[][] Tiles
{
get { return tiles.ToArray(); }
}
/// <summary>
/// Gets the number of tiles in the tileset.
/// </summary>
public int TileCount
{
get { return tileCount; }
}
/// <summary>
/// Loads a tileset from disk.
/// </summary>
/// <param name="file">Tileset to load.</param>
/// <returns>Number of tiles loaded.</returns>
public static Tileset FromFile(string file)
{
// Create File Stream
FileStream stream = new FileStream(file,
FileMode.Open, FileAccess.Read, FileShare.Read);
// Load Tiles from File Stream
Tileset tileset = LoadTiles(stream);
tileset.name = Path.GetFileNameWithoutExtension(file).ToUpper();
tileset.filename = file;
// Return Tileset
return tileset;
}
/// <summary>
/// Loads a tileset from raw data bytes.
/// </summary>
/// <param name="data">Raw data to use.</param>
/// <returns>Number of tiles loaded.</returns>
public static Tileset FromRawData(byte[] data)
{
// Create Memory Stream
MemoryStream stream = new MemoryStream(data);
// Load Tiles from Memory Stream
Tileset tileset = LoadTiles(stream);
tileset.name = "Unknown Tileset";
// Return Tileset
return tileset;
}
/// <summary>
/// Loads a tilset from an archive (case-sensitive).
/// </summary>
/// <param name="file">Tileset to load.</param>
/// <param name="archive">Data archive to load from.</param>
/// <returns>Number of tiles loaded.</returns>
public static Tileset FromArchive(string file, DATArchive archive)
{
// Check if File Exists
if (!archive.Contains(file))
return null;
// Extract and Create File
Tileset tileset = LoadTiles(new MemoryStream(archive.ExtractFile(file)));
tileset.name = Path.GetFileNameWithoutExtension(file).ToUpper();
tileset.filename = file;
// Return Tileset
return tileset;
}
/// <summary>
/// Loads a tileset from an archive.
/// </summary>
/// <param name="file">Tileset to load.</param>
/// <param name="ignoreCase">Ignore case (noncase-sensitive).</param>
/// <param name="archive">Data archive to load from.</param>
/// <returns>Number of tiles loaded.</returns>
public static Tileset FromArchive(string file, bool ignoreCase, DATArchive archive)
{
// Check if File Exists
if (!archive.Contains(file, ignoreCase))
return null;
// Extract and Create File
Tileset tileset = LoadTiles(new MemoryStream(archive.ExtractFile(file, ignoreCase)));
tileset.name = Path.GetFileNameWithoutExtension(file).ToUpper();
tileset.filename = file;
// Return Tileset
return tileset;
}
/// <summary>
/// Internal function that loads a tileset from a given data stream.
/// </summary>
/// <param name="stream">Data stream.</param>
/// <returns>Numbers of tiles loaded.</returns>
private static Tileset LoadTiles(Stream stream)
{
// Create Binary Reader
stream.Seek(0, SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
// Create TileSet
Tileset tileset = new Tileset();
tileset.tileCount = (int)(reader.BaseStream.Length / TileSize);
// Load Tiles
for (int i = 0; i < tileset.tileCount; i++)
{
// Read Tile
byte[] tileData = reader.ReadBytes(TileSize);
tileset.tiles.Add(tileData);
} reader.Close();
// Return Tileset
return tileset;
}
/// <summary>
/// Gets the string representation of the object.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "{Name = " + name + ", Tiles = " + tiles.Count.ToString() + "}";
}
}