From 6b35522c877cbb27282df776db8f08532d0441f5 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 16 Feb 2024 23:59:08 +0100 Subject: Improve video drawing --- src/ahoy/config.h | 1 + src/ahoy/emulator.c | 2 -- src/ahoy/emulator.h | 1 - src/ahoy/gui.c | 37 ++++++++++++++++++++++--------------- src/ahoy/main.c | 11 +++++++---- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/ahoy/config.h b/src/ahoy/config.h index 6fa60c2..15ad895 100644 --- a/src/ahoy/config.h +++ b/src/ahoy/config.h @@ -4,6 +4,7 @@ #include struct config { + bool scanls; bool seeded; uint16_t seed; unsigned cpu_hz; diff --git a/src/ahoy/emulator.c b/src/ahoy/emulator.c index 79ff4dc..d240fbc 100644 --- a/src/ahoy/emulator.c +++ b/src/ahoy/emulator.c @@ -81,7 +81,6 @@ opexec(uint16_t op) switch (op) { case 0x00E0: memset(c8.screen, 0, sizeof(c8.screen)); - c8.needs_redraw = true; break; case 0x00EE: if (c8.SP == 0) @@ -228,7 +227,6 @@ opexec(uint16_t op) c8.screen[scr_row] ^= msk; } - c8.needs_redraw = true; break; } diff --git a/src/ahoy/emulator.h b/src/ahoy/emulator.h index 24aa8ce..69438db 100644 --- a/src/ahoy/emulator.h +++ b/src/ahoy/emulator.h @@ -15,7 +15,6 @@ 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 0997bf9..1933849 100644 --- a/src/ahoy/gui.c +++ b/src/ahoy/gui.c @@ -5,6 +5,7 @@ #include #include "cerr.h" +#include "config.h" #include "emulator.h" #include "gui.h" #include "macros.h" @@ -12,6 +13,7 @@ #define SCR_SCALE 20 #define SCR_WDTH 64 #define SCR_HIGH 32 +#define SCNL_HGHT 2 #define diesx(fmt) diex(fmt ": %s", SDL_GetError()) @@ -65,12 +67,9 @@ winfree(void) void windrw(void) { - SDL_Rect r = { - .x = 0, - .y = 0, - .w = SCR_SCALE, - .h = SCR_SCALE, - }; + int sw, sh; + div_t qw, qh; + SDL_Rect r; static const uint64_t cols[] = { 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, @@ -78,22 +77,19 @@ windrw(void) 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, }; - c8.needs_redraw = false; + SDL_GetWindowSize(win, &sw, &sh); + + r.w = (qw = div(sw, SCR_WDTH)).quot; + r.h = (qh = div(sh, SCR_HIGH)).quot; - for (size_t i = 0; i < lengthof(c8.screen); i++) { - if (c8.screen[i]) - goto noclr; - } SDL_SetRenderDrawColor(rndr, 0, 0, 0, UINT8_MAX); SDL_RenderClear(rndr); - return; -noclr: for (size_t i = 0; i < lengthof(c8.screen); i++) { for (size_t j = 64; j-- > 0;) { bool set = (UINT64_C(1) << j) & c8.screen[i]; - r.x = cols[j] * SCR_SCALE; - r.y = i * SCR_SCALE; + r.x = cols[j] * qw.quot + qw.rem / 2; + r.y = i * qh.quot + qh.rem / 2; if (set) SDL_SetRenderDrawColor(rndr, 0, UINT8_MAX, 0, UINT8_MAX); else @@ -102,6 +98,17 @@ noclr: } } + if (cfg.scanls) { + SDL_Rect r = { + .w = sw, + .h = SCNL_HGHT, + }; + + SDL_SetRenderDrawColor(rndr, 0, 0, 0, UINT8_MAX); + for (r.y = 0; r.y < sh; r.y += SCNL_HGHT * 2) + SDL_RenderFillRect(rndr, &r); + } + SDL_RenderPresent(rndr); } diff --git a/src/ahoy/main.c b/src/ahoy/main.c index b107fa2..467b1fa 100644 --- a/src/ahoy/main.c +++ b/src/ahoy/main.c @@ -59,7 +59,7 @@ void usage(void) { fprintf(stderr, - "Usage: %s [-c clock speed] [-s seed] [file]\n" + "Usage: %s [-S] [-c clock speed] [-s seed] [file]\n" " %s -h\n", argv0, argv0); exit(EXIT_FAILURE); @@ -89,13 +89,14 @@ main(int argc, char **argv) {"clock-speed", required_argument, nullptr, 'c'}, {"help", no_argument, nullptr, 'h'}, {"seed", required_argument, nullptr, 's'}, + {"scanlines", no_argument, nullptr, 'S'}, {nullptr, no_argument, nullptr, 0 }, }; argv0 = argv[0]; cerrinit(*argv); - while ((opt = getopt_long(argc, argv, "c:hs:", longopts, nullptr)) != -1) { + while ((opt = getopt_long(argc, argv, "c:hs:S", longopts, nullptr)) != -1) { switch (opt) { case 'h': execlp("man", "man", "1", argv[0], nullptr); @@ -114,6 +115,9 @@ main(int argc, char **argv) NUMERIC_ARG(uint16_t, UINT16_MAX, PRIu16, strtou16, seed, "seed"); cfg.seeded = true; break; + case 'S': + cfg.scanls = true; + break; default: usage(); } @@ -200,8 +204,7 @@ reset: dt = (double)((et - st) * 1000) / SDL_GetPerformanceFrequency(); SDL_Delay(16.67f > dt ? 16.67f - dt : 0); - if (c8.needs_redraw) - windrw(); + windrw(); if (c8.DT > 0) c8.DT--; -- cgit v1.2.3