-
Notifications
You must be signed in to change notification settings - Fork 4
/
spritesheetexport.pas
523 lines (459 loc) · 14.9 KB
/
spritesheetexport.pas
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
unit SpriteSheetExport;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
Spin, ComCtrls, Clipbrd, rmcodegen, rwxgf, rmclipboard, rmthumb, rmconfig,
rwpng, LazFileUtils, SpinEx;
type
{ TSpriteSheetExportForm }
TSpriteSheetExportForm = class(TForm)
Apply: TButton;
DescExportToClipboard: TButton;
ExportToClipBoard: TButton;
ExportToFile: TButton;
CSWidth: TSpinEdit;
CSHeight: TSpinEdit;
DescExportToFile: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
SaveDialog1: TSaveDialog;
SaveDialog2: TSaveDialog;
SpinEditCustomSpriteWidth: TSpinEditEx;
SpinEditCustomSpriteHeight: TSpinEditEx;
SpriteSheet: TComboBox;
SpriteSize: TComboBox;
Direction: TComboBox;
SpriteSheetPaintBox: TPaintBox;
Panel1: TPanel;
ScrollBox1: TScrollBox;
ItemsPerRow: TSpinEdit;
Splitter1: TSplitter;
DescriptionFile: TComboBox;
StaticText1: TStaticText;
StaticText2: TStaticText;
StaticText3: TStaticText;
StaticText4: TStaticText;
StaticText5: TStaticText;
StaticText6: TStaticText;
StaticText7: TStaticText;
StaticText8: TStaticText;
StaticText9: TStaticText;
ZoomTrackBar: TTrackBar;
procedure ApplyClick(Sender: TObject);
procedure DescExportToFileClick(Sender: TObject);
procedure DirectionChange(Sender: TObject);
procedure ExportToClipBoardClick(Sender: TObject);
procedure ExportToFileClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure SpriteSheetChange(Sender: TObject);
procedure SpriteSheetPaintBoxPaint(Sender: TObject);
procedure ZoomTrackBarChange(Sender: TObject);
private
public
// Picture1 : TPicture;
SpriteSheetBitMap : TBitMap;
SpriteWidth : integer;
SpriteHeight: integer;
SpriteSheetWidth : integer;
SpriteSheetHeight: integer;
ZoomSize : integer;
procedure UpdateSpriteValues;
procedure UpdateSpriteSheetValues;
procedure UpdateSpriteSheet;
procedure UpdatePaintBoxSize;
procedure UpdateSpriteSheetSize;
procedure ImportSprites;
end;
var
SpriteSheetExportForm: TSpriteSheetExportForm;
implementation
{$R *.lfm}
{ TSpriteSheetExportForm }
procedure calcHoriz(snum,CSWidth,CSHeight,ipr : integer; var x,y,x2,y2 : integer);
var
row,col : integer;
begin
row:=(snum+ipr-1) div ipr;
col:=snum-((row-1)*ipr);
y:=(row-1)*CSHeight;
x:=(col-1)*CSWidth;
x2:=x+CSWidth-1;
y2:=y+CSHeight-1;
end;
procedure calcVirt(snum,CSWidth,CSHeight,ipr : integer; var x,y,x2,y2 : integer);
var
row,col : integer;
begin
col:=(snum+ipr-1) div ipr;
row:=snum-((col-1)*ipr);
y:=(row-1)*CSHeight;
x:=(col-1)*CSWidth;
x2:=x+CSWidth-1;
y2:=y+CSHeight-1;
end;
procedure TSpriteSheetExportForm.FormCreate(Sender: TObject);
begin
SpriteWidth:=32;
SpriteHeight:=32;
SpriteSheetWidth:=320;
SpriteSheetHeight:=200;
UpdateSpriteValues;
UpdateSpriteSheetValues;
ZoomSize:=ZoomTrackBar.Position;
SpriteSheetBitMap:=TBitMap.Create;
// SpriteSheetBitMap.PixelFormat:=pf32bit;
SpriteSheetBitMap.SetSize(SpriteSheetWidth,SpriteSheetHeight);
SpriteSheetBitMap.Canvas.FillRect(0,0,SpriteSheetWidth,SpriteSheetHeight);
UpdateSpriteSheet;
//SpriteSheetBitMap.Clear;
SpriteSheetPaintBox.Width:=SpriteSheetWidth*ZoomSize;
SpriteSheetPaintBox.Height:=SpriteSheetHeight*ZoomSize;
SpriteSheetPaintBox.Invalidate;
end;
procedure TSpriteSheetExportForm.FormDestroy(Sender: TObject);
begin
SpriteSheetBitMap.Free;
end;
procedure TSpriteSheetExportForm.SpriteSheetChange(Sender: TObject);
begin
UpdateSpriteSheetValues;
UpdateSpriteValues;
if Direction.ItemIndex = 0 then
begin
ItemsPerRow.Value:=SpriteSheetWidth div SpriteWidth;
end
else
begin
ItemsPerRow.Value:=SpriteSheetHeight div SpriteHeight;
end;
end;
procedure TSpriteSheetExportForm.ApplyClick(Sender: TObject);
begin
// SpriteSheetBitMap.Clear;
UpdateSpriteValues;
UpdateSpriteSheetValues;
UpdateSpriteSheetSize;
SpriteSheetBitMap.Canvas.FillRect(0,0,SpriteSheetWidth,SpriteSheetHeight);
UpdatePaintBoxSize;
ImportSprites;
SpriteSheetPaintBox.Invalidate;
end;
function menuToLan(menuid : integer) : integer;
begin
case menuid of 0:menutoLan:=QB64Lan;
1:menutoLan:=QBLan;
2:menutoLan:=FPLan;
3:menutoLan:=TPLan;
4:menutoLan:=TMTLan;
5:menutoLan:=APLan;
6:menutoLan:=QPLan;
7:menutoLan:=gccLan;
8:menutoLan:=OWLan;
9:menutoLan:=TCLan;
10:menutoLan:=QCLan;
11:menutoLan:=ACLan;
12:menutoLan:=JSonLan;
end;
end;
function LanToFileFilter(Lan : integer) : string;
begin
case Lan of QB64Lan,QBLan:LanToFileFilter:='BAS|*.bas|All Files|*.*';
FPLan,TPLan,TMTLan,APLan:LanToFileFilter:='PAS|*.pas|All Files|*.*';
QCLan,gccLan,OWLan,TCLan,ACLan:LanToFileFilter:='C|*.c|All Files|*.*';
JSonLan:LanToFileFilter:='JSON|*.json|All Files|*.*';
end;
end;
procedure WriteHeader(var F : Text;lan : integer);
var
headstr : string;
begin
headstr:='Sprite Sheet Description Created By Raster Master';
case Lan of QB64Lan,QBLan:Writeln(F,#39,' ',headstr);
FPLan,TPLan,TMTLan,APLan:Writeln(F,'(* ',headstr,' *)');
QCLan,gccLan,OWLan,TCLan,ACLan:Writeln(F,'/* ',headstr,' */');
end;
end;
procedure WriteDesc(var F : Text;lan : integer;DescName : string; swidth,sheight,x,y,x2,y2 : integer;csprite,snum : integer);
begin
case Lan of QBLan,QB64Lan:begin
Writeln(F,DescName,'Desc:');
Writeln(F,#39,' Width=',SWidth,' Height=',SHeight);
Writeln(F,'DATA ',x,',',y,',',x2,',',y2);
end;
APLan,QPLan,TMTLan,FPLan,TPLan:begin
Writeln(F,'(* ',DescName,' Width=',SWidth,' Height=',SHeight,' *)');
Writeln(F,DescName,'Desc: Array[1..4] of Integer=(',x,',',y,',',x2,',',y2,');');
end;
gccLan,OWLan,TCLan,QCLan,ACLan:begin
Writeln(F,'/* ',DescName,' Width=',SWidth,' Height=',SHeight,' */');
Writeln(F,'int ',DescName,'[] = {',x,',',y,',',x2,',',y2,'};');
end;
JsonLan:begin
if csprite = 1 then Writeln(F,'[');
Writeln(F,' {');
Writeln(F,' DescName: "',DescName,'",');
Writeln(F,' Width:',SWidth,',');
Writeln(F,' Height:',SHeight,',');
Writeln(F,' x:',x,',');
Writeln(F,' y:',y,',');
Writeln(F,' x2:',x2,',');
Writeln(F,' y2:',y2);
if csprite < snum then Writeln(F,' },') else Writeln(F,' }');
if csprite = snum then Writeln(F,']');
end;
end;
end;
procedure TSpriteSheetExportForm.DescExportToFileClick(Sender: TObject);
var
DescName : String;
F : Text;
c : integer;
Lan,snum : integer;
SWidth,SHeight,x,y,x2,y2 : integer;
FileName : string;
begin
Lan:=menutoLan(DescriptionFile.ItemIndex);
if (Sender As TButton).Name = 'DescExportToFile' then
begin
// SaveDialog2.Filter := 'BAS|*.bas|All Files|*.*';
SaveDialog2.Filter := LanToFileFilter(Lan);
if NOT SaveDialog2.Execute then exit;
FileName:=SaveDialog2.FileName;
end
else
begin
FileName:=GetTemporaryPathAndFileName;
end;
{$I-}
System.Assign(F,FileName);
Rewrite(F);
// Writeln(F,#39,' Sprite Sheet Description Created By Raster Master');
WriteHeader(F,Lan);
snum:=ImageThumbBase.GetCount;
for c:=0 to snum-1 do
begin
DescName:=ImageThumbBase.GetExportName(c);
SWidth:=ImageThumbBase.GetWidth(c);
SHeight:=ImageThumbBase.GetHeight(c);
if Direction.ItemIndex = 0 then
CalcHoriz(c+1,SWidth,SHeight,ItemsPerRow.Value,x,y,x2,y2)
else CalcVirt(c+1,SWidth,SHeight,ItemsPerRow.Value,x,y,x2,y2);
// Writeln(F,DescName,'Desc:');
// Writeln(F,#39,' Width=',SWidth,' Height=',SHeight);
// Writeln(F,'DATA ',x,',',y,',',x2,',',y2);
WriteDesc(F,Lan,DescName,swidth,sheight,x,y,x2,y2,c+1,snum);
end;
{$I+}
if IORESULT<>0 then exit;
{$I-}
System.close(F);
{$I+}
if (Sender As TButton).Name = 'DescExportToClipboard' then
begin
ReadFileAndCopyToClipboard(FileName);
EraseFile(FileName);
end;
end;
procedure TSpriteSheetExportForm.DirectionChange(Sender: TObject);
begin
if Direction.ItemIndex = 0 then
begin
ItemsPerRow.Value:=SpriteSheetWidth div SpriteWidth;
end
else
begin
ItemsPerRow.Value:=SpriteSheetHeight div SpriteHeight;
end;
end;
procedure TSpriteSheetExportForm.ExportToClipBoardClick(Sender: TObject);
begin
Clipboard.Assign(SpriteSheetBitMap);
end;
procedure TSpriteSheetExportForm.ExportToFileClick(Sender: TObject);
var
i,j : integer;
pixeldata : PByte;
pixelpos : longint;
cl : TColor;
PngRGBA : PngRGBASettingsRec;
Picture1 : TPicture;
ext : string;
begin
RMConfigBase.GetProps(PngRGBA);
Picture1:=TPicture.Create;
Picture1.Bitmap.Width:=SpriteSheetWidth;
Picture1.Bitmap.height:=SpriteSheetHeight;
Picture1.BitMap.PixelFormat:=pf32bit; //change format to 32 bit/RGBA
pixeldata:=picture1.Bitmap.RawImage.Data;
pixelpos:=0;
for j:=0 to SpriteSheetHeight-1 do
begin
for i:=0 to SpriteSheetWidth-1 do
begin
cl:=SpriteSheetBitMap.Canvas.Pixels[i,j];
pixeldata[pixelpos]:=Blue(cl); // Blue
pixeldata[pixelpos+1]:=Green(cl); // Green
pixeldata[pixelpos+2]:=Red(cl); // Red
pixeldata[pixelpos+3]:=255; // Alpha 255 = solid
// if (PngRGBA.UseColorIndex) and (PngRGBA.ColorIndex=ci) then
// begin
// pixeldata[pixelpos+3]:=0; // Alpha 0 = transparent
// end;
if (PngRGBA.UseFuschia) and (Red(cl) = 255) and (Green(cl)=0) and (Blue(cl)=255) then //use fuschia
begin
pixeldata[pixelpos+3]:=0; // Alpha 0 = transparent
end;
if (PngRGBA.UseCustom) and (Red(cl) = PngRGBA.R) and (Blue(cl)=PngRGBA.B) and (Green(cl)=PngRGBA.G) then //use fuschia
begin
pixeldata[pixelpos+3]:=PngRGBA.A; // use Custom Alpha level for transperancy
end;
inc(pixelpos,4);
end;
end;
SaveDialog1.Filter := 'PNG|*.png|All Files|*.*';
if SaveDialog1.Execute then
begin
ext:=UpperCase(ExtractFileExt(SaveDialog1.Filename));
if ext = '.PNG' then Picture1.SaveToFile(SaveDialog1.FileName,'.PNG');
end;
Picture1.Free;
end;
procedure TSpriteSheetExportForm.FormActivate(Sender: TObject);
begin
UpdateSpriteSheet;
end;
procedure TSpriteSheetExportForm.SpriteSheetPaintBoxPaint(Sender: TObject);
begin
UpdateSpriteSheet;
end;
procedure TSpriteSheetExportForm.UpdateSpriteSheet;
begin
SpriteSheetPaintBox.Canvas.CopyRect(Rect(0,0,SpriteSheetWidth*ZoomSize,SpriteSheetHeight*ZoomSize),SpriteSheetBitMap.Canvas,Rect(0,0,SpriteSheetWidth,SpriteSheetHeight));
end;
procedure TSpriteSheetExportForm.UpdatePaintBoxSize;
begin
SpriteSheetPaintBox.Width:=SpriteSheetWidth*ZoomSize;
SpriteSheetPaintBox.Height:=SpriteSheetHeight*ZoomSize;
end;
procedure TSpriteSheetExportForm.UpdateSpriteSheetSize;
begin
SpriteSheetBitMap.SetSize(SpriteSheetWidth,SpriteSheetHeight);
end;
procedure TSpriteSheetExportForm.ZoomTrackBarChange(Sender: TObject);
begin
ZoomSize:=ZoomTrackBar.Position;
SpriteSheetPaintBox.Width:=SpriteSheetWidth*ZoomSize;
SpriteSheetPaintBox.Height:=SpriteSheetHeight*ZoomSize;
SpriteSheetPaintBox.Invalidate;
end;
procedure TSpriteSheetExportForm.UpdateSpriteValues;
begin
Case SpriteSize.ItemIndex of 0:begin
SpriteWidth:=8;
SpriteHeight:=8;
end;
1:begin
SpriteWidth:=16;
SpriteHeight:=16;
end;
2:begin
SpriteWidth:=32;
SpriteHeight:=32;
end;
3:begin
SpriteWidth:=64;
SpriteHeight:=64;
end;
4:begin
SpriteWidth:=128;
SpriteHeight:=128;
end;
5:begin
SpriteWidth:=256;
SpriteHeight:=256;
end;
6:begin
SpriteWidth:=SpinEditCustomSpriteWidth.Value;
SpriteHeight:=SpinEditCustomSpriteHeight.Value;
end;
end;
end;
procedure TSpriteSheetExportForm.UpdateSpriteSheetValues;
begin
Case SpriteSheet.ItemIndex of 0:begin
SpriteSheetWidth:=320;
SpriteSheetHeight:=200;
end;
1:begin
SpriteSheetWidth:=640;
SpriteSheetHeight:=200;
end;
2:begin
SpriteSheetWidth:=640;
SpriteSheetHeight:=350;
end;
3:begin
SpriteSheetWidth:=640;
SpriteSheetHeight:=480;
end;
4:begin
SpriteSheetWidth:=800;
SpriteSheetHeight:=600;
end;
5:begin
SpriteSheetWidth:=1024;
SpriteSheetHeight:=768;
end;
6:begin
SpriteSheetWidth:=CSWidth.Value;
SpriteSheetHeight:=CSHeight.Value;
end;
end;
end;
procedure TSpriteSheetExportForm.ImportSprites;
var
c,i,j : integer;
cc: TColor;
ipr : integer;
xstart,ystart : integer;
begin
ipr:=0;
xstart:=0;
ystart:=0;
ImageThumbBase.CopyCoreToIndexImage(ImageThumbBase.GetCurrent);
for c:=0 to ImageThumbBase.GetCount-1 do
begin
for i:=0 to ImageThumbBase.GetWidth(c)-1 do
begin
for j:=0 to ImageThumbBase.GetHeight(c)-1 do
begin
cc:=ImageThumbBase.GetPixelTColor(c,i,j);
SpriteSheetBitMap.Canvas.Pixels[i+xstart,j+ystart]:=cc;
end
end;
inc(ipr);
if ipr = ItemsPerRow.Value then
begin
ipr:=0;
if Direction.ItemIndex = 0 then
begin
xstart:=0;
inc(ystart,SpriteHeight);
end
else
begin
ystart:=0;
inc(xstart,SpriteWidth);
end;
end
else
begin
if Direction.ItemIndex = 0 then inc(xstart,Spritewidth) else inc(ystart,SpriteHeight);
end;
end;
end;
end.