From 51c4aeffd0ae26da20ead7e5155a4ededff7c241 Mon Sep 17 00:00:00 2001 From: Nathaniel Walizer Date: Wed, 5 Feb 2025 18:49:00 -0800 Subject: [PATCH] Simplify menu calls --- src/menu.c | 23 ++++++++++------------- src/menu.h | 9 +++------ src/nese.c | 38 ++++++++++++++++---------------------- src/state.h | 21 +++++++++++---------- 4 files changed, 40 insertions(+), 51 deletions(-) diff --git a/src/menu.c b/src/menu.c index 41bdae7..11cc083 100644 --- a/src/menu.c +++ b/src/menu.c @@ -223,10 +223,8 @@ char* run_main_menu(menu_state* state, nese_Components* comp, DIR* dir = opendir("rom"); if (NULL == dir) { - modal_popup( - "No ROMS found!\nPress any key to exit", - comp, cart - ); + modal_popup("No ROMS found!\nPress any key to exit", + comp); } else { file_list files = {0}; @@ -267,8 +265,7 @@ char* run_main_menu(menu_state* state, nese_Components* comp, return cart_filename; } -int run_game_menu(menu_state* state, nese_Components* comp, - const nese_State* nese) { +int run_game_menu(menu_state* state, nese_Components* comp) { char* items[] = { "Resume", "Save", @@ -276,12 +273,12 @@ int run_game_menu(menu_state* state, nese_Components* comp, "Reset", "Select ROM", #ifndef STANDALONE - (nese->flags & (1 << State_Bit_Fullscreen)) ? + (comp->state->flags & (1 << State_Bit_Fullscreen)) ? "Windowed" : "Fullscreen", #endif - (nese->flags & (1 << State_Bit_Integer_Scale)) ? + (comp->state->flags & (1 << State_Bit_Integer_Scale)) ? "Force Aspect" : "Integer Scaling", - (nese->flags & (1 << State_Bit_CRT_Effect)) ? + (comp->state->flags & (1 << State_Bit_CRT_Effect)) ? "No Effect" : "CRT Effect", "Exit", }; @@ -307,7 +304,7 @@ int run_game_menu(menu_state* state, nese_Components* comp, if (NULL != state) menu = *state; int status = run_menu(&menu, &options, 100, - comp, &nese->cart); + comp, &comp->state->cart); if (input_Result_Menu == status) { status = input_Result_Cancel; @@ -322,8 +319,7 @@ int run_game_menu(menu_state* state, nese_Components* comp, return status; } -int modal_popup(const char* message, nese_Components* comp, - const cart_info* cart) { +int modal_popup(const char* message, nese_Components* comp) { int w = 0; int h = 0; @@ -338,7 +334,8 @@ int modal_popup(const char* message, nese_Components* comp, int status = (input_Result_Refresh << 8); while (input_Result_Refresh == (status >> 8)) { - nes_draw_last_frame(comp->rend, NULL != cart->file); + nes_draw_last_frame(comp->rend, + NULL != comp->state->cart.file); nes_draw_text(comp->rend, message, x, y, color_error); nes_draw_done(comp->rend); diff --git a/src/menu.h b/src/menu.h index ef0a372..46d77d1 100644 --- a/src/menu.h +++ b/src/menu.h @@ -30,15 +30,12 @@ typedef struct { // Returns filename of selected ROM -char* run_main_menu(menu_state*, nese_Components*, - const cart_info*); +char* run_main_menu(menu_state*, nese_Components*, const cart_info*); // Returns nes_Input_Result indicating the choice -int run_game_menu(menu_state*, nese_Components*, - const nese_State*); +int run_game_menu(menu_state*, nese_Components*); -int modal_popup(const char* message, nese_Components*, - const cart_info*); +int modal_popup(const char* message, nese_Components*); #endif // NESE_MENU_H_ diff --git a/src/nese.c b/src/nese.c index 2b2133d..e405545 100644 --- a/src/nese.c +++ b/src/nese.c @@ -108,14 +108,10 @@ static int loadsave_tick(loadsave_state* loadsave) { } static int select_rom(menu_state* menu, - nese_Components* comp, - cart_info* cur_cart) { + nese_Components* comp) { int status = 0; cart_info cart = {0}; - cart_info ref_cart = { - .filename = cur_cart->filename, - .file = cur_cart->file, - }; + cart_info ref_cart = comp->state->cart; nes_cart new_cart = {0}; while (0 == status && NULL == cart.file) { @@ -124,7 +120,7 @@ static int select_rom(menu_state* menu, char message[1024]; snprintf(message, sizeof(message) - 1, "Could not load\n%s", cart.filename + 4); - int button = modal_popup(message, comp, cur_cart); + int button = modal_popup(message, comp); if (input_Result_Quit == (button >> 8)) { // Program closed inside modal status = input_Result_Quit; @@ -152,13 +148,13 @@ static int select_rom(menu_state* menu, } if (0 == status && NULL != cart.file) { - save_sram(&comp->sys->cart, cur_cart->filename); + save_sram(&comp->sys->cart, comp->state->cart.filename); nes_cart_done(&comp->sys->cart); - cart_info_done(cur_cart); + cart_info_done(&comp->state->cart); comp->sys->cart = new_cart; - *cur_cart = cart; + comp->state->cart = cart; nes_setup_cart(comp->sys); @@ -170,13 +166,12 @@ static int select_rom(menu_state* menu, } static int do_game_menu(menu_state* menu, - nese_Components* comp, - nese_State* state) { + nese_Components* comp) { int status = 0; menu_state rom_menu = {0}; while (1) { - status = run_game_menu(menu, comp, state); + status = run_game_menu(menu, comp); if ( input_Result_View == status || input_Result_Scale == status || @@ -184,21 +179,20 @@ static int do_game_menu(menu_state* menu, if (input_Result_View == status) { #ifndef STANDALONE - state->flags ^= (1 << State_Bit_Fullscreen); + comp->state->flags ^= (1 << State_Bit_Fullscreen); #endif } else if (input_Result_Scale == status) { - state->flags ^= (1 << State_Bit_Integer_Scale); + comp->state->flags ^= (1 << State_Bit_Integer_Scale); } else { - state->flags ^= (1 << State_Bit_CRT_Effect); + comp->state->flags ^= (1 << State_Bit_CRT_Effect); } - nes_render_set_flags(comp->rend, state->flags); + nes_render_set_flags(comp->rend, comp->state->flags); continue; } else if (input_Result_Menu == status) { - status = select_rom(&rom_menu, comp, &state->cart); + status = select_rom(&rom_menu, comp); if (input_Result_Cancel == status) { - status = input_Result_OK; continue; } } @@ -229,6 +223,7 @@ int main(int argc, char* argv[]) { .reader = &sdl_input, .audio = &sdl_audio, .sys = &sys, + .state = &state, }; if (status == 0) { @@ -262,7 +257,7 @@ int main(int argc, char* argv[]) { // If we didn't launch with a file, run the loader if (0 == status && NULL == state.cart.file) { - status = select_rom(NULL, &components, &state.cart); + status = select_rom(NULL, &components); } if (status == 0) { @@ -323,8 +318,7 @@ int main(int argc, char* argv[]) { } if (input_Result_Menu == status) { status = do_game_menu( - &game_menu, &components, &state - ); + &game_menu, &components); if ( input_Result_Load == status || input_Result_Save == status) { loadsave.mode = status; diff --git a/src/state.h b/src/state.h index c751bec..c470ce9 100644 --- a/src/state.h +++ b/src/state.h @@ -12,16 +12,6 @@ #include "nes.h" -// Running State - -typedef struct nese_Components { - nes_Renderer* rend; - nes_Input_Reader* reader; - nes_Audio_Stream* audio; - nes* sys; -} nese_Components; - - // Persistent State typedef struct { @@ -54,4 +44,15 @@ int load_prefs_filename(nese_State*, const char*); int save_prefs_filename(const nese_State*, const char*); +// Running State + +typedef struct nese_Components { + nes_Renderer* rend; + nes_Input_Reader* reader; + nes_Audio_Stream* audio; + nes* sys; + nese_State* state; +} nese_Components; + + #endif // NESE_STATE_H_