nesemu/gui/gui.c

106 lines
2.1 KiB
C
Raw Normal View History

2024-05-17 00:33:37 -04:00
//
// Created by william on 16/05/24.
//
2024-06-16 19:22:40 -04:00
#include <SDL_ttf.h>
2024-05-17 00:33:37 -04:00
#include "log.h"
2024-05-17 13:16:21 -04:00
#include "gui.h"
2024-06-16 19:22:40 -04:00
#include "main_window.h"
#include "pattern_window.h"
#include "../include/system.h"
2024-05-17 00:33:37 -04:00
2024-05-17 13:16:21 -04:00
typedef struct nes_gui {
2024-06-16 19:22:40 -04:00
NesMainWindow main_window;
NesPatternWindow pattern_window;
TTF_Font *font;
// Debug info
bool debug_enabled;
Uint32 last_frame_tick;
Uint32 frame_delay;
2024-05-17 13:16:21 -04:00
} NesGui;
2024-05-17 00:33:37 -04:00
NesGui gui;
2024-06-16 19:22:40 -04:00
bool gui_init() {
TTF_Init();
gui.font = TTF_OpenFont("../nintendo-nes-font.ttf", 16);
if (gui.font == NULL) {
log_error("Failed to open TTF font");
return false;
}
2024-06-21 13:47:28 -04:00
gui.debug_enabled = false;
2024-06-16 19:22:40 -04:00
main_window_init(&gui.main_window, gui.font);
if (gui.debug_enabled) {
pattern_window_init(&gui.pattern_window);
}
return true;
}
2024-05-17 00:33:37 -04:00
void gui_uninit() {
2024-06-16 19:22:40 -04:00
main_window_uninit(&gui.main_window);
if (gui.debug_enabled) {
pattern_window_uninit(&gui.pattern_window);
}
TTF_CloseFont(gui.font);
}
void gui_post_sysinit() {
byte *pattern_memory = system_get_mapper()->ppu_read(0);
pattern_window_build_table(&gui.pattern_window, pattern_memory);
2024-05-17 00:33:37 -04:00
}
int gui_input() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE) {
return -1;
2024-05-17 00:33:37 -04:00
}
}
return 1;
}
void gui_render() {
2024-06-16 19:22:40 -04:00
main_window_render(&gui.main_window, ppu_get_state()->pixels);
if (gui.debug_enabled) {
pattern_window_render(&gui.pattern_window);
}
2024-05-17 00:33:37 -04:00
}
void gui_present() {
2024-06-16 19:22:40 -04:00
main_window_present(&gui.main_window);
if (gui.debug_enabled) {
pattern_window_present(&gui.pattern_window);
}
2024-05-17 00:33:37 -04:00
}
2024-05-17 13:16:21 -04:00
void gui_delay() {
2024-06-16 19:22:40 -04:00
Uint32 tick_now = SDL_GetTicks();
gui.frame_delay = tick_now - gui.last_frame_tick;
2024-05-17 13:16:21 -04:00
2024-06-16 19:22:40 -04:00
if (gui.frame_delay < 16) {
Uint32 additional_delay = 16 - gui.frame_delay;
SDL_Delay(additional_delay);
gui.frame_delay += additional_delay;
}
2024-05-17 00:33:37 -04:00
2024-06-16 19:22:40 -04:00
gui.last_frame_tick = SDL_GetTicks();
}
bool gui_debug_enabled() {
return gui.debug_enabled;
}
unsigned int gui_get_frame_delay() {
return gui.frame_delay;
2024-05-17 00:33:37 -04:00
}