-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.cs
423 lines (368 loc) · 12.4 KB
/
Sprite.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Merthsoft.Extensions;
namespace Merthsoft.TokenIDE {
public class Sprite {
private List<List<int>> sprite;
public int Height { get; private set; }
public int Width { get; private set; }
public int this[int i, int j] {
get { return sprite[i][j]; }
set { setPoint(i, j, value); }
}
public Rectangle DirtyRectangle { get; set; }
public Sprite(string hexData, int width, out int height, int bitsPerPixel) {
Width = width;
sprite = HexHelper.HexTo2DList(hexData, Width, out height, bitsPerPixel);
Height = height;
DirtyRectangle = new Rectangle(0, 0, Width, Height);
}
public Sprite(int[,] sprite) {
Width = sprite.GetLength(0);
Height = sprite.GetLength(1);
DirtyRectangle = new Rectangle(0, 0, Width, Height);
this.sprite = new List<List<int>>();
for (int i = 0; i < Width; i++) {
List<int> row = new List<int>(Width);
for (int j = 0; j < Height; j++) {
row.Add(sprite[i, j]);
}
this.sprite.Add(row);
row = new List<int>(Width);
}
}
public Sprite(int width, int height, int defaultTile = 0) {
Height = height;
Width = width;
DirtyRectangle = new Rectangle(0, 0, Width, Height);
this.sprite = new List<List<int>>();
for (int i = 0; i < Width; i++) {
List<int> row = new List<int>(Width);
for (int j = 0; j < Height; j++) {
row.Add(defaultTile);
}
this.sprite.Add(row);
row = new List<int>(Width);
}
}
public Sprite(Sprite oldSprite) {
Height = oldSprite.Height;
Width = oldSprite.Width;
DirtyRectangle = new Rectangle(0, 0, Width, Height);
this.sprite = new List<List<int>>();
for (int i = 0; i < Width; i++) {
List<int> row = new List<int>(Width);
for (int j = 0; j < Height; j++) {
row.Add(oldSprite[i, j]);
}
this.sprite.Add(row);
row = new List<int>(Width);
}
}
public Sprite(Bitmap image, List<Color> colors, int transparent = -1)
: this(image.PalettizeImage(colors, transparent)) { }
public Sprite(Bitmap image) {
Width = image.Width;
Height = image.Height;
this.sprite = new List<List<int>>();
for (int i = 0; i < Width; i++) {
List<int> row = new List<int>(Width);
for (int j = 0; j < Height; j++) {
row.Add(image.GetPixel(i, j).ToArgb());
}
this.sprite.Add(row);
row = new List<int>(Width);
}
DirtyRectangle = new Rectangle(0, 0, Width, Height);
}
public Sprite Copy() {
return new Sprite(this);
}
public void Resize(int width, int height) {
int bigHeight = (int)Math.Max(height, Height);
int littleHeight = (int)Math.Min(height, Height);
int bigWidth = (int)Math.Max(width, Width);
int littleWidth = (int)Math.Min(width, Width);
for (int i = littleWidth; i < bigWidth; i++) {
if (width < Width) {
sprite.RemoveAt(width);
} else if (width > Width) {
int[] newRow = new int[height];
sprite.Add(newRow.ToList());
}
}
Width = width;
for (int i = 0; i < Width; i++) {
for (int j = littleHeight; j < bigHeight; j++) {
if (height< Height) {
sprite[i].RemoveAt(height);
} else if (height > Height) {
sprite[i].Add(0);
}
}
}
Height = height;
}
public void DrawSprite(int x, int y, Sprite otherSprite) {
int maxWidth = x + otherSprite.Width;
int maxHeight = y + otherSprite.Height;
if (maxWidth > Width) { maxWidth = Width; }
if (maxHeight > Height) { maxHeight = Height; }
for (int i = x; i < maxWidth; i++) {
for (int j = y; j < maxWidth; j++) {
int color = otherSprite[i, j];
if (color != -1) {
Plot(i, j, color);
}
}
}
}
public Sprite SubSprite(int x, int y, int width, int height) {
Sprite newSprite = new Sprite(width, height);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
newSprite[i, j] = this[x + i, y + j];
}
}
return newSprite;
}
public override bool Equals(object obj) {
Sprite comp = obj as Sprite;
if (comp == null) {
return base.Equals(obj);
}
if (comp.Width != Width || comp.Height != Height) {
return false;
}
for (int i = 0; i < Width; i++) {
for (int j = 0; j < Width; j++) {
if (comp[i, j] != this[i, j]) { return false; }
}
}
return true;
}
public override int GetHashCode() {
return sprite.GetHashCode();
}
public override string ToString() {
return string.Format("Sprite {{{0}, {1}}}", Width, Height);
}
public void ClearDirtyRectangle() {
DirtyRectangle = Rectangle.Empty;
}
public void Invalidate() {
DirtyRectangle = new Rectangle(0, 0, Width, Height);
}
/// <summary>
/// Plots a point to the sprite.
/// </summary>
/// <param name="x">The X coordinate to plot.</param>
/// <param name="y">The Y coordinate to plot.</param>
/// <param name="color">The color to draw.</param>
/// <param name="plotWidth">The pen width.</param>
public void Plot(int x, int y, int color, int plotWidth = 1) {
if (plotWidth == 1) {
if (x >= 0 && y >= 0 && x < Width && y < Height) {
setPoint(x, y, color);
}
} else {
int r = (int)Math.Floor((plotWidth) / 2.0);
int evenOffSet = plotWidth % 2 == 0 ? 1 : 0;
DrawRectangle(x - r + evenOffSet, y - r + evenOffSet, x + r, y + r, color, 1, true);
}
}
private void setPoint(int x, int y, int color) {
if (x < 0 || x >= Width || y < 0 || y >= Height)
return;
if (DirtyRectangle.IsEmpty) {
DirtyRectangle = new Rectangle(x, y, 1, 1);
} else if (!DirtyRectangle.Contains(x, y)) {
DirtyRectangle = Rectangle.Union(DirtyRectangle, new Rectangle(x, y, 1, 1));
}
sprite[x][y] = color;
}
/// <summary>
/// Draws a line to the sprite.
/// </summary>
/// <param name="x1">The X coordinate of one end point.</param>
/// <param name="y1">The Y coordinate of one end point.</param>
/// <param name="x2">The X coordinate of the second end point.</param>
/// <param name="y2">The Y coordinate of the second end point.</param>
/// <param name="color">The color to draw.</param>
/// <param name="plotWidth">The pen width.</param>
public void DrawLine(int x1, int y1, int x2, int y2, int color, int plotWidth = 1) {
int deltaX = (int)Math.Abs(x1 - x2);
int deltaY = (int)Math.Abs(y1 - y2);
int stepX = x2 < x1 ? 1 : -1;
int stepY = y2 < y1 ? 1 : -1;
int err = deltaX - deltaY;
while (true) {
Plot(x2, y2, color, plotWidth);
if (x2 == x1 && y2 == y1) { break; }
int e2 = 2 * err;
if (e2 > -deltaY) {
err = err - deltaY;
x2 = x2 + stepX;
}
if (x2 == x1 && y2 == y1) {
Plot(x2, y2, color, plotWidth);
break;
}
if (e2 < deltaX) {
err = err + deltaX;
y2 = y2 + stepY;
}
}
}
/// <summary>
/// Draws a rectangle to the sprite.
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <param name="color">The color to draw.</param>
/// <param name="plotWidth">The pen width.</param>
/// <param name="fill">True to fill the rectangle.</param>
public void DrawRectangle(int x1, int y1, int x2, int y2, int color, int plotWidth = 1, bool fill = false) {
if (!fill) {
DrawLine(x1, y1, x1, y2, color, plotWidth);
DrawLine(x1, y2, x2, y2, color, plotWidth);
DrawLine(x2, y2, x2, y1, color, plotWidth);
DrawLine(x1, y1, x2, y1, color, plotWidth);
} else {
if (x1 > x2) {
MerthsoftExtensions.Swap(ref x1, ref x2);
}
for (int x = x1; x <= x2; x++) {
DrawLine(x, y1, x, y2, color, plotWidth);
}
}
}
/// <summary>
/// Draws an ellipse to the sprite.
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <param name="color">The color to draw.</param>
/// <param name="plotWidth">The pen width.</param>
/// <param name="fill">True to fill the ellipse.</param>
public void DrawEllipse(int x1, int y1, int x2, int y2, int color, int plotWidth = 1, bool fill = false) {
if (x2 < x1) { MerthsoftExtensions.Swap(ref x1, ref x2); }
if (y2 < y1) { MerthsoftExtensions.Swap(ref y1, ref y2); }
int hr = (x2 - x1) / 2;
int kr = (y2 - y1) / 2;
int h = x1 + hr;
int k = y1 + kr;
DrawEllipseUsingRadius(h, k, hr, kr, color, plotWidth, fill);
}
private void incrementX(ref int x, ref int dxt, ref int d2xt, ref int t) { x++; dxt += d2xt; t += dxt; }
private void incrementY(ref int y, ref int dyt, ref int d2yt, ref int t) { y--; dyt += d2yt; t += dyt; }
/// <summary>
/// Draws a filled ellipse to the sprite.
/// </summary>
/// <remarks>Taken from http://enchantia.com/graphapp/doc/tech/ellipses.html. </remarks>
/// <param name="x">The center point X coordinate.</param>
/// <param name="y">The center point Y coordinate.</param>
/// <param name="xRadius">The x radius.</param>
/// <param name="yRadius">The y radius.</param>
/// <param name="color">The color to draw.</param>
/// <param name="plotWidth">The pen width.</param>
/// <param name="fill">True to fill the ellipse.</param>
public void DrawEllipseUsingRadius(int x, int y, int xRadius, int yRadius, int color, int plotWidth = 1, bool fill = false) {
int plotX = 0;
int plotY = yRadius;
int xRadiusSquared = xRadius * xRadius;
int yRadiusSquared = yRadius * yRadius;
int crit1 = -(xRadiusSquared / 4 + xRadius % 2 + yRadiusSquared);
int crit2 = -(yRadiusSquared / 4 + yRadius % 2 + xRadiusSquared);
int crit3 = -(yRadiusSquared / 4 + yRadius % 2);
int t = -xRadiusSquared * plotY;
int dxt = 2 * yRadiusSquared * plotX, dyt = -2 * xRadiusSquared * plotY;
int d2xt = 2 * yRadiusSquared, d2yt = 2 * xRadiusSquared;
while (plotY >= 0 && plotX <= xRadius) {
Plot(x + plotX, y + plotY, color, plotWidth);
if (plotX != 0 || plotY != 0) {
Plot(x - plotX, y - plotY, color, plotWidth);
}
if (plotX != 0 && plotY != 0) {
Plot(x + plotX, y - plotY, color, plotWidth);
Plot(x - plotX, y + plotY, color, plotWidth);
}
if (t + yRadiusSquared * plotX <= crit1 || t + xRadiusSquared * plotY <= crit3) {
incrementX(ref plotX, ref dxt, ref d2xt, ref t);
} else if (t - xRadiusSquared * plotY > crit2) {
incrementY(ref plotY, ref dyt, ref d2yt, ref t);
} else {
incrementX(ref plotX, ref dxt, ref d2xt, ref t);
incrementY(ref plotY, ref dyt, ref d2yt, ref t);
}
}
if (fill) {
FloodFill(x, y, color);
}
}
/// <summary>
/// Draws a circle to the sprite.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="r"></param>
/// <param name="color">The color to draw.</param>
/// <param name="plotWidth">The pen width.</param>
/// <param name="fill">True to fill the circle.</param>
public void DrawCircle(int x, int y, int r, int color, int plotWidth = 1, bool fill = false) {
DrawEllipseUsingRadius(x, y, r, r, color, plotWidth, fill);
}
/// <summary>
/// Performs a flood fill to the sprite.
/// </summary>
/// <param name="x">The starting X coordinate.</param>
/// <param name="y">The starting Y coordinate.</param>
/// <param name="color">The color to draw.</param>
public void FloodFill(int x, int y, int color) {
if (x < 0 || y < 0 || x >= Width || y >= Height) { return; }
if (sprite[x][y] == color) { return; }
int baseColor = sprite[x][y];
Stack<Point> s = new Stack<Point>();
s.Push(new Point(x, y));
while (s.Count > 0) {
Point p = s.Pop();
if (p.X < 0 || p.Y < 0 || p.X >= Width || p.Y >= Height) {
continue;
}
if (sprite[p.X][p.Y] == baseColor) {
Plot(p.X, p.Y, color, 1);
s.Push(new Point(p.X + 1, p.Y));
s.Push(new Point(p.X - 1, p.Y));
s.Push(new Point(p.X, p.Y + 1));
s.Push(new Point(p.X, p.Y - 1));
}
}
}
/// <summary>
/// Replaces all pixels with color at x,y with color
/// </summary>
/// <param name="x">The starting X coordinate.</param>
/// <param name="y">The starting Y coordinate.</param>
/// <param name="color">The color to draw.</param>
public void ReplaceAll(int x, int y, int color) {
if (x < 0 || y < 0 || x >= Width || y >= Height) { return; }
if (sprite[x][y] == color) { return; }
int baseColor = sprite[x][y];
for (int i = 0; i < Width; i++) {
for (int j = 0; j < Height; j++) {
if (sprite[i][j] == baseColor) {
sprite[i][j] = color;
}
}
}
DirtyRectangle = new Rectangle(0, 0, Width, Height);
}
}
}