From 21d4da585174e2785f865495427e1be33e968ee7 Mon Sep 17 00:00:00 2001 From: sfja Date: Wed, 11 Nov 2026 14:25:14 +0100 Subject: [PATCH] improve sdl rendering --- src/emu.c | 77 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/src/emu.c b/src/emu.c index 4b7c6ac..a176a52 100644 --- a/src/emu.c +++ b/src/emu.c @@ -316,30 +316,62 @@ typedef struct { static uint64_t font_char(uint8_t ch); -static void vcd_render_char(Vcd *vcd, uint16_t ch, int x, int y) +static void vcd_render_char( + Vcd *vcd, uint16_t ch, int x, int y, SDL_Color *tex_buffer, int pitch) { - SDL_Color *buffer; - int pitch; - bool sdl_ok - = SDL_LockTexture(vcd->ch_tex, nullptr, (void **)&buffer, &pitch); - assert(sdl_ok); + (void)vcd; constexpr SDL_Color fg = { 0xff, 0xff, 0xff, 0xff }; constexpr SDL_Color bg = { 0x00, 0x00, 0x00, 0xff }; uint64_t font_ch = font_char((uint8_t)ch & 0xff); + size_t row_size = pitch / 4; + for (int ch_y = 0; ch_y < 8; ++ch_y) { uint8_t row = font_ch >> (7 - ch_y) * 8 & 0xff; for (int ch_x = 0; ch_x < 8; ++ch_x) { bool ch = row >> (7 - ch_x) & 1; - buffer[(y * 8 + ch_y) * 40 * 8 + x * 8 + ch_x] = ch ? fg : bg; + tex_buffer[(y * 8 + ch_y) * row_size + x * 8 + ch_x] = ch ? fg : bg; } } +} - SDL_UnlockTexture(vcd->ch_tex); +static void vcd_render(Vcd *vcd, VM *vm) +{ + if (vm->vcd_updated) { + vm->vcd_updated = false; + + size_t buffer_size = 40 * 25; + uint16_t *buffer = malloc(sizeof(uint16_t) * buffer_size); + + vm_copy_vcd_area(vm, buffer, buffer_size); + + SDL_Color *tex_buffer; + int pitch; + bool sdl_ok = SDL_LockTexture( + vcd->ch_tex, nullptr, (void **)&tex_buffer, &pitch); + assert(sdl_ok); + + for (int y = 0; y < 25; ++y) { + for (int x = 0; x < 40; ++x) { + + uint16_t ch = buffer[y * 40 + x]; + uint16_t *alt_ch = &vcd->alt_char_buffer[y * 40 + x]; + + if (*alt_ch == ch) + continue; + + *alt_ch = ch; + vcd_render_char(vcd, ch, x, y, tex_buffer, pitch); + } + } + + SDL_UnlockTexture(vcd->ch_tex); + free(buffer); + } SDL_FRect dest = { .x = 0, @@ -351,34 +383,6 @@ static void vcd_render_char(Vcd *vcd, uint16_t ch, int x, int y) SDL_RenderTexture(vcd->renderer, vcd->ch_tex, nullptr, &dest); } -static void vcd_render(Vcd *vcd, VM *vm) -{ - if (!vm->vcd_updated) // Not protected by mutex. - return; - vm->vcd_updated = false; - - size_t buffer_size = 40 * 25; - uint16_t *buffer = malloc(sizeof(uint16_t) * buffer_size); - - vm_copy_vcd_area(vm, buffer, buffer_size); - - for (int y = 0; y < 25; ++y) { - for (int x = 0; x < 40; ++x) { - - uint16_t ch = buffer[y * 40 + x]; - uint16_t *alt_ch = &vcd->alt_char_buffer[y * 40 + x]; - - if (*alt_ch == ch) - continue; - - *alt_ch = ch; - vcd_render_char(vcd, ch, x, y); - } - } - - free(buffer); -} - static int vm_thread_start(void *opt) { VM *vm = opt; @@ -494,6 +498,7 @@ int main(int argc, char *argv[]) } } + SDL_RenderClear(renderer); vcd_render(&vcd, &vm); SDL_RenderPresent(renderer);