Skip to content

Commit

Permalink
sdl: use the same textures for hires/lores
Browse files Browse the repository at this point in the history
Simpilfies Renderer a bit and only requires minor changes to System::update_texture
  • Loading branch information
Daft-Freak committed Nov 19, 2023
1 parent e567e97 commit eed6738
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
25 changes: 6 additions & 19 deletions 32blit-sdl/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Renderer::Renderer(SDL_Window *window, int width, int height) : sys_width(width)
std::cerr << "could not create renderer: " << SDL_GetError() << std::endl;
}

current = fb_hires_texture;
current = fb_texture;

int w, h;
SDL_GetWindowSize(window, &w, &h);
Expand All @@ -28,17 +28,13 @@ Renderer::Renderer(SDL_Window *window, int width, int height) : sys_width(width)
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);

fb_lores_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, sys_width/2, sys_height/2);
fb_hires_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, sys_width, sys_height);
fb_lores_565_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGR565, SDL_TEXTUREACCESS_STREAMING, sys_width/2, sys_height/2);
fb_hires_565_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGR565, SDL_TEXTUREACCESS_STREAMING, sys_width, sys_height);
fb_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, sys_width, sys_height);
fb_565_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGR565, SDL_TEXTUREACCESS_STREAMING, sys_width, sys_height);
}

Renderer::~Renderer() {
SDL_DestroyTexture(fb_lores_texture);
SDL_DestroyTexture(fb_hires_texture);
SDL_DestroyTexture(fb_lores_565_texture);
SDL_DestroyTexture(fb_hires_565_texture);
SDL_DestroyTexture(fb_texture);
SDL_DestroyTexture(fb_565_texture);
SDL_DestroyRenderer(renderer);
}

Expand Down Expand Up @@ -70,11 +66,7 @@ void Renderer::resize(int width, int height) {
void Renderer::update(System *sys) {
auto format = blit::PixelFormat(sys->format());

if (sys->mode() == 0) {
current = format == blit::PixelFormat::RGB565 ? fb_lores_565_texture : fb_lores_texture;
} else {
current = format == blit::PixelFormat::RGB565 ? fb_hires_565_texture : fb_hires_texture;
}
current = format == blit::PixelFormat::RGB565 ? fb_565_texture : fb_texture;

if(is_lores != (sys->mode() == 0)) {
is_lores = sys->mode() == 0;
Expand All @@ -98,11 +90,6 @@ void Renderer::present() {
dest.w = sys_width;
dest.h = sys_height;

if (is_lores) {
dest.w /= 2;
dest.h /= 2;
}

_render(nullptr, &dest);
SDL_RenderPresent(renderer);
}
Expand Down
6 changes: 2 additions & 4 deletions 32blit-sdl/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class Renderer {
Mode mode = KeepPixels;
SDL_Renderer *renderer = nullptr;

SDL_Texture *fb_lores_texture = nullptr;
SDL_Texture *fb_hires_texture = nullptr;
SDL_Texture *fb_lores_565_texture = nullptr;
SDL_Texture *fb_hires_565_texture = nullptr;
SDL_Texture *fb_texture = nullptr;
SDL_Texture *fb_565_texture = nullptr;
SDL_Texture *current = nullptr;
};
10 changes: 6 additions & 4 deletions 32blit-sdl/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,15 @@ Uint32 System::format() {

void System::update_texture(SDL_Texture *texture) {
bool is_lores = _mode == blit::ScreenMode::lores;
auto stride = (is_lores ? width / 2 : width) * blit::pixel_format_stride[int(cur_format)];

SDL_Rect dest_rect{0, 0, is_lores ? width / 2 : width, is_lores ? height / 2 : height};
auto stride = dest_rect.w * blit::pixel_format_stride[int(cur_format)];

if(cur_format == blit::PixelFormat::P) {
uint8_t col_fb[max_width * max_height * 3];

auto in = framebuffer, out = col_fb;
auto size = is_lores ? (width / 2) * (height / 2) : width * height;
auto size = dest_rect.w * dest_rect.h;

for(int i = 0; i < size; i++) {
uint8_t index = *(in++);
Expand All @@ -361,9 +363,9 @@ void System::update_texture(SDL_Texture *texture) {
(*out++) = palette[index].b;
}

SDL_UpdateTexture(texture, nullptr, col_fb, stride * 3);
SDL_UpdateTexture(texture, &dest_rect, col_fb, stride * 3);
} else
SDL_UpdateTexture(texture, nullptr, framebuffer, stride);
SDL_UpdateTexture(texture, &dest_rect, framebuffer, stride);
}

void System::notify_redraw() {
Expand Down

0 comments on commit eed6738

Please sign in to comment.