Skip to content

Commit

Permalink
Add a new "auto-pause" and "pause on reset" (dbg) setting.
Browse files Browse the repository at this point in the history
This commit also revamps how settings are ordered and laid out.
  • Loading branch information
Arignir committed Mar 6, 2024
1 parent 3362dba commit 18771b8
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 293 deletions.
8 changes: 7 additions & 1 deletion include/app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ struct app {
// Skip BIOS
bool skip_bios;

// Automatically pause the game when the window looses focus
bool auto_pause;

// Pause immediately after resetting the game
bool pause_on_reset;

// Backup storage
struct {
bool autodetect;
Expand Down Expand Up @@ -424,7 +430,7 @@ void app_config_push_recent_rom(struct app *app, char const *path);

/* emulator.c */
void app_emulator_process_all_notifs(struct app *app);
bool app_emulator_configure(struct app *app, char const *rom_path);
bool app_emulator_configure_and_run(struct app *app, char const *rom_path);
void app_emulator_reset(struct app *app);
void app_emulator_stop(struct app *app);
void app_emulator_run(struct app *app);
Expand Down
2 changes: 1 addition & 1 deletion source/app/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ app_bindings_handle(
case BIND_EMULATOR_QUICKSAVE: app_emulator_quicksave(app, 0); break;
case BIND_EMULATOR_QUICKLOAD: app_emulator_quickload(app, 0); break;
case BIND_EMULATOR_PAUSE: app->emulation.is_running ? app_emulator_pause(app) : app_emulator_run(app); break;
case BIND_EMULATOR_RESET: app_emulator_reset(app); app_emulator_run(app); break;
case BIND_EMULATOR_RESET: app_emulator_reset(app); break;
default: break;
}
}
14 changes: 13 additions & 1 deletion source/app/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ app_config_load(
if (mjson_get_bool(data, data_len, "$.emulation.skip_bios", &b)) {
app->emulation.skip_bios = b;
}

if (mjson_get_bool(data, data_len, "$.emulation.auto_pause", &b)) {
app->emulation.auto_pause = b;
}

if (mjson_get_bool(data, data_len, "$.emulation.pause_on_reset", &b)) {
app->emulation.pause_on_reset = b;
}
}

// Video
Expand Down Expand Up @@ -251,7 +259,9 @@ app_config_save(
"rtc": {
"autodetect": %B,
"enabled": %B
}
},
"auto_pause": %B,
"pause_on_reset": %B
},

// Video
Expand Down Expand Up @@ -283,6 +293,8 @@ app_config_save(
(int)app->emulation.backup_storage.type,
(int)app->emulation.rtc.autodetect,
(int)app->emulation.rtc.enabled,
(int)app->emulation.auto_pause,
(int)app->emulation.pause_on_reset,
(int)app->video.display_size,
(int)app->video.aspect_ratio,
(int)app->video.vsync,
Expand Down
11 changes: 9 additions & 2 deletions source/app/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,10 @@ app_emulator_configure_backup(
** - Update the gba's launch configuration
** - Reset the emulator
** - Wait for the reset notification
** - Run/Pause the emulator, according to the configuration
*/
bool
app_emulator_configure(
app_emulator_configure_and_run(
struct app *app,
char const *rom_path
) {
Expand Down Expand Up @@ -601,6 +602,12 @@ app_emulator_configure(

logln(HS_INFO, "Game successfully loaded.");

if (app->emulation.pause_on_reset) {
app_emulator_pause(app);
} else {
app_emulator_run(app);
}

return (false);
}

Expand All @@ -614,7 +621,7 @@ app_emulator_reset(
char *game_path;

game_path = strdup(app->emulation.game_path);
app_emulator_configure(app, game_path);
app_emulator_configure_and_run(app, game_path);
free(game_path);
}

Expand Down
9 changes: 1 addition & 8 deletions source/app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,7 @@ main(
);

if (app.args.rom_path) {
app_emulator_configure(&app, app.args.rom_path);
if (app.emulation.launch_config) {
#ifdef WITH_DEBUGGER
app_emulator_pause(&app);
#else
app_emulator_run(&app);
#endif
}
app_emulator_configure_and_run(&app, app.args.rom_path);
}

#ifdef WITH_DEBUGGER
Expand Down
12 changes: 12 additions & 0 deletions source/app/sdl/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ app_sdl_handle_events(
}
break;
};
case SDL_WINDOWEVENT_FOCUS_GAINED: {
if (app->emulation.auto_pause && app->emulation.is_started && !app->emulation.is_running) {
app_emulator_run(app);
}
break;
};
case SDL_WINDOWEVENT_FOCUS_LOST: {
if (app->emulation.auto_pause && app->emulation.is_started && app->emulation.is_running) {
app_emulator_pause(app);
}
break;
};
}
break;
};
Expand Down
11 changes: 3 additions & 8 deletions source/app/windows/menubar.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ app_win_menubar_file(
);

if (result == NFD_OKAY) {
if (!app_emulator_configure(app, path)) {
app_emulator_run(app);
}
app_emulator_configure_and_run(app, path);
NFD_FreePath(path);
}
}
Expand All @@ -50,13 +48,11 @@ app_win_menubar_file(
if (igMenuItem_Bool(hs_basename(app->file.recent_roms[x]), NULL, false, true)) {
char *path;

// app->file.recent_roms[] is modified by `app_emulator_configure()` so we need to copy
// app->file.recent_roms[] is modified by `app_emulator_configure_and_run()` so we need to copy
// the path to a safe space first.
path = strdup(app->file.recent_roms[x]);

if (!app_emulator_configure(app, path)) {
app_emulator_run(app);
}
app_emulator_configure_and_run(app, path);

free(path);
}
Expand Down Expand Up @@ -199,7 +195,6 @@ app_win_menubar_emulation(
bind = SDL_GetKeyName(app->binds.keyboard[BIND_EMULATOR_RESET]);
if (igMenuItem_Bool("Reset", bind ?: "", false, app->emulation.is_started)) {
app_emulator_reset(app);
app_emulator_run(app);
}

igSeparator();
Expand Down
Loading

0 comments on commit 18771b8

Please sign in to comment.