improve sdl rendering

This commit is contained in:
sfja 2026-11-11 14:25:14 +01:00
parent a4a0332846
commit 21d4da5851

View File

@ -316,45 +316,32 @@ 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);
SDL_FRect dest = {
.x = 0,
.y = 0,
.w = 40 * 8 * 4,
.h = 25 * 8 * 4,
};
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;
if (vm->vcd_updated) {
vm->vcd_updated = false;
size_t buffer_size = 40 * 25;
@ -362,6 +349,12 @@ static void vcd_render(Vcd *vcd, VM *vm)
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) {
@ -372,13 +365,24 @@ static void vcd_render(Vcd *vcd, VM *vm)
continue;
*alt_ch = ch;
vcd_render_char(vcd, ch, x, y);
vcd_render_char(vcd, ch, x, y, tex_buffer, pitch);
}
}
SDL_UnlockTexture(vcd->ch_tex);
free(buffer);
}
SDL_FRect dest = {
.x = 0,
.y = 0,
.w = 40 * 8 * 4,
.h = 25 * 8 * 4,
};
SDL_RenderTexture(vcd->renderer, vcd->ch_tex, nullptr, &dest);
}
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);