Browse Source

[sdl] fix Windows 10 HiDPI, seed timer properly for RNG

master
asie 1 month ago
parent
commit
14e9ecc11e
11 changed files with 82 additions and 36 deletions
  1. 8
    0
      mingw/app.manifest
  2. 3
    0
      mingw/resources.rc
  3. 1
    0
      src/config.h
  4. 4
    4
      src/frontend_curses.c
  5. 1
    0
      src/frontend_posix.c
  6. 32
    16
      src/frontend_sdl.c
  7. 3
    4
      src/render_software.c
  8. 17
    5
      src/zzt.c
  9. 7
    3
      src/zzt.h
  10. 2
    2
      zeta_sdl.sh
  11. 4
    2
      zeta_sdl_mingw.sh

+ 8
- 0
mingw/app.manifest View File

@@ -0,0 +1,8 @@
1
+<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
2
+  <asmv3:application>
3
+    <asmv3:windowsSettings>
4
+      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
5
+      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">permonitorv2,permonitor</dpiAwareness>
6
+    </asmv3:windowsSettings>
7
+  </asmv3:application>
8
+</assembly>

+ 3
- 0
mingw/resources.rc View File

@@ -0,0 +1,3 @@
1
+#include "winuser.h"
2
+
3
+CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "app.manifest"

+ 1
- 0
src/config.h View File

@@ -27,6 +27,7 @@
27 27
 
28 28
 // SDL port configuration
29 29
 
30
+#define BLINK_TOGGLE_DURATION_MS 233
30 31
 #define ENABLE_SCREENSHOTS
31 32
 #define USE_GETOPT
32 33
 #define USE_OPENGL

+ 4
- 4
src/frontend_curses.c View File

@@ -34,7 +34,7 @@
34 34
 
35 35
 static int video_blink;
36 36
 
37
-long zeta_time_ms() {
37
+long zeta_time_ms(void) {
38 38
 	struct timespec spec;
39 39
 
40 40
 	clock_gettime(CLOCK_REALTIME, &spec);
@@ -46,7 +46,7 @@ void cpu_ext_log(const char* s) {
46 46
 }
47 47
 
48 48
 void speaker_on(double freq) {}
49
-void speaker_off() {}
49
+void speaker_off(void) {}
50 50
 
51 51
 static WINDOW* window;
52 52
 
@@ -62,7 +62,7 @@ void zeta_update_charset(int width, int height, u8* data) {
62 62
 void zeta_update_palette(u32* data) {
63 63
 }
64 64
 
65
-static void platform_kbd_tick() {
65
+static void platform_kbd_tick(void) {
66 66
 	int c = getch();
67 67
 	if (c == ERR) return;
68 68
 	if (c == 263) c = 8;
@@ -80,7 +80,7 @@ static int nc_color_map[] = {
80 80
 	COLOR_RED, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE
81 81
 };
82 82
 
83
-static void init_ncurses_colors() {
83
+static void init_ncurses_colors(void) {
84 84
 	for (int i = 0; i < 64; i++) {
85 85
 		init_pair(i+1, nc_color_map[i&7], nc_color_map[i>>3]);
86 86
 	}

+ 1
- 0
src/frontend_posix.c View File

@@ -70,6 +70,7 @@ static int posix_zzt_init(int argc, char **argv) {
70 70
 #endif
71 71
 
72 72
 	zzt_init();
73
+	zzt_set_timer_ticks(time(NULL));
73 74
 
74 75
 #ifdef USE_GETOPT
75 76
 	if (argc > optind && posix_vfs_exists(argv[optind])) {

+ 32
- 16
src/frontend_sdl.c View File

@@ -29,7 +29,9 @@
29 29
 #include <stdio.h>
30 30
 #include <stdlib.h>
31 31
 #include <string.h>
32
+#include <time.h>
32 33
 #include <SDL2/SDL.h>
34
+#include <SDL2/SDL_main.h>
33 35
 #ifdef USE_OPENGL
34 36
 #ifdef USE_OPENGL_ES
35 37
 #include <SDL2/SDL_opengles.h>
@@ -63,6 +65,7 @@ static const u8 sdl_to_pc_scancode[] = {
63 65
 
64 66
 static const int sdl_to_pc_scancode_max = sizeof(sdl_to_pc_scancode) - 1;
65 67
 
68
+#ifdef USE_OPENGL
66 69
 static SDL_Texture *create_texture_from_array(SDL_Renderer *renderer, int access, unsigned char *data, int height) {
67 70
 	SDL_Texture *texture;
68 71
 	SDL_Rect rect;
@@ -92,8 +95,9 @@ static SDL_Texture *create_texture_from_array(SDL_Renderer *renderer, int access
92 95
 
93 96
 	return texture;
94 97
 }
98
+#endif
95 99
 
96
-long zeta_time_ms() {
100
+long zeta_time_ms(void) {
97 101
 	return SDL_GetTicks();
98 102
 }
99 103
 
@@ -121,7 +125,7 @@ void speaker_on(double freq) {
121 125
 	SDL_UnlockMutex(audio_mutex);
122 126
 }
123 127
 
124
-void speaker_off() {
128
+void speaker_off(void) {
125 129
 	SDL_LockMutex(audio_mutex);
126 130
 	audio_stream_append_off(zeta_time_ms());
127 131
 	SDL_UnlockMutex(audio_mutex);
@@ -129,7 +133,9 @@ void speaker_off() {
129 133
 
130 134
 static SDL_mutex *zzt_thread_lock;
131 135
 static SDL_cond *zzt_thread_cond;
136
+#ifdef USE_OPENGL
132 137
 static u8 zzt_vram_copy[80*25*2];
138
+#endif
133 139
 static u8 zzt_thread_running;
134 140
 static atomic_int zzt_renderer_waiting = 0;
135 141
 static u8 video_blink = 1;
@@ -161,12 +167,16 @@ static Uint32 sdl_timer_thread(Uint32 interval, void *param) {
161 167
 	return tick_time;
162 168
 }
163 169
 
164
-static void sdl_timer_init() {
170
+static void sdl_timer_init(void) {
165 171
 	first_timer_tick = zeta_time_ms();
166 172
 	timer_time = 0;
167 173
 	SDL_AddTimer((int) SYS_TIMER_TIME, sdl_timer_thread, (void*)NULL);
168 174
 }
169 175
 
176
+static int sdl_is_blink_phase(long curr_time) {
177
+	return ((curr_time % (BLINK_TOGGLE_DURATION_MS*2)) >= BLINK_TOGGLE_DURATION_MS);
178
+}
179
+
170 180
 // try to keep a budget of ~5ms per call
171 181
 
172 182
 static int zzt_thread_func(void *ptr) {
@@ -284,7 +294,7 @@ static void calc_render_area(SDL_Rect *rect, int w, int h, int *scale_out, int f
284 294
 }
285 295
 
286 296
 #ifdef USE_OPENGL
287
-/* static void oglguard() {
297
+/* static void oglguard(void) {
288 298
 	GLenum err;
289 299
 
290 300
 	while ((err = glGetError()) != GL_NO_ERROR) {
@@ -292,7 +302,7 @@ static void calc_render_area(SDL_Rect *rect, int w, int h, int *scale_out, int f
292 302
 	}
293 303
 } */
294 304
 
295
-static void prepare_render_opengl() {
305
+static void prepare_render_opengl(void) {
296 306
 	SDL_Rect rect;
297 307
 	int w, h, scale;
298 308
 	SDL_GL_GetDrawableSize(window, &w, &h);
@@ -340,7 +350,7 @@ static void update_opengl_colcache(u32* pal) {
340 350
 	}
341 351
 }
342 352
 
343
-static void init_opengl() {
353
+static void init_opengl(void) {
344 354
 	ogl_buf_pos = malloc((80 * 25) * GL_COMPONENT_POINTS * 2 * sizeof(short));
345 355
 	ogl_buf_pos40 = malloc((40 * 25) * GL_COMPONENT_POINTS * 2 * sizeof(short));
346 356
 	ogl_buf_col = malloc(2 * (80 * 25) * GL_COMPONENT_POINTS * 4 * sizeof(char));
@@ -441,7 +451,7 @@ static void init_opengl() {
441 451
 #endif
442 452
 }
443 453
 
444
-static void deinit_opengl() {
454
+static void deinit_opengl(void) {
445 455
 	free(ogl_buf_texcache);
446 456
 	free(ogl_buf_colcache);
447 457
 	free(ogl_buf_tex);
@@ -451,7 +461,7 @@ static void deinit_opengl() {
451 461
 }
452 462
 
453 463
 static void render_opengl(long curr_time, int regen_visuals) {
454
-	u8 blink_local = video_blink && ((curr_time % 466) >= 233);
464
+	u8 blink_local = video_blink && sdl_is_blink_phase(curr_time);
455 465
 	float texw, texh;
456 466
 	int width = (zzt_video_mode() & 2) ? 80 : 40;
457 467
 
@@ -542,9 +552,9 @@ static void render_software_copy(long curr_time) {
542 552
 	}
543 553
 
544 554
 	if (!video_blink) sflags |= RENDER_BLINK_OFF;
545
-	else if ((zeta_time_ms() % 466) >= 233) sflags |= RENDER_BLINK_PHASE;
555
+	else if (sdl_is_blink_phase(zeta_time_ms())) sflags |= RENDER_BLINK_PHASE;
546 556
 
547
-	SDL_GetWindowSize(window, &w, &h);
557
+	SDL_GetRendererOutputSize(renderer, &w, &h);
548 558
 	calc_render_area(&dest, w, h, NULL, 0);
549 559
 
550 560
 	SDL_LockTexture(playfieldtex, NULL, &buffer, &pitch);
@@ -661,7 +671,7 @@ int main(int argc, char **argv) {
661 671
 		fprintf(stderr, "Could not initialize OpenGL (%s), using software renderer...", SDL_GetError());
662 672
 #endif
663 673
 		window = SDL_CreateWindow("Zeta", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
664
-			80*charw, 25*charh, 0);
674
+			80*charw, 25*charh, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
665 675
 		SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
666 676
 	} else {
667 677
 		SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
@@ -699,7 +709,9 @@ int main(int argc, char **argv) {
699 709
 
700 710
 	sdl_timer_init();
701 711
 
702
-	int should_render;
712
+#ifdef USE_OPENGL
713
+	int should_render = 1;
714
+#endif
703 715
 
704 716
 	while (cont_loop) {
705 717
 		if (!zzt_thread_running) { cont_loop = 0; break; }
@@ -707,9 +719,13 @@ int main(int argc, char **argv) {
707 719
 		atomic_fetch_add(&zzt_renderer_waiting, 1);
708 720
 		SDL_LockMutex(zzt_thread_lock);
709 721
 		atomic_fetch_sub(&zzt_renderer_waiting, 1);
710
-		u8* ram = zzt_get_ram();
711
-		should_render = memcmp(ram + 0xB8000, zzt_vram_copy, 80*25*2);
712
-		if (should_render) memcpy(zzt_vram_copy, ram + 0xB8000, 80*25*2);
722
+#ifdef USE_OPENGL
723
+		if (use_opengl) {
724
+			u8* ram = zzt_get_ram();
725
+			should_render = memcmp(ram + 0xB8000, zzt_vram_copy, 80*25*2);
726
+			if (should_render) memcpy(zzt_vram_copy, ram + 0xB8000, 80*25*2);
727
+		}
728
+#endif
713 729
 		zzt_mark_frame();
714 730
 
715 731
 		// do KEYUPs before KEYDOWNS - fixes key loss issues w/ Windows
@@ -740,7 +756,7 @@ int main(int argc, char **argv) {
740 756
 						}
741 757
 
742 758
 						if (!video_blink) sflags |= RENDER_BLINK_OFF;
743
-						else if ((zeta_time_ms() % 466) >= 233) sflags |= RENDER_BLINK_PHASE;
759
+						else if (sdl_is_blink_phase(zeta_time_ms())) sflags |= RENDER_BLINK_PHASE;
744 760
 
745 761
 						while ((++i) <= 9999) {
746 762
 							snprintf(filename, 23, "screen%d.bmp", i);

+ 3
- 4
src/render_software.c View File

@@ -51,10 +51,9 @@ void render_software_rgb(u32 *buffer, int scr_width, int row_length, int flags,
51 51
 				int line = *co;
52 52
 				for (int cx = 0; cx < char_width; cx++, line <<= 1) {
53 53
 					int bpos = ((y * char_height + cy) * row_length) + ((x * char_width + cx) * pos_mul);
54
-					buffer[bpos] = palette[(line & 0x80) ? fg : bg];
55
-					if (pos_mul == 2) {
56
-						buffer[bpos+1] = palette[(line & 0x80) ? fg : bg];
57
-					}
54
+					int col = palette[(line & 0x80) ? fg : bg];
55
+					buffer[bpos] = col;
56
+					if (pos_mul == 2) buffer[bpos+1] = col;
58 57
 				}
59 58
 			}
60 59
 		}

+ 17
- 5
src/zzt.c View File

@@ -110,7 +110,7 @@ void zzt_kmod_clear(int mod) {
110 110
 	zzt.kmod &= ~mod;
111 111
 }
112 112
 
113
-static long zzt_internal_time() {
113
+static long zzt_internal_time(void) {
114 114
 	return zzt.timer_time;
115 115
 //	return (long) (zzt.timer_time + (zeta_time_ms() - zzt.real_time));
116 116
 }
@@ -130,11 +130,11 @@ static int zzt_key_append(int qch, int qke) {
130 130
 	return 0;
131 131
 }
132 132
 
133
-int zzt_key_get_delay() {
133
+int zzt_key_get_delay(void) {
134 134
 	return zzt.key_delay;
135 135
 }
136 136
 
137
-int zzt_key_get_repeat_delay() {
137
+int zzt_key_get_repeat_delay(void) {
138 138
 	return zzt.key_repeat_delay;
139 139
 }
140 140
 
@@ -358,6 +358,18 @@ static void cpu_func_intr_0x33(cpu_state* cpu) {
358 358
 	}
359 359
 }
360 360
 
361
+u32 zzt_get_timer_ticks(void) {
362
+	return zzt.cpu.ram[0x46c] | (zzt.cpu.ram[0x46d] << 8)
363
+	    | (zzt.cpu.ram[0x46e] << 16) | (zzt.cpu.ram[0x46f] << 24);
364
+}
365
+
366
+void zzt_set_timer_ticks(u32 time) {
367
+	zzt.cpu.ram[0x46c] = time & 0xFF;
368
+	zzt.cpu.ram[0x46d] = (time>>8) & 0xFF;
369
+	zzt.cpu.ram[0x46e] = (time>>16) & 0xFF;
370
+	zzt.cpu.ram[0x46f] = (time>>24) & 0xFF;
371
+}
372
+
361 373
 static int cpu_func_interrupt_main(cpu_state* cpu, u8 intr) {
362 374
 #ifdef DEBUG_INTERRUPTS
363 375
 	fprintf(stderr, "dbg: interrupt %02X %04X\n", intr, cpu->ax);
@@ -946,7 +958,7 @@ void zzt_load_binary(int handle, const char *arg) {
946 958
 	fprintf(stderr, "wrote %d bytes to %d\n", bytes_read, (offset_pars * 16 + 256));
947 959
 }
948 960
 
949
-void zzt_init() {
961
+void zzt_init(void) {
950 962
 /*	for (int i = 0; i < MAX_ALLOC; i++)
951 963
 		seg_allocs[i] = (i < 256) ? (256-i) : 0; */
952 964
 
@@ -1006,7 +1018,7 @@ int zzt_execute(int opcodes) {
1006 1018
 	return cpu_execute(&(zzt.cpu), opcodes);
1007 1019
 }
1008 1020
 
1009
-static void zzt_update_keys() {
1021
+static void zzt_update_keys(void) {
1010 1022
 	long ctime = zeta_time_ms();
1011 1023
 	zzt_key_entry* key = &(zzt.key);
1012 1024
 

+ 7
- 3
src/zzt.h View File

@@ -49,7 +49,7 @@ void zzt_mouse_axis(int axis, int value /* delta, in pixels */);
49 49
 USER_FUNCTION
50 50
 void zzt_mouse_clear(int button);
51 51
 USER_FUNCTION
52
-void zzt_init();
52
+void zzt_init(void);
53 53
 USER_FUNCTION
54 54
 void zzt_load_binary(int handle, const char *arg);
55 55
 USER_FUNCTION
@@ -61,11 +61,15 @@ void zzt_mark_frame(void);
61 61
 USER_FUNCTION
62 62
 void zzt_mark_timer(void);
63 63
 USER_FUNCTION
64
-int zzt_key_get_delay();
64
+int zzt_key_get_delay(void);
65 65
 USER_FUNCTION
66
-int zzt_key_get_repeat_delay();
66
+int zzt_key_get_repeat_delay(void);
67 67
 USER_FUNCTION
68 68
 void zzt_key_set_delay(int ms, int repeat_ms);
69
+USER_FUNCTION
70
+u32 zzt_get_timer_ticks(void);
71
+USER_FUNCTION
72
+void zzt_set_timer_ticks(u32 ticks);
69 73
 
70 74
 IMPLEMENT_FUNCTION
71 75
 long zeta_time_ms(void);

+ 2
- 2
zeta_sdl.sh View File

@@ -1,7 +1,7 @@
1 1
 #!/bin/sh
2 2
 xxd -i res/8x14.bin > res/8x14.c
3
-gcc -o build/zeta86 -g -O2 -std=c11 -Wall \
3
+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
-  -lGL -lSDL2
7
+  -lGL -lSDL2 -lSDL2main

+ 4
- 2
zeta_sdl_mingw.sh View File

@@ -1,6 +1,8 @@
1 1
 #!/bin/sh
2 2
 xxd -i res/8x14.bin > res/8x14.c
3
-i686-w64-mingw32-gcc -o build/zeta86.exe -g -O2 -std=c11 -Wall \
3
+if [ -f build/mingw_resources.o ]; then rm build/mingw_resources.o; fi
4
+i686-w64-mingw32-windres mingw/resources.rc build/mingw_resources.o
5
+i686-w64-mingw32-gcc -o build/zeta86.exe -g -O2 -std=c18 -Wall -mwindows \
4 6
   res/8x14.c src/posix_vfs.c src/audio_stream.c src/frontend_sdl.c src/zzt.c src/cpu.c \
5
-  src/screenshot_writer.c src/render_software.c \
7
+  src/screenshot_writer.c src/render_software.c build/mingw_resources.o \
6 8
   -lmingw32 -lSDL2main -lSDL2 -lopengl32

Loading…
Cancel
Save