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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
|
// BitmapSpriteEd - Bitmap Sprite Editor
// Copyright (C) 2005 Ian Cowburn
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// $Id$
//
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Collections;
namespace BitmapSpriteEd
{
/// <summary>
/// A sprite frame.
/// </summary>
public class Frame
{
public Frame(int width, int height)
{
CheckSize(width,height);
m_width=width;
m_height=height;
m_data=new Color[m_width,m_height];
Clear(Color.Transparent);
m_changed=false;
}
public Frame() : this(16,16)
{
}
public Frame(Frame old)
{
CopyFrom(old);
}
public void CopyFrom(Frame old)
{
m_width=old.m_width;
m_height=old.m_height;
m_data=new Color[m_width,m_height];
for(int x=0;x<m_width;x++)
{
for(int y=0;y<m_height;y++)
{
m_data[x,y]=old.m_data[x,y];
}
}
m_changed=old.m_changed;
}
public int Width
{
get {return m_width;}
}
public int Height
{
get {return m_height;}
}
public bool Changed
{
get {return m_changed;}
}
public void Resize(int width, int height)
{
CheckSize(width,height);
if (width==m_width && height==m_height)
{
return;
}
Color[,] data=new Color[width,height];
for(int y=0;y<height;y++)
{
for(int x=0;x<width;x++)
{
data[x,y]=Color.Transparent;
}
}
int mx=Math.Min(width,m_width);
int my=Math.Min(height,m_height);
for(int x=0;x<mx;x++)
{
for(int y=0;y<my;y++)
{
data[x,y]=m_data[x,y];
}
}
m_width=width;
m_height=height;
m_data=data;
m_changed=true;
}
public Color this [int x, int y]
{
set
{
m_data[x,y]=value;
m_changed=true;
}
get {return m_data[x,y];}
}
public void Clear(Color c)
{
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
m_data[x,y]=c;
}
}
m_changed=true;
}
public void MirrorHorizontal()
{
Color[,] d=new Color[m_width,m_height];
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
d[x,y]=m_data[m_width-x-1,y];
}
}
m_data=d;
m_changed=true;
}
public void MirrorVertical()
{
Color[,] d=new Color[m_width,m_height];
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
d[x,y]=m_data[x,m_height-y-1];
}
}
m_data=d;
m_changed=true;
}
public void Scroll(int dx, int dy)
{
Color[,] d=new Color[m_width,m_height];
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
d[Mod(x+dx,m_width),Mod(y+dy,m_height)]=m_data[x,y];
}
}
m_data=d;
m_changed=true;
}
public void RotateRight()
{
Color[,] d=new Color[m_width,m_height];
int min=Math.Min(m_width,m_height);
for(int y=0;y<m_height;y++)
for(int x=0;x<m_width;x++)
{
long nx=min-y-1;
long ny=x;
if (nx>=0 && nx<m_width && ny>=0 && ny<m_height)
{
d[nx,ny]=m_data[x,y];
}
}
m_data=d;
m_changed=true;
}
public void RotateLeft()
{
Color[,] d=new Color[m_width,m_height];
int min=Math.Min(m_width,m_height);
for(int y=0;y<m_height;y++)
for(int x=0;x<m_width;x++)
{
long nx=y;
long ny=min-x-1;
if (nx>=0 && nx<m_width && ny>=0 && ny<m_height)
{
d[nx,ny]=m_data[x,y];
}
}
m_data=d;
m_changed=true;
}
public void DropShadow(int dx, int dy, Color col)
{
Color[,] d=new Color[m_width,m_height];
// I'm sure this is too convoluted...
//
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
d[x,y]=Color.Transparent;
}
}
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
if (m_data[x,y].A!=0)
{
int nx=x+dx;
int ny=y+dy;
if (nx>=0 && nx<m_width && ny>=0 && ny<m_height &&
m_data[nx,ny].A==0)
{
d[nx,ny]=col;
}
}
}
}
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
if (d[x,y].A!=0)
{
m_data[x,y]=d[x,y];
}
}
}
m_changed=true;
}
public void Edge(Color col)
{
Color[,] d=new Color[m_width,m_height];
// I'm sure this is too convoluted...
//
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
d[x,y]=Color.Transparent;
}
}
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
if (m_data[x,y].A!=0)
{
for(int dx=-1;dx<2;dx++)
for(int dy=-1;dy<2;dy++)
{
int nx=x+dx;
int ny=y+dy;
if (nx>=0 && nx<m_width && ny>=0 && ny<m_height &&
m_data[nx,ny].A==0)
{
d[nx,ny]=col;
}
}
}
}
}
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
if (d[x,y].A!=0)
{
m_data[x,y]=d[x,y];
}
}
}
m_changed=true;
}
public void Replace(Color from, Color to)
{
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
if (m_data[x,y]==from)
{
m_data[x,y]=to;
}
}
}
m_changed=true;
}
public void Replace(Color to)
{
for(int y=0;y<m_height;y++)
{
for(int x=0;x<m_width;x++)
{
if (m_data[x,y].A>0)
{
m_data[x,y]=to;
}
}
}
m_changed=true;
}
public void Output(Stream stream)
{
Util.WriteInt(stream,m_width);
Util.WriteInt(stream,m_height);
for(int x=0;x<m_width;x++)
{
for(int y=0;y<m_height;y++)
{
stream.WriteByte(m_data[x,y].B);
stream.WriteByte(m_data[x,y].G);
stream.WriteByte(m_data[x,y].R);
stream.WriteByte(m_data[x,y].A);
}
}
m_changed=false;
}
public static Frame Input(Stream stream)
{
int width=Util.ReadInt(stream);
int height=Util.ReadInt(stream);
Frame s=new Frame(width,height);
for(int x=0;x<width;x++)
{
for(int y=0;y<height;y++)
{
int B=stream.ReadByte();
int G=stream.ReadByte();
int R=stream.ReadByte();
int A=stream.ReadByte();
s[x,y]=Color.FromArgb(A,R,G,B);
}
}
s.m_changed=false;
return s;
}
public Bitmap Bitmap
{
get
{
Bitmap bmp=new Bitmap(m_width,m_height,PixelFormat.Format32bppArgb);
for(int x=0;x<m_width;x++)
{
for(int y=0;y<m_height;y++)
{
bmp.SetPixel(x,y,m_data[x,y]);
}
}
return bmp;
}
}
// ------------------------------------------------
// PRIVATE
//
private int m_width;
private int m_height;
private Color[,] m_data;
private bool m_changed;
private void CheckSize(int width, int height)
{
if (width<0 || height<0)
{
throw new ArgumentException("Cannot use negative sizes");
}
}
private int Mod(int v, int m)
{
while (v<0)
{
v+=m;
}
return v%m;
}
}
/// <summary>
/// Describes a set of sprite frames that make up a single sprite
/// </summary>
public class Sprite : IEnumerable
{
// ------------------------------------------------
// PUBLIC
//
public Sprite()
{
m_changed=false;
m_name="";
m_frames=new ArrayList();
m_frames.Add(new Frame(16,16));
}
public IEnumerator GetEnumerator()
{
return m_frames.GetEnumerator();
}
public string Name
{
get {return m_name;}
set {m_name=value;}
}
public static bool IsValidNameChar(char c)
{
return (Char.IsLetterOrDigit(c) || c=='_');
}
public Frame this[int i]
{
get
{
return (Frame)m_frames[i];
}
set
{
m_frames[i]=value;
m_changed=true;
}
}
public void Add(Frame s)
{
m_frames.Add(s);
m_changed=true;
}
public void Remove(Frame s)
{
m_frames.Remove(s);
m_changed=true;
}
public bool Changed
{
get
{
if (!m_changed)
{
foreach (Frame s in m_frames)
{
if (s.Changed)
{
return true;
}
}
}
return m_changed;
}
set {m_changed=value;}
}
public int Count
{
get {return m_frames.Count;}
}
public void Output(Stream stream)
{
Util.WriteString(stream,m_name);
Util.WriteInt(stream,m_frames.Count);
foreach (Frame s in m_frames)
{
s.Output(stream);
}
m_changed=false;
}
public static Sprite Input(Stream stream)
{
Sprite fr=new Sprite();
fr.m_frames.Clear();
fr.Name=Util.ReadString(stream);
int num=Util.ReadInt(stream);
for(int f=0;f<num;f++)
{
fr.Add(Frame.Input(stream));
}
fr.Changed=false;
return fr;
}
// ------------------------------------------------
// PRIVATE
//
private bool m_changed;
private string m_name;
private ArrayList m_frames;
}
/// <summary>
/// Describes a list of sprites
/// </summary>
public class SpriteList : IEnumerable
{
// ------------------------------------------------
// PUBLIC
//
public SpriteList()
{
m_changed=false;
m_list=new ArrayList();
}
public IEnumerator GetEnumerator()
{
return m_list.GetEnumerator();
}
public Sprite this[int i]
{
get
{
return (Sprite)m_list[i];
}
set
{
m_list[i]=value;
m_changed=true;
}
}
public void Add(Sprite s)
{
m_list.Add(s);
m_changed=true;
}
public void Remove(Sprite s)
{
m_list.Remove(s);
m_changed=true;
}
public bool Changed
{
get
{
if (!m_changed)
{
foreach (Sprite s in m_list)
{
if (s.Changed)
{
return true;
}
}
}
return m_changed;
}
set {m_changed=value;}
}
public int Count
{
get {return m_list.Count;}
}
public void Validate()
{
int num=m_list.Count;
for(int i=0;i<num;i++)
{
if (this[i].Name.Length==0)
{
throw new ApplicationException("Empty sprite name found (sprite number " + (i+1).ToString() + ")");
}
for(int j=0;j<num;j++)
{
if (i!=j)
{
if (this[i].Name.ToLower()==this[j].Name.ToLower())
{
throw new ApplicationException
("Sprite name " + this[i].Name + " is used more than once");
}
}
}
}
}
public void Output(Stream stream)
{
Util.WriteInt(stream,MAGIC);
Util.WriteInt(stream,m_list.Count);
foreach (Sprite s in m_list)
{
s.Output(stream);
}
m_changed=false;
}
public static SpriteList Input(Stream stream)
{
SpriteList l=new SpriteList();
int magic=Util.ReadInt(stream);
if (magic!=MAGIC)
{
throw new ApplicationException("Not a recognised sprite file");
}
int num=Util.ReadInt(stream);
for(int f=0;f<num;f++)
{
l.Add(Sprite.Input(stream));
}
l.Changed=false;
return l;
}
// ------------------------------------------------
// PRIVATE
//
private const int MAGIC=0x424d5331;
private bool m_changed;
private ArrayList m_list;
}
}
|