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
/
lemsop.c
7908 lines (7526 loc) · 248 KB
/
lemsop.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 */
/* lemsop v1.0 (December 2022)
* Copyright (C) 2019-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 <dirent.h>
#include <stdio.h>
#include <math.h> /*** For round() on WIN32. ***/
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h> /*** Maybe remove, and just change CHAR_BIT to 8? ***/
#include <string.h>
#include <ctype.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 PNG_VARIOUS "png" SLASH "various" SLASH
#define PNG_BUTTONS "png" SLASH "buttons" SLASH
#define PNG_GAMEPAD "png" SLASH "gamepad" SLASH
#define PNG_DUNGEON "png" SLASH "dungeon" SLASH
#define PNG_PALACE "png" SLASH "palace" SLASH
#define PNG_ELEMENTS "png" SLASH "elements" SLASH
#define PNG_EXTRAS "png" SLASH "extras" SLASH
#define PNG_ROOMS "png" SLASH "rooms" SLASH
#define PNG_CHARS "png" SLASH "chars" SLASH
#define EXIT_NORMAL 0
#define EXIT_ERROR 1
#define EDITOR_NAME "lemsop"
#define EDITOR_VERSION "v1.0 (December 2022)"
#define COPYRIGHT "Copyright (C) 2022 Norbert de Jonge"
#define SMS_DIR "sms"
#define BACKUP SMS_DIR SLASH "backup.bak"
#define MAX_PATHFILE 300
#define MAX_OPTION 100
#define NR_LEVELS 14
#define ROOMS 24
#define COLS 16
#define ROWS 12
#define MAX_ELEM 10
#define WINDOW_WIDTH 562 /*** (COLS * 32) + 50 ***/
#define WINDOW_HEIGHT 459 /*** (ROWS * 32) + 75 ***/
#define MAX_IMG 200
#define MAX_CON 30
#define NUM_SOUNDS 20 /*** Sounds that may play at the same time. ***/
#define BAR_FULL 435
#define MAX_TEXT 100
#define REFRESH 25 /*** That is 40 frames per second, 1000/25. ***/
#define MAX_TOWRITE 720
#define ADJ_BASE_X 291
#define ADJ_BASE_Y 62
#define MAX_STATUS 100 /*** Cannot be more than MAX_TEXT. ***/
#define MAX_ERROR 200
#define TEXTS 16
#define MAX_DATA 720
#define MAX_GUARDS 37
#define VERIFY_OFFSET 0x7FF0
#define VERIFY_TEXT "TMR SEGA"
#define VERIFY_SIZE 8
#define OFFSET_PASSWORD 0x2FEF7
/*** https://stackoverflow.com/a/3208376 ***/
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
#ifndef O_BINARY
#define O_BINARY 0
#endif
/*========== Defines ==========*/
int iDebug;
char sPathFile[MAX_PATHFILE + 2];
int iNoAudio;
int iScale;
int iFullscreen;
int iStartLevel;
int iNoController;
int iBrokenRoomLinks;
int iDone[ROOMS + 2];
int iCurRoom;
SDL_Window *window;
unsigned int iWindowID;
SDL_Renderer *ascreen;
unsigned int iActiveWindowID;
SDL_Cursor *curArrow;
SDL_Cursor *curWait;
SDL_Cursor *curHand;
int iPreLoaded;
int iNrToPreLoad;
int iCurrentBarHeight;
TTF_Font *font11;
TTF_Font *font15;
TTF_Font *font20;
int iStatusBarFrame;
int iScreen;
int iXPos, iYPos;
int iOKOn;
int iChanged;
int iCurLevel;
char cCurType;
int iDownAt;
int iSelectedCol;
int iSelectedRow;
int iExtras;
int iLastTile;
int iCloseOn;
int iOnTileCol;
int iOnTileRow;
int iOnTileColOld;
int iOnTileRowOld;
int iNoAnim;
int iMovingRoom;
int iMovingNewBusy;
int iChangingBrokenRoom;
int iChangingBrokenSide;
int iRoomArray[ROOMS + 1 + 2][ROOMS + 2];
int iMovingOldX;
int iMovingOldY;
int iMovingNewX;
int iMovingNewY;
int iHelpOK;
int iInfo;
int arElementsCopyPaste[MAX_ELEM + 2][9 + 2];
int arTilesCopyPaste[COLS + 2][ROWS + 2];
int iCopied;
char sStatus[MAX_STATUS + 2], sStatusOld[MAX_STATUS + 2];
int iModified;
int iPlaytest;
int iYesOn, iNoOn;
char arToSave[4 + 2][8 + 2];
int iActiveElement;
int iActiveGuard;
int iFlameFrameDP;
int iEXESave;
/*** Levels ("The level table"). ***/
/*** (0x60C9) ***/
int arOffsetTG1[NR_LEVELS + 2];
int arOffsetTG2[NR_LEVELS + 2];
int arOffsetTiles[NR_LEVELS + 2];
char arLevelType[NR_LEVELS + 2];
int arKidDir[NR_LEVELS + 2];
int arKidPosX[NR_LEVELS + 2];
int arKidPosY[NR_LEVELS + 2];
int arKidRoom[NR_LEVELS + 2];
int arKidAnim[NR_LEVELS + 2];
int arUnknown[NR_LEVELS + 2][4 + 2];
/*** (address) ***/
/*** Room links and elements ("Room links and objects table"). ***/
/*** (chunk length) ***/
int arOffsetsElements[ROOMS + 2];
int iRoomConnections[NR_LEVELS + 2][ROOMS + 2][4 + 2];
int arElements[NR_LEVELS + 2][ROOMS + 2][MAX_ELEM + 2][9 + 2];
/*** Tiles ("Graphics map") ***/
int arDefinitions[64 + 2];
int arTiles[ROOMS + 2][COLS + 2][ROWS + 2];
unsigned char sEmpty[32 + 2];
/*** Guards ("Guard table") ***/
int arGuardR[MAX_GUARDS + 2];
int arGuardG[MAX_GUARDS + 2];
int arGuardB[MAX_GUARDS + 2];
int arGuardGameLevel[MAX_GUARDS + 2];
int arGuardHP[MAX_GUARDS + 2];
int arGuardDir[MAX_GUARDS + 2];
int arGuardMove[MAX_GUARDS + 2];
int arValue[12 + 2]; /*** 12 is OK ***/
int arOffsetRlElTable[NR_LEVELS + 2];
int iRoomConnectionsBroken[ROOMS + 2][4 + 2];
int iMinX, iMaxX, iMinY, iMaxY, iStartRoomsX, iStartRoomsY;
unsigned int gamespeed;
Uint32 looptime;
static const int iOffsetLevelTable = 0x6A7C;
static const int iOffsetGuardTable = 0x6CAD;
static const int iOffsetRlElTable = 0xAD8E;
unsigned char sOldPassword[6 + 2];
static const char *arPasswords[NR_LEVELS + 1] =
{ "", "AAAAAA", "BJJHAK", "CJJHAL", "DJJHAM", "EJJHAN", "FJJHAO", "GJJHAP", "HJJHAQ", "IJJHAR", "JJJHAS", "KJJHAT", "LJJHAU", "MJJHAV", "NJJHAW" };
unsigned char arTextChars[TEXTS + 2][MAX_TEXT + 2];
int iTextActiveText, iTextActiveChar;
static const int arTextOffset[TEXTS + 1] = { 0x00, 0x2FD03, 0x2FD19, 0x2FD31, 0x2FD46, 0x2FD5D, 0x2FD75, 0x2FD8F, 0x2FDA7, 0x2FDC1, 0x2FDD9, 0x2FDF9, 0x2FE13, 0x2FE2E, 0x2FE46, 0x2FE61, 0x2FE79 };
static const int arTextLength[TEXTS + 1] = { -1, 21, 22, 20, 22, 23, 25, 23, 25, 21, 28, 21, 23, 23, 23, 23, 23 };
static const int arTextX[TEXTS + 1] = { -1, 42, 42, 42, 42, 42, 42, 42, 42, 42, 10, 42+16, 42+16, 42+16, 42+16, 42+16, 42+16 };
static const int arTextY[TEXTS + 1] = { -1, 66, 85, 123, 142, 161, 180, 199, 218, 237, 256, 275, 294, 313, 332, 351, 370 };
static const int broken_room_x = 307;
static const int broken_room_y = 78;
static const int broken_side_x[5] = { 0, 292, 322, 307, 307 };
static const int broken_side_y[5] = { 0, 78, 78, 63, 93 };
/*** controller ***/
int iController;
SDL_GameController *controller;
char sControllerName[MAX_CON + 2];
SDL_Joystick *joystick;
SDL_Haptic *haptic;
Uint32 joyleft;
Uint32 joyright;
Uint32 joyup;
Uint32 joydown;
Uint32 trigleft;
Uint32 trigright;
/*** for text ***/
SDL_Surface *message;
SDL_Texture *messaget;
SDL_Rect offset;
SDL_Color color_bl = {0x00, 0x00, 0x00, 255};
SDL_Color color_wh = {0xff, 0xff, 0xff, 255};
SDL_Color color_f4 = {0xf4, 0xf4, 0xf4, 255};
SDL_Texture *imgloading;
SDL_Texture *imgblack;
SDL_Texture *imgfaded;
SDL_Texture *imgpopup;
SDL_Texture *imgok[2 + 2];
SDL_Texture *imgdungeon[255 + 2];
SDL_Texture *imgpalace[255 + 2];
SDL_Texture *imgselected;
SDL_Texture *imgunknown;
SDL_Texture *imgmask;
SDL_Texture *imgleft_0, *imgleft_1;
SDL_Texture *imgright_0, *imgright_1;
SDL_Texture *imgup_0, *imgup_1;
SDL_Texture *imgdown_0, *imgdown_1;
SDL_Texture *imglrno, *imgudno, *imgudnonfo;
SDL_Texture *imgroomsoff, *imgroomson_0, *imgroomson_1;
SDL_Texture *imgbroomsoff, *imgbroomson_0, *imgbroomson_1;
SDL_Texture *imgelementsoff, *imgelementson_0, *imgelementson_1;
SDL_Texture *imgsaveoff, *imgsaveon_0, *imgsaveon_1;
SDL_Texture *imgquit_0, *imgquit_1;
SDL_Texture *imgprevoff, *imgprevon_0, *imgprevon_1;
SDL_Texture *imgnextoff, *imgnexton_0, *imgnexton_1;
/***/
SDL_Texture *imgkidl, *imgkidr;
SDL_Texture *imgguardl, *imgguardr;
SDL_Texture *imgskell, *imgskelr;
SDL_Texture *imgjaffarl, *imgjaffarr;
SDL_Texture *imgloose;
SDL_Texture *imgloosefall;
SDL_Texture *imggate;
SDL_Texture *imgsword;
SDL_Texture *imgspikes;
SDL_Texture *imgleveldoor;
SDL_Texture *imgchomper;
SDL_Texture *imgmouse;
SDL_Texture *imgshadowstep;
SDL_Texture *imgshadowfight;
SDL_Texture *imgshadowdrink;
SDL_Texture *imgpotionred;
SDL_Texture *imgpotionblue;
SDL_Texture *imgpotiongreen;
SDL_Texture *imgpotionpink;
SDL_Texture *imgmirror;
SDL_Texture *imgskelalive;
SDL_Texture *imgskelrespawn;
SDL_Texture *imgdropd;
SDL_Texture *imgdropdwall;
SDL_Texture *imgdropp;
SDL_Texture *imgdroppwall;
SDL_Texture *imgraised;
SDL_Texture *imgraisedwall;
SDL_Texture *imgraisep;
SDL_Texture *imgraisepwall;
SDL_Texture *imgprincess;
/***/
SDL_Texture *imgbar;
SDL_Texture *extras[10 + 2];
SDL_Texture *imgdungeont, *imgpalacet;
SDL_Texture *imgclosebig_0, *imgclosebig_1;
SDL_Texture *imgborder, *imgborderl;
SDL_Texture *imgrl, *imgbrl;
SDL_Texture *imgroom[25 + 2]; /*** 25 is "?", for all high room links ***/
SDL_Texture *imgsrc, *imgsrs, *imgsrm, *imgsrp, *imgsrb;
SDL_Texture *imgelements;
SDL_Texture *imgsele;
SDL_Texture *imgselg;
SDL_Texture *imghelp;
SDL_Texture *imgsave[2 + 2];
SDL_Texture *imgexe;
SDL_Texture *imgstatusbarsprite;
SDL_Texture *imgplaytest;
SDL_Texture *imgpopup_yn;
SDL_Texture *imglinkwarnlr, *imglinkwarnud;
SDL_Texture *imgyes[2 + 2], *imgno[2 + 2];
SDL_Texture *imgchars[58 + 2];
SDL_Texture *imgactivechar;
SDL_Texture *spriteflamed;
SDL_Texture *spriteflamep;
SDL_Texture *imgguards;
struct sample {
Uint8 *data;
Uint32 dpos;
Uint32 dlen;
} sounds[NUM_SOUNDS];
void ShowUsage (void);
void PrIfDe (char *sString);
void GetPathFile (void);
void LoadLevel (int iLevel);
int ReadFromFile (int iFd, char *sWhat, int iSize, unsigned char *sRetString);
char cShowDirection (int iDirection);
int BinToDec (char *sBin);
void TilesToArray (int iRoom, int iRow, int iIn1, int iIn2, int iIn3,
int iOut1, int iOut2, int iOut3, int iOut4);
void InitScreenAction (char *sAction);
void InitScreen (void);
void LoadFonts (void);
void MixAudio (void *unused, Uint8 *stream, int iLen);
void PreLoad (char *sPath, char *sPNG, SDL_Texture **imgImage);
void ShowImage (SDL_Texture *img, int iX, int iY, char *sImageInfo,
SDL_Renderer *screen, float fMultiply, int iXYScale);
void LoadingBar (int iBarHeight);
void GetOptionValue (char *sArgv, char *sValue);
void ShowScreen (int iScreenS);
void InitPopUp (void);
void PlaySound (char *sFile);
void ShowPopUp (void);
int MapEvents (SDL_Event event);
int InArea (int iUpperLeftX, int iUpperLeftY,
int iLowerRightX, int iLowerRightY);
void Quit (void);
void DisplayText (int iStartX, int iStartY, int iFontSize,
char arText[9 + 2][MAX_TEXT + 2], int iLines, TTF_Font *font,
SDL_Color back, SDL_Color fore, int iOnMap);
void InitPopUpSave (void);
void CustomRenderCopy (SDL_Texture* src, char *sImageInfo, SDL_Rect* srcrect,
SDL_Renderer* dst, SDL_Rect *dstrect);
void ShowPopUpSave (void);
void SaveLevel (int iLevel);
void ShowTile (int iRoom, int iRow, int iCol);
void Prev (void);
void Next (void);
int BrokenRoomLinks (int iPrint, int iLevel);
void CheckSides (int iRoom, int iX, int iY, int iLevel);
int OnLevelBar (void);
void RunLevel (int iLevel);
int StartGame (void *unused);
void ModifyForPlaytest (int iLevel);
void ModifyBack (void);
void WriteByte (int iFd, int iValue);
void WriteWord (int iFd, int iValue);
void GetAsEightBits (unsigned char cChar, char *sBinary);
void SaveFourAsThree (int iFd, int iRoom);
void Zoom (int iToggleFull);
void ChangePosAction (char *sAction);
void ChangePos (int iLocationCol, int iLocationRow);
void ShowChange (void);
void OnTileOld (void);
int UseTile (int iLocationCol, int iLocationRow, int iRoom);
void SetLocation (int iRoom, int iLocationCol, int iLocationRow, int iUseTile);
void InitRooms (void);
void WhereToStart (void);
void ShowRooms (int iRoom, int iX, int iY, int iNext);
int MouseSelectAdj (void);
void LinkPlus (void);
void LinkMinus (void);
void ClearRoom (void);
void RemoveOldRoom (void);
void AddNewRoom (int iX, int iY, int iRoom);
void Help (void);
void ShowHelp (void);
void OpenURL (char *sURL);
void EXE (void);
void ShowEXE (void);
void EXELoad (void);
void EXESave (void);
void UpdateStatusBar (void);
void CenterNumber (SDL_Renderer *screen, int iNumber, int iX, int iY,
SDL_Color fore, int iHex);
int PlusMinus (int *iWhat, int iX, int iY,
int iMin, int iMax, int iChange, int iAddChanged);
void CopyPaste (int iRoom, int iAction);
void CreateBAK (void);
void Sprinkle (void);
unsigned long BytesAsLU (unsigned char *sData, int iBytes);
void ColorRect (int iX, int iY, int iW, int iH, int iR, int iG, int iB);
int BankedToLinear (int iAddr);
int LinearToBanked (int iAddr);
int GetChunkLength (int iLevel, int iNrElementRooms);
void Guards (void);
void ShowGuards (void);
void AddressToRoomAndElem (void);
/*****************************************************************************/
int main (int argc, char *argv[])
/*****************************************************************************/
{
int iLoopArg;
char sStartLevel[MAX_OPTION + 2];
time_t tm;
SDL_version verc, verl;
iDebug = 0;
iNoAudio = 0;
iScale = 1;
iFullscreen = 0;
iStartLevel = 1;
iNoController = 0;
iExtras = 0;
iLastTile = 0;
iNoAnim = 0;
iInfo = 0;
iCopied = 0;
iModified = 0;
iActiveElement = 1;
iActiveGuard = 0;
if (argc > 1)
{
for (iLoopArg = 1; iLoopArg <= argc - 1; iLoopArg++)
{
if ((strcmp (argv[iLoopArg], "-h") == 0) ||
(strcmp (argv[iLoopArg], "-?") == 0) ||
(strcmp (argv[iLoopArg], "--help") == 0))
{
ShowUsage();
}
else if ((strcmp (argv[iLoopArg], "-v") == 0) ||
(strcmp (argv[iLoopArg], "--version") == 0))
{
printf ("%s %s\n", EDITOR_NAME, EDITOR_VERSION);
exit (EXIT_NORMAL);
}
else if ((strcmp (argv[iLoopArg], "-d") == 0) ||
(strcmp (argv[iLoopArg], "--debug") == 0))
{
iDebug = 1;
}
else if ((strcmp (argv[iLoopArg], "-n") == 0) ||
(strcmp (argv[iLoopArg], "--noaudio") == 0))
{
iNoAudio = 1;
}
else if ((strcmp (argv[iLoopArg], "-z") == 0) ||
(strcmp (argv[iLoopArg], "--zoom") == 0))
{
iScale = 2;
}
else if ((strcmp (argv[iLoopArg], "-f") == 0) ||
(strcmp (argv[iLoopArg], "--fullscreen") == 0))
{
iFullscreen = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
else if ((strncmp (argv[iLoopArg], "-l=", 3) == 0) ||
(strncmp (argv[iLoopArg], "--level=", 8) == 0))
{
GetOptionValue (argv[iLoopArg], sStartLevel);
iStartLevel = atoi (sStartLevel);
if ((iStartLevel < 1) || (iStartLevel > NR_LEVELS))
{
iStartLevel = 1;
}
}
else if ((strcmp (argv[iLoopArg], "-s") == 0) ||
(strcmp (argv[iLoopArg], "--static") == 0))
{
iNoAnim = 1;
}
else if ((strcmp (argv[iLoopArg], "-k") == 0) ||
(strcmp (argv[iLoopArg], "--keyboard") == 0))
{
iNoController = 1;
}
else
{
ShowUsage();
}
}
}
GetPathFile();
srand ((unsigned)time(&tm));
LoadLevel (iStartLevel);
/*** 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 PrIfDe (char *sString)
/*****************************************************************************/
{
if (iDebug == 1) { printf ("%s", sString); }
}
/*****************************************************************************/
void GetPathFile (void)
/*****************************************************************************/
{
int iFound;
DIR *dDir;
struct dirent *stDirent;
char sExtension[100 + 2];
char sError[MAX_ERROR + 2];
int iFd;
char sVerify[VERIFY_SIZE + 2];
iFound = 0;
dDir = opendir (SMS_DIR);
if (dDir == NULL)
{
printf ("[FAILED] Cannot open directory \"%s\": %s!\n",
SMS_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]) == 'S') &&
(toupper (sExtension[2]) == 'M') &&
(toupper (sExtension[3]) == 'S'))
{
iFound = 1;
snprintf (sPathFile, MAX_PATHFILE, "%s%s%s", SMS_DIR, SLASH,
stDirent->d_name);
if (iDebug == 1)
{
printf ("[ OK ] Found Sega Master System disk image \"%s\".\n",
sPathFile);
}
}
}
}
}
closedir (dDir);
if (iFound == 0)
{
snprintf (sError, MAX_ERROR, "Cannot find a .sms disk image in"
" directory \"%s\"!", SMS_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 SMS disk image? ***/
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);
sVerify[VERIFY_SIZE] = '\0';
if (strcmp (sVerify, VERIFY_TEXT) != 0)
{
snprintf (sError, MAX_ERROR, "File %s is not a Prince of Persia"
" for SMS disk image!", sPathFile);
printf ("[FAILED] %s\n", sError);
SDL_ShowSimpleMessageBox (SDL_MESSAGEBOX_ERROR,
"Error", sError, NULL);
exit (EXIT_ERROR);
}
close (iFd);
}
/*****************************************************************************/
void LoadLevel (int iLevel)
/*****************************************************************************/
{
int iFd;
unsigned char sRoomLinks[(ROOMS * 4) + 2];
unsigned char sDefinitions[64 + 2];
unsigned char sTiles[(ROOMS * 12 * ROWS) + 2]; /*** 12 is OK ***/
unsigned char sRead[1 + 2];
int iNextRoom;
unsigned char cElement;
int iOffset, iOffsetOld;
unsigned char sData[100 + 2];
int iElementOffset;
char sElementBase[MAX_TEXT + 2];
char sElement[MAX_TEXT + 2];
int iNrElem;
int iDefinition;
char sReadW[10 + 2];
int iChunkLength;
int iRooms;
int iOffsetTG, iOffsetT;
/*** Used for looping. ***/
int iLoopLevel;
int iLoopRoom;
int iLoopLink;
int iLoopDef;
int iLoopRow;
int iLoopCol;
int iLoopElem;
int iLoopEmpty;
int iLoopGuard;
iFd = open (sPathFile, O_RDONLY|O_BINARY);
if (iFd == -1)
{
printf ("[FAILED] Could not open \"%s\": %s!\n",
sPathFile, strerror (errno));
exit (EXIT_ERROR);
}
iChanged = 0;
/* Everything is loaded for ALL levels.
* The reason is that the elements are variable in length, so saving
* requires knowledge of all this information.
*/
for (iLoopLevel = 1; iLoopLevel <= NR_LEVELS; iLoopLevel++)
{
/*******************************/
/* Levels ("The level table"). */
/*******************************/
lseek (iFd, iOffsetLevelTable + ((iLoopLevel - 1) * 20), SEEK_SET);
/*** 0x60C9 ***/
ReadFromFile (iFd, "", 2, sData);
if ((sData[0] != 0xC9) || (sData[1] != 0x60))
{
printf ("[ WARN ] Level %i, 0x60C9 not found: 0x%02X%02X!\n",
iLoopLevel, sData[1], sData[0]);
}
/*** Tile graphics. ***/
ReadFromFile (iFd, "", 2, sData);
iOffsetTG = BytesAsLU (sData, 2);
arOffsetTG1[iLoopLevel] = BankedToLinear (iOffsetTG);
ReadFromFile (iFd, "", 2, sData);
iOffsetTG = BytesAsLU (sData, 2);
arOffsetTG2[iLoopLevel] = iOffsetTG;
/*** Offset tiles. ***/
ReadFromFile (iFd, "", 2, sData);
iOffsetT = BytesAsLU (sData, 2);
arOffsetTiles[iLoopLevel] = BankedToLinear (iOffsetT);
if (iDebug == 1)
{
printf ("[ INFO ] (Offset) Tiles level %i start at: 0x%02X\n",
iLoopLevel, arOffsetTiles[iLoopLevel]);
}
/*** Level type. ***/
ReadFromFile (iFd, "", 1, sData);
switch (sData[0])
{
case 0x05: arLevelType[iLoopLevel] = 'd'; break;
case 0x85: arLevelType[iLoopLevel] = 'p'; break;
}
/*** Load start position. ***/
ReadFromFile (iFd, "", 5, sData);
arKidDir[iLoopLevel] = sData[0];
arKidPosX[iLoopLevel] = sData[1];
arKidPosY[iLoopLevel] = sData[2];
arKidRoom[iLoopLevel] = sData[3] + 1;
arKidAnim[iLoopLevel] = sData[4];
if (iDebug == 1)
{
printf ("[ INFO ] The kid starts turned: %c, x/y: %i/%i, room: %i, "
"anim: %i\n",
cShowDirection (arKidDir[iLoopLevel]),
arKidPosX[iLoopLevel],
arKidPosY[iLoopLevel],
arKidRoom[iLoopLevel],
arKidAnim[iLoopLevel]);
}
/*** Unknown. ***/
ReadFromFile (iFd, "", 4, sData);
arUnknown[iLoopLevel][1] = sData[0];
arUnknown[iLoopLevel][2] = sData[1];
arUnknown[iLoopLevel][3] = sData[2];
arUnknown[iLoopLevel][4] = sData[3];
/*** Room links address. ***/
ReadFromFile (iFd, "", 2, sData);
iOffset = BytesAsLU (sData, 2);
if (iDebug == 1)
{
printf ("[ INFO ] (Offset) Room links level %i start at: 0x%02X\n",
iLoopLevel, iOffset);
}
iElementOffset = iOffset;
lseek (iFd, iOffset, SEEK_SET);
/*************************************************************/
/* Room links and elements ("Room links and objects table"). */
/*************************************************************/
/*** Load chunk length. ***/
ReadFromFile (iFd, "", 2, sData);
iOffset+=2;
iChunkLength = BytesAsLU (sData, 2);
if (iDebug == 1)
{
printf ("[ INFO ] Chunk length level %i: %i\n",
iLoopLevel, iChunkLength);
}
/*** Load offsets of elements. ***/
for (iLoopRoom = 1; iLoopRoom <= ROOMS; iLoopRoom++)
{
ReadFromFile (iFd, "", 2, sData);
if ((sData[0] != 0x00) || (sData[1] != 0x00))
{
arOffsetsElements[iLoopRoom] = iOffset + BytesAsLU (sData, 2);
} else {
if ((iLoopLevel != 14) || (iLoopRoom < 9))
{
printf ("[ WARN ] Level %i: incorrect elements offset!\n",
iLoopLevel);
}
arOffsetsElements[iLoopRoom] = 0;
}
if (iDebug == 1)
{
printf ("[ INFO ] (Offset) Elements l %i, r %i, start at: 0x%02X\n",
iLoopLevel, iLoopRoom, arOffsetsElements[iLoopRoom]);
}
}
iOffset+=48; /*** Do NOT move up. ***/
/*** Load the room links. ***/
switch (iLoopLevel)
{
case 14: iRooms = 5; break;
default: iRooms = ROOMS; break;
}
ReadFromFile (iFd, "Room Links", (iRooms * 4), sRoomLinks);
iOffset+=(iRooms * 4);
for (iLoopLink = 0; iLoopLink < (iRooms * 4); iLoopLink+=4)
{
iRoomConnections[iLoopLevel][(iLoopLink / 4) + 1][1] =
sRoomLinks[iLoopLink + 3] + 1; /*** left ***/
iRoomConnections[iLoopLevel][(iLoopLink / 4) + 1][2] =
sRoomLinks[iLoopLink + 1] + 1; /*** right ***/
iRoomConnections[iLoopLevel][(iLoopLink / 4) + 1][3] =
sRoomLinks[iLoopLink + 0] + 1; /*** up ***/
iRoomConnections[iLoopLevel][(iLoopLink / 4) + 1][4] =
sRoomLinks[iLoopLink + 2] + 1; /*** down ***/
if (iDebug == 1)
{
printf ("[ INFO ] Room %i is connected to room (256 = none): u%i, "
"r%i, d%i, l%i\n",
(iLoopLink / 4) + 1,
sRoomLinks[iLoopLink] + 1,
sRoomLinks[iLoopLink + 1] + 1,
sRoomLinks[iLoopLink + 2] + 1,
sRoomLinks[iLoopLink + 3] + 1);
}
}
/*** Load elements. ***/
for (iLoopRoom = 1; iLoopRoom <= ROOMS; iLoopRoom++)
{
if (arOffsetsElements[iLoopRoom] != 0)
{
/*** clear ***/
for (iLoopElem = 1; iLoopElem <= MAX_ELEM; iLoopElem++)
{
arElements[iLoopLevel][iLoopRoom][iLoopElem][0] = 0x01;
arElements[iLoopLevel][iLoopRoom][iLoopElem][8] = 0x00; /*** off ***/
}
iNrElem = 0;
iOffsetOld = iOffset;
iOffset = arOffsetsElements[iLoopRoom];
if (iOffset != iOffsetOld)
{ printf ("[ WARN ] Level %i: offset difference!\n", iLoopLevel); }
lseek (iFd, iOffset, SEEK_SET);
iNextRoom = 0;
do {
read (iFd, sRead, 1);
iOffset++;
iNrElem++;
cElement = sRead[0];
arElements[iLoopLevel][iLoopRoom][iNrElem][9] =
iOffset - iElementOffset - 3;
switch (cElement)
{
case 0x27: /*** level door (8 bytes) ***/
snprintf (sElementBase, MAX_TEXT, "%s", "Level door");
arElements[iLoopLevel][iLoopRoom][iNrElem][0] = 0x27;
read (iFd, sRead, 1); /*** x ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][1] = sRead[0];
read (iFd, sRead, 1); /*** y ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][2] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][3] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][4] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][5] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][6] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][7] = sRead[0];
arElements[iLoopLevel][iLoopRoom][iNrElem][8] = 0x01; /*** on ***/
iOffset+=7;
snprintf (sElement, MAX_TEXT, "%s (x:%i, y:%i,"
" ?:%i, ?:%i, ?:%i, ?:%i, ?:%i)",
sElementBase,
arElements[iLoopLevel][iLoopRoom][iNrElem][1],
arElements[iLoopLevel][iLoopRoom][iNrElem][2],
arElements[iLoopLevel][iLoopRoom][iNrElem][3],
arElements[iLoopLevel][iLoopRoom][iNrElem][4],
arElements[iLoopLevel][iLoopRoom][iNrElem][5],
arElements[iLoopLevel][iLoopRoom][iNrElem][6],
arElements[iLoopLevel][iLoopRoom][iNrElem][7]);
break;
case 0x29: /*** mirror (6 bytes) ***/
snprintf (sElementBase, MAX_TEXT, "%s", "Mirror");
arElements[iLoopLevel][iLoopRoom][iNrElem][0] = 0x29;
read (iFd, sRead, 1); /*** x ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][1] = sRead[0];
read (iFd, sRead, 1); /*** y ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][2] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][3] = sRead[0];
read (iFd, sRead, 1); /*** reflection x ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][4] = sRead[0];
read (iFd, sRead, 1); /*** reflection y ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][5] = sRead[0];
arElements[iLoopLevel][iLoopRoom][iNrElem][8] = 0x01; /*** on ***/
iOffset+=5;
snprintf (sElement, MAX_TEXT, "%s (x:%i, y:%i,"
" ?:%i, refl x:%i, refl y:%i)",
sElementBase,
arElements[iLoopLevel][iLoopRoom][iNrElem][1],
arElements[iLoopLevel][iLoopRoom][iNrElem][2],
arElements[iLoopLevel][iLoopRoom][iNrElem][3],
arElements[iLoopLevel][iLoopRoom][iNrElem][4],
arElements[iLoopLevel][iLoopRoom][iNrElem][5]);
break;
case 0x32: /*** gate (8 bytes) ***/
snprintf (sElementBase, MAX_TEXT, "%s", "Gate");
arElements[iLoopLevel][iLoopRoom][iNrElem][0] = 0x32;
read (iFd, sRead, 1); /*** x ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][1] = sRead[0];
read (iFd, sRead, 1); /*** y ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][2] = sRead[0];
read (iFd, sRead, 1); /*** anim ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][3] = sRead[0];
read (iFd, sRead, 1); /*** anim spd ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][4] = sRead[0];
read (iFd, sRead, 1); /*** openness ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][5] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][6] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][7] = sRead[0];
arElements[iLoopLevel][iLoopRoom][iNrElem][8] = 0x01; /*** on ***/
iOffset+=7;
snprintf (sElement, MAX_TEXT, "%s (x:%i, y:%i,"
" anim:%i, anim spd:%i, openness:%i, ?:%i, ?:%i)",
sElementBase,
arElements[iLoopLevel][iLoopRoom][iNrElem][1],
arElements[iLoopLevel][iLoopRoom][iNrElem][2],
arElements[iLoopLevel][iLoopRoom][iNrElem][3],
arElements[iLoopLevel][iLoopRoom][iNrElem][4],
arElements[iLoopLevel][iLoopRoom][iNrElem][5],
arElements[iLoopLevel][iLoopRoom][iNrElem][6],
arElements[iLoopLevel][iLoopRoom][iNrElem][7]);
break;
case 0x3E: /*** skel alive (6 bytes) ***/
snprintf (sElementBase, MAX_TEXT, "%s", "Skel alive");
arElements[iLoopLevel][iLoopRoom][iNrElem][0] = 0x3E;
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][1] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][2] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][3] = sRead[0];
read (iFd, sRead, 1); /*** x ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][4] = sRead[0];
read (iFd, sRead, 1); /*** y ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][5] = sRead[0];
arElements[iLoopLevel][iLoopRoom][iNrElem][8] = 0x01; /*** on ***/
iOffset+=5;
snprintf (sElement, MAX_TEXT, "%s (?:%i, ?:%i,"
" ?:%i, x:%i, y:%i)",
sElementBase,
arElements[iLoopLevel][iLoopRoom][iNrElem][1],
arElements[iLoopLevel][iLoopRoom][iNrElem][2],
arElements[iLoopLevel][iLoopRoom][iNrElem][3],
arElements[iLoopLevel][iLoopRoom][iNrElem][4],
arElements[iLoopLevel][iLoopRoom][iNrElem][5]);
break;
case 0x3F: /*** skel respawn (6 bytes) ***/
snprintf (sElementBase, MAX_TEXT, "%s", "Skel respawn");
arElements[iLoopLevel][iLoopRoom][iNrElem][0] = 0x3F;
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][1] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][2] = sRead[0];
read (iFd, sRead, 1); /*** ? ***/
arElements[iLoopLevel][iLoopRoom][iNrElem][3] = sRead[0];
read (iFd, sRead, 1); /*** x ***/