commit 1cc1bbc

neauoire  ·  2023-08-16 19:50:46 +0000 UTC
parent c7c597a
Moved game loop into emu_run
2 files changed,  +47, -35
+1, -1
1@@ -3,7 +3,7 @@ CLI_src=src/uxn.c src/devices/system.c src/devices/console.c src/devices/file.c
2 EMU_src=${CLI_src} src/devices/screen.c src/devices/controller.c src/devices/mouse.c
3 
4 RELEASE_flags=-DNDEBUG -O2 -g0 -s
5-DEBUG_flags=-std=c89 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined
6+RELEASE_flags=-std=c89 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined
7 
8 .PHONY: all debug dest rom run test install uninstall format clean
9 
+46, -34
  1@@ -78,6 +78,9 @@ emu_deo(Uxn *u, Uint8 addr)
  2 int
  3 emu_resize(int width, int height)
  4 {
  5+	(void)width;
  6+	(void)height;
  7+	return 1;
  8 }
  9 
 10 static int
 11@@ -197,15 +200,53 @@ display_start(char *title)
 12 	return 1;
 13 }
 14 
 15-int
 16-main(int argc, char **argv)
 17+static int
 18+emu_run(Uxn *u, char *rom)
 19 {
 20-	Uxn u;
 21 	int i = 1, n;
 22 	char expirations[8];
 23 	char coninp[CONINBUFSIZE];
 24 	struct pollfd fds[3];
 25 	static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
 26+
 27+	/* timer */
 28+	fds[0].fd = XConnectionNumber(display);
 29+	fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0);
 30+	timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL);
 31+	fds[2].fd = STDIN_FILENO;
 32+	fds[0].events = fds[1].events = fds[2].events = POLLIN;
 33+	/* main loop */
 34+	while(!u->dev[0x0f]) {
 35+		if(poll(fds, 3, 1000) <= 0)
 36+			continue;
 37+		while(XPending(display))
 38+			emu_event(u);
 39+		if(poll(&fds[1], 1, 0)) {
 40+			read(fds[1].fd, expirations, 8);   /* Indicate we handled the timer */
 41+			uxn_eval(u, PEEK2(u->dev + 0x20)); /* Call the vector once, even if the timer fired multiple times */
 42+		}
 43+		if((fds[2].revents & POLLIN) != 0) {
 44+			n = read(fds[2].fd, coninp, CONINBUFSIZE - 1);
 45+			coninp[n] = 0;
 46+			for(i = 0; i < n; i++)
 47+				console_input(u, coninp[i], CONSOLE_STD);
 48+		}
 49+		if(uxn_screen.x2) {
 50+			int x1 = uxn_screen.x1, y1 = uxn_screen.y1, x2 = uxn_screen.x2, y2 = uxn_screen.y2;
 51+			screen_redraw();
 52+			if(u->dev[0x0e])
 53+				screen_debugger(u);
 54+			XPutImage(display, window, DefaultGC(display, 0), ximage, x1, y1, x1 + PAD, y1 + PAD, x2 - x1, y2 - y1);
 55+		}
 56+	}
 57+	return 1;
 58+}
 59+
 60+int
 61+main(int argc, char **argv)
 62+{
 63+	Uxn u;
 64+	int i = 1;
 65 	if(i == argc)
 66 		return system_error("usage", "uxn11 [-v] file.rom [args...]");
 67 	/* Connect Varvara */
 68@@ -219,7 +260,7 @@ main(int argc, char **argv)
 69 	system_connect(0xc, DATETIME_VERSION, DATETIME_DEIMASK, DATETIME_DEOMASK);
 70 	/* Read flags */
 71 	if(argv[i][0] == '-' && argv[i][1] == 'v')
 72-		return system_version("Uxn11 - Graphical Varvara Emulator", "10 Aug 2023");
 73+		return system_version("Uxn11 - Graphical Varvara Emulator", "16 Aug 2023");
 74 
 75 	rom_path = argv[1];
 76 	if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8))))
 77@@ -235,36 +276,7 @@ main(int argc, char **argv)
 78 		while(*p) console_input(&u, *p++, CONSOLE_ARG);
 79 		console_input(&u, '\n', i == argc ? CONSOLE_END : CONSOLE_EOA);
 80 	}
 81-	/* timer */
 82-	fds[0].fd = XConnectionNumber(display);
 83-	fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0);
 84-	timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL);
 85-	fds[2].fd = STDIN_FILENO;
 86-	fds[0].events = fds[1].events = fds[2].events = POLLIN;
 87-	/* main loop */
 88-	while(!u.dev[0x0f]) {
 89-		if(poll(fds, 3, 1000) <= 0)
 90-			continue;
 91-		while(XPending(display))
 92-			emu_event(&u);
 93-		if(poll(&fds[1], 1, 0)) {
 94-			read(fds[1].fd, expirations, 8);   /* Indicate we handled the timer */
 95-			uxn_eval(&u, PEEK2(&u.dev[0x20])); /* Call the vector once, even if the timer fired multiple times */
 96-		}
 97-		if((fds[2].revents & POLLIN) != 0) {
 98-			n = read(fds[2].fd, coninp, CONINBUFSIZE - 1);
 99-			coninp[n] = 0;
100-			for(i = 0; i < n; i++)
101-				console_input(&u, coninp[i], CONSOLE_STD);
102-		}
103-		if(uxn_screen.x2) {
104-			int x1 = uxn_screen.x1, y1 = uxn_screen.y1, x2 = uxn_screen.x2, y2 = uxn_screen.y2;
105-			screen_redraw();
106-			if(u.dev[0x0e])
107-				screen_debugger(&u);
108-			XPutImage(display, window, DefaultGC(display, 0), ximage, x1, y1, x1 + PAD, y1 + PAD, x2 - x1, y2 - y1);
109-		}
110-	}
111+	emu_run(&u, rom_path);
112 	XDestroyImage(ximage);
113 	return 0;
114 }