Browse code

Housekeeping

neauoire authored on 12/11/2023 04:32:52
Showing 6 changed files
... ...
@@ -334,3 +334,14 @@ audio_get_position(int instance)
334 334
 {
335 335
 	return channel[instance].sample.pos;
336 336
 }
337
+
338
+Uint8
339
+audio_dei(int instance, Uint8 *d, Uint8 port)
340
+{
341
+	switch(port) {
342
+	case 0x2: return audio_get_position(instance) >> 8;
343
+	case 0x3: return audio_get_position(instance);
344
+	case 0x4: return audio_get_vu(instance);
345
+	}
346
+	return d[port];
347
+}
... ...
@@ -25,3 +25,4 @@ int audio_render(int instance, Sint16 *sample, Sint16 *end);
25 25
 void audio_start(int instance, Uint8 *d, Uxn *u);
26 26
 void audio_finished_handler(int instance);
27 27
 void audio_handler(void *ctx, Uint8 *out_stream, int len);
28
+Uint8 audio_dei(int instance, Uint8 *d, Uint8 port);
... ...
@@ -39,7 +39,7 @@ screen_change(Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2)
39 39
 }
40 40
 
41 41
 void
42
-screen_fill(Uint8 *layer, int x1, int y1, int x2, int y2, int color)
42
+screen_rect(Uint8 *layer, int x1, int y1, int x2, int y2, int color)
43 43
 {
44 44
 	int x, y, width = uxn_screen.width, height = uxn_screen.height;
45 45
 	for(y = y1; y < y2 && y < height; y++)
... ...
@@ -153,8 +153,8 @@ screen_resize(Uint16 width, Uint16 height)
153 153
 	uxn_screen.pixels = pixels;
154 154
 	uxn_screen.width = width;
155 155
 	uxn_screen.height = height;
156
-	screen_fill(uxn_screen.bg, 0, 0, width, height, 0);
157
-	screen_fill(uxn_screen.fg, 0, 0, width, height, 0);
156
+	screen_rect(uxn_screen.bg, 0, 0, width, height, 0);
157
+	screen_rect(uxn_screen.fg, 0, 0, width, height, 0);
158 158
 	emu_resize(width, height);
159 159
 	screen_change(0, 0, width, height);
160 160
 }
... ...
@@ -219,7 +219,7 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
219 219
 			Uint16 y2 = uxn_screen.height;
220 220
 			if(ctrl & 0x10) x2 = x, x = 0;
221 221
 			if(ctrl & 0x20) y2 = y, y = 0;
222
-			screen_fill(layer, x, y, x2, y2, color);
222
+			screen_rect(layer, x, y, x2, y2, color);
223 223
 			screen_change(x, y, x2, y2);
224 224
 		}
225 225
 		/* pixel mode */
... ...
@@ -22,7 +22,7 @@ typedef struct UxnScreen {
22 22
 extern UxnScreen uxn_screen;
23 23
 extern int emu_resize(int width, int height);
24 24
 
25
-void screen_fill(Uint8 *layer, int x1, int y1, int x2, int y2, int color);
25
+void screen_rect(Uint8 *layer, int x1, int y1, int x2, int y2, int color);
26 26
 void screen_palette(Uint8 *addr);
27 27
 void screen_resize(Uint16 width, Uint16 height);
28 28
 void screen_change(Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2);
... ...
@@ -72,7 +72,7 @@ main(int argc, char **argv)
72 72
 		return system_error("usage", "uxncli [-v] file.rom [args..]");
73 73
 	/* Read flags */
74 74
 	if(argv[i][0] == '-' && argv[i][1] == 'v')
75
-		return system_version("Uxncli - Console Varvara Emulator", "9 Nov 2023");
75
+		return system_version("Uxncli - Console Varvara Emulator", "11 Nov 2023");
76 76
 	if(!system_init(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++]))
77 77
 		return system_error("Init", "Failed to initialize uxn.");
78 78
 	/* Game Loop */
... ...
@@ -55,8 +55,7 @@ static SDL_Thread *stdin_thread;
55 55
 
56 56
 /* devices */
57 57
 
58
-static int window_created = 0;
59
-static int fullscreen = 0, borderless = 0;
58
+static int window_created, fullscreen, borderless;
60 59
 static Uint32 stdin_event, audio0_event, zoom = 1;
61 60
 static Uint64 exec_deadline, deadline_interval, ms_interval;
62 61
 
... ...
@@ -67,25 +66,6 @@ clamp(int v, int min, int max)
67 66
 								   : v;
68 67
 }
69 68
 
70
-static Uint8
71
-audio_dei(int instance, Uint8 *d, Uint8 port)
72
-{
73
-	switch(port) {
74
-	case 0x2:
75
-		return audio_get_position(instance) >> 8;
76
-	case 0x3:
77
-		return audio_get_position(instance);
78
-	case 0x4:
79
-		return audio_get_vu(instance);
80
-	case 0x0:
81
-	case 0x8:
82
-	case 0xa:
83
-	case 0xc: return PEEK2(d + port);
84
-	default: return d[port];
85
-	}
86
-	return d[port];
87
-}
88
-
89 69
 static void
90 70
 audio_deo(int instance, Uint8 *d, Uint8 port, Uxn *u)
91 71
 {
... ...
@@ -122,8 +102,7 @@ emu_deo(Uxn *u, Uint8 addr, Uint8 value)
122 102
 	switch(d) {
123 103
 	case 0x00:
124 104
 		system_deo(u, &u->dev[d], p);
125
-		if(p > 0x7 && p < 0xe)
126
-			screen_palette(&u->dev[0x8]);
105
+		if(p > 0x7 && p < 0xe) screen_palette(&u->dev[0x8]);
127 106
 		break;
128 107
 	case 0x10: console_deo(&u->dev[d], p); break;
129 108
 	case 0x20: screen_deo(u->ram, &u->dev[d], p); break;
... ...
@@ -160,23 +139,20 @@ stdin_handler(void *p)
160 139
 static void
161 140
 set_window_size(SDL_Window *window, int w, int h)
162 141
 {
163
-	SDL_Point win, win_old;
164
-	SDL_GetWindowPosition(window, &win.x, &win.y);
142
+	SDL_Point win_old;
165 143
 	SDL_GetWindowSize(window, &win_old.x, &win_old.y);
166 144
 	if(w == win_old.x && h == win_old.y) return;
167 145
 	SDL_RenderClear(emu_renderer);
168
-	/* SDL_SetWindowPosition(window, (win.x + win_old.x / 2) - w / 2, (win.y + win_old.y / 2) - h / 2); */
169 146
 	SDL_SetWindowSize(window, w, h);
170 147
 }
171 148
 
172 149
 static void
173 150
 set_zoom(Uint8 z, int win)
174 151
 {
175
-	if(z >= 1) {
176
-		zoom = z;
177
-		if(win)
178
-			set_window_size(emu_window, (uxn_screen.width + PAD2) * zoom, (uxn_screen.height + PAD2) * zoom);
179
-	}
152
+	if(z < 1) return;
153
+	if(win)
154
+		set_window_size(emu_window, (uxn_screen.width + PAD2) * z, (uxn_screen.height + PAD2) * z);
155
+	zoom = z;
180 156
 }
181 157
 
182 158
 static void
... ...
@@ -184,9 +160,8 @@ set_fullscreen(int value, int win)
184 160
 {
185 161
 	Uint32 flags = 0; /* windowed mode; SDL2 has no constant for this */
186 162
 	fullscreen = value;
187
-	if(fullscreen) {
163
+	if(fullscreen)
188 164
 		flags = SDL_WINDOW_FULLSCREEN_DESKTOP;
189
-	}
190 165
 	if(win)
191 166
 		SDL_SetWindowFullscreen(emu_window, flags);
192 167
 }
... ...
@@ -203,7 +178,7 @@ static void
203 178
 set_debugger(Uxn *u, int value)
204 179
 {
205 180
 	u->dev[0x0e] = value;
206
-	screen_fill(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
181
+	screen_rect(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
207 182
 	screen_redraw(u);
208 183
 }
209 184
 
... ...
@@ -254,7 +229,6 @@ emu_init(Uxn *u)
254 229
 	as.userdata = u;
255 230
 	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0)
256 231
 		return system_error("sdl", SDL_GetError());
257
-
258 232
 	audio_id = SDL_OpenAudioDevice(NULL, 0, &as, NULL, 0);
259 233
 	if(!audio_id)
260 234
 		system_error("sdl_audio", SDL_GetError());
... ...
@@ -279,8 +253,8 @@ static void
279 253
 emu_restart(Uxn *u, char *rom, int soft)
280 254
 {
281 255
 	screen_resize(WIDTH, HEIGHT);
282
-	screen_fill(uxn_screen.bg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
283
-	screen_fill(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
256
+	screen_rect(uxn_screen.bg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
257
+	screen_rect(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
284 258
 	system_reboot(u, rom, soft);
285 259
 	SDL_SetWindowTitle(emu_window, boot_rom);
286 260
 }
... ...
@@ -428,37 +402,15 @@ handle_events(Uxn *u)
428 402
 		else if(event.type == SDL_JOYHATMOTION) {
429 403
 			/* NOTE: Assuming there is only one joyhat in the controller */
430 404
 			switch(event.jhat.value) {
431
-			case SDL_HAT_UP:
432
-				controller_down(u, &u->dev[0x80], 0x10);
433
-				break;
434
-			case SDL_HAT_DOWN:
435
-				controller_down(u, &u->dev[0x80], 0x20);
436
-				break;
437
-			case SDL_HAT_LEFT:
438
-				controller_down(u, &u->dev[0x80], 0x40);
439
-				break;
440
-			case SDL_HAT_RIGHT:
441
-				controller_down(u, &u->dev[0x80], 0x80);
442
-				break;
443
-			case SDL_HAT_LEFTDOWN:
444
-				controller_down(u, &u->dev[0x80], 0x40 | 0x20);
445
-				break;
446
-			case SDL_HAT_LEFTUP:
447
-				controller_down(u, &u->dev[0x80], 0x40 | 0x10);
448
-				break;
449
-			case SDL_HAT_RIGHTDOWN:
450
-				controller_down(u, &u->dev[0x80], 0x80 | 0x20);
451
-				break;
452
-			case SDL_HAT_RIGHTUP:
453
-				controller_down(u, &u->dev[0x80], 0x80 | 0x10);
454
-				break;
455
-			case SDL_HAT_CENTERED:
456
-				/* Set all directions to down */
457
-				controller_up(u, &u->dev[0x80], 0x10 | 0x20 | 0x40 | 0x80);
458
-				break;
459
-			default:
460
-				/* Ignore */
461
-				break;
405
+			case SDL_HAT_UP: controller_down(u, &u->dev[0x80], 0x10); break;
406
+			case SDL_HAT_DOWN: controller_down(u, &u->dev[0x80], 0x20); break;
407
+			case SDL_HAT_LEFT: controller_down(u, &u->dev[0x80], 0x40); break;
408
+			case SDL_HAT_RIGHT: controller_down(u, &u->dev[0x80], 0x80); break;
409
+			case SDL_HAT_LEFTDOWN: controller_down(u, &u->dev[0x80], 0x40 | 0x20); break;
410
+			case SDL_HAT_LEFTUP: controller_down(u, &u->dev[0x80], 0x40 | 0x10); break;
411
+			case SDL_HAT_RIGHTDOWN: controller_down(u, &u->dev[0x80], 0x80 | 0x20); break;
412
+			case SDL_HAT_RIGHTUP: controller_down(u, &u->dev[0x80], 0x80 | 0x10); break;
413
+			case SDL_HAT_CENTERED: controller_up(u, &u->dev[0x80], 0x10 | 0x20 | 0x40 | 0x80); break;
462 414
 			}
463 415
 		}
464 416
 		/* Console */
... ...
@@ -478,7 +430,12 @@ emu_run(Uxn *u, char *rom)
478 430
 	window_created = 1;
479 431
 	if(fullscreen)
480 432
 		window_flags = window_flags | SDL_WINDOW_FULLSCREEN_DESKTOP;
481
-	emu_window = SDL_CreateWindow(rom, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (uxn_screen.width + PAD2) * zoom, (uxn_screen.height + PAD2) * zoom, window_flags);
433
+	emu_window = SDL_CreateWindow(rom,
434
+		SDL_WINDOWPOS_UNDEFINED,
435
+		SDL_WINDOWPOS_UNDEFINED,
436
+		(uxn_screen.width + PAD2) * zoom,
437
+		(uxn_screen.height + PAD2) * zoom,
438
+		window_flags);
482 439
 	if(emu_window == NULL)
483 440
 		return system_error("sdl_window", SDL_GetError());
484 441
 	emu_renderer = SDL_CreateRenderer(emu_window, -1, SDL_RENDERER_ACCELERATED);
... ...
@@ -540,7 +497,7 @@ main(int argc, char **argv)
540 497
 	/* Read flag. Right now, there can be only one. */
541 498
 	if(argv[i][0] == '-') {
542 499
 		if(argv[i][1] == 'v')
543
-			return system_version("Uxnemu - Graphical Varvara Emulator", "9 Nov 2023");
500
+			return system_version("Uxnemu - Graphical Varvara Emulator", "11 Nov 2023");
544 501
 		if(argv[i][1] == '-')
545 502
 			i++;
546 503
 		if(strcmp(argv[i], "-2x") == 0 || strcmp(argv[i], "-3x") == 0)
... ...
@@ -553,12 +510,10 @@ main(int argc, char **argv)
553 510
 	/* Start system. */
554 511
 	Uint8 *ram = (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8));
555 512
 	char *rom = argv[i++];
556
-	if(!system_init(&u, ram, rom)) {
513
+	if(!system_init(&u, ram, rom))
557 514
 		return system_error("Init", "Failed to initialize uxn.");
558
-	}
559
-	if(!system_init(&u_audio, ram, rom)) {
515
+	if(!system_init(&u_audio, ram, rom))
560 516
 		return system_error("Init", "Failed to initialize uxn.");
561
-	}
562 517
 	if(!emu_init(&u_audio))
563 518
 		return system_error("Init", "Failed to initialize varvara.");
564 519
 	/* Game Loop */