From e95bd27755ae955fd451374def6f736509d444b1 Mon Sep 17 00:00:00 2001 From: Nathaniel Walizer Date: Tue, 7 Jan 2025 22:31:51 -0800 Subject: [PATCH] Add reset key (escape) --- src/input.h | 7 +++++++ src/nese.c | 7 ++++++- src/sdl_input.c | 13 +++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/input.h b/src/input.h index cb488b1..6b331a8 100644 --- a/src/input.h +++ b/src/input.h @@ -7,6 +7,13 @@ // Emulator Implementation +typedef enum { + input_Result_Error = -1, + input_Result_OK = 0, + input_Result_Quit = 1, + input_Result_Reset = 2, +} nes_Input_Result; + #define nes_controller_num_buttons (8U) typedef enum { diff --git a/src/nese.c b/src/nese.c index 4195969..1e96cd0 100644 --- a/src/nese.c +++ b/src/nese.c @@ -142,7 +142,12 @@ int main(int argc, char* argv[]) { // Update button states every rendered frame status = nes_input_update(input, &sys.input); - if (status >= 0) { + if (input_Result_Reset == status) { + nes_reset(&sys); + status = 0; + } + + if (status == 0) { // Update audio, too status = nes_audio_fill(audio, &sys.apu); } diff --git a/src/sdl_input.c b/src/sdl_input.c index 2229341..e1fff06 100644 --- a/src/sdl_input.c +++ b/src/sdl_input.c @@ -48,6 +48,8 @@ static void sdl_input_done(nes_Input_Reader* input) { sdl_lose_gamepad(input->data); } +static const int sdl_reset_key = SDLK_ESCAPE; + static const int sdl_keycodes[nes_controller_num_buttons] = { SDLK_a, SDLK_s, @@ -78,13 +80,12 @@ static int button_index(int keycode, const int* codes) { static int sdl_input_update(nes_Input_Reader* reader, nes_input* input) { - int status = 0; + int status = input_Result_OK; SDL_Event event = {0}; - while (0 != SDL_PollEvent(&event)) { + while (0 == status && 0 != SDL_PollEvent(&event)) { if (SDL_QUIT == event.type) { - status = -1; - break; + status = input_Result_Quit; } else if ( SDL_KEYDOWN == event.type || SDL_KEYUP == event.type) { @@ -97,6 +98,10 @@ static int sdl_input_update(nes_Input_Reader* reader, } else { input->controllers[0].buttons &= ~mask; } + + } else if ( event.key.keysym.sym == sdl_reset_key && + SDL_KEYDOWN == event.type) { + status = input_Result_Reset; } } else if ( SDL_CONTROLLERBUTTONDOWN == event.type ||