// // Created by william on 12/07/24. // #include "nametable_window.h" #include "dbg_nametable.h" #include "dbg_palette.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) NametableWindow *nametable_window_create(int *window_id) { NametableWindow *window = malloc(sizeof(NametableWindow)); window->window = window_create("Nametable", NW_WIDTH, NW_HEIGHT, NW_SCALE); window->texture = window_create_texture(&window->window, SDL_TEXTUREACCESS_STREAMING); dbg_nametable_init(); *window_id = window->window.id; return window; } void nametable_window_destroy(NametableWindow *window) { SDL_DestroyTexture(window->texture); window_destroy(&window->window); free(window); } void nametable_window_update_bank(NametableWindow *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(NametableWindow *window) { dbg_palette_update(); 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(NametableWindow *window) { window_render_texture(&window->window, window->texture); window_present(&window->window); }