-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfMain.cpp
1475 lines (1465 loc) · 50.4 KB
/
fMain.cpp
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------
#include "agdv.pch.h"
#include <System.IOUtils.hpp>
#include <System.Character.hpp>
#include <System.StrUtils.hpp>
#include <string.h>
#include "fMain.h"
#include "ErrorReporter.h"
#include "Importer.h"
#include "Project.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain * frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent * Owner)
: TForm(Owner)
, m_Scale(2.f)
, m_MonochromeAttribute(0x47)
, m_Map(nullptr)
, m_MapIsPanning(false)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject * Sender)
{
g_ErrorReporter.Memo = memReport;
imgView->Picture->Bitmap->Canvas->Brush->Color = clBlack;
imgView->Picture->Bitmap->Width = imgView->Width;
imgView->Picture->Bitmap->Height = imgView->Height;
imgView->Picture->Bitmap->Canvas->Brush->Style = bsClear;
// Set the brush style to transparent.
imgView->Picture->Bitmap->Canvas->Font->Color = clWhite;
imgView->Picture->Bitmap->Canvas->Font->Name = "Courier New";
imgView->Picture->Bitmap->Canvas->Font->Height = 24;
imgView->Picture->Bitmap->Canvas->Font->Style <<= fsBold;
// check command line parameters
OpenFile(ParamStr(1));
DragAcceptFiles(Handle, true);
Application->OnActivate = FormActivate;
Application->OnDeactivate = FormDeactivate;
pgcViews->ActivePage = tabImages;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormDestroy(TObject * Sender)
{
DragAcceptFiles(Handle, false);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormActivate(TObject * Sender)
{
Timer2->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormDeactivate(TObject * Sender)
{
Timer2->Enabled = m_File.Trim() != "";
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormResize(TObject * Sender)
{
imgView->Picture->Bitmap->Width = imgView->Width;
imgView->Picture->Bitmap->Height = imgView->Height;
RefreshImagesView();
RefreshMapView();
imgMap->Width = imgMap->Picture->Bitmap->Width;
imgMap->Height = imgMap->Picture->Bitmap->Height;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Timer1Timer(TObject * Sender)
{
// animate
m_Animate++ ;
RefreshImagesView();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Timer2Timer(TObject * Sender)
{
OpenFile(m_File);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actFileOpenAccept(TObject * Sender)
{
OpenFile(actFileOpen->Dialog->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actZoomInExecute(TObject * Sender)
{
m_Scale = std::min(16.f, m_Scale + 1.f);
RefreshImagesView();
RefreshMapView();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actZoomOutExecute(TObject * Sender)
{
m_Scale = std::max(1.f, m_Scale - 1.f);
RefreshImagesView();
RefreshMapView();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actZoomActualSizeExecute(TObject * Sender)
{
m_Scale = 1;
RefreshImagesView();
RefreshMapView();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actZoomToFitExecute(TObject * Sender)
{
//
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actPlayExecute(TObject * Sender)
{
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actStopExecute(TObject * Sender)
{
Timer1->Enabled = false;
RefreshImagesView();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actToggleMonochromeExecute(TObject * Sender)
{
if (btnToggle->Down)
{
for (const auto &image : m_Font)
{
image->SetAttribute(0x44);
}
for (const auto &image : m_Blocks)
{
image->SetAttribute(0x44);
}
for (const auto &image : m_Objects)
{
image->SetAttribute(0x44);
}
for (const auto &image : m_Sprites)
{
image->SetAttribute(0x44);
}
}
else
{
for (const auto &image : m_Font)
{
image->ResetAttribute();
}
for (const auto &image : m_Blocks)
{
image->ResetAttribute();
}
for (const auto &image : m_Objects)
{
image->ResetAttribute();
}
for (const auto &image : m_Sprites)
{
image->ResetAttribute();
}
}
RefreshImagesView();
RefreshMapView();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actExportExecute(TObject * Sender)
{
if (actExportAGDX->Checked)
{
ExportAGDXProject();
}
else if (actExportAGD->Checked)
{
ExportAGDFile();
}
else if (actExportCustom->Checked)
{
ExportCustom();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actExportAGDXExecute(TObject * Sender)
{
btnExport->Hint = actExportAGDX->Hint;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actExportAGDExecute(TObject * Sender)
{
btnExport->Hint = actExportAGD->Hint;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::actExportCustomExecute(TObject * Sender)
{
btnExport->Hint = actExportCustom->Hint;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OpenFile(const String & file)
{
g_ErrorReporter.Clear();
auto ext = Ioutils::TPath::GetExtension(file).LowerCase();
txtEvents->Lines->Clear();
memMessages->Lines->Clear();
if (ext == ".agd")
{
LoadAGDFile(file);
}
else
{
ImportSnapshot(file);
}
// report on the load
g_ErrorReporter.Add("Info: Window - " + IntToStr(m_Window.x) + ", " + IntToStr(m_Window.y) + " - " + IntToStr(m_Window.w) + "x" + IntToStr(m_Window.h));
g_ErrorReporter.Add("Info: Sprites - " + IntToStr((int)m_Sprites.size()));
g_ErrorReporter.Add("Info: Blocks - " + IntToStr((int)m_Blocks.size()));
g_ErrorReporter.Add("Info: Objects - " + IntToStr((int)m_Objects.size()));
RefreshImagesView();
RefreshMapView();
txtEvents->Lines->Add(m_EventCode);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::LoadAGDFile(const String & file)
{
if (TFile::Exists(file))
{
m_File = file;
Caption = "AGDv - " + file;
m_Blocks.clear();
m_Objects.clear();
m_Sprites.clear();
m_Font.clear();
m_Screens.clear();
m_Messages.clear();
m_Map = nullptr;
// Load the file
auto lines = System::Ioutils::TFile::ReadAllLines(file);
try
{
String data = "";
for (auto line : lines)
{
// find image marker
auto tline = line + "\r\n";
auto lline = tline.LowerCase();
// remove comments
auto cPos = tline.Pos(";");
if (cPos > 0)
{
tline = tline.SubString(1, cPos - 1);
}
if (tline.Length() > 0)
{
// process the line
if (lline.Pos("define") > 0 || lline.Pos("map") > 0 || lline.Pos("event"))
{
if (lline.Pos("endmap") == 1)
{
ConvertMap(data);
data = "";
}
else
{
if (data.Length() > 0)
{
// convert data
Convert(data);
}
}
// defining something
data = tline;
}
else
{
data += tline;
}
}
}
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while processing file. [" + file + "]");
}
}
else
{
g_ErrorReporter.Add("Error: File not found. [" + file + "]");
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ImportSnapshot(const String & file)
{
// Try importing the file
Importer importer;
if (importer.Convert(file))
{
m_File = file;
Caption = "AGDv - " + file;
m_Blocks.clear();
m_Objects.clear();
m_Sprites.clear();
m_Font.clear();
m_Screens.clear();
m_Messages.clear();
m_Events.clear();
m_EventCode = importer.Events;
m_Map = nullptr;
auto window = importer.Window;
ConvertWindow(window);
m_Keys = importer.Keys;
m_JumpTable = importer.JumpTable;
auto blocks = SplitString(importer.Blocks, "ª");
for (auto block : blocks)
{
block = block.Trim();
if (block != "")
{
auto pdata = PreProcess(block);
ConvertBlock(pdata);
}
}
auto objects = SplitString(importer.Objects, "ª");
for (auto object : objects)
{
object = object.Trim();
if (object != "")
{
auto pdata = PreProcess(object);
ConvertObject(pdata);
}
}
auto sprites = SplitString(importer.Sprites, "ª");
for (auto sprite : sprites)
{
sprite = sprite.Trim();
if (sprite != "")
{
auto pdata = PreProcess(sprite);
ConvertSprite(pdata, importer.SpriteHeight);
}
}
auto fonts = SplitString(importer.Font, "ª");
for (auto font : fonts)
{
font = font.Trim();
if (font != "")
{
ConvertFont(font);
}
}
auto screens = SplitString(importer.Screens, "ª");
for (auto screen : screens)
{
screen = screen.Trim();
if (screen != "")
{
auto pdata = PreProcess(screen);
ConvertScreen(pdata);
}
}
auto msgs = SplitString(importer.Messages, "ª");
for (auto msg : msgs)
{
msg = msg.Trim();
if (msg != "")
{
ConvertMessages(msg);
}
}
ConvertMap(importer.Map);
ConvertEventCode();
}
}
//---------------------------------------------------------------------------
String __fastcall TfrmMain::PreProcess(const String & data) const
{
String line;
auto sPos = data.Pos(' ');
auto tPos = data.Pos('\t');
try
{
sPos = sPos != 0 && tPos != 0 ? std::min(sPos, tPos) : (sPos == 0 ? tPos : sPos);
// remove the define/map/endmap statement
line = data.SubString(sPos, data.Length()).Trim();
// replace all newlines + tabs with space
int rPos, nPos;
do
{
rPos = line.Pos("\r");
if (rPos > 0)
{
line = line.SubString(1, rPos - 1) + + " " + line.SubString(rPos + 1, line.Length());
}
nPos = line.Pos("\n");
if (nPos > 0)
{
line = line.SubString(1, nPos - 1) + + " " + line.SubString(nPos + 1, line.Length());
}
tPos = line.Pos("\t");
if (tPos > 0)
{
line = line.SubString(1, tPos - 1) + " " + line.SubString(tPos + 1, line.Length());
}
}
while (rPos != 0 || nPos != 0 || tPos != 0);
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while pre-processing white space");
throw;
}
// change ',' to ' '
for (auto i = 1; i <= line.Length(); i++)
{
if (line[i] == ',')
{
line[i] = ' ';
}
}
// all data should now be on 1 line
// convert hex to decimal
try
{
int hPos = 0;
do
{
hPos = line.Pos("$");
if (hPos > 0)
{
auto l1 = line.SubString(1, hPos - 1).Trim();
auto l2 = line.SubString(hPos + 1, line.Length());
auto l3 = String();
sPos = l2.Pos(' ');
if (sPos > 0)
{
l3 = l2.SubString(sPos, l2.Length()).Trim();
l2 = l2.SubString(1, sPos).Trim();
}
auto value = StrToInt("0x" + l2);
line = l1 + " " + IntToStr(value) + " " + l3;
}
}
while (hPos != 0);
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while pre-processing hex into decimal");
throw;
}
return line.Trim();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Convert(const String & data)
{
try
{
auto ld = data.LowerCase();
if (ld.Pos("event ") == 1)
{
txtEvents->Lines->Add(data);
m_Events.push_back(data);
}
else if (ld.Pos("definemessages") == 0)
{
auto pdata = PreProcess(data);
if (ld.Pos("defineblock") == 1)
{
ConvertBlock(pdata);
}
else if (ld.Pos("defineobject") == 1)
{
ConvertObject(pdata);
}
else if (ld.Pos("definesprite") == 1)
{
ConvertSprite(pdata, 16);
}
else if (ld.Pos("definefont") == 1)
{
ConvertFont(pdata);
}
else if (ld.Pos("definewindow") == 1)
{
ConvertWindow(pdata);
}
else if (ld.Pos("definescreen") == 1)
{
ConvertScreen(pdata);
}
}
else
{
ConvertMessages(data);
}
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting data to valid AGD type");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ConvertScreen(const String & data)
{
try
{
// convert the data
m_Screens.push_back(std::move(std::make_unique<GameScreen>(data, m_Screens.size())));
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting Screen data");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ConvertMap(const String & data)
{
try
{
auto pdata = PreProcess(data);
m_Map = std::make_unique <Map> (pdata);
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting Map data");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ConvertEventCode()
{
try
{
auto lines = SplitString(m_EventCode, "\n");
String content;
for (auto line : lines)
{
line += "\n";
if (line.SubString(1, 5).UpperCase() == "EVENT" && content != "")
{
m_Events.push_back(content);
content = "";
}
if (line.Trim() != "")
{
content += line;
}
}
m_Events.push_back(content);
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting Event code");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ConvertWindow(const String & data)
{
try
{
auto tokens = SplitString(data, " ");
auto i = 0;
for (auto token : tokens)
{
switch (i++)
{
case 0:
m_Window.y = StrToInt(token);
break;
case 1:
m_Window.x = StrToInt(token);
break;
case 2:
m_Window.h = StrToInt(token);
break;
case 3:
m_Window.w = StrToInt(token);
break;
}
}
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting Window data");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ConvertBlock(const String & data)
{
try
{
// convert the data
// get the type
auto tPos = data.Pos(" ");
auto subType = data.SubString(1, tPos - 1);
auto idata = data.SubString(tPos + 1, data.Length());
m_Blocks.push_back(std::move(std::make_unique <ImageBlock> (subType, idata)));
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting Block data");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ConvertObject(const String & data)
{
try
{
// convert the data
m_Objects.push_back(std::move(std::make_unique <ImageObject> (data)));
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting Object data");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ConvertSprite(const String & data, unsigned int spriteHeight)
{
try
{
// convert the data
m_Sprites.push_back(std::move(std::make_unique <ImageSprite> (data, spriteHeight)));
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting Sprite data");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ConvertFont(const String & data)
{
try
{
// convert the data
m_Font.push_back(std::move(std::make_unique <ImageFont> (data)));
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting Font data");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ConvertMessages(const String & data)
{
try
{
auto mdata = data;
// convert the data
if (data.Pos("definemessages "))
{
auto mPos = data.Pos(" ");
mdata = data.SubString(mPos + 1, data.Length());
}
//auto tokens = SplitString(mdata, "\"");
m_Messages.push_back(mdata);
memMessages->Lines->Add(mdata);
// for (auto token : tokens)
// {
// token = token.Trim();
// if (token != "")
// {
// m_Messages.push_back(token1;
// memMessages->Lines->Add("\"" + token + "\"");
// }
// }
}
catch (...)
{
g_ErrorReporter.Add("Error: Exception caught while converting Message data");
throw;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::DisplayImages(const String & title, const ImageList & images, int & y, int scale, int index)
{
auto my = 0;
auto x = 8;
imgView->Picture->Bitmap->Canvas->Pen->Color = clWhite;
imgView->Picture->Bitmap->Canvas->TextOut(8, y, title);
y += imgView->Picture->Bitmap->Canvas->TextHeight(title) + 8;
m_ImageY[index] = y;
for (const auto &image : images)
{
my = std::max(my, image->Height * scale);
int w = image->Width * image->Frames * image->ScalarX * scale;
if (x + w > imgView->Width - 8)
{
x = 8;
y += image->Height * scale + 8;
}
x += image->Draw(x, y, imgView->Picture->Bitmap, scale, Timer1->Enabled ? m_Animate : -1) + 8;
}
y += my + 32;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::DisplayMessasges(const String & title, int & y, int scale)
{
if (m_Font.size() == 0)
{
// initialise the ZX Spectrum font
auto bmp = std::make_unique <TBitmap> ();
bmp->Assign(imgZXFont->Picture->Graphic);
m_Font.push_back(std::move(std::make_unique <ImageFont> (bmp.get())));
}
y -= 16;
imgView->Picture->Bitmap->Canvas->Pen->Color = clWhite;
imgView->Picture->Bitmap->Canvas->TextOut(8, y, title);
y += imgView->Picture->Bitmap->Canvas->TextHeight(title) + 8;
const auto font = dynamic_cast <ImageFont *> (m_Font[0].get());
auto newLined = false;
for (const auto &message : m_Messages)
{
auto x = 32;
for (const auto &chr : message)
{
if (chr - 32 >= 0)
{
x += font->DrawChr(x, y, imgView->Picture->Bitmap, scale, chr - 32);
newLined = false;
}
else
{
if (!newLined)
{
x = 32;
y += font->Height * scale;
newLined = true;
}
}
}
y += font->Height * scale;
}
y += 16;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::RefreshImagesView()
{
// clear the viewer
BitBlt(imgView->Picture->Bitmap->Canvas->Handle, 0, 0, imgView->Picture->Bitmap->Width, imgView->Picture->Bitmap->Height, NULL, 0, 0, BLACKNESS);
// display all the images types
auto y = 8;
DisplayImages("FONT", m_Font, y, m_Scale, 0);
DisplayMessasges("MESSAGES", y, m_Scale);
DisplayImages("BLOCKS", m_Blocks, y, m_Scale, 1);
DisplayImages("OBJECTS", m_Objects, y, m_Scale, 2);
DisplayImages("SPRITES", m_Sprites, y, m_Scale, 3);
// update the statistics
barStatus->Panels->Items[0]->Text = "Blocks: " + IntToStr((int)m_Blocks.size());
barStatus->Panels->Items[1]->Text = "Objects: " + IntToStr((int)m_Objects.size());
barStatus->Panels->Items[2]->Text = "Sprites: " + IntToStr((int)m_Sprites.size());
barStatus->Panels->Items[3]->Text = "Font: " + IntToStr((int)m_Font.size());
barStatus->Panels->Items[4]->Text = "Messages: " + IntToStr((int)m_Messages.size());
// refresh the image
if (y + 32 != imgView->Height)
{
imgView->Height = y + 32;
imgView->Picture->Bitmap->Height = imgView->Height;
RefreshImagesView();
}
if (m_ImageCursorPos.Y != 0)
{
// draw the image cursor
imgView->Picture->Bitmap->Canvas->Pen->Color = clFuchsia;
imgView->Picture->Bitmap->Canvas->Rectangle(m_ImageCursorPos.X, m_ImageCursorPos.Y, m_ImageCursorPos.X + m_ImageCursorSize.cx, m_ImageCursorPos.Y + m_ImageCursorSize.cy);
imgView->Picture->Bitmap->Canvas->Pen->Color = clBlack;
}
imgView->Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::RefreshMapView()
{
if (m_Map)
{
m_Map->Draw(imgMap->Picture->Bitmap, (int)m_Scale, m_Window, m_Blocks, m_Objects, m_Sprites, m_Screens);
imgMap->Width = imgMap->Picture->Bitmap->Width;
imgMap->Height = imgMap->Picture->Bitmap->Height;
imgMap->Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ExportAGDXProject()
{
int id = 1;
auto project = std::make_unique <Project> ();
auto name = System::File::NameWithoutExtension(m_File);
project->Create(name, "ZX Spectrum 256x192 16 Colour");
project->WindowInfo = m_Window;
project->Save();
// save messages
auto path = System::Path::Project;
auto file = System::File::Combine(path, "Messages.txt");
for (auto msg : m_Messages)
{
System::File::AppendText(file, msg + "\r\n");
}
// save events
std::map <String, String> EventToFileMap;
EventToFileMap["EVENT PLAYER"] = "Player control";
EventToFileMap["EVENT SPRITETYPE1"] = "Sprite type 1";
EventToFileMap["EVENT SPRITETYPE2"] = "Sprite type 2";
EventToFileMap["EVENT SPRITETYPE3"] = "Sprite type 3";
EventToFileMap["EVENT SPRITETYPE4"] = "Sprite type 4";
EventToFileMap["EVENT SPRITETYPE5"] = "Sprite type 5";
EventToFileMap["EVENT SPRITETYPE6"] = "Sprite type 6";
EventToFileMap["EVENT SPRITETYPE7"] = "Sprite type 7";
EventToFileMap["EVENT SPRITETYPE8"] = "Sprite type 8";
EventToFileMap["EVENT GAMEINIT"] = "Game initialisation";
EventToFileMap["EVENT INITSPRITE"] = "Initialise sprite";
EventToFileMap["EVENT KILLPLAYER"] = "Kill player";
EventToFileMap["EVENT INTROMENU"] = "Introduction menu";
EventToFileMap["EVENT MAINLOOP1"] = "Main loop 1";
EventToFileMap["EVENT MAINLOOP2"] = "Main loop 2";
EventToFileMap["EVENT RESTARTSCREEN"] = "Restart screen";
EventToFileMap["EVENT FELLTOOFAR"] = "Fell too far";
EventToFileMap["EVENT LOSTGAME"] = "Lost game";
EventToFileMap["EVENT COMPLETEDGAME"] = "Completed game";
EventToFileMap["EVENT NEWHIGHSCORE"] = "New high score";
EventToFileMap["EVENT COLLECTBLOCK"] = "Collect block";
for (auto code : m_Events)
{
auto event = code.SubString(1, code.Pos("\r") - 1);
// write the file out
if (EventToFileMap.count(event) == 1)
{
auto file = System::File::Combine(path, EventToFileMap[event] + ".event");
System::File::AppendText(file, ";" + code);
// // read the file back
// auto lines = System::File::ReadLines(file);
// // replace SCREEN = with new reference
// auto newFile = String();
// for (auto line : lines)
// {
// auto p = line.UpperCase().Pos("SCREEN");
// if (p > 0)
// {
// p += 6;
// // find the start of the room number
// for (auto i = p; i <= line.Length(); i++)
// {
// if (IsNumber(line[i]))
// {
// auto roomNum = StrToInt(line.SubString(i, line.Length()));
// // replace the room number with the new room x, y
// auto roomPt = m_Map->GetRoomPt(roomNum);
// auto roomCoord = " " + IntToStr((int)roomPt.X) + ", " + IntToStr((int)roomPt.Y);
// line = line.SubString(1, i - 1) + roomCoord;
// break;
// }
// }
// }
// newFile += line + "\r\n";
// }
// System::File::WriteText(file, newFile);
}
}
// save images
// font
if (m_Font.size() == 1)
{
auto file = System::File::Combine(path, "Game Font.json");
String content = "{\r\n \"Image\": {\r\n \"Width\": 8,\r\n \"Height\": 8,\r\n \"Frames\": [\r\n";
BitmapData data;
for (const auto &chr : m_Font)
{
String line = " \"";
chr->GetBitmapData(data);
int i = 0;
for (auto byte : data)
{
line += IntToHex(byte, 2);
if ((++i % 8) == 0)
{
content += line + "07\"" + (i == data.size() ? "" : ",") + "\r\n";
line = " \"";
}
}
}
content += " ]\r\n }\r\n}";
System::File::WriteText(file, content);
project->AddFile("Game Font.json", "Image", "Character Set");
project->Save();
}
auto wx = m_Window.x * 8;
auto wy = m_Window.y * 8;
// objects
int objId = id + 1;
if (m_Objects.size() != 0)
{
auto i = 0;
for (auto &obj : m_Objects)
{
auto object = dynamic_cast<ImageObject*>(obj.get());
object->Id = ++id;
auto file = System::File::Combine(path, "Object " + IntToStr(++i) + ".json");
String content = "{\r\n \"Document\": {\r\n \"RefId\": " + IntToStr(object->Id) + "\r\n },\r\n \"Image\": {\r\n \"Width\": 16,\r\n \"Height\": 16,\r\n";
content += " \"RoomIndex\": " + IntToStr(object->Room) + ",\r\n";
content += " \"Position\": {\r\n \"X\": " + IntToStr((int)object->Position.X - wx) + ",\r\n \"Y\": " + IntToStr((int)object->Position.Y - wy) + "\r\n },\r\n";
content += " \"State\": " + IntToStr(object->Room < 254 ? 0 : (object->Room == 254 ? 1 : 2)) + ",\r\n";
content += " \"Frames\": [\r\n";
BitmapData data;
String line = " \"";
obj->GetBitmapData(data);
for (auto byte : data)
{
line += IntToHex(byte, 2);
}
for (auto j = 0; j < 4; j++)
{
line += IntToHex(obj->Attribute);
}
content += line + "\"\r\n ]\r\n }\r\n}";
System::File::WriteText(file, content);
project->AddFile(System::File::NameWithExtension(file), "Image", "Object");
project->Save();
}
}
// sprites
int sprId = id + 1;
if (m_Sprites.size() != 0)
{
auto i = 0;
for (auto &obj : m_Sprites)
{
obj->Id = ++id;
auto file = System::File::Combine(path, "Sprite " + IntToStr(++i) + ".json");
String content = "{\r\n \"Document\": {\r\n \"RefId\": " + IntToStr(obj->Id) + "\r\n },\r\n \"Image\": {\r\n \"Width\": " + IntToStr(obj->Width) + ",\r\n \"Height\": " + IntToStr(obj->Height) + ",\r\n \"Frames\": [\r\n";
BitmapData data;
obj->GetBitmapData(data);
auto stride = 2 * obj->Height;
for (auto f = 0; f < obj->Frames; f++)
{
String line = " \"";
for (auto i = 0; i < 2 * obj->Height; i++)
{
line += IntToHex(data[(f * stride) + i], 2);
}
for (auto j = 0; j < 4; j++)
{
line += IntToHex(0x47, 2);
}
content += line + "\"" + (f == obj->Frames - 1 ? "" : ",") + "\r\n";
}
content += " ]\r\n }\r\n}";
System::File::WriteText(file, content);
project->AddFile(System::File::NameWithExtension(file), "Image", "Sprite");
project->Save();
}
}
// blocks/tiles
int blkId = id + 1;
if (m_Blocks.size() != 0)
{
std::map <String, String> blockTypeMap;
blockTypeMap["E"] = "0";
blockTypeMap["P"] = "1";
blockTypeMap["W"] = "2";
blockTypeMap["L"] = "3";
blockTypeMap["F"] = "4";
blockTypeMap["D"] = "5";
blockTypeMap["C"] = "6";
auto i = 0;
for (auto &obj : m_Blocks)
{
obj->Id = ++id;
auto file = System::File::Combine(path, "Block " + IntToStr(++i) + ".json");
String content = "{\r\n \"Document\": {\r\n \"RefId\": " + IntToStr(obj->Id) + "\r\n },\r\n \"Image\": {\r\n \"Width\": " + IntToStr(obj->Width) + ",\r\n \"Height\": " + IntToStr(obj->Height) + ",\r\n \"Frames\": [\r\n";
BitmapData data;
String line = " \"";
obj->GetBitmapData(data);
for (auto byte : data)
{
line += IntToHex(byte, 2);
}
line += IntToHex(obj->Attribute);
content += line + "\"\r\n ],\r\n \"Layers\": [\r\n {\r\n \"Name\": \"blocktype\",\r\n \"Data\": \"" + blockTypeMap[obj->SubType[1]] + "\"\r\n }\r\n ]\r\n }\r\n}";
System::File::WriteText(file, content);
project->AddFile(System::File::NameWithExtension(file), "Image", "Tile");
project->Save();
}
}
// save map/screens
if (m_Map->Width > 0 && m_Map->Height > 0)
{
auto file = System::File::Combine(path, "Tile Map.json");
String content = "{\r\n \"Map\": {"
"\r\n \"StartLocation\": " + IntToStr(m_Map->StartScreen) + ","
"\r\n \"Entities\": [\r\n";
// output each rooms entity list
auto screensDone = std::vector<int>();
auto rx = 0;
auto ry = 0;
for (auto y = 0; y < 16; y++)