Browse Source

[curses] make real

master
asie 4 months ago
parent
commit
487539cc55
5 changed files with 559 additions and 197 deletions
  1. 88
    69
      src/frontend_curses.c
  2. 322
    0
      src/frontend_curses_tables.c
  3. 144
    0
      src/frontend_posix.c
  4. 2
    126
      src/frontend_sdl.c
  5. 3
    2
      zeta_curses.sh

+ 88
- 69
src/frontend_curses.c View File

@@ -17,33 +17,51 @@
17 17
  * along with Zeta.  If not, see <http://www.gnu.org/licenses/>.
18 18
  */
19 19
 
20
+#define _DEFAULT_SOURCE
21
+#define _POSIX_C_SOURCE 2
22
+#include <unistd.h>
23
+
20 24
 #include <stdio.h>
21 25
 #include <stdlib.h>
22 26
 #include <string.h>
23 27
 #include <time.h>
24
-#include <ncurses.h>
28
+#include <locale.h>
29
+
25 30
 #include "zzt.h"
26 31
 #include "posix_vfs.h"
27 32
 
33
+#include <ncurses.h>
34
+
35
+static int video_blink;
36
+
28 37
 long zeta_time_ms() {
29
-	clock_t c = clock();
30
-	return c / (CLOCKS_PER_SEC/1000);
38
+	struct timespec spec;
39
+
40
+	clock_gettime(CLOCK_REALTIME, &spec);
41
+	return ((long) spec.tv_sec * 1000) + (long) (spec.tv_nsec / 1000000);
31 42
 }
32 43
 
33 44
 void cpu_ext_log(const char* s) {
34
-	fprintf(stderr, "%s\n", s);
45
+//	fprintf(stderr, "%s\n", s);
35 46
 }
36 47
 
37 48
 void speaker_on(double freq) {}
38 49
 void speaker_off() {}
39 50
 
40
-static int map_char_to_key[512];
41 51
 static WINDOW* window;
42 52
 
53
+#include "frontend_curses_tables.c"
54
+
43 55
 int zeta_has_feature(int feature) {
44 56
 	return 1;
45 57
 }
46 58
 
59
+void zeta_update_charset(int width, int height, u8* data) {
60
+}
61
+
62
+void zeta_update_palette(u32* data) {
63
+}
64
+
47 65
 static void platform_kbd_tick() {
48 66
 	int c = getch();
49 67
 	if (c == ERR) return;
@@ -51,59 +69,33 @@ static void platform_kbd_tick() {
51 69
 	if (c == 0 || map_char_to_key[c] != 0) {
52 70
 		int k = map_char_to_key[c];
53 71
 		zzt_key(c, k);
72
+		zzt_keyup(k);
54 73
 	}
55 74
 }
56 75
 
57
-static void init_map_char_to_key() {
58
-	map_char_to_key[258] = 0x50; // down
59
-	map_char_to_key[260] = 0x4B; // left
60
-	map_char_to_key[259] = 0x48; // up
61
-	map_char_to_key[261] = 0x4D; // right
62
-	map_char_to_key[13] = 0x1C; // enter
63
-	map_char_to_key[0] = 0;
64
-	map_char_to_key[0x1C] = 1; // esc
65
-	for (int i = 1; i <= 9; i++) map_char_to_key[i + 48] = i + 1;
66
-	char* chrs = "!@#$%^&*()";
67
-	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = i + 1;
68
-	map_char_to_key[48] = 11; // numbers
69
-	map_char_to_key[45] = 12; // -
70
-	map_char_to_key[95] = 12; // _
71
-	map_char_to_key[61] = 13; // =
72
-	map_char_to_key[43] = 12; // +
73
-	map_char_to_key[8] = 14; // backspace
74
-	map_char_to_key[9] = 15; // tab
75
-	chrs = "qwertyuiop[]";
76
-	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 15 + i;
77
-	chrs = "QWERTYUIOP{}";
78
-	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 15 + i;
79
-	map_char_to_key[13] = 28; // enter
80
-	// 29?
81
-	chrs = "asdfghjkl;'";
82
-	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 29 + i;
83
-	chrs = "ASDFGHJKL:\"";
84
-	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 29 + i;
85
-	map_char_to_key[96] = 41; // `
86
-	map_char_to_key[126] = 41; // ~
87
-	// 42?
88
-	map_char_to_key[92] = 43;
89
-	map_char_to_key[124] = 43; // |
90
-	chrs = "zxcvbnm,./";
91
-	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 43 + i;
92
-	chrs = "ZXCVBNM<>?";
93
-	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 43 + i;
94
-	// 54?
95
-	map_char_to_key[42] = 55; // *
96
-	// 56?
97
-	map_char_to_key[32] = 57; // space
76
+#include "frontend_posix.c"
77
+
78
+static int nc_color_map[] = {
79
+	COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN,
80
+	COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE
81
+};
82
+
83
+static void init_ncurses_colors() {
84
+	for (int i = 0; i < 64; i++) {
85
+		init_pair(i+1, nc_color_map[i&7], nc_color_map[i>>3]);
86
+	}
98 87
 }
99 88
 
100 89
 int main(int argc, char** argv) {
101
-	init_posix_vfs("vfs/");
90
+	init_posix_vfs("");
102 91
 	init_map_char_to_key();
103
-	zzt_init();
104
-	int handle = vfs_open(argv[1]);
105
-	zzt_load_binary(handle, argc > 2 ? argv[2] : "");
106
-	vfs_close(handle);
92
+
93
+	if (posix_zzt_init(argc, argv) < 0) {
94
+		fprintf(stderr, "Could not load ZZT!\n");
95
+		return 1;
96
+	}
97
+
98
+	setlocale(LC_ALL, "");
107 99
 
108 100
 	window = initscr();
109 101
 	cbreak();
@@ -115,31 +107,58 @@ int main(int argc, char** argv) {
115 107
 	idlok(window, 1);
116 108
 	keypad(window, 1);
117 109
 
118
-	clock_t last = clock();
119
-	clock_t curr = last;
120
-
121
-	while (zzt_execute(1600000)) {
122
-		// refresh screen
123
-		u8* ram = zzt_get_ram();
124
-		for (int y = 0; y < 25; y++) {
125
-			for (int x = 0; x < 80; x++) {
126
-				u8 chr = ram[TEXT_ADDR(x,y)];
127
-				if (chr == 2) chr = '@';
128
-				else if (chr == 0) chr = 32;
129
-				else if (chr < 32 || chr >= 128)
130
-					chr = '0' + (chr % 10);
131
-				mvwaddch(window, y, x, chr);
110
+	start_color();
111
+	init_ncurses_colors();
112
+/*	clock_t last = clock();
113
+	clock_t curr = last; */
114
+
115
+	long timer_ms = zeta_time_ms();
116
+	long render_ms = timer_ms;
117
+
118
+	int rcode = 0;
119
+
120
+	while ((rcode = zzt_execute(64000)) > 0) {
121
+		long curr_ms = zeta_time_ms();
122
+
123
+		if ((curr_ms - render_ms) >= 10) {
124
+			// refresh screen
125
+			u8* ram = zzt_get_ram();
126
+			for (int y = 0; y < 25; y++) {
127
+				for (int x = 0; x < 80; x++) {
128
+					u8 chr = ram[TEXT_ADDR(x,y)];
129
+					u8 col = ram[TEXT_ADDR(x,y)+1];
130
+
131
+					int attr = COLOR_PAIR(1+((col & 7) | ((col & 0x70) >> 1) ));
132
+					if ((col & 0x08) != 0) attr |= A_BOLD;
133
+					wattrset(window, attr);
134
+					mvwaddstr(window, y, x, map_char_to_unicode[chr]);
135
+				}
132 136
 			}
137
+			render_ms = curr_ms;
133 138
 		}
139
+
134 140
 		wrefresh(window);
135 141
 		zzt_mark_frame();
136 142
 
137
-		curr = clock();
143
+/*		curr = clock();
138 144
 		float secs = (float) (curr - last) / CLOCKS_PER_SEC;
139 145
 		fprintf(stderr, "%.2f opc/sec\n", 1600000.0f / secs);
140
-		platform_kbd_tick();
141
-		last = curr;
146
+		last = curr; */
147
+
148
+		if (rcode == 3) {
149
+			long sleep_time = 55 - (curr_ms - timer_ms);
150
+			if (sleep_time > 1) {
151
+				usleep(sleep_time * 1000);
152
+			}
153
+		}
142 154
 
143
-		zzt_mark_timer();
155
+		if ((curr_ms - timer_ms) >= 55) {
156
+			zzt_mark_timer();
157
+			timer_ms = curr_ms;
158
+		}
159
+
160
+		platform_kbd_tick();
144 161
 	}
162
+
163
+	endwin();
145 164
 }

+ 322
- 0
src/frontend_curses_tables.c View File

@@ -0,0 +1,322 @@
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
+static u8 map_char_to_key[512];
21
+
22
+static void init_map_char_to_key() {
23
+	map_char_to_key[258] = 0x50; // down
24
+	map_char_to_key[260] = 0x4B; // left
25
+	map_char_to_key[259] = 0x48; // up
26
+	map_char_to_key[261] = 0x4D; // right
27
+	map_char_to_key[13] = 0x1C; // enter
28
+	map_char_to_key[0] = 0;
29
+	map_char_to_key[0x1C] = 1; // esc
30
+	for (int i = 1; i <= 9; i++) map_char_to_key[i + 48] = i + 1;
31
+	char* chrs = "!@#$%^&*()";
32
+	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = i + 1;
33
+	map_char_to_key[48] = 11; // numbers
34
+	map_char_to_key[45] = 12; // -
35
+	map_char_to_key[95] = 12; // _
36
+	map_char_to_key[61] = 13; // =
37
+	map_char_to_key[43] = 12; // +
38
+	map_char_to_key[8] = 14; // backspace
39
+	map_char_to_key[9] = 15; // tab
40
+	chrs = "qwertyuiop[]";
41
+	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 15 + i;
42
+	chrs = "QWERTYUIOP{}";
43
+	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 15 + i;
44
+	map_char_to_key[13] = 28; // enter
45
+	// 29?
46
+	chrs = "asdfghjkl;'";
47
+	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 29 + i;
48
+	chrs = "ASDFGHJKL:\"";
49
+	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 29 + i;
50
+	map_char_to_key[96] = 41; // `
51
+	map_char_to_key[126] = 41; // ~
52
+	// 42?
53
+	map_char_to_key[92] = 43;
54
+	map_char_to_key[124] = 43; // |
55
+	chrs = "zxcvbnm,./";
56
+	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 43 + i;
57
+	chrs = "ZXCVBNM<>?";
58
+	for (int i = 0; i < strlen(chrs); i++) map_char_to_key[(int)chrs[i]] = 43 + i;
59
+	// 54?
60
+	map_char_to_key[42] = 55; // *
61
+	// 56?
62
+	map_char_to_key[32] = 57; // space
63
+}
64
+
65
+static const char* map_char_to_unicode[] = {
66
+ "\x20",
67
+ "\u263A",
68
+ "\u263B",
69
+ "\u2665",
70
+ "\u2666",
71
+ "\u2663",
72
+ "\u2660",
73
+ "\u2022",
74
+ "\u25D8",
75
+ "\u25EF",
76
+ "\u25D9",
77
+ "\u2642",
78
+ "\u2640",
79
+ "\u266A",
80
+ "\u266C",
81
+ "\u263C",
82
+ "\u25BA",
83
+ "\u25C4",
84
+ "\u2195",
85
+ "\u203C",
86
+ "\u00B6",
87
+ "\u00A7",
88
+ "\u25AC",
89
+ "\u21A8",
90
+ "\u2191",
91
+ "\u2193",
92
+ "\u2192",
93
+ "\u2190",
94
+ "\u2319",
95
+ "\u2194",
96
+ "\u25B2",
97
+ "\u25BC",
98
+ "\x20",
99
+ "\x21",
100
+ "\x22",
101
+ "\x23",
102
+ "\x24",
103
+ "\x25",
104
+ "\x26",
105
+ "\x27",
106
+ "\x28",
107
+ "\x29",
108
+ "\x2A",
109
+ "\x2B",
110
+ "\x2C",
111
+ "\x2D",
112
+ "\x2E",
113
+ "\x2F",
114
+ "\x30",
115
+ "\x31",
116
+ "\x32",
117
+ "\x33",
118
+ "\x34",
119
+ "\x35",
120
+ "\x36",
121
+ "\x37",
122
+ "\x38",
123
+ "\x39",
124
+ "\x3A",
125
+ "\x3B",
126
+ "\x3C",
127
+ "\x3D",
128
+ "\x3E",
129
+ "\x3F",
130
+ "\x40",
131
+ "\x41",
132
+ "\x42",
133
+ "\x43",
134
+ "\x44",
135
+ "\x45",
136
+ "\x46",
137
+ "\x47",
138
+ "\x48",
139
+ "\x49",
140
+ "\x4A",
141
+ "\x4B",
142
+ "\x4C",
143
+ "\x4D",
144
+ "\x4E",
145
+ "\x4F",
146
+ "\x50",
147
+ "\x51",
148
+ "\x52",
149
+ "\x53",
150
+ "\x54",
151
+ "\x55",
152
+ "\x56",
153
+ "\x57",
154
+ "\x58",
155
+ "\x59",
156
+ "\x5A",
157
+ "\x5B",
158
+ "\x5C",
159
+ "\x5D",
160
+ "\x5E",
161
+ "\x5F",
162
+ "\x60",
163
+ "\x61",
164
+ "\x62",
165
+ "\x63",
166
+ "\x64",
167
+ "\x65",
168
+ "\x66",
169
+ "\x67",
170
+ "\x68",
171
+ "\x69",
172
+ "\x6A",
173
+ "\x6B",
174
+ "\x6C",
175
+ "\x6D",
176
+ "\x6E",
177
+ "\x6F",
178
+ "\x70",
179
+ "\x71",
180
+ "\x72",
181
+ "\x73",
182
+ "\x74",
183
+ "\x75",
184
+ "\x76",
185
+ "\x77",
186
+ "\x78",
187
+ "\x79",
188
+ "\x7A",
189
+ "\x7B",
190
+ "\x7C",
191
+ "\x7D",
192
+ "\x7E",
193
+ "\x7F",
194
+ "\u00C7",
195
+ "\u00FC",
196
+ "\u00E9",
197
+ "\u00E2",
198
+ "\u00E4",
199
+ "\u00E0",
200
+ "\u00E5",
201
+ "\u00E7",
202
+ "\u00EA",
203
+ "\u00EB",
204
+ "\u00E8",
205
+ "\u00EF",
206
+ "\u00EE",
207
+ "\u00EC",
208
+ "\u00C4",
209
+ "\u00C5",
210
+ "\u00C9",
211
+ "\u00E6",
212
+ "\u00C6",
213
+ "\u00F4",
214
+ "\u00F6",
215
+ "\u00F2",
216
+ "\u00FB",
217
+ "\u00F9",
218
+ "\u00FF",
219
+ "\u00D6",
220
+ "\u00DC",
221
+ "\u00A2",
222
+ "\u00A3",
223
+ "\u00A5",
224
+ "\u20A7",
225
+ "\u0192",
226
+ "\u00E1",
227
+ "\u00ED",
228
+ "\u00F3",
229
+ "\u00FA",
230
+ "\u00F1",
231
+ "\u00D1",
232
+ "\u00AA",
233
+ "\u00BA",
234
+ "\u00BF",
235
+ "\u2310",
236
+ "\u00AC",
237
+ "\u00BD",
238
+ "\u00BC",
239
+ "\u00A1",
240
+ "\u00AB",
241
+ "\u00BB",
242
+ "\u2591",
243
+ "\u2592",
244
+ "\u2593",
245
+ "\u2502",
246
+ "\u2524",
247
+ "\u2561",
248
+ "\u2562",
249
+ "\u2556",
250
+ "\u2555",
251
+ "\u2563",
252
+ "\u2551",
253
+ "\u2557",
254
+ "\u255D",
255
+ "\u255C",
256
+ "\u255B",
257
+ "\u2510",
258
+ "\u2514",
259
+ "\u2534",
260
+ "\u252C",
261
+ "\u251C",
262
+ "\u2500",
263
+ "\u253C",
264
+ "\u255E",
265
+ "\u255F",
266
+ "\u255A",
267
+ "\u2554",
268
+ "\u2569",
269
+ "\u2566",
270
+ "\u2560",
271
+ "\u2550",
272
+ "\u256C",
273
+ "\u2567",
274
+ "\u2568",
275
+ "\u2564",
276
+ "\u2565",
277
+ "\u2559",
278
+ "\u2558",
279
+ "\u2552",
280
+ "\u2553",
281
+ "\u256B",
282
+ "\u256A",
283
+ "\u2518",
284
+ "\u250C",
285
+ "\u2588",
286
+ "\u2584",
287
+ "\u258C",
288
+ "\u2590",
289
+ "\u2580",
290
+ "\u03B1",
291
+ "\u00DF",
292
+ "\u0393",
293
+ "\u03C0",
294
+ "\u03A3",
295
+ "\u03C3",
296
+ "\u00B5",
297
+ "\u03C4",
298
+ "\u03A6",
299
+ "\u0398",
300
+ "\u03A9",
301
+ "\u03B4",
302
+ "\u221E",
303
+ "\u03C6",
304
+ "\u03B5",
305
+ "\u2229",
306
+ "\u2261",
307
+ "\u00B1",
308
+ "\u2265",
309
+ "\u2264",
310
+ "\u2320",
311
+ "\u2321",
312
+ "\u00F7",
313
+ "\u2248",
314
+ "\u00B0",
315
+ "\u2219",
316
+ "\u00B7",
317
+ "\u221A",
318
+ "\u207F",
319
+ "\u00B2",
320
+ "\u25A0",
321
+ "\x20"
322
+};

+ 144
- 0
src/frontend_posix.c View File

@@ -0,0 +1,144 @@
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
+static int posix_vfs_exists(const char *filename) {
21
+	int h = vfs_open(filename, 0);
22
+	if (h >= 0) { vfs_close(h); return 1; }
23
+	else { return 0; }
24
+}
25
+
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");
28
+	fprintf(stderr, "\n");
29
+	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");
32
+	fprintf(stderr, "  -t     enable world testing mode (skip K, C, ENTER)\n");
33
+	fprintf(stderr, "\n");
34
+	fprintf(stderr, "See <https://zeta.asie.pl/> for more information.\n");
35
+}
36
+
37
+static int posix_zzt_init(int argc, char **argv) {
38
+	char arg_name[257];
39
+	char *execs[16];
40
+	int exec_count = 0;
41
+	int c;
42
+	int skip_kc = 0;
43
+
44
+#ifndef NO_GETOPT
45
+	while ((c = getopt(argc, argv, "be:ht")) >= 0) {
46
+		switch(c) {
47
+			case 'b':
48
+				video_blink = 0;
49
+				break;
50
+			case 'e':
51
+				if (exec_count > 16) {
52
+					fprintf(stderr, "Too many -e commands!\n");
53
+					return -1;
54
+				}
55
+				execs[exec_count++] = optarg;
56
+				break;
57
+			case 'h':
58
+				posix_zzt_help(argc, argv);
59
+				exit(0);
60
+				return -1;
61
+			case 't':
62
+				skip_kc = 1;
63
+				break;
64
+			case '?':
65
+				fprintf(stderr, "Could not parse options! Try %s -h for help.\n", argv > 0 ? argv[0] : "running with");
66
+				exit(0);
67
+				return -1;
68
+		}
69
+	}
70
+#endif
71
+
72
+	zzt_init();
73
+
74
+#ifdef NO_GETOPT
75
+	if (argc > 1 && posix_vfs_exists(argv[1])) {
76
+		strncpy(arg_name, argv[1], 256);
77
+#else
78
+	if (argc > optind && posix_vfs_exists(argv[optind])) {
79
+		strncpy(arg_name, argv[optind], 256);
80
+#endif
81
+	} else if (argc > 0) {
82
+		char *sl_ptr = strrchr(argv[0], '/');
83
+		if (sl_ptr == NULL)
84
+			sl_ptr = strrchr(argv[0], '\\');
85
+		if (sl_ptr == NULL)
86
+			sl_ptr = argv[0] - 1;
87
+
88
+		strncpy(arg_name, sl_ptr + 1, 256);
89
+		char *dot_ptr = strrchr(arg_name, '.');
90
+		if (dot_ptr == NULL) dot_ptr = arg_name + strlen(arg_name);
91
+		strncpy(dot_ptr, ".zzt", 256 - (dot_ptr - arg_name));
92
+
93
+		if (!posix_vfs_exists(arg_name)) {
94
+			arg_name[0] = 0;
95
+		}
96
+	}
97
+
98
+	if (exec_count > 0) {
99
+		int exeh = 0;
100
+		for (int i = 0; i < exec_count; i++) {
101
+			char *space_ptr = strchr(execs[i], ' ');
102
+			if (space_ptr != NULL) {
103
+				space_ptr[0] = '\0';
104
+				exeh = vfs_open(execs[i], 0);
105
+				if (exeh < 0) {
106
+					fprintf(stderr, "Could not load %s!\n", execs[i]);
107
+					space_ptr[0] = ' ';
108
+					return -1;
109
+				}
110
+				space_ptr[0] = ' ';
111
+				fprintf(stderr, "'%s'\n", space_ptr + 1);
112
+				zzt_load_binary(exeh, space_ptr + 1);
113
+				vfs_close(exeh);
114
+			} else {
115
+				exeh = vfs_open(execs[i], 0);
116
+				if (exeh < 0) {
117
+					fprintf(stderr, "Could not load %s!\n", execs[i]);
118
+					return -1;
119
+				}
120
+				zzt_load_binary(exeh, (i == exec_count - 1) ? arg_name : NULL);
121
+				vfs_close(exeh);
122
+			}
123
+
124
+			// last binary is engine
125
+			if (i == exec_count - 1) break;
126
+			while (zzt_execute(10000) != STATE_END) { }
127
+		}
128
+	} else {
129
+		int exeh = vfs_open("zzt.exe", 0);
130
+		if (exeh < 0)
131
+			exeh = vfs_open("superz.exe", 0);
132
+		if (exeh < 0)
133
+			return -1;
134
+		zzt_load_binary(exeh, arg_name);
135
+		vfs_close(exeh);
136
+	}
137
+
138
+	if (skip_kc) {
139
+		zzt_key('k', 0x25);
140
+		zzt_key('c', 0x2E);
141
+		zzt_key('\r', 0x1C);
142
+	}
143
+	return 0;
144
+}

+ 2
- 126
src/frontend_sdl.c View File

@@ -485,131 +485,7 @@ void zeta_update_palette(u32* data) {
485 485
 	SDL_UnlockMutex(render_data_update_mutex);
486 486
 }
487 487
 
488
-static int sdl_vfs_exists(const char *filename) {
489
-	int h = vfs_open(filename, 0);
490
-	if (h >= 0) { vfs_close(h); return 1; }
491
-	else { return 0; }
492
-}
493
-
494
-static void sdl_zzt_help(int argc, char **argv) {
495
-	fprintf(stderr, "Usage: %s [-e command] [-bt] [world file]\n", argv > 0 ? argv[0] : "zeta");
496
-	fprintf(stderr, "\n");
497
-	fprintf(stderr, "  -b     disable blinking, enable bright backgrounds\n");
498
-	fprintf(stderr, "  -e []  execute command - repeat to run multiple commands\n");
499
-	fprintf(stderr, "         by default, runs either ZZT.EXE or SUPERZ.EXE\n");
500
-	fprintf(stderr, "  -t     enable world testing mode (skip K, C, ENTER)\n");
501
-	fprintf(stderr, "\n");
502
-	fprintf(stderr, "See <https://zeta.asie.pl/> for more information.\n");
503
-}
504
-
505
-static int sdl_zzt_init(int argc, char **argv) {
506
-	char arg_name[257];
507
-	char *execs[16];
508
-	int exec_count = 0;
509
-	int c;
510
-	int skip_kc = 0;
511
-
512
-#ifndef NO_GETOPT
513
-	while ((c = getopt(argc, argv, "be:ht")) >= 0) {
514
-		switch(c) {
515
-			case 'b':
516
-				video_blink = 0;
517
-				break;
518
-			case 'e':
519
-				if (exec_count > 16) {
520
-					fprintf(stderr, "Too many -e commands!\n");
521
-					return -1;
522
-				}
523
-				execs[exec_count++] = optarg;
524
-				break;
525
-			case 'h':
526
-				sdl_zzt_help(argc, argv);
527
-				exit(0);
528
-				return -1;
529
-			case 't':
530
-				skip_kc = 1;
531
-				break;
532
-			case '?':
533
-				fprintf(stderr, "Could not parse options! Try %s -h for help.\n", argv > 0 ? argv[0] : "running with");
534
-				exit(0);
535
-				return -1;
536
-		}
537
-	}
538
-#endif
539
-
540
-	zzt_init();
541
-
542
-#ifdef NO_GETOPT
543
-	if (argc > 1 && sdl_vfs_exists(argv[1])) {
544
-		strncpy(arg_name, argv[1], 256);
545
-#else
546
-	if (argc > optind && sdl_vfs_exists(argv[optind])) {
547
-		strncpy(arg_name, argv[optind], 256);
548
-#endif
549
-	} else if (argc > 0) {
550
-		char *sl_ptr = strrchr(argv[0], '/');
551
-		if (sl_ptr == NULL)
552
-			sl_ptr = strrchr(argv[0], '\\');
553
-		if (sl_ptr == NULL)
554
-			sl_ptr = argv[0] - 1;
555
-
556
-		strncpy(arg_name, sl_ptr + 1, 256);
557
-		char *dot_ptr = strrchr(arg_name, '.');
558
-		if (dot_ptr == NULL) dot_ptr = arg_name + strlen(arg_name);
559
-		strncpy(dot_ptr, ".zzt", 256 - (dot_ptr - arg_name));
560
-
561
-		if (!sdl_vfs_exists(arg_name)) {
562
-			arg_name[0] = 0;
563
-		}
564
-	}
565
-
566
-	if (exec_count > 0) {
567
-		int exeh = 0;
568
-		for (int i = 0; i < exec_count; i++) {
569
-			char *space_ptr = strchr(execs[i], ' ');
570
-			if (space_ptr != NULL) {
571
-				space_ptr[0] = '\0';
572
-				exeh = vfs_open(execs[i], 0);
573
-				if (exeh < 0) {
574
-					fprintf(stderr, "Could not load %s!\n", execs[i]);
575
-					space_ptr[0] = ' ';
576
-					return -1;
577
-				}
578
-				space_ptr[0] = ' ';
579
-				fprintf(stderr, "'%s'\n", space_ptr + 1);
580
-				zzt_load_binary(exeh, space_ptr + 1);
581
-				vfs_close(exeh);
582
-			} else {
583
-				exeh = vfs_open(execs[i], 0);
584
-				if (exeh < 0) {
585
-					fprintf(stderr, "Could not load %s!\n", execs[i]);
586
-					return -1;
587
-				}
588
-				zzt_load_binary(exeh, (i == exec_count - 1) ? arg_name : NULL);
589
-				vfs_close(exeh);
590
-			}
591
-
592
-			// last binary is engine
593
-			if (i == exec_count - 1) break;
594
-			while (zzt_execute(10000) != STATE_END) { }
595
-		}
596
-	} else {
597
-		int exeh = vfs_open("zzt.exe", 0);
598
-		if (exeh < 0)
599
-			exeh = vfs_open("superz.exe", 0);
600
-		if (exeh < 0)
601
-			return -1;
602
-		zzt_load_binary(exeh, arg_name);
603
-		vfs_close(exeh);
604
-	}
605
-
606
-	if (skip_kc) {
607
-		zzt_key('k', 0x25);
608
-		zzt_key('c', 0x2E);
609
-		zzt_key('\r', 0x1C);
610
-	}
611
-	return 0;
612
-}
488
+#include "frontend_posix.c"
613 489
 
614 490
 int main(int argc, char **argv) {
615 491
 	int scancodes_lifted[sdl_to_pc_scancode_max + 1];
@@ -636,7 +512,7 @@ int main(int argc, char **argv) {
636 512
 	zzt_thread_cond = SDL_CreateCond();
637 513
 	audio_mutex = SDL_CreateMutex();
638 514
 
639
-	if (sdl_zzt_init(argc, argv) < 0) {
515
+	if (posix_zzt_init(argc, argv) < 0) {
640 516
 		fprintf(stderr, "Could not load ZZT!\n");
641 517
 		SDL_Quit();
642 518
 		return 1;

+ 3
- 2
zeta_curses.sh View File

@@ -1,3 +1,4 @@
1 1
 #!/bin/sh
2
-gcc -o build/zeta86 -g -O2 -std=c11 -Wall -lncurses \
3
-  src/frontend_curses.c src/posix_vfs.c src/zzt.c src/cpu.c
2
+xxd -i res/8x14.bin > res/8x14.c
3
+gcc -o build/zeta86 -g -O2 -std=c11 -Wall -lncursesw \
4
+  src/frontend_curses.c src/posix_vfs.c src/zzt.c src/cpu.c res/8x14.c

Loading…
Cancel
Save