From 18d4d36065048a1c61b4b8a5cfc3a7a64737d832 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 17 Feb 2024 00:31:48 +0100 Subject: Optimize redrawing --- src/ahoy/emulator.c | 3 ++- src/ahoy/emulator.h | 1 + src/ahoy/gui.c | 9 ++++++++- src/ahoy/gui.h | 2 +- src/ahoy/main.c | 5 +++-- 5 files changed, 15 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ahoy/emulator.c b/src/ahoy/emulator.c index d240fbc..980a0a7 100644 --- a/src/ahoy/emulator.c +++ b/src/ahoy/emulator.c @@ -80,6 +80,7 @@ opexec(uint16_t op) case 0x0: switch (op) { case 0x00E0: + c8.needs_redraw = true; memset(c8.screen, 0, sizeof(c8.screen)); break; case 0x00EE: @@ -211,9 +212,9 @@ opexec(uint16_t op) unsigned y = (op & 0x00F0) >> 4; unsigned n = (op & 0x000F) >> 0; + c8.needs_redraw = true; if (c8.I + n > lengthof(mem)) diex("%s: attempted to draw sprite beyond bounds of RAM", filename); - for (unsigned i = 0; i < n; i++) { uint8_t spr_row = mem[c8.I + i]; uint8_t scr_row = c8.V[y] + i; diff --git a/src/ahoy/emulator.h b/src/ahoy/emulator.h index 69438db..24aa8ce 100644 --- a/src/ahoy/emulator.h +++ b/src/ahoy/emulator.h @@ -15,6 +15,7 @@ I — register to hold addresses */ struct chip8 { + bool needs_redraw; bool kbd[16]; uint8_t V[16]; uint8_t DT, ST, SP; diff --git a/src/ahoy/gui.c b/src/ahoy/gui.c index 1933849..d7514e9 100644 --- a/src/ahoy/gui.c +++ b/src/ahoy/gui.c @@ -82,6 +82,7 @@ windrw(void) r.w = (qw = div(sw, SCR_WDTH)).quot; r.h = (qh = div(sh, SCR_HIGH)).quot; + c8.needs_redraw = false; SDL_SetRenderDrawColor(rndr, 0, 0, 0, UINT8_MAX); SDL_RenderClear(rndr); @@ -119,7 +120,7 @@ auplay(bool stop) } void -readkb(void) +readevnt(void) { SDL_Event e; @@ -129,6 +130,12 @@ readkb(void) estate = ES_STOP; break; + case SDL_WINDOWEVENT: + /* For some reason checking for SDL_WINDOWEVENT_RESIZE and friends + doesn’t work, so just catch all window events */ + c8.needs_redraw = true; + break; + case SDL_KEYDOWN: switch (e.key.keysym.sym) { case SDLK_SPACE: diff --git a/src/ahoy/gui.h b/src/ahoy/gui.h index 0760437..220bcb1 100644 --- a/src/ahoy/gui.h +++ b/src/ahoy/gui.h @@ -15,7 +15,7 @@ void wininit(void); void winfree(void); void windrw(void); void auplay(bool); -void readkb(void); +void readevnt(void); extern emustate estate; diff --git a/src/ahoy/main.c b/src/ahoy/main.c index 467b1fa..e47598c 100644 --- a/src/ahoy/main.c +++ b/src/ahoy/main.c @@ -189,7 +189,7 @@ reset: double dt; uint64_t st, et; - readkb(); + readevnt(); if (estate == ES_PAUSED) continue; if (estate == ES_RESET) { @@ -204,7 +204,8 @@ reset: dt = (double)((et - st) * 1000) / SDL_GetPerformanceFrequency(); SDL_Delay(16.67f > dt ? 16.67f - dt : 0); - windrw(); + if (c8.needs_redraw) + windrw(); if (c8.DT > 0) c8.DT--; -- cgit v1.2.3