nesemu/gui/gui.c

185 lines
4.2 KiB
C
Raw Permalink 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-07-12 13:07:16 -04:00
#include "nametable_window.h"
2024-07-21 16:41:38 -04:00
#include "dbg_pattern_table.h"
2024-07-23 20:46:13 -04:00
#include "dbg_nametable.h"
2024-07-25 21:22:00 -04:00
#include "char_map.h"
2024-08-03 21:51:31 -04:00
#include "dbg_palette.h"
2024-05-17 00:33:37 -04:00
2024-09-01 15:54:41 -04:00
#if DEBUG
#define WINDOW_ID_MAIN 3
#else
#define WINDOW_ID_MAIN 1
#endif
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;
2024-07-12 13:07:16 -04:00
NesNametableWindow nametable_window;
2024-06-16 19:22:40 -04:00
TTF_Font *font;
Uint32 last_frame_tick;
Uint32 frame_delay;
2024-07-25 21:22:00 -04:00
#if DEBUG
2024-07-21 16:41:38 -04:00
unsigned long tick;
2024-07-25 21:22:00 -04:00
#endif
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();
2024-07-10 22:34:14 -04:00
gui.font = TTF_OpenFont("./nintendo-nes-font.ttf", 16);
2024-06-16 19:22:40 -04:00
if (gui.font == NULL) {
log_error("Failed to open TTF font");
return false;
}
2024-07-25 21:22:00 -04:00
#if DEBUG
gui.tick = 0;
pattern_window_init(&gui.pattern_window);
nametable_window_init(&gui.nametable_window);
2024-06-16 19:22:40 -04:00
2024-09-01 15:54:41 -04:00
char_map_init(gui.main_window.window.sdl_context.renderer, gui.font);
2024-07-25 21:22:00 -04:00
#endif
2024-06-16 19:22:40 -04:00
2024-07-25 22:08:08 -04:00
main_window_init(&gui.main_window);
2024-06-16 19:22:40 -04:00
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);
2024-07-25 21:22:00 -04:00
#if DEBUG
char_map_uninit();
pattern_window_uninit(&gui.pattern_window);
nametable_window_uninit(&gui.nametable_window);
#endif
2024-06-16 19:22:40 -04:00
TTF_CloseFont(gui.font);
}
void gui_post_sysinit() {
2024-07-25 21:22:00 -04:00
#if DEBUG
2024-08-03 21:51:31 -04:00
dbg_palette_init();
2024-07-25 21:22:00 -04:00
dbg_pattern_table_init();
dbg_nametable_init();
2024-07-21 16:41:38 -04:00
2024-09-01 15:54:41 -04:00
// TODO: The background_texture is rendered before the palette data is in the PPU memory, so the only color is grey
2024-07-25 21:22:00 -04:00
pattern_window_build_table(&gui.pattern_window);
nametable_window_update(&gui.nametable_window);
#endif
2024-05-17 00:33:37 -04:00
}
bool lctrl = false;
2024-05-17 00:33:37 -04:00
int gui_input() {
SDL_Event event;
#if DEBUG
PPUDebugFlags *ppu_debug = &ppu_get_state()->debug;
#endif
2024-05-17 00:33:37 -04:00
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
}
2024-08-03 21:51:31 -04:00
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_LCTRL) {
lctrl = true;
}
if (event.type == SDL_KEYUP) {
switch (event.key.keysym.sym) {
case SDLK_LCTRL:
lctrl = false;
break;
case SDLK_p:
system_toggle_pause(lctrl);
break;
#if DEBUG
2024-09-01 15:54:41 -04:00
case SDLK_t:
ppu_debug->flags.tile_debugger = !ppu_debug->flags.tile_debugger;
break;
case SDLK_n:
ppu_debug->flags.tile_debugger_pattern_half = (ppu_debug->flags.tile_debugger_pattern_half + 1) % 3;
break;
default:
2024-09-01 15:54:41 -04:00
pattern_window_key_up(&gui.pattern_window, event.key.keysym.sym);
break;
2024-09-01 15:54:41 -04:00
#else
default:
break;
#endif
}
2024-08-03 21:51:31 -04:00
}
2024-09-01 15:54:41 -04:00
if (event.type == SDL_MOUSEMOTION) {
int x = event.motion.x;
int y = event.motion.y;
if (event.window.windowID == WINDOW_ID_MAIN) {
main_window_mouse_motion(&gui.main_window, x, y);
}
}
if (event.type == SDL_MOUSEBUTTONUP && event.window.windowID == WINDOW_ID_MAIN) {
main_window_mouse_click(&gui.main_window);
}
2024-05-17 00:33:37 -04:00
}
return 1;
}
void gui_render() {
main_window_render(&gui.main_window, ppu_get_state()->pixels);
2024-07-25 21:22:00 -04:00
#if DEBUG
2024-08-03 21:51:31 -04:00
dbg_palette_init();
2024-07-25 21:22:00 -04:00
pattern_window_render(&gui.pattern_window);
2024-07-21 16:41:38 -04:00
2024-07-25 21:22:00 -04:00
nametable_window_update(&gui.nametable_window);
nametable_window_render(&gui.nametable_window);
2024-07-21 16:41:38 -04:00
gui.tick++;
2024-07-25 21:22:00 -04:00
#endif
2024-05-17 00:33:37 -04:00
}
void gui_present() {
2024-07-25 21:22:00 -04:00
#if DEBUG
pattern_window_present(&gui.pattern_window);
nametable_window_present(&gui.nametable_window);
#endif
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();
}
2024-09-01 15:54:41 -04:00
TTF_Font *gui_get_font() {
return gui.font;
}
2024-06-16 19:22:40 -04:00
unsigned int gui_get_frame_delay() {
return gui.frame_delay;
2024-05-17 00:33:37 -04:00
}