This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegbop.c
8423 lines (7947 loc) · 240 KB
/
legbop.c
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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* legbop v1.0 (December 2022)
* Copyright (C) 2016-2022 Norbert de Jonge <[email protected]>
*
* 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 3 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, see [ www.gnu.org/licenses/ ].
*
* To properly read this code, set your program's tab stop to: 2.
*/
/*========== Includes ==========*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#if defined WIN32 || _WIN32 || WIN64 || _WIN64
#include <windows.h>
#undef PlaySound
#endif
#include <SDL.h>
#include <SDL_audio.h>
#include <SDL_thread.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
/*========== Includes ==========*/
/*========== Defines ==========*/
#if defined WIN32 || _WIN32 || WIN64 || _WIN64
#define SLASH "\\"
#define DEVNULL "NUL"
#else
#define SLASH "/"
#define DEVNULL "/dev/null"
#endif
#define EXIT_NORMAL 0
#define EXIT_ERROR 1
#define EDITOR_NAME "legbop"
#define EDITOR_VERSION "v1.0 (December 2022)"
#define COPYRIGHT "Copyright (C) 2022 Norbert de Jonge"
#define OFFSET_LEVEL0 0x1DCC4 /*** various ***/
#define MAX_LEVEL_SIZE 20000
#define LEVELS 17
#define ROOMS 24
#define TILES 30
#define EVENTS 256
#define ROM_DIR "rom"
#define BACKUP ROM_DIR SLASH "rom.bak"
#define MAX_PATHFILE 200
#define MAX_TOWRITE 720
#define WINDOW_WIDTH 640 + 2 + 50 /*** 692 ***/
#define WINDOW_HEIGHT 496 + 2 + 75 /*** 573 ***/
#define MAX_IMG 200
#define MAX_CON 30
#define MAX_TYPE 10
#define REFRESH 25 /*** That is 40 frames per second, 1000/25. ***/
#define FONT_SIZE_15 15
#define FONT_SIZE_11 11
#define FONT_SIZE_20 20
#define NUM_SOUNDS 20 /*** Sounds that may play at the same time. ***/
#define MAX_TEXT 100
#define ADJ_BASE_X 422
#define ADJ_BASE_Y 60
#define MAX_OPTION 100
#define BYTE_SIZE 255
#define UNKNOWN 145
#define SAVE_SPACE 4
#define WARN_BYTES_FREE 100
#define MAX_WARNING 200
#define MAX_ERROR 200
#define MAX_INFO 200
/*** Intro slides. ***/
#define SLIDES 5
#define SLIDES_LINES 4
#define SLIDES_LINES_CHARS 14 /*** 13 + \0 ***/
#define SLIDES_BYTES 150
#define SLIDES_BYTES_MAX SLIDES * SLIDES_LINES * SLIDES_LINES_CHARS
#define SLIDES_OFFSET 0x13016
#define VERIFY_OFFSET 0x134
#define VERIFY_TEXT "PRINCE O PERSIA" /*** Sic. ***/
#define VERIFY_SIZE 15
#define BROKEN_ROOM_X 438
#define BROKEN_ROOM_Y 76
#define BROKEN_LEFT_X 423
#define BROKEN_LEFT_Y 76
#define BROKEN_RIGHT_X 453
#define BROKEN_RIGHT_Y 76
#define BROKEN_UP_X 438
#define BROKEN_UP_Y 61
#define BROKEN_DOWN_X 438
#define BROKEN_DOWN_Y 91
#define PNG_VARIOUS "various"
#define PNG_LIVING "living"
#define PNG_SLIVING "sliving"
#define PNG_BUTTONS "buttons"
#define PNG_EXTRAS "extras"
#define PNG_ROOMS "rooms"
#define PNG_GAMEPAD "gamepad"
#define PNG_ALPHABET "alphabet"
#define OFFSETD_X 25 /*** Pixels from the left, where tiles are visible. ***/
#define OFFSETD_Y 50 /*** Pixels from the top, where tiles are visible. ***/
#define TTPD_1 -16 /*** Top row, pixels behind interface. ***/
#define TTPD_O 0 /*** Other rows, pixels behind superjacent rows. ***/
#define DD_X 64 /*** Horizontal distance between (overlapping) tiles. ***/
#define DD_Y 160 /*** Vertical distance between (overlapping) tiles. ***/
#define TILEWIDTH 63 /*** On tiles screen. ***/
#define TILEHEIGHT 79 /*** On tiles screen. ***/
#define TILESX1 2 + (TILEWIDTH + 2) * 0
#define TILESX2 2 + (TILEWIDTH + 2) * 1
#define TILESX3 2 + (TILEWIDTH + 2) * 2
#define TILESX4 2 + (TILEWIDTH + 2) * 3
#define TILESX5 2 + (TILEWIDTH + 2) * 4
#define TILESX6 2 + (TILEWIDTH + 2) * 5
#define TILESX7 2 + (TILEWIDTH + 2) * 6
#define TILESX8 2 + (TILEWIDTH + 2) * 7
#define TILESX9 2 + (TILEWIDTH + 2) * 8
#define TILESX10 2 + (TILEWIDTH + 2) * 9
#define TILESY1 2 + (TILEHEIGHT + 2) * 0
#define TILESY2 2 + (TILEHEIGHT + 2) * 1
#define TILESY3 2 + (TILEHEIGHT + 2) * 2
#define TILESY4 2 + (TILEHEIGHT + 2) * 3
#define TILESY5 2 + (TILEHEIGHT + 2) * 4
#define TILESY6 2 + (TILEHEIGHT + 2) * 5
/*** Playtesting. ***/
#define OFFSET_TRAINING 0x664
#define OFFSET_START 0x2E3
#define OFFSET_REDORB 0x23E
#ifndef O_BINARY
#define O_BINARY 0
#endif
/*========== Defines ==========*/
int iDebug;
unsigned char arLevel[MAX_LEVEL_SIZE + 2];
unsigned char arLevelOut[MAX_LEVEL_SIZE + 2];
int iLevelSize;
int iLevelRead;
char sPathFile[MAX_PATHFILE + 2];
int iChanged;
int iScreen;
TTF_Font *font1;
TTF_Font *font2;
TTF_Font *font3;
SDL_Window *window;
SDL_Renderer *ascreen;
int iScale;
int iFullscreen;
SDL_Cursor *curArrow;
SDL_Cursor *curWait;
SDL_Cursor *curHand;
SDL_Cursor *curText;
int iNoAudio;
int iNoController;
int iPreLoaded;
int iDownAt;
int iSelected;
int iChangeEvent;
int iCurLevel;
int iExtras;
int arBrokenRoomLinks[LEVELS + 2];
int iCurRoom;
int iMovingRoom;
int iMovingNewBusy;
int iChangingBrokenRoom;
int iChangingBrokenSide;
int iLastTile;
int iXPos, iYPos;
int iInfo;
int arMovingRooms[ROOMS + 1 + 2][ROOMS + 2];
unsigned int gamespeed;
Uint32 looptime;
char cCurType;
int arDone[ROOMS + 2];
int iStartRoomsX, iStartRoomsY;
int iMovingNewX, iMovingNewY;
int iMinX, iMaxX, iMinY, iMaxY;
int iMovingOldX, iMovingOldY;
int arRoomConnectionsBroken[LEVELS + 2][ROOMS + 2][4 + 2];
int iOnTile;
int iCloseOn;
int iHelpOK;
int iEXESave;
int iOKOn;
int iYesOn;
int iNoOn;
int iCopied;
int iStartLevel;
int iOutOffset;
int iRepeatOffset;
int iHighStore;
unsigned char sUnknown[UNKNOWN + 2];
int iCustomTile;
int iEventTooltip, iEventTooltipOld;
int iCustomHover, iCustomHoverOld;
int iMednafen;
char sInfo[MAX_INFO + 2];
int iNoAnim;
int iFlameFrame;
int iModified;
/*** EXE ***/
int iEXEMinutesLeft;
int iEXEHitPoints;
int iEXEHair, iEXEHairR, iEXEHairG, iEXEHairB;
int iEXESkin, iEXESkinR, iEXESkinG, iEXESkinB;
int iEXESuit, iEXESuitR, iEXESuitG, iEXESuitB;
unsigned char sIntroSlides[SLIDES_BYTES_MAX + 2];
char arIntroSlides[SLIDES + 2]
[SLIDES_LINES + 2][SLIDES_LINES_CHARS + 2];
int iS, iL, iN;
int iBytesLeft;
int arSlideSizes[SLIDES + 2];
/*** for text ***/
SDL_Color color_bl = {0x00, 0x00, 0x00, 255};
SDL_Color color_wh = {0xff, 0xff, 0xff, 255};
SDL_Color color_blue = {0x00, 0x00, 0xff, 255};
SDL_Color color_red = {0xff, 0x00, 0x00, 255};
SDL_Surface *message;
SDL_Texture *messaget;
SDL_Rect offset;
/*** for copying ***/
unsigned char arCopyPasteTile[TILES + 2];
unsigned char cCopyPasteGuardTile;
unsigned char cCopyPasteGuardDir;
/*** controller ***/
int iController;
SDL_GameController *controller;
char sControllerName[MAX_CON + 2];
SDL_Joystick *joystick;
SDL_Haptic *haptic;
Uint32 joyleft, joyright, joyup, joydown;
Uint32 trigleft, trigright;
int iXJoy1, iYJoy1, iXJoy2, iYJoy2;
/*** These are the levels. ***/
unsigned char arRoomTiles[LEVELS + 2][ROOMS + 2][TILES + 2];
unsigned char arRoomLinks[LEVELS + 2][ROOMS + 2][4 + 2];
unsigned char arStartLocation[LEVELS + 2][3 + 2];
unsigned char arGuardTile[LEVELS + 2][ROOMS + 2];
unsigned char arGuardDir[LEVELS + 2][ROOMS + 2];
unsigned char arEventsFromRoom[LEVELS + 2][EVENTS + 2];
unsigned char arEventsFromTile[LEVELS + 2][EVENTS + 2];
unsigned char arEventsOpenClose[LEVELS + 2][EVENTS + 2];
unsigned char arEventsToRoom[LEVELS + 2][EVENTS + 2];
unsigned char arEventsToTile[LEVELS + 2][EVENTS + 2];
int arNrEvents[LEVELS + 2];
int iDX, iDY, iTTP1, iTTPO;
int iHor[10 + 2];
int iVer0, iVer1, iVer2, iVer3, iVer4;
SDL_Texture *imgloading;
SDL_Texture *imgd[0xFF + 2][2 + 2];
SDL_Texture *imgp[0xFF + 2][2 + 2];
SDL_Texture *imgblack;
SDL_Texture *imgprincel[2 + 2], *imgprincer[2 + 2];
SDL_Texture *imgguardl[2 + 2], *imgguardr[2 + 2];
SDL_Texture *imgskeletonl[2 + 2], *imgskeletonr[2 + 2];
SDL_Texture *imgshadowl[2 + 2], *imgshadowr[2 + 2];
SDL_Texture *imgjaffarl[2 + 2], *imgjaffarr[2 + 2];
SDL_Texture *imgdisabled;
SDL_Texture *imgunk[2 + 2];
SDL_Texture *imgup_0;
SDL_Texture *imgup_1;
SDL_Texture *imgdown_0;
SDL_Texture *imgdown_1;
SDL_Texture *imgleft_0;
SDL_Texture *imgleft_1;
SDL_Texture *imgright_0;
SDL_Texture *imgright_1;
SDL_Texture *imgudno;
SDL_Texture *imglrno;
SDL_Texture *imgudnonfo;
SDL_Texture *imgprevon_0;
SDL_Texture *imgprevon_1;
SDL_Texture *imgnexton_0;
SDL_Texture *imgnexton_1;
SDL_Texture *imgprevoff;
SDL_Texture *imgnextoff;
SDL_Texture *imgbar;
SDL_Texture *imgextras[10 + 2];
SDL_Texture *imgroomson_0;
SDL_Texture *imgroomson_1;
SDL_Texture *imgroomsoff;
SDL_Texture *imgbroomson_0;
SDL_Texture *imgbroomson_1;
SDL_Texture *imgbroomsoff;
SDL_Texture *imgeventson_0;
SDL_Texture *imgeventson_1;
SDL_Texture *imgeventsoff;
SDL_Texture *imgsaveon_0;
SDL_Texture *imgsaveon_1;
SDL_Texture *imgsaveoff;
SDL_Texture *imgquit_0;
SDL_Texture *imgquit_1;
SDL_Texture *imgrl;
SDL_Texture *imgbrl;
SDL_Texture *imgsrc;
SDL_Texture *imgsrs;
SDL_Texture *imgsrm;
SDL_Texture *imgsrp;
SDL_Texture *imgsrb;
SDL_Texture *imgevents;
SDL_Texture *imgsele;
SDL_Texture *imgeventu;
SDL_Texture *imgsell;
SDL_Texture *imgdungeon;
SDL_Texture *imgpalace;
SDL_Texture *imgclosebig_0;
SDL_Texture *imgclosebig_1;
SDL_Texture *imgborderb;
SDL_Texture *imgborders;
SDL_Texture *imgbordersl;
SDL_Texture *imgborderbl;
SDL_Texture *imgfadedl;
SDL_Texture *imgpopup;
SDL_Texture *imgok[2 + 2];
SDL_Texture *imgsave[2 + 2];
SDL_Texture *imgpopup_yn;
SDL_Texture *imgyes[2 + 2];
SDL_Texture *imgno[2 + 2];
SDL_Texture *imghelp;
SDL_Texture *imgexe;
SDL_Texture *imgexewarning;
SDL_Texture *imgfadeds;
SDL_Texture *imgroom[25 + 2]; /*** 25 is "?", for all high room links ***/
SDL_Texture *imgetooltip;
SDL_Texture *imgchover;
SDL_Texture *imgmednafen;
SDL_Texture *imglinkwarnlr;
SDL_Texture *imglinkwarnud;
SDL_Texture *imgspriteflamed;
SDL_Texture *imgspriteflamep;
SDL_Texture *imgalphabet[26 + 2];
SDL_Texture *imgspace;
SDL_Texture *imgunknown;
SDL_Texture *imgseltextline;
SDL_Texture *imgvwarning;
struct sample {
Uint8 *data;
Uint32 dpos;
Uint32 dlen;
} sounds[NUM_SOUNDS];
void ShowUsage (void);
void GetPathFile (void);
void LoadLevels (void);
int DecompressLevel (int iFd, int iOffset);
void SaveLevels (void);
void PrintTileName (int iLevel, int iRoom, int iTile, int iTileValue);
void AddDuplicates (int iByteToWrite, int iNrDuplicates);
void PrIfDe (char *sString);
char cShowDirection (int iDirection);
char cShowOpenClose (int iOpenClose);
int CompressLevel (int iNrBytes);
int AddRepeat (int iNrRepeated);
void Quit (void);
void InitScreen (void);
void InitPopUpSave (void);
void ShowPopUpSave (void);
void LoadFonts (void);
void MixAudio (void *unused, Uint8 *stream, int iLen);
void PlaySound (char *sFile);
void PreLoadSet (char cTypeP, int iTile);
void PreLoad (char *sPath, char *sPNG, SDL_Texture **imgImage);
void ShowScreen (void);
void InitPopUp (void);
void ShowPopUp (void);
void Help (void);
void ShowHelp (void);
void EXE (void);
void ShowEXE (void);
void InitScreenAction (char *sAction);
void RunLevel (int iLevel);
int StartGame (void *unused);
void ClearRoom (void);
void UseTile (int iTile, int iLocation, int iRoom);
void Zoom (int iToggleFull);
void LinkMinus (void);
int BrokenRoomLinks (int iPrint);
void ChangeEvent (int iAmount, int iChangePos);
void ChangeCustom (int iAmount);
void Prev (void);
void Next (void);
void CallSave (void);
void Sprinkle (void);
void SetLocation (int iRoom, int iLocation, int iTile);
void FlipRoom (int iAxis);
void CopyPaste (int iAction);
int InArea (int iUpperLeftX, int iUpperLeftY,
int iLowerRightX, int iLowerRightY);
int MouseSelectAdj (void);
int OnLevelBar (void);
void ChangePos (void);
void RemoveOldRoom (void);
void AddNewRoom (int iX, int iY, int iRoom);
void LinkPlus (void);
void EventRoom (int iRoom, int iFromTo);
void EventTile (int iX, int iY, int iFromTo);
void ShowImage (SDL_Texture *img, int iX, int iY, char *sImageInfo);
void CustomRenderCopy (SDL_Texture* src, SDL_Rect* srcrect,
SDL_Rect *dstrect, char *sImageInfo);
void CreateBAK (void);
void DisplayText (int iStartX, int iStartY, int iFontSize,
char arText[9 + 2][MAX_TEXT + 2], int iLines, TTF_Font *font);
void InitRooms (void);
void WhereToStart (void);
void CheckSides (int iRoom, int iX, int iY);
void ShowRooms (int iRoom, int iX, int iY, int iNext);
void BrokenRoomChange (int iRoom, int iSide, int *iX, int *iY);
void ShowChange (void);
int OnTile (void);
void ChangePosAction (char *sAction);
void DisableSome (void);
int IsDisabled (int iTile);
void CenterNumber (int iNumber, int iX, int iY,
SDL_Color fore, int iHex);
int Unused (int iTile);
int RaiseDropEvent (int iTile, int iEvent, int iAmount);
void OpenURL (char *sURL);
void EXELoad (void);
void EXESave (void);
int PlusMinus (int *iWhat, int iX, int iY,
int iMin, int iMax, int iChange, int iAddChanged);
void ColorRect (int iX, int iY, int iW, int iH, int iR, int iG, int iB);
void ToFiveBitRGB (int iIn, int *iR, int *iG, int *iB);
void TotalEvents (int iAmount);
void GetOptionValue (char *sArgv, char *sValue);
int IsEven (int iValue);
void IntroSlides (void);
void ModifyForMednafen (int iLevel);
void ModifyBack (void);
/*****************************************************************************/
int main (int argc, char *argv[])
/*****************************************************************************/
{
int iArgLoop;
SDL_version verc, verl;
time_t tm;
char sStartLevel[MAX_OPTION + 2];
iDebug = 0;
iExtras = 0;
iLastTile = 0x00;
iInfo = 0;
iScale = 1;
iOnTile = 1;
iCopied = 0;
iNoAudio = 0;
iFullscreen = 0;
iNoController = 0;
iStartLevel = 1;
iCustomTile = 0x00;
iMednafen = 0;
iNoAnim = 0;
iModified = 0;
if (argc > 1)
{
for (iArgLoop = 1; iArgLoop <= argc - 1; iArgLoop++)
{
if ((strcmp (argv[iArgLoop], "-h") == 0) ||
(strcmp (argv[iArgLoop], "-?") == 0) ||
(strcmp (argv[iArgLoop], "--help") == 0))
{
ShowUsage();
}
else if ((strcmp (argv[iArgLoop], "-v") == 0) ||
(strcmp (argv[iArgLoop], "--version") == 0))
{
printf ("%s %s\n", EDITOR_NAME, EDITOR_VERSION);
exit (EXIT_NORMAL);
}
else if ((strcmp (argv[iArgLoop], "-d") == 0) ||
(strcmp (argv[iArgLoop], "--debug") == 0))
{
iDebug = 1;
}
else if ((strcmp (argv[iArgLoop], "-n") == 0) ||
(strcmp (argv[iArgLoop], "--noaudio") == 0))
{
iNoAudio = 1;
}
else if ((strcmp (argv[iArgLoop], "-z") == 0) ||
(strcmp (argv[iArgLoop], "--zoom") == 0))
{
iScale = 2;
}
else if ((strcmp (argv[iArgLoop], "-f") == 0) ||
(strcmp (argv[iArgLoop], "--fullscreen") == 0))
{
iFullscreen = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
else if ((strncmp (argv[iArgLoop], "-l=", 3) == 0) ||
(strncmp (argv[iArgLoop], "--level=", 8) == 0))
{
GetOptionValue (argv[iArgLoop], sStartLevel);
iStartLevel = atoi (sStartLevel);
if ((iStartLevel < 1) || (iStartLevel > LEVELS))
{
iStartLevel = 1;
}
}
else if ((strcmp (argv[iArgLoop], "-s") == 0) ||
(strcmp (argv[iArgLoop], "--static") == 0))
{
iNoAnim = 1;
}
else if ((strcmp (argv[iArgLoop], "-k") == 0) ||
(strcmp (argv[iArgLoop], "--keyboard") == 0))
{
iNoController = 1;
}
else
{
ShowUsage();
}
}
}
GetPathFile();
srand ((unsigned)time(&tm));
LoadLevels();
/*** Show the SDL version used for compiling and linking. ***/
if (iDebug == 1)
{
SDL_VERSION (&verc);
SDL_GetVersion (&verl);
printf ("[ INFO ] Compiled with SDL %u.%u.%u, linked with SDL %u.%u.%u.\n",
verc.major, verc.minor, verc.patch, verl.major, verl.minor, verl.patch);
}
InitScreen();
Quit();
return 0;
}
/*****************************************************************************/
void ShowUsage (void)
/*****************************************************************************/
{
printf ("%s %s\n%s\n\n", EDITOR_NAME, EDITOR_VERSION, COPYRIGHT);
printf ("Usage:\n");
printf (" %s [OPTIONS]\n\nOptions:\n", EDITOR_NAME);
printf (" -h, -?, --help display this help and exit\n");
printf (" -v, --version output version information and"
" exit\n");
printf (" -d, --debug also show levels on the console\n");
printf (" -n, --noaudio do not play sound effects\n");
printf (" -z, --zoom double the interface size\n");
printf (" -f, --fullscreen start in fullscreen mode\n");
printf (" -l=NR, --level=NR start in level NR\n");
printf (" -s, --static do not display animations\n");
printf (" -k, --keyboard do not use a game controller\n");
printf ("\n");
exit (EXIT_NORMAL);
}
/*****************************************************************************/
void GetPathFile (void)
/*****************************************************************************/
{
int iFound;
DIR *dDir;
struct dirent *stDirent;
char sExtension[100 + 2];
char sError[MAX_ERROR + 2];
char sVerify[VERIFY_SIZE + 2];
int iFd;
iFound = 0;
dDir = opendir (ROM_DIR);
if (dDir == NULL)
{
printf ("[FAILED] Cannot open directory \"%s\": %s!\n",
ROM_DIR, strerror (errno));
exit (EXIT_ERROR);
}
while ((stDirent = readdir (dDir)) != NULL)
{
if (iFound == 0)
{
if ((strcmp (stDirent->d_name, ".") != 0) &&
(strcmp (stDirent->d_name, "..") != 0))
{
snprintf (sExtension, 100, "%s", strrchr (stDirent->d_name, '.'));
if ((toupper (sExtension[1]) == 'G') &&
(toupper (sExtension[2]) == 'B') &&
(toupper (sExtension[3]) == 'C'))
{
iFound = 1;
snprintf (sPathFile, MAX_PATHFILE, "%s%s%s", ROM_DIR, SLASH,
stDirent->d_name);
if (iDebug == 1)
{
printf ("[ OK ] Found Game Boy Color ROM \"%s\".\n", sPathFile);
}
}
}
}
}
closedir (dDir);
if (iFound == 0)
{
snprintf (sError, MAX_ERROR, "Cannot find a .gbc ROM in"
" directory \"%s\"!", ROM_DIR);
printf ("[FAILED] %s\n", sError);
SDL_ShowSimpleMessageBox (SDL_MESSAGEBOX_ERROR,
"Error", sError, NULL);
exit (EXIT_ERROR);
}
/*** Is the file accessible? ***/
if (access (sPathFile, R_OK|W_OK) == -1)
{
printf ("[FAILED] Cannot access \"%s\": %s!\n",
sPathFile, strerror (errno));
exit (EXIT_ERROR);
}
/*** Is the file a PoP1 for GBC ROM file? ***/
iFd = open (sPathFile, O_RDONLY|O_BINARY);
if (iFd == -1)
{
printf ("[FAILED] Could not open \"%s\": %s!\n",
sPathFile, strerror (errno));
exit (EXIT_ERROR);
}
lseek (iFd, VERIFY_OFFSET, SEEK_SET);
read (iFd, sVerify, VERIFY_SIZE);
close (iFd);
sVerify[VERIFY_SIZE] = '\0';
if (strcmp (sVerify, VERIFY_TEXT) != 0)
{
snprintf (sError, MAX_ERROR, "File %s is not a Prince of Persia"
" for GBC ROM!", sPathFile);
printf ("[FAILED] %s\n", sError);
SDL_ShowSimpleMessageBox (SDL_MESSAGEBOX_ERROR,
"Error", sError, NULL);
exit (EXIT_ERROR);
}
}
/*****************************************************************************/
void LoadLevels (void)
/*****************************************************************************/
{
int iFd;
int iOffsetStart;
int iOffsetEnd;
int iLevel;
int iTileValue;
int iTiles;
int iTemp;
int iEventStart;
/*** Used for looping. ***/
int iRoomLoop;
int iTileLoop;
int iByteLoop;
int iSideLoop;
int iGuardLoop;
int iEventLoop;
int iUnknownLoop;
int iLevelLoop;
iFd = open (sPathFile, O_RDONLY|O_BINARY);
if (iFd == -1)
{
printf ("[FAILED] Could not open \"%s\": %s!\n",
sPathFile, strerror (errno));
exit (EXIT_ERROR);
}
iOffsetStart = OFFSET_LEVEL0;
for (iLevelLoop = 1; iLevelLoop <= LEVELS; iLevelLoop++)
{
/*** We present level 0 to users as level 15. ***/
switch (iLevelLoop)
{
case 1: iLevel = 15; break;
case 16: iLevel = 16; break;
case 17: iLevel = 17; break;
default: iLevel = iLevelLoop - 1; break;
}
/*** This is the princess room during the ending. ***/
if (iLevel == 16)
{
read (iFd, sUnknown, UNKNOWN);
iOffsetStart+=UNKNOWN;
if (iDebug == 1)
{
for (iUnknownLoop = 0; iUnknownLoop < UNKNOWN; iUnknownLoop++)
{
printf ("0x%02x ", sUnknown[iUnknownLoop]);
}
printf ("\n\n");
}
}
/*** Decompress the level into arLevel. ***/
if (iDebug == 1)
{
printf ("[ INFO ] Level %i starts at offset 0x%02x (%i).\n",
iLevel, iOffsetStart, iOffsetStart);
}
iOffsetEnd = DecompressLevel (iFd, iOffsetStart);
if (iDebug == 1)
{
printf ("[ INFO ] Level %i ends at offset 0x%02x (%i).\n",
iLevel, iOffsetEnd, iOffsetEnd);
printf ("[ INFO ] Compressed level size: %i\n",
iOffsetEnd - iOffsetStart + 1);
printf ("\n");
for (iByteLoop = 0; iByteLoop < iLevelSize; iByteLoop++)
{
printf ("0x%02x ", arLevel[iByteLoop]);
}
printf ("\n");
}
iOffsetStart = iOffsetEnd + 1;
/*** Extract tiles. ***/
iTiles = -1;
for (iRoomLoop = 1; iRoomLoop <= ROOMS; iRoomLoop++)
{
if (iDebug == 1)
{
printf ("\n[Level %i] Room %i:\n\n", iLevel, iRoomLoop);
}
for (iTileLoop = 1; iTileLoop <= TILES; iTileLoop++)
{
iTiles++;
iTileValue = arLevel[iTiles];
arRoomTiles[iLevel][iRoomLoop][iTileLoop] = iTileValue;
/*** Debug. ***/
if (iDebug == 1)
{
if (iTileValue == 0xFF)
{
printf ("Unused room.\n");
} else {
PrintTileName (iLevel, iRoomLoop, iTileLoop, iTileValue);
if ((iTileLoop == 10) || (iTileLoop == 20))
{
printf ("\n");
for (iTemp = 1; iTemp <= 79; iTemp++) { printf ("-"); }
printf ("\n");
} else if (iTileLoop != 30) { printf ("|"); }
}
}
if (iTileValue == 0xFF) { break; }
}
PrIfDe ("\n");
}
/*** Extract room links. ***/
PrIfDe ("[ OK ] Loading: Room Links\n");
for (iRoomLoop = 1; iRoomLoop <= ROOMS; iRoomLoop++)
{
for (iSideLoop = 1; iSideLoop <= 4; iSideLoop++)
{
iTiles++;
arRoomLinks[iLevel][iRoomLoop][iSideLoop] = arLevel[iTiles];
}
if (iDebug == 1)
{
printf ("[ INFO ] Room %i is connected to room (0 = none):"
" l%i, r%i, u%i, d%i\n", iRoomLoop,
arRoomLinks[iLevel][iRoomLoop][1],
arRoomLinks[iLevel][iRoomLoop][2],
arRoomLinks[iLevel][iRoomLoop][3],
arRoomLinks[iLevel][iRoomLoop][4]);
}
}
/*** Extract start location. ***/
iTiles++;
arStartLocation[iLevel][1] = arLevel[iTiles]; /*** Room. ***/
iTiles++;
arStartLocation[iLevel][2] = arLevel[iTiles] + 1; /*** Tile. ***/
iTiles++;
arStartLocation[iLevel][3] = arLevel[iTiles]; /*** Direction. ***/
if (iDebug == 1)
{
printf ("[ INFO ] The prince starts in room: %i, tile %i, turned: %c\n",
arStartLocation[iLevel][1], arStartLocation[iLevel][2],
cShowDirection (arStartLocation[iLevel][3]));
}
/*** Extract guards. ***/
for (iGuardLoop = 1; iGuardLoop <= ROOMS; iGuardLoop++)
{
iTiles++;
arGuardTile[iLevel][iGuardLoop] = arLevel[iTiles];
/*** This forces the direction bit to 0. Thanks Stack Overflow. ***/
arGuardTile[iLevel][iGuardLoop]
^= (-0 ^ arGuardTile[iLevel][iGuardLoop]) & (1 << 7);
arGuardTile[iLevel][iGuardLoop]++;
/*** Get the direction bit. ***/
if (arLevel[iTiles] >= 128)
{
arGuardDir[iLevel][iGuardLoop] = 0xFF; /*** l ***/
} else {
arGuardDir[iLevel][iGuardLoop] = 0x00; /*** r ***/
}
/***
switch (arLevel[iTiles] & 1) // 1 = 00000001
{
case 1: arGuardDir[iLevel][iGuardLoop] = 0xFF; break; // l
case 0: arGuardDir[iLevel][iGuardLoop] = 0x00; break; // r
}
***/
if (iDebug == 1)
{
if (arGuardTile[iLevel][iGuardLoop] <= TILES)
{
printf ("[ INFO ] A guard in room: %i, tile %i, turned: %c\n",
iGuardLoop, arGuardTile[iLevel][iGuardLoop],
cShowDirection (arGuardDir[iLevel][iGuardLoop]));
}
}
}
/*** Extract events. ***/
arNrEvents[iLevel] = 0;
for (iEventLoop = 1; iEventLoop <= EVENTS; iEventLoop++)
{
iEventStart = iTiles + ((iEventLoop - 1) * 5);
if (arLevel[iEventStart + 1] == 0xFF) { break; }
arEventsFromRoom[iLevel][iEventLoop] = arLevel[iEventStart + 1];
arEventsFromTile[iLevel][iEventLoop] = arLevel[iEventStart + 2] + 1;
arEventsOpenClose[iLevel][iEventLoop] = arLevel[iEventStart + 3];
arEventsToRoom[iLevel][iEventLoop] = arLevel[iEventStart + 4];
arEventsToTile[iLevel][iEventLoop] = arLevel[iEventStart + 5] + 1;
arNrEvents[iLevel]++;
if (iDebug == 1)
{
printf ("[ INFO ] Event: room %i, tile %i -%c- room %i tile %i\n",
arEventsFromRoom[iLevel][iEventLoop],
arEventsFromTile[iLevel][iEventLoop],
cShowOpenClose (arEventsOpenClose[iLevel][iEventLoop]),
arEventsToRoom[iLevel][iEventLoop],
arEventsToTile[iLevel][iEventLoop]);
}
}
PrIfDe ("[ OK ] Checking for broken room links.\n");
arBrokenRoomLinks[iLevel] = BrokenRoomLinks (1);
if (iDebug == 1)
{
printf ("[ OK ] Done processing level %i.\n\n", iLevel);
}
}
close (iFd);
}
/*****************************************************************************/
int DecompressLevel (int iFd, int iOffset)
/*****************************************************************************/
{
int iRead;
unsigned char sRead[2 + 2];
int iEOF;
int iSection;
int iRepeatedBytes;
int iSubLoop;
int arSubFrom[256 + 2];
int arSubTo[256 + 2];
int iNrDuplicates;
int iNrDuplicatesNext;
int iHighNibble, iLowNibble;
int iNeedDuplicates;
int iByteToWrite;
int iDone;
/*** Used for the level size. ***/
int iLevelSizeA;
int iLevelSizeB;
char sLevelSize[10 + 2];
lseek (iFd, iOffset, SEEK_SET);
iEOF = 0;
iSection = 0;
iLevelRead = 0;
iNrDuplicates = -1;
iNrDuplicatesNext = -1;
iRepeatedBytes = 0; /*** To prevent warnings. ***/
do {
iRead = read (iFd, sRead, 1);
iOffset++;
switch (iRead)
{
case -1:
printf ("[FAILED] Could not read from \"%s\": %s!\n",
sPathFile, strerror (errno));
exit (EXIT_ERROR);
break;
case 0: iEOF = 1; break;
default:
switch (iSection)
{
case 0:
/* The number of repeated bytes that have single
* byte replacements.
*/
iRepeatedBytes = sRead[0];
/* Store the repeated bytes and their single
* byte replacements.
*/
for (iSubLoop = 1; iSubLoop <= iRepeatedBytes; iSubLoop++)
{
iRead = read (iFd, sRead, 2);
iOffset+=2;
arSubFrom[iSubLoop] = sRead[1];
arSubTo[iSubLoop] = sRead[0];
if (iDebug == 1)
{
printf ("[ INFO ] Substitute: 0x%02x -> 0x%02x\n",
sRead[1], sRead[0]);
}
}
iSection++;
break;
case 1:
/*** Uncompressed level size. ***/
iLevelSizeA = sRead[0];
iRead = read (iFd, sRead, 1);
iOffset++;
iLevelSizeB = sRead[0];
snprintf (sLevelSize, 10, "%02x%02x", iLevelSizeB, iLevelSizeA);
iLevelSize = strtoul (sLevelSize, NULL, 16);
if (iDebug == 1)
{
printf ("[ INFO ] Uncompressed level size: %i (0x%02x 0x%02x)\n",
iLevelSize, iLevelSizeB, iLevelSizeA);
}
iSection++;
break;
case 2:
/*** Level. ***/
/*** Write the byte. ***/
iByteToWrite = sRead[0];
for (iSubLoop = 1; iSubLoop <= iRepeatedBytes; iSubLoop++)
{
if (arSubFrom[iSubLoop] == iByteToWrite)
{
iByteToWrite = arSubTo[iSubLoop];
break;
}
}