commit a5d1172

neauoire  ·  2022-03-28 02:10:32 +0000 UTC
parent 6b2c815
Added timer
3 files changed,  +19, -4
+1, -1
1@@ -9,7 +9,7 @@ echo "Building.."
2 mkdir -p bin
3 
4 # Build(debug)
5-gcc -std=c89 -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 src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -o bin/uxn11 -lX11
6+gcc -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 src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -o bin/uxn11 -lX11
7 
8 # Build(release)
9 # gcc src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/file.c src/devices/datetime.c src/uxn11.c -o bin/uxn11 -lX11
+0, -0
+18, -3
 1@@ -2,6 +2,9 @@
 2 #include <stdlib.h>
 3 #include <X11/Xlib.h>
 4 #include <X11/Xutil.h>
 5+#include <sys/timerfd.h>
 6+#include <unistd.h>
 7+#include <poll.h>
 8 
 9 #include "uxn.h"
10 #include "devices/system.h"
11@@ -206,6 +209,9 @@ main(int argc, char **argv)
12 {
13 	Uxn u;
14 	int i;
15+	char expirations[8];
16+	struct pollfd fds[2];
17+	static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
18 	if(argc < 2)
19 		return error("Usage", "uxncli game.rom args");
20 	if(!start(&u, argv[1]))
21@@ -218,13 +224,22 @@ main(int argc, char **argv)
22 		while(*p) console_input(&u, *p++);
23 		console_input(&u, '\n');
24 	}
25+	fds[0].fd = XConnectionNumber(display);
26+	fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0);
27+	timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL);
28+	fds[0].events = fds[1].events = POLLIN;
29 	/* main loop */
30 	while(1) {
31-		processEvent();
32-		uxn_eval(&u, GETVECTOR(devscreen));
33+		if(poll(fds, 2, 1000) <= 0)
34+			continue;
35+		while(XPending(display))
36+			processEvent();
37+		if(poll(&fds[1], 1, 0)) {
38+			read(fds[1].fd, expirations, 8);    /* Indicate we handled the timer */
39+			uxn_eval(&u, GETVECTOR(devscreen)); /* Call the vector once, even if the timer fired multiple times */
40+		}
41 		if(uxn_screen.fg.changed || uxn_screen.bg.changed)
42 			redraw();
43-		/* sleep(0.01); */
44 	}
45 	XDestroyImage(ximage);
46 	return 0;