nesemu/gui/main_window.c

63 lines
1.8 KiB
C

//
// Created by william on 6/8/24.
//
#include <SDL_ttf.h>
#include "main_window.h"
#include "log.h"
#include "char_map.h"
#include "gui.h"
void main_window_init(NesMainWindow *window, TTF_Font *font) {
window->sdl_context = window_init("NES Emulator", MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT, MAIN_WINDOW_SCALE);
window->texture = SDL_CreateTexture(window->sdl_context.renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT);
char_map_init(window->sdl_context.renderer, font);
}
void main_window_uninit(NesMainWindow *window) {
char_map_uninit();
SDL_DestroyTexture(window->texture);
window_uninit(window->sdl_context);
}
void main_window_render_delay(SDL_Renderer *renderer) {
Uint32 delay = gui_get_frame_delay();
char buffer[5];
buffer[0] = (char) ((delay / 10) + 48);
buffer[1] = (char) ((delay % 10) + 48);
buffer[2] = ' ';
buffer[3] = 'm';
buffer[4] = 's';
char_map_render(renderer, buffer);
}
void main_window_render(NesMainWindow *window, PPUPixel *pixels) {
SDL_RenderClear(window->sdl_context.renderer);
unsigned int frame_buffer[240 * 256];
for (int i = 0; i < 240 * 256; i++) {
PPUPixel pixel = pixels[i];
unsigned int *data = &frame_buffer[i];
*data = 0xff000000;
*data |= pixel.r << 16;
*data |= pixel.g << 8;
*data |= pixel.b;
}
SDL_UpdateTexture(window->texture, NULL, &frame_buffer, 240 * sizeof(unsigned int));
SDL_RenderCopy(window->sdl_context.renderer, window->texture, NULL, NULL);
if (gui_debug_enabled()) {
main_window_render_delay(window->sdl_context.renderer);
}
}
void main_window_present(NesMainWindow *window) {
SDL_RenderPresent(window->sdl_context.renderer);
}