Browse Source

[sdl] add CHR/PAL/PLD loading support

master
asie 1 month ago
parent
commit
8c41e96181
8 changed files with 229 additions and 13 deletions
  1. 93
    0
      src/asset_loader.c
  2. 28
    0
      src/asset_loader.h
  3. 77
    4
      src/frontend_posix.c
  4. 1
    0
      src/frontend_sdl.c
  5. 23
    9
      src/zzt.c
  6. 5
    0
      src/zzt.h
  7. 1
    0
      zeta_sdl.sh
  8. 1
    0
      zeta_sdl_mingw.sh

+ 93
- 0
src/asset_loader.c View File

@@ -0,0 +1,93 @@
1
+/**
2
+ * Copyright (c) 2018, 2019 Adrian Siekierka
3
+ *
4
+ * This file is part of Zeta.
5
+ *
6
+ * Zeta is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * Zeta is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with Zeta.  If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+#include <stdio.h>
21
+#include <stdlib.h>
22
+#include <string.h>
23
+#include "zzt.h"
24
+
25
+static u8 pld_to_pal[] = {
26
+	0, 1, 2, 3, 4, 5, 20, 7,
27
+	56, 57, 58, 59, 60, 61, 62, 63
28
+};
29
+
30
+static int zzt_load_chr(void *data, int dlen) {
31
+	u8 *data8 = (u8*) data;
32
+
33
+	if ((dlen & 0xFF) != 0) return -1;
34
+	return zzt_load_charset(8, dlen >> 8, data8);
35
+}
36
+
37
+static int zzt_load_pal(void *data, int dlen) {
38
+	u32 palette[16];
39
+	u8 *data8 = (u8*) data;
40
+
41
+	if (dlen < 48) return -1;
42
+
43
+	for (int i = 0; i < 16; i++) {
44
+		int pos = i * 3;
45
+
46
+		palette[i] =
47
+			  (((u32) (data8[pos] & 0x3F) * 255 / 63) << 16)
48
+			| (((u32) (data8[pos + 1] & 0x3F) * 255 / 63) << 8)
49
+			| (((u32) (data8[pos + 2] & 0x3F) * 255 / 63));
50
+	}
51
+
52
+	return zzt_load_palette(palette);
53
+}
54
+
55
+static int zzt_load_pld(void *data, int dlen) {
56
+	u32 palette[16];
57
+	u8 *data8 = (u8*) data;
58
+
59
+	if (dlen < 192) return -1;
60
+
61
+	for (int i = 0; i < 16; i++) {
62
+		int pos = (int)(pld_to_pal[i]) * 3;
63
+
64
+		palette[i] =
65
+			  (((u32) (data8[pos] & 0x3F) * 255 / 63) << 16)
66
+			| (((u32) (data8[pos + 1] & 0x3F) * 255 / 63) << 8)
67
+			| (((u32) (data8[pos + 2] & 0x3F) * 255 / 63));
68
+	}
69
+
70
+	return zzt_load_palette(palette);
71
+}
72
+
73
+int zzt_load_asset(char *type, void *data, int dlen) {
74
+	char category[17];
75
+	char *format = strchr(type, ':') + 1;
76
+	if ((format - type) > 16) return -1; // overflow protection
77
+	strncpy(category, type, format - type - 1);
78
+	category[format - type - 1] = '\0';
79
+
80
+	if (strcmp(category, "charset") == 0) {
81
+		if (strcmp(format, "chr") == 0) {
82
+			return zzt_load_chr(data, dlen);
83
+		}
84
+	} else if (strcmp(category, "palette") == 0) {
85
+		if (strcmp(format, "pal") == 0) {
86
+			return zzt_load_pal(data, dlen);
87
+		} else if (strcmp(format, "pld") == 0) {
88
+			return zzt_load_pld(data, dlen);
89
+		}
90
+	}
91
+
92
+	return -2;
93
+}

+ 28
- 0
src/asset_loader.h View File

@@ -0,0 +1,28 @@
1
+/**
2
+ * Copyright (c) 2018, 2019 Adrian Siekierka
3
+ *
4
+ * This file is part of Zeta.
5
+ *
6
+ * Zeta is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * Zeta is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with Zeta.  If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+#ifndef __ASSET_LOADER_H__
21
+#define __ASSET_LOADER_H__
22
+
23
+#include "zzt.h"
24
+
25
+USER_FUNCTION
26
+int zzt_load_asset(char *type, void *data, int dlen);
27
+
28
+#endif /* __ASSET_LOADER_H__ */

+ 77
- 4
src/frontend_posix.c View File

@@ -24,25 +24,41 @@ static int posix_vfs_exists(const char *filename) {
24 24
 }
25 25
 
26 26
 static void posix_zzt_help(int argc, char **argv) {
27
-	fprintf(stderr, "Usage: %s [-e command] [-bt] [world file]\n", argv > 0 ? argv[0] : "zeta");
27
+	char *owner = (argv > 0 && argv[0] != NULL && strlen(argv[0]) > 0) ? argv[0] : "zeta";
28
+
29
+	fprintf(stderr, "Usage: %s [arguments] [world file]\n", owner);
28 30
 	fprintf(stderr, "\n");
31
+	fprintf(stderr, "Arguments ([] - parameter; * - may specify multiple times):\n");
29 32
 	fprintf(stderr, "  -b     disable blinking, enable bright backgrounds\n");
30
-	fprintf(stderr, "  -e []  execute command - repeat to run multiple commands\n");
31
-	fprintf(stderr, "         by default, runs either ZZT.EXE or SUPERZ.EXE\n");
33
+	fprintf(stderr, " *-e []  execute command - repeat to run multiple commands\n");
34
+	fprintf(stderr, "         by default, ZZT.EXE or SUPERZ.EXE is executed\n");
35
+	fprintf(stderr, "  -h     show help\n");
36
+	fprintf(stderr, " *-l []  load asset - in \"type:format:filename\" form or\n");
37
+	fprintf(stderr, "         \"filename\" form to attempt a guess\n");
38
+	fprintf(stderr, "         available types/formats: \n");
39
+	fprintf(stderr, "         - charset:\n");
40
+	fprintf(stderr, "             - chr (MegaZeux-like; 8x[height], 256 chars)\n");
41
+	fprintf(stderr, "         - palette:\n");
42
+	fprintf(stderr, "             - pal (MegaZeux-like; 16 colors ranged 00-3F)\n");
43
+	fprintf(stderr, "             - pld (Toshiba UPAL; 64 EGA colors ranged 00-3F)\n");
32 44
 	fprintf(stderr, "  -t     enable world testing mode (skip K, C, ENTER)\n");
33 45
 	fprintf(stderr, "\n");
34 46
 	fprintf(stderr, "See <https://zeta.asie.pl/> for more information.\n");
35 47
 }
36 48
 
49
+#define IS_EXTENSION(s, e) (strcasecmp((s + strlen((s)) - strlen((e))), (e)) == 0)
50
+
37 51
 static int posix_zzt_init(int argc, char **argv) {
38 52
 	char arg_name[257];
39 53
 	char *execs[16];
54
+	char *loads[16];
40 55
 	int exec_count = 0;
56
+	int load_count = 0;
41 57
 	int c;
42 58
 	int skip_kc = 0;
43 59
 
44 60
 #ifdef USE_GETOPT
45
-	while ((c = getopt(argc, argv, "be:ht")) >= 0) {
61
+	while ((c = getopt(argc, argv, "be:hl:t")) >= 0) {
46 62
 		switch(c) {
47 63
 			case 'b':
48 64
 				video_blink = 0;
@@ -54,6 +70,13 @@ static int posix_zzt_init(int argc, char **argv) {
54 70
 				}
55 71
 				execs[exec_count++] = optarg;
56 72
 				break;
73
+			case 'l':
74
+				if (load_count > 16) {
75
+					fprintf(stderr, "Too many -l commands!\n");
76
+					return -1;
77
+				}
78
+				loads[load_count++] = optarg;
79
+				break;
57 80
 			case 'h':
58 81
 				posix_zzt_help(argc, argv);
59 82
 				exit(0);
@@ -95,6 +118,56 @@ static int posix_zzt_init(int argc, char **argv) {
95 118
 		}
96 119
 	}
97 120
 
121
+	for (int i = 0; i < load_count; i++) {
122
+		char *type, *filename;
123
+
124
+		if (strrchr(loads[i], ':') == NULL) {
125
+			filename = loads[i];
126
+			if (IS_EXTENSION(filename, ".chr")) type = "charset:chr";
127
+			else if (IS_EXTENSION(filename, ".pal")) type = "palette:pal";
128
+			else if (IS_EXTENSION(filename, ".pld")) type = "palette:pld";
129
+			else {
130
+				fprintf(stderr, "Could not guess type of %s!\n", filename);
131
+				continue;
132
+			}
133
+		} else {
134
+			type = loads[i];
135
+			filename = strrchr(type, ':') + 1;
136
+			type[filename - type - 1] = '\0';
137
+		}
138
+
139
+		FILE *file = fopen(filename, "rb");
140
+		if (!file) {
141
+			fprintf(stderr, "Could not open %s!\n", filename);
142
+			continue;
143
+		}
144
+
145
+		fseek(file, 0, SEEK_END);
146
+		long fsize = ftell(file);
147
+		fseek(file, 0, SEEK_SET);
148
+
149
+		if (fsize <= 0 || fsize >= 1048576) {
150
+			fprintf(stderr, "Could not read %s!\n", filename);
151
+			fclose(file);
152
+			continue;
153
+		}
154
+
155
+		u8 *buffer = (u8*) malloc((int) fsize);
156
+		if (!fread(buffer, (int) fsize, 1, file)) {
157
+			fprintf(stderr, "Could not read %s!\n", filename);
158
+			fclose(file);
159
+			continue;
160
+		}
161
+
162
+		fclose(file);
163
+
164
+		if (zzt_load_asset(type, buffer, (int) fsize) < 0) {
165
+			fprintf(stderr, "Could not load %s!\n", filename);
166
+		}
167
+
168
+		free(buffer);
169
+	}
170
+
98 171
 	if (exec_count > 0) {
99 172
 		int exeh = 0;
100 173
 		for (int i = 0; i < exec_count; i++) {

+ 1
- 0
src/frontend_sdl.c View File

@@ -587,6 +587,7 @@ void zeta_update_palette(u32* data) {
587 587
 	SDL_UnlockMutex(render_data_update_mutex);
588 588
 }
589 589
 
590
+#include "asset_loader.h"
590 591
 #include "frontend_posix.c"
591 592
 
592 593
 int main(int argc, char **argv) {

+ 23
- 9
src/zzt.c View File

@@ -956,6 +956,26 @@ void zzt_load_binary(int handle, const char *arg) {
956 956
 	fprintf(stderr, "wrote %d bytes to %d\n", bytes_read, (offset_pars * 16 + 256));
957 957
 }
958 958
 
959
+int zzt_load_charset(int width, int height, u8 *data) {
960
+	if (width != 8 || height <= 0 || height > 16) return -1;
961
+
962
+	for (int i = 0; i < 256*height; i++) {
963
+		zzt.charset[i] = data[i];
964
+	}
965
+
966
+	zeta_update_charset(8, 14, zzt.charset);
967
+	return 0;
968
+}
969
+
970
+int zzt_load_palette(u32 *colors) {
971
+	for (int i = 0; i < 16; i++) {
972
+		zzt.palette[i] = colors[i];
973
+	}
974
+
975
+	zeta_update_palette(zzt.palette);
976
+	return 0;
977
+}
978
+
959 979
 void zzt_init(void) {
960 980
 /*	for (int i = 0; i < MAX_ALLOC; i++)
961 981
 		seg_allocs[i] = (i < 256) ? (256-i) : 0; */
@@ -1000,16 +1020,10 @@ void zzt_init(void) {
1000 1020
 	zzt.cpu.func_port_out = cpu_func_port_out_main;
1001 1021
 	zzt.cpu.func_interrupt = cpu_func_interrupt_main;
1002 1022
 
1003
-	for (int i = 0; i < 256*14; i++) {
1004
-		zzt.charset[i] = res_8x14_bin[i];
1005
-	}
1006
-
1007
-	for (int i = 0; i < 16; i++) {
1008
-		zzt.palette[i] = def_palette[i];
1009
-	}
1023
+	// default assets
1010 1024
 
1011
-	zeta_update_charset(8, 14, zzt.charset);
1012
-	zeta_update_palette(zzt.palette);
1025
+	zzt_load_charset(8, 14, res_8x14_bin);
1026
+	zzt_load_palette(def_palette);
1013 1027
 }
1014 1028
 
1015 1029
 int zzt_execute(int opcodes) {

+ 5
- 0
src/zzt.h View File

@@ -69,6 +69,11 @@ void zzt_key_set_delay(int ms, int repeat_ms);
69 69
 USER_FUNCTION
70 70
 void zzt_set_timer_offset(long ms);
71 71
 
72
+USER_FUNCTION
73
+int zzt_load_charset(int width, int height, u8* data);
74
+USER_FUNCTION
75
+int zzt_load_palette(u32* colors);
76
+
72 77
 IMPLEMENT_FUNCTION
73 78
 long zeta_time_ms(void);
74 79
 

+ 1
- 0
zeta_sdl.sh View File

@@ -4,4 +4,5 @@ gcc -o build/zeta86 -g -O2 -std=c18 -Wall \
4 4
   res/8x14.c src/posix_vfs.c src/audio_stream.c \
5 5
   src/frontend_sdl.c src/zzt.c src/cpu.c \
6 6
   src/screenshot_writer.c src/render_software.c \
7
+  src/asset_loader.c \
7 8
   -lGL -lSDL2 -lSDL2main

+ 1
- 0
zeta_sdl_mingw.sh View File

@@ -5,4 +5,5 @@ i686-w64-mingw32-windres mingw/resources.rc build/mingw_resources.o
5 5
 i686-w64-mingw32-gcc -o build/zeta86.exe -g -O2 -std=c18 -Wall -mwindows \
6 6
   res/8x14.c src/posix_vfs.c src/audio_stream.c src/frontend_sdl.c src/zzt.c src/cpu.c \
7 7
   src/screenshot_writer.c src/render_software.c build/mingw_resources.o \
8
+  src/asset_loader.c \
8 9
   -lmingw32 -lSDL2main -lSDL2 -lopengl32

Loading…
Cancel
Save