Moved game loop into emu_run

This commit is contained in:
neauoire 2023-08-16 12:50:46 -07:00
parent c7c597a39f
commit 1cc1bbc594
2 changed files with 47 additions and 35 deletions

View File

@ -3,7 +3,7 @@ CLI_src=src/uxn.c src/devices/system.c src/devices/console.c src/devices/file.c
EMU_src=${CLI_src} src/devices/screen.c src/devices/controller.c src/devices/mouse.c EMU_src=${CLI_src} src/devices/screen.c src/devices/controller.c src/devices/mouse.c
RELEASE_flags=-DNDEBUG -O2 -g0 -s RELEASE_flags=-DNDEBUG -O2 -g0 -s
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 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
.PHONY: all debug dest rom run test install uninstall format clean .PHONY: all debug dest rom run test install uninstall format clean

View File

@ -78,6 +78,9 @@ emu_deo(Uxn *u, Uint8 addr)
int int
emu_resize(int width, int height) emu_resize(int width, int height)
{ {
(void)width;
(void)height;
return 1;
} }
static int static int
@ -197,15 +200,53 @@ display_start(char *title)
return 1; return 1;
} }
int static int
main(int argc, char **argv) emu_run(Uxn *u, char *rom)
{ {
Uxn u;
int i = 1, n; int i = 1, n;
char expirations[8]; char expirations[8];
char coninp[CONINBUFSIZE]; char coninp[CONINBUFSIZE];
struct pollfd fds[3]; struct pollfd fds[3];
static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}}; static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
/* timer */
fds[0].fd = XConnectionNumber(display);
fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0);
timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL);
fds[2].fd = STDIN_FILENO;
fds[0].events = fds[1].events = fds[2].events = POLLIN;
/* main loop */
while(!u->dev[0x0f]) {
if(poll(fds, 3, 1000) <= 0)
continue;
while(XPending(display))
emu_event(u);
if(poll(&fds[1], 1, 0)) {
read(fds[1].fd, expirations, 8); /* Indicate we handled the timer */
uxn_eval(u, PEEK2(u->dev + 0x20)); /* Call the vector once, even if the timer fired multiple times */
}
if((fds[2].revents & POLLIN) != 0) {
n = read(fds[2].fd, coninp, CONINBUFSIZE - 1);
coninp[n] = 0;
for(i = 0; i < n; i++)
console_input(u, coninp[i], CONSOLE_STD);
}
if(uxn_screen.x2) {
int x1 = uxn_screen.x1, y1 = uxn_screen.y1, x2 = uxn_screen.x2, y2 = uxn_screen.y2;
screen_redraw();
if(u->dev[0x0e])
screen_debugger(u);
XPutImage(display, window, DefaultGC(display, 0), ximage, x1, y1, x1 + PAD, y1 + PAD, x2 - x1, y2 - y1);
}
}
return 1;
}
int
main(int argc, char **argv)
{
Uxn u;
int i = 1;
if(i == argc) if(i == argc)
return system_error("usage", "uxn11 [-v] file.rom [args...]"); return system_error("usage", "uxn11 [-v] file.rom [args...]");
/* Connect Varvara */ /* Connect Varvara */
@ -219,7 +260,7 @@ main(int argc, char **argv)
system_connect(0xc, DATETIME_VERSION, DATETIME_DEIMASK, DATETIME_DEOMASK); system_connect(0xc, DATETIME_VERSION, DATETIME_DEIMASK, DATETIME_DEOMASK);
/* Read flags */ /* Read flags */
if(argv[i][0] == '-' && argv[i][1] == 'v') if(argv[i][0] == '-' && argv[i][1] == 'v')
return system_version("Uxn11 - Graphical Varvara Emulator", "10 Aug 2023"); return system_version("Uxn11 - Graphical Varvara Emulator", "16 Aug 2023");
rom_path = argv[1]; rom_path = argv[1];
if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)))) if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8))))
@ -235,36 +276,7 @@ main(int argc, char **argv)
while(*p) console_input(&u, *p++, CONSOLE_ARG); while(*p) console_input(&u, *p++, CONSOLE_ARG);
console_input(&u, '\n', i == argc ? CONSOLE_END : CONSOLE_EOA); console_input(&u, '\n', i == argc ? CONSOLE_END : CONSOLE_EOA);
} }
/* timer */ emu_run(&u, rom_path);
fds[0].fd = XConnectionNumber(display);
fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0);
timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL);
fds[2].fd = STDIN_FILENO;
fds[0].events = fds[1].events = fds[2].events = POLLIN;
/* main loop */
while(!u.dev[0x0f]) {
if(poll(fds, 3, 1000) <= 0)
continue;
while(XPending(display))
emu_event(&u);
if(poll(&fds[1], 1, 0)) {
read(fds[1].fd, expirations, 8); /* Indicate we handled the timer */
uxn_eval(&u, PEEK2(&u.dev[0x20])); /* Call the vector once, even if the timer fired multiple times */
}
if((fds[2].revents & POLLIN) != 0) {
n = read(fds[2].fd, coninp, CONINBUFSIZE - 1);
coninp[n] = 0;
for(i = 0; i < n; i++)
console_input(&u, coninp[i], CONSOLE_STD);
}
if(uxn_screen.x2) {
int x1 = uxn_screen.x1, y1 = uxn_screen.y1, x2 = uxn_screen.x2, y2 = uxn_screen.y2;
screen_redraw();
if(u.dev[0x0e])
screen_debugger(&u);
XPutImage(display, window, DefaultGC(display, 0), ximage, x1, y1, x1 + PAD, y1 + PAD, x2 - x1, y2 - y1);
}
}
XDestroyImage(ximage); XDestroyImage(ximage);
return 0; return 0;
} }