From 6797eeaf9e92830f11924afffaed6683f706f0f5 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Sun, 1 Oct 2017 16:38:33 -0400 Subject: [PATCH] Experiment with streamable texture --- src/tty.c | 17 +++++++++++++++++ src/tty.h | 1 + src/tty_test.c | 8 +------- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/tty.c b/src/tty.c index 22298aa..4eb3232 100644 --- a/src/tty.c +++ b/src/tty.c @@ -35,6 +35,8 @@ struct ewm_tty_t *ewm_tty_create(SDL_Renderer *renderer) { EWM_ONE_TTY_ROWS * ewm_chr_height(tty->chr), 32, 4 * EWM_ONE_TTY_COLUMNS * ewm_chr_width(tty->chr), ewm_sdl_pixel_format(renderer)); + tty->texture = SDL_CreateTexture(renderer, ewm_sdl_pixel_format(renderer), SDL_TEXTUREACCESS_STREAMING, 280, 192); + ewm_tty_reset(tty); return tty; } @@ -115,6 +117,21 @@ void ewm_tty_reset(struct ewm_tty_t *tty) { } void ewm_tty_refresh(struct ewm_tty_t *tty, uint32_t phase, uint32_t fps) { + + uint32_t *pixels; + int pitch; + if (SDL_LockTexture(tty->texture, NULL, (void**) &pixels, &pitch) != 0) { + printf("Cannot lock texture: %s\n", SDL_GetError()); + exit(1); + } + + uint32_t green = tty->chr->green; + for (int i = 0; i < 192 * 280; i++) { + *pixels++ = green; + } + + SDL_UnlockTexture(tty->texture); + for (int row = 0; row < 24; row++) { for (int column = 0; column < 40; column++) { ewm_tty_render_character(tty, row, column, tty->screen_buffer[(row * EWM_ONE_TTY_COLUMNS) + column]); diff --git a/src/tty.h b/src/tty.h index 41e70ff..519cace 100644 --- a/src/tty.h +++ b/src/tty.h @@ -46,6 +46,7 @@ struct ewm_tty_t { uint32_t *pixels; SDL_Surface *surface; + SDL_Texture *texture; }; struct ewm_tty_t *ewm_tty_create(SDL_Renderer *renderer); diff --git a/src/tty_test.c b/src/tty_test.c index 65fcd18..e958d33 100644 --- a/src/tty_test.c +++ b/src/tty_test.c @@ -18,13 +18,7 @@ void test(struct ewm_tty_t *tty) { SDL_RenderClear(tty->renderer); ewm_tty_refresh(tty, 1, EWM_ONE_FPS); - - SDL_Texture *texture = SDL_CreateTextureFromSurface(tty->renderer, tty->surface); - if (texture != NULL) { - SDL_RenderCopy(tty->renderer, texture, NULL, NULL); - SDL_DestroyTexture(texture); - } - + SDL_RenderCopy(tty->renderer, tty->texture, NULL, NULL); SDL_RenderPresent(tty->renderer); } Uint64 now = SDL_GetPerformanceCounter();