nesemu/gui/nametable_window.c

54 lines
1.9 KiB
C

//
// Created by william on 12/07/24.
//
#include "nametable_window.h"
#include "dbg_nametable.h"
#define NW_WIDTH (NW_ROW_TILE_COUNT * PATTERN_DRAW_SIZE)
#define NW_HEIGHT (NW_ROW_COUNT * PATTERN_DRAW_SIZE)
#define NW_BUFFER_SIZE (NAMETABLE_ROW_WIDTH * NAMETABLE_COL_HEIGHT * PATTERN_DRAW_SIZE * PATTERN_DRAW_SIZE)
void nametable_window_init(NesNametableWindow *window) {
int win_size = pattern_display_get_size(NW_ROW_TILE_COUNT);
window->sdl_context = window_init("Nametable", win_size, win_size, NW_SCALE);
window->texture = SDL_CreateTexture(window->sdl_context.renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, NW_WIDTH, NW_HEIGHT);
}
void nametable_window_uninit(NesNametableWindow *window) {
SDL_DestroyTexture(window->texture);
window_uninit(window->sdl_context);
}
void nametable_window_update_bank(NesNametableWindow *window, int bank, pixel* buffer) {
dbg_nametable_render_bank(bank, buffer);
SDL_Rect rect;
rect.w = NAMETABLE_ROW_WIDTH * PATTERN_DRAW_SIZE;
rect.h = NAMETABLE_COL_HEIGHT * PATTERN_DRAW_SIZE;
rect.x = (bank & 1) * rect.w;
rect.y = ((bank & 2) >> 1) * rect.h;
SDL_UpdateTexture(window->texture, &rect, buffer, (NW_WIDTH / 2) * sizeof(pixel));
}
void nametable_window_update(NesNametableWindow *window) {
dbg_nametable_update();
pixel buffer[NW_BUFFER_SIZE * 4] = {0};
nametable_window_update_bank(window, 0, buffer);
nametable_window_update_bank(window, 1, buffer);
nametable_window_update_bank(window, 2, buffer);
nametable_window_update_bank(window, 3, buffer);
}
void nametable_window_render(NesNametableWindow *window) {
SDL_RenderClear(window->sdl_context.renderer);
SDL_RenderCopy(window->sdl_context.renderer, window->texture, NULL, NULL);
}
void nametable_window_present(NesNametableWindow *window) {
SDL_RenderPresent(window->sdl_context.renderer);
}