/
seg009.c
4091 lines (3747 loc) · 132 KB
/
seg009.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
/*
SDLPoP, a port/conversion of the DOS game Prince of Persia.
Copyright (C) 2013-2021 Dávid Nagy
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 <https://www.gnu.org/licenses/>.
The authors of this program may be contacted at https://forum.princed.org
*/
#include "common.h"
#include <time.h>
#include <errno.h>
#ifdef _WIN32
#include <windows.h>
#include <wchar.h>
#else
#include "dirent.h"
#endif
// Most functions in this file are different from those in the original game.
void sdlperror(const char* header) {
const char* error = SDL_GetError();
printf("%s: %s\n",header,error);
//quit(1);
}
char exe_dir[POP_MAX_PATH] = ".";
bool found_exe_dir = false;
void find_exe_dir(void) {
if (found_exe_dir) return;
snprintf_check(exe_dir, sizeof(exe_dir), "%s", g_argv[0]);
char* last_slash = NULL;
char* pos = exe_dir;
for (char c = *pos; c != '\0'; c = *(++pos)) {
if (c == '/' || c == '\\') {
last_slash = pos;
}
}
if (last_slash != NULL) {
*last_slash = '\0';
}
found_exe_dir = true;
}
bool file_exists(const char* filename) {
return (access(filename, F_OK) != -1);
}
const char* locate_file_(const char* filename, char* path_buffer, int buffer_size) {
if(file_exists(filename)) {
return filename;
} else {
// If failed, it may be that SDLPoP is being run from the wrong different working directory.
// We can try to rescue the situation by loading from the directory of the executable.
find_exe_dir();
snprintf_check(path_buffer, buffer_size, "%s/%s", exe_dir, filename);
return (const char*) path_buffer;
}
}
#ifdef _WIN32
// These macros are from the SDL2 source. (src/core/windows/SDL_windows.h)
// The pointers returned by these macros must be freed with SDL_free().
#define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(S), (SDL_wcslen(S)+1)*sizeof(WCHAR))
#define WIN_UTF8ToString(S) (WCHAR *)SDL_iconv_string("UTF-16LE", "UTF-8", (char *)(S), SDL_strlen(S)+1)
// This hack is needed because SDL uses UTF-8 everywhere (even in argv!), but fopen on Windows uses whatever code page is currently set.
FILE* fopen_UTF8(const char* filename_UTF8, const char* mode_UTF8) {
WCHAR* filename_UTF16 = WIN_UTF8ToString(filename_UTF8);
WCHAR* mode_UTF16 = WIN_UTF8ToString(mode_UTF8);
FILE* result = _wfopen(filename_UTF16, mode_UTF16);
SDL_free(mode_UTF16);
SDL_free(filename_UTF16);
return result;
}
int chdir_UTF8(const char* path_UTF8) {
WCHAR* path_UTF16 = WIN_UTF8ToString(path_UTF8);
int result = _wchdir(path_UTF16);
SDL_free(path_UTF16);
return result;
}
int mkdir_UTF8(const char* path_UTF8) {
WCHAR* path_UTF16 = WIN_UTF8ToString(path_UTF8);
int result = _wmkdir(path_UTF16);
SDL_free(path_UTF16);
return result;
}
int access_UTF8(const char* filename_UTF8, int mode) {
WCHAR* filename_UTF16 = WIN_UTF8ToString(filename_UTF8);
int result = _waccess(filename_UTF16, mode);
SDL_free(filename_UTF16);
return result;
}
int stat_UTF8(const char *filename_UTF8, struct stat *_Stat) {
WCHAR* filename_UTF16 = WIN_UTF8ToString(filename_UTF8);
// There is a _wstat() function as well, but it expects the second argument to be a different type than stat().
int result = wstat(filename_UTF16, _Stat);
SDL_free(filename_UTF16);
return result;
}
#endif //_WIN32
// OS abstraction for listing directory contents
// Directory listing using dirent.h is available using MinGW on Windows, but not using MSVC (need to use Win32 API).
// - Under GNU/Linux, etc (or if compiling with MinGW on Windows), we can use dirent.h
// - Under Windows, we'd like to directly call the Win32 API. (Note: MSVC does not include dirent.h)
// NOTE: If we are using MinGW, we'll opt to use the Win32 API as well: dirent.h would just wrap Win32 anyway!
#ifdef _WIN32
struct directory_listing_type {
WIN32_FIND_DATAW find_data;
HANDLE search_handle;
char* current_filename_UTF8;
};
directory_listing_type* create_directory_listing_and_find_first_file(const char* directory, const char* extension) {
directory_listing_type* directory_listing = calloc(1, sizeof(directory_listing_type));
char search_pattern[POP_MAX_PATH];
snprintf_check(search_pattern, POP_MAX_PATH, "%s/*.%s", directory, extension);
WCHAR* search_pattern_UTF16 = WIN_UTF8ToString(search_pattern);
directory_listing->search_handle = FindFirstFileW( search_pattern_UTF16, &directory_listing->find_data );
SDL_free(search_pattern_UTF16);
if (directory_listing->search_handle != INVALID_HANDLE_VALUE) {
return directory_listing;
} else {
free(directory_listing);
return NULL;
}
}
char* get_current_filename_from_directory_listing(directory_listing_type* data) {
SDL_free(data->current_filename_UTF8);
data->current_filename_UTF8 = NULL;
data->current_filename_UTF8 = WIN_StringToUTF8(data->find_data.cFileName);
return data->current_filename_UTF8;
}
bool find_next_file(directory_listing_type* data) {
return (bool) FindNextFileW( data->search_handle, &data->find_data );
}
void close_directory_listing(directory_listing_type* data) {
FindClose(data->search_handle);
SDL_free(data->current_filename_UTF8);
data->current_filename_UTF8 = NULL;
free(data);
}
#else // use dirent.h API for listing files
struct directory_listing_type {
DIR* dp;
char* found_filename;
const char* extension;
};
directory_listing_type* create_directory_listing_and_find_first_file(const char* directory, const char* extension) {
directory_listing_type* data = calloc(1, sizeof(directory_listing_type));
bool ok = false;
data->dp = opendir(directory);
if (data->dp != NULL) {
struct dirent* ep;
while ((ep = readdir(data->dp))) {
char *ext = strrchr(ep->d_name, '.');
if (ext != NULL && strcasecmp(ext+1, extension) == 0) {
data->found_filename = ep->d_name;
data->extension = extension;
ok = true;
break;
}
}
}
if (ok) {
return data;
} else {
free(data);
return NULL;
}
}
char* get_current_filename_from_directory_listing(directory_listing_type* data) {
return data->found_filename;
}
bool find_next_file(directory_listing_type* data) {
bool ok = false;
struct dirent* ep;
while ((ep = readdir(data->dp))) {
char *ext = strrchr(ep->d_name, '.');
if (ext != NULL && strcasecmp(ext+1, data->extension) == 0) {
data->found_filename = ep->d_name;
ok = true;
break;
}
}
return ok;
}
void close_directory_listing(directory_listing_type *data) {
closedir(data->dp);
free(data);
}
#endif //_WIN32
dat_type* dat_chain_ptr = NULL;
char last_text_input;
// seg009:000D
int __pascal far read_key() {
// stub
int key = last_key_scancode;
last_key_scancode = 0;
return key;
}
// seg009:019A
void __pascal far clear_kbd_buf() {
// stub
last_key_scancode = 0;
last_text_input = 0;
}
// seg009:040A
word __pascal far prandom(word max) {
if (!seed_was_init) {
// init from current time
random_seed = (dword)time(NULL);
seed_was_init = 1;
}
random_seed = random_seed * 214013 + 2531011;
return (random_seed >> 16) % (max + 1);
}
// seg009:0467
int __pascal far round_xpos_to_byte(int xpos,int round_direction) {
// stub
return xpos;
}
// seg009:0C7A
void __pascal far quit(int exit_code) {
restore_stuff();
exit(exit_code);
}
// seg009:0C90
void __pascal far restore_stuff() {
SDL_Quit();
}
// seg009:0E33
int __pascal far key_test_quit() {
word key;
key = read_key();
if (key == (SDL_SCANCODE_Q | WITH_CTRL)) { // Ctrl+Q
#ifdef USE_REPLAY
if (recording) save_recorded_replay_dialog();
#endif
#ifdef USE_MENU
if (is_menu_shown) menu_was_closed();
#endif
quit(0);
}
return key;
}
// seg009:0E54
const char* __pascal far check_param(const char *param) {
// stub
short arg_index;
for (arg_index = 1; arg_index < g_argc; ++arg_index) {
char* curr_arg = g_argv[arg_index];
// Filenames (e.g. replays) should never be a valid 'normal' param so we should skip these to prevent conflicts.
// We can lazily distinguish filenames from non-filenames by checking whether they have a dot in them.
// (Assumption: all relevant files, e.g. replay files, have some file extension anyway)
if (strchr(curr_arg, '.') != NULL) {
continue;
}
// List of params that expect a specifier ('sub-') arg directly after it (e.g. the mod's name, after "mod" arg)
// Such sub-args may conflict with the normal params (so, we should 'skip over' them)
static const char params_with_one_subparam[][16] = { "mod", "validate", /*...*/ };
bool curr_arg_has_one_subparam = false;
int i;
for (i = 0; i < COUNT(params_with_one_subparam); ++i) {
if (strncasecmp(curr_arg, params_with_one_subparam[i], strlen(params_with_one_subparam[i])) == 0) {
curr_arg_has_one_subparam = true;
break;
}
}
if (curr_arg_has_one_subparam) {
// Found an arg that has one sub-param, so we want to:
// 1: skip over the next arg (if we are NOT checking for this specific param)
// 2: return a pointer below to the SUB-arg (if we ARE checking for this specific param)
++arg_index;
if (!(arg_index < g_argc)) return NULL; // not enough arguments
}
if (/*strnicmp*/strncasecmp(curr_arg, param, strlen(param)) == 0) {
return g_argv[arg_index];
}
}
return NULL;
}
// seg009:0EDF
int __pascal far pop_wait(int timer_index,int time) {
start_timer(timer_index, time);
return do_wait(timer_index);
}
static FILE* open_dat_from_root_or_data_dir(const char* filename) {
FILE* fp = NULL;
fp = fopen(filename, "rb");
// if failed, try if the DAT file can be opened in the data/ directory, instead of the main folder
if (fp == NULL) {
char data_path[POP_MAX_PATH];
snprintf_check(data_path, sizeof(data_path), "data/%s", filename);
if (!file_exists(data_path)) {
find_exe_dir();
snprintf_check(data_path, sizeof(data_path), "%s/data/%s", exe_dir, filename);
}
// verify that this is a regular file and not a directory (otherwise, don't open)
struct stat path_stat;
stat(data_path, &path_stat);
if (S_ISREG(path_stat.st_mode)) {
fp = fopen(data_path, "rb");
}
}
return fp;
}
int __pascal far showmessage(char far *text,int arg_4,void far *arg_0);
// seg009:0F58
dat_type *__pascal open_dat(const char *filename, int optional) {
FILE* fp = NULL;
if (!use_custom_levelset) {
fp = open_dat_from_root_or_data_dir(filename);
}
else {
// Don't complain about missing data files if we are only looking in the mod folder, because they might exist in the data folder.
// (This is possible only if open_dat() was called by load_all_sounds().)
if (!skip_mod_data_files && skip_normal_data_files) optional = 1;
if (!skip_mod_data_files && !(always_use_original_graphics && optional == 'G')) {
char filename_mod[POP_MAX_PATH];
// before checking the root directory, first try mods/MODNAME/
snprintf_check(filename_mod, sizeof(filename_mod), "%s/%s", mod_data_path, filename);
fp = fopen(filename_mod, "rb");
}
if (fp == NULL && !skip_normal_data_files) {
fp = open_dat_from_root_or_data_dir(filename);
}
}
dat_header_type dat_header;
dat_table_type* dat_table = NULL;
dat_type* pointer = (dat_type*) calloc(1, sizeof(dat_type));
snprintf_check(pointer->filename, sizeof(pointer->filename), "%s", filename);
pointer->next_dat = dat_chain_ptr;
dat_chain_ptr = pointer;
if (fp != NULL) {
if (fread(&dat_header, 6, 1, fp) != 1)
goto failed;
dat_table = (dat_table_type*) malloc(dat_header.table_size);
if (dat_table == NULL ||
fseek(fp, dat_header.table_offset, SEEK_SET) ||
fread(dat_table, dat_header.table_size, 1, fp) != 1)
goto failed;
pointer->handle = fp;
pointer->dat_table = dat_table;
} else if (optional == 0) {
// showmessage will crash if we call if before certain things are initialized!
// Solution: In pop_main(), I moved the first open_dat() call after init_copyprot_dialog().
// /*
// There is no DAT file, verify whether the corresponding directory exists.
char filename_no_ext[POP_MAX_PATH];
// strip the .DAT file extension from the filename (use folders simply named TITLE, KID, VPALACE, etc.)
strncpy(filename_no_ext, pointer->filename, sizeof(filename_no_ext));
size_t len = strlen(filename_no_ext);
if (len >= 5 && filename_no_ext[len-4] == '.') {
filename_no_ext[len-4] = '\0'; // terminate, so ".DAT" is deleted from the filename
}
char foldername[POP_MAX_PATH];
snprintf_check(foldername,sizeof(foldername),"data/%s",filename_no_ext);
const char* data_path = locate_file(foldername);
struct stat path_stat;
int result = stat(data_path, &path_stat);
if (result != 0 || !S_ISDIR(path_stat.st_mode)) {
char error_message[256];
snprintf_check(error_message, sizeof(error_message), "Cannot find a required data file: %s or folder: %s\nPress any key to quit.", filename, foldername);
if (onscreen_surface_ != NULL && copyprot_dialog != NULL) { // otherwise showmessage will crash
showmessage(error_message, 1, &key_test_quit);
quit(1);
}
}
// */
}
out:
// stub
return pointer;
failed:
perror(filename);
if (fp)
fclose(fp);
if (dat_table)
free(dat_table);
goto out;
}
// seg009:9CAC
void __pascal far set_loaded_palette(dat_pal_type far *palette_ptr) {
int dest_row, dest_index, source_row;
for (dest_row = dest_index = source_row = 0; dest_row < 16; ++dest_row, dest_index += 0x10) {
if (palette_ptr->row_bits & (1 << dest_row)) {
set_pal_arr(dest_index, 16, palette_ptr->vga + source_row*0x10, 1);
++source_row;
}
}
}
// data:3356
word chtab_palette_bits = 1;
// seg009:104E
chtab_type* __pascal load_sprites_from_file(int resource,int palette_bits, int quit_on_error) {
int i;
int n_images = 0;
//int has_palette_bits = 1;
chtab_type* chtab = NULL;
dat_shpl_type* shpl = (dat_shpl_type*) load_from_opendats_alloc(resource, "pal", NULL, NULL);
if (shpl == NULL) {
printf("Can't load sprites from resource %d.\n", resource);
if (quit_on_error) {
char error_message[256];
// Unfortunately we don't know at this point which data file is missing. So we use the name of the last opened DAT file.
// It's also possible that the DAT file exists and it just doesn't contain the needed resource.
snprintf_check(error_message, sizeof(error_message), "Can't load sprites from resource %d.\nThe last opened data file is: %s\nPress any key to quit.", resource, dat_chain_ptr->filename);
showmessage(error_message, 1, &key_test_quit);
quit(1);
}
return NULL;
}
dat_pal_type* pal_ptr = &shpl->palette;
if (graphics_mode == gmMcgaVga) {
if (palette_bits == 0) {
/*
palette_bits = add_palette_bits(pal_ptr->n_colors);
if (palette_bits == 0) {
quit(1);
}
*/
} else {
chtab_palette_bits |= palette_bits;
//has_palette_bits = 0;
}
pal_ptr->row_bits = palette_bits;
}
n_images = shpl->n_images;
size_t alloc_size = sizeof(chtab_type) + sizeof(void far *) * n_images;
chtab = (chtab_type*) malloc(alloc_size);
memset(chtab, 0, alloc_size);
chtab->n_images = n_images;
for (i = 1; i <= n_images; i++) {
SDL_Surface* image = load_image(resource + i, pal_ptr);
// if (image == NULL) printf(" failed");
if (image != NULL) {
if (SDL_SetSurfaceAlphaMod(image, 0) != 0) {
sdlperror("load_sprites_from_file: SDL_SetAlpha");
quit(1);
}
/*
if (SDL_SetColorKey(image, SDL_SRCCOLORKEY, 0) != 0) {
sdlperror("load_sprites_from_file: SDL_SetColorKey");
quit(1);
}
*/
}
// printf("\n");
chtab->images[i-1] = image;
}
set_loaded_palette(pal_ptr);
return chtab;
}
// seg009:11A8
void __pascal far free_chtab(chtab_type *chtab_ptr) {
image_type far* curr_image;
word id;
word n_images;
if (graphics_mode == gmMcgaVga && chtab_ptr->has_palette_bits) {
chtab_palette_bits &= ~ chtab_ptr->chtab_palette_bits;
}
n_images = chtab_ptr->n_images;
for (id = 0; id < n_images; ++id) {
curr_image = chtab_ptr->images[id];
if (curr_image) {
SDL_FreeSurface(curr_image);
}
}
free_near(chtab_ptr);
}
// seg009:8CE6
void __pascal far decompress_rle_lr(byte far *destination,const byte far *source,int dest_length) {
const byte* src_pos = source;
byte* dest_pos = destination;
short rem_length = dest_length;
while (rem_length) {
sbyte count = *(src_pos++);
if (count >= 0) { // copy
++count;
do {
*(dest_pos++) = *(src_pos++);
--rem_length;
} while (--count && rem_length);
} else { // repeat
byte al = *(src_pos++);
count = -count;
do {
*(dest_pos++) = al;
--rem_length;
} while (--count && rem_length);
}
}
}
// seg009:8D1C
void __pascal far decompress_rle_ud(byte far *destination,const byte far *source,int dest_length,int width,int height) {
short rem_height = height;
const byte* src_pos = source;
byte* dest_pos = destination;
short rem_length = dest_length;
--dest_length;
--width;
while (rem_length) {
sbyte count = *(src_pos++);
if (count >= 0) { // copy
++count;
do {
*(dest_pos++) = *(src_pos++);
dest_pos += width;
if (--rem_height == 0) {
dest_pos -= dest_length;
rem_height = height;
}
--rem_length;
} while (--count && rem_length);
} else { // repeat
byte al = *(src_pos++);
count = -count;
do {
*(dest_pos++) = al;
dest_pos += width;
if (--rem_height == 0) {
dest_pos -= dest_length;
rem_height = height;
}
--rem_length;
} while (--count && rem_length);
}
}
}
// seg009:90FA
byte far* __pascal far decompress_lzg_lr(byte far *dest,const byte far *source,int dest_length) {
byte* window = (byte*) malloc_near(0x400);
if (window == NULL) return NULL;
memset(window, 0, 0x400);
byte* window_pos = window + 0x400 - 0x42; // bx
short remaining = dest_length; // cx
byte* window_end = window + 0x400; // dx
const byte* source_pos = source;
byte* dest_pos = dest;
word mask = 0;
do {
mask >>= 1;
if ((mask & 0xFF00) == 0) {
mask = *(source_pos++) | 0xFF00;
}
if (mask & 1) {
*(window_pos++) = *(dest_pos++) = *(source_pos++);
if (window_pos >= window_end) window_pos = window;
--remaining;
} else {
word copy_info = *(source_pos++);
copy_info = (copy_info << 8) | *(source_pos++);
byte* copy_source = window + (copy_info & 0x3FF);
byte copy_length = (copy_info >> 10) + 3;
do {
*(window_pos++) = *(dest_pos++) = *(copy_source++);
if (copy_source >= window_end) copy_source = window;
if (window_pos >= window_end) window_pos = window;
} while (--remaining && --copy_length);
}
} while (remaining);
// end:
free(window);
return dest;
}
// seg009:91AD
byte far* __pascal far decompress_lzg_ud(byte far *dest,const byte far *source,int dest_length,int stride,int height) {
byte* window = (byte*) malloc_near(0x400);
if (window == NULL) return NULL;
memset(window, 0, 0x400);
byte* window_pos = window + 0x400 - 0x42; // bx
short remaining = height; // cx
byte* window_end = window + 0x400; // dx
const byte* source_pos = source;
byte* dest_pos = dest;
word mask = 0;
short var_6 = dest_length - 1;
do {
mask >>= 1;
if ((mask & 0xFF00) == 0) {
mask = *(source_pos++) | 0xFF00;
}
if (mask & 1) {
*(window_pos++) = *dest_pos = *(source_pos++);
dest_pos += stride;
if (--remaining == 0) {
dest_pos -= var_6;
remaining = height;
}
if (window_pos >= window_end) window_pos = window;
--dest_length;
} else {
word copy_info = *(source_pos++);
copy_info = (copy_info << 8) | *(source_pos++);
byte* copy_source = window + (copy_info & 0x3FF);
byte copy_length = (copy_info >> 10) + 3;
do {
*(window_pos++) = *dest_pos = *(copy_source++);
dest_pos += stride;
if (--remaining == 0) {
dest_pos -= var_6;
remaining = height;
}
if (copy_source >= window_end) copy_source = window;
if (window_pos >= window_end) window_pos = window;
} while (--dest_length && --copy_length);
}
} while (dest_length);
// end:
free(window);
return dest;
}
// seg009:938E
void __pascal far decompr_img(byte far *dest,const image_data_type far *source,int decomp_size,int cmeth, int stride) {
switch (cmeth) {
case 0: // RAW left-to-right
memcpy_far(dest, &source->data, decomp_size);
break;
case 1: // RLE left-to-right
decompress_rle_lr(dest, source->data, decomp_size);
break;
case 2: // RLE up-to-down
decompress_rle_ud(dest, source->data, decomp_size, stride, source->height);
break;
case 3: // LZG left-to-right
decompress_lzg_lr(dest, source->data, decomp_size);
break;
case 4: // LZG up-to-down
decompress_lzg_ud(dest, source->data, decomp_size, stride, source->height);
break;
}
}
int calc_stride(image_data_type* image_data) {
int width = image_data->width;
int flags = image_data->flags;
int depth = ((flags >> 12) & 7) + 1;
return (depth * width + 7) / 8;
}
byte* conv_to_8bpp(byte* in_data, int width, int height, int stride, int depth) {
byte* out_data = (byte*) malloc(width * height);
int y, x_pixel, x_byte, pixel_in_byte;
int pixels_per_byte = 8 / depth;
int mask = (1 << depth) - 1;
for (y = 0; y < height; ++y) {
byte* in_pos = in_data + y*stride;
byte* out_pos = out_data + y*width;
for (x_pixel = x_byte = 0; x_byte < stride; ++x_byte) {
byte v = *in_pos;
int shift = 8;
for (pixel_in_byte = 0; pixel_in_byte < pixels_per_byte && x_pixel < width; ++pixel_in_byte, ++x_pixel) {
shift -= depth;
*out_pos = (v >> shift) & mask;
++out_pos;
}
++in_pos;
}
}
return out_data;
}
image_type* decode_image(image_data_type* image_data, dat_pal_type* palette) {
int height = image_data->height;
if (height == 0) return NULL;
int width = image_data->width;
int flags = image_data->flags;
int depth = ((flags >> 12) & 7) + 1;
int cmeth = (flags >> 8) & 0x0F;
int stride = calc_stride(image_data);
int dest_size = stride * height;
byte* dest = (byte*) malloc(dest_size);
memset(dest, 0, dest_size);
decompr_img(dest, image_data, dest_size, cmeth, stride);
byte* image_8bpp = conv_to_8bpp(dest, width, height, stride, depth);
free(dest); dest = NULL;
image_type* image = SDL_CreateRGBSurface(0, width, height, 8, 0, 0, 0, 0);
if (image == NULL) {
sdlperror("decode_image: SDL_CreateRGBSurface");
quit(1);
}
if (SDL_LockSurface(image) != 0) {
sdlperror("decode_image: SDL_LockSurface");
}
int y;
for (y = 0; y < height; ++y) {
// fill image with data
memcpy((byte*)image->pixels + y*image->pitch, image_8bpp + y*width, width);
}
SDL_UnlockSurface(image);
free(image_8bpp); image_8bpp = NULL;
SDL_Color colors[16];
int i;
for (i = 0; i < 16; ++i) {
colors[i].r = palette->vga[i].r << 2;
colors[i].g = palette->vga[i].g << 2;
colors[i].b = palette->vga[i].b << 2;
colors[i].a = SDL_ALPHA_OPAQUE; // SDL2's SDL_Color has a fourth alpha component
}
// Force 0th color to be black for non-transparent blitters. (hitpoints, shadow)
// This is needed to remove the colored rectangles around hitpoints and the shadow, when using Brain's SNES graphics for example.
colors[0].r = 0;
colors[0].g = 0;
colors[0].b = 0;
colors[0].a = SDL_ALPHA_TRANSPARENT;
SDL_SetPaletteColors(image->format->palette, colors, 0, 16); // SDL_SetColors = deprecated
return image;
}
// seg009:121A
image_type* far __pascal far load_image(int resource_id, dat_pal_type* palette) {
// stub
data_location result;
int size;
void* image_data = load_from_opendats_alloc(resource_id, "png", &result, &size);
image_type* image = NULL;
switch (result) {
case data_none:
return NULL;
break;
case data_DAT: { // DAT
image = decode_image((image_data_type*) image_data, palette);
} break;
case data_directory: { // directory
SDL_RWops* rw = SDL_RWFromConstMem(image_data, size);
if (rw == NULL) {
sdlperror("load_image: SDL_RWFromConstMem");
return NULL;
}
image = IMG_Load_RW(rw, 0);
if (image == NULL) {
printf("load_image: IMG_Load_RW: %s\n", IMG_GetError());
}
if (SDL_RWclose(rw) != 0) {
sdlperror("load_image: SDL_RWclose");
}
} break;
}
if (image_data != NULL) free(image_data);
if (image != NULL) {
// should immediately start using the onscreen pixel format, so conversion will not be needed
if (SDL_SetColorKey(image, SDL_TRUE, 0) != 0) { //sdl 1.2: SDL_SRCCOLORKEY
sdlperror("load_image: SDL_SetColorKey");
quit(1);
}
// printf("bpp = %d\n", image->format->BitsPerPixel);
if (SDL_SetSurfaceAlphaMod(image, 0) != 0) { //sdl 1.2: SDL_SetAlpha removed
sdlperror("load_image: SDL_SetAlpha");
quit(1);
}
// image_type* colored_image = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_ARGB8888, 0);
// if (!colored_image) {
// sdlperror("load_image: SDL_ConvertSurfaceFormat");
// quit(1);
// }
// SDL_FreeSurface(image);
// image = colored_image;
}
return image;
}
// seg009:13C4
void __pascal far draw_image_transp(image_type far *image,image_type far *mask,int xpos,int ypos) {
if (graphics_mode == gmMcgaVga) {
draw_image_transp_vga(image, xpos, ypos);
} else {
// ...
}
}
// seg009:157E
int __pascal far set_joy_mode() {
// stub
if (SDL_NumJoysticks() < 1) {
is_joyst_mode = 0;
} else {
if (gamecontrollerdb_file[0] != '\0') {
SDL_GameControllerAddMappingsFromFile(gamecontrollerdb_file);
}
if (SDL_IsGameController(0)) {
sdl_controller_ = SDL_GameControllerOpen(0);
if (sdl_controller_ == NULL) {
is_joyst_mode = 0;
} else {
is_joyst_mode = 1;
}
}
// We have a joystick connected, but it's NOT compatible with the SDL_GameController
// interface, so we resort to the classic SDL_Joystick interface instead
else {
sdl_joystick_ = SDL_JoystickOpen(0);
is_joyst_mode = 1;
using_sdl_joystick_interface = 1;
}
}
if (enable_controller_rumble && is_joyst_mode) {
sdl_haptic = SDL_HapticOpen(0);
SDL_HapticRumbleInit(sdl_haptic); // initialize the device for simple rumble
} else {
sdl_haptic = NULL;
}
is_keyboard_mode = !is_joyst_mode;
return is_joyst_mode;
}
// seg009:178B
surface_type far *__pascal make_offscreen_buffer(const rect_type far *rect) {
// stub
#ifndef USE_ALPHA
// Bit order matches onscreen buffer, good for fading.
return SDL_CreateRGBSurface(0, rect->right, rect->bottom, 24, 0xFF, 0xFF<<8, 0xFF<<16, 0); //RGB888 (little endian)
#else
return SDL_CreateRGBSurface(0, rect->right, rect->bottom, 32, 0xFF, 0xFF<<8, 0xFF<<16, 0xFF<<24);
#endif
//return surface;
}
// seg009:17BD
void __pascal far free_surface(surface_type *surface) {
SDL_FreeSurface(surface);
}
// seg009:17EA
void __pascal far free_peel(peel_type *peel_ptr) {
SDL_FreeSurface(peel_ptr->peel);
free(peel_ptr);
}
// seg009:182F
void __pascal far set_hc_pal() {
// stub
if (graphics_mode == gmMcgaVga) {
set_pal_arr(0, 16, custom->vga_palette, 1);
} else {
// ...
}
}
// seg009:2446
void __pascal far flip_not_ega(byte far *memory,int height,int stride) {
byte* row_buffer = (byte*) malloc(stride);
byte* top_ptr;
byte* bottom_ptr;
bottom_ptr = top_ptr = memory;
bottom_ptr += (height - 1) * stride;
short rem_rows = height >> 1;
do {
memcpy(row_buffer, top_ptr, stride);
memcpy(top_ptr, bottom_ptr, stride);
memcpy(bottom_ptr, row_buffer, stride);
top_ptr += stride;
bottom_ptr -= stride;
--rem_rows;
} while (rem_rows);
free(row_buffer);
}
// seg009:19B1
void __pascal far flip_screen(surface_type far *surface) {
// stub
if (graphics_mode != gmEga) {
if (SDL_LockSurface(surface) != 0) {
sdlperror("flip_screen: SDL_LockSurface");
quit(1);
}
flip_not_ega((byte*) surface->pixels, surface->h, surface->pitch);
SDL_UnlockSurface(surface);
} else {
// ...
}
}
#ifndef USE_FADE
// seg009:19EF
void __pascal far fade_in_2(surface_type near *source_surface,int which_rows) {
// stub
method_1_blit_rect(onscreen_surface_, source_surface, &screen_rect, &screen_rect, 0);
}
// seg009:1CC9
void __pascal far fade_out_2(int rows) {
// stub
}
#endif // USE_FADE
// seg009:2288
void __pascal far draw_image_transp_vga(image_type far *image,int xpos,int ypos) {
// stub
method_6_blit_img_to_scr(image, xpos, ypos, blitters_10h_transp);
}
#ifdef USE_TEXT
/*const*/ byte hc_font_data[] = {
0x20,0x83,0x07,0x00,0x02,0x00,0x01,0x00,0x01,0x00,0xD2,0x00,0xD8,0x00,0xE5,0x00,
0xEE,0x00,0xFA,0x00,0x07,0x01,0x14,0x01,0x21,0x01,0x2A,0x01,0x37,0x01,0x44,0x01,
0x50,0x01,0x5C,0x01,0x6A,0x01,0x74,0x01,0x81,0x01,0x8E,0x01,0x9B,0x01,0xA8,0x01,
0xB5,0x01,0xC2,0x01,0xCF,0x01,0xDC,0x01,0xE9,0x01,0xF6,0x01,0x03,0x02,0x10,0x02,
0x1C,0x02,0x2A,0x02,0x37,0x02,0x42,0x02,0x4F,0x02,0x5C,0x02,0x69,0x02,0x76,0x02,
0x83,0x02,0x90,0x02,0x9D,0x02,0xAA,0x02,0xB7,0x02,0xC4,0x02,0xD1,0x02,0xDE,0x02,
0xEB,0x02,0xF8,0x02,0x05,0x03,0x12,0x03,0x1F,0x03,0x2C,0x03,0x39,0x03,0x46,0x03,
0x53,0x03,0x60,0x03,0x6D,0x03,0x7A,0x03,0x87,0x03,0x94,0x03,0xA1,0x03,0xAE,0x03,
0xBB,0x03,0xC8,0x03,0xD5,0x03,0xE2,0x03,0xEB,0x03,0xF9,0x03,0x02,0x04,0x0F,0x04,
0x1C,0x04,0x29,0x04,0x36,0x04,0x43,0x04,0x50,0x04,0x5F,0x04,0x6C,0x04,0x79,0x04,
0x88,0x04,0x95,0x04,0xA2,0x04,0xAF,0x04,0xBC,0x04,0xC9,0x04,0xD8,0x04,0xE7,0x04,
0xF4,0x04,0x01,0x05,0x0E,0x05,0x1B,0x05,0x28,0x05,0x35,0x05,0x42,0x05,0x51,0x05,
0x5E,0x05,0x6B,0x05,0x78,0x05,0x85,0x05,0x8D,0x05,0x9A,0x05,0xA7,0x05,0xBB,0x05,
0xD9,0x05,0x00,0x00,0x03,0x00,0x00,0x00,0x07,0x00,0x02,0x00,0x01,0x00,0xC0,0xC0,
0xC0,0xC0,0xC0,0x00,0xC0,0x03,0x00,0x05,0x00,0x01,0x00,0xD8,0xD8,0xD8,0x06,0x00,
0x07,0x00,0x01,0x00,0x00,0x6C,0xFE,0x6C,0xFE,0x6C,0x07,0x00,0x07,0x00,0x01,0x00,
0x10,0x7C,0xD0,0x7C,0x16,0x7C,0x10,0x07,0x00,0x08,0x00,0x01,0x00,0xC3,0xC6,0x0C,
0x18,0x30,0x63,0xC3,0x07,0x00,0x08,0x00,0x01,0x00,0x38,0x6C,0x38,0x7A,0xCC,0xCE,
0x7B,0x03,0x00,0x03,0x00,0x01,0x00,0x60,0x60,0xC0,0x07,0x00,0x04,0x00,0x01,0x00,
0x30,0x60,0xC0,0xC0,0xC0,0x60,0x30,0x07,0x00,0x04,0x00,0x01,0x00,0xC0,0x60,0x30,
0x30,0x30,0x60,0xC0,0x06,0x00,0x07,0x00,0x01,0x00,0x00,0x6C,0x38,0xFE,0x38,0x6C,
0x06,0x00,0x06,0x00,0x01,0x00,0x00,0x30,0x30,0xFC,0x30,0x30,0x08,0x00,0x03,0x00,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0xC0,0x04,0x00,0x04,0x00,0x01,0x00,
0x00,0x00,0x00,0xF0,0x07,0x00,0x02,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,
0xC0,0x07,0x00,0x08,0x00,0x01,0x00,0x03,0x06,0x0C,0x18,0x30,0x60,0xC0,0x07,0x00,
0x06,0x00,0x01,0x00,0x78,0xCC,0xCC,0xCC,0xCC,0xCC,0x78,0x07,0x00,0x06,0x00,0x01,