Skip to content

Commit

Permalink
Add a shortcut for pausing and reseting the game.
Browse files Browse the repository at this point in the history
This commit also adds a "GAME PAUSED" text when the game is paused.
  • Loading branch information
Arignir committed Dec 11, 2023
1 parent 05ebf22 commit 4f02773
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
9 changes: 8 additions & 1 deletion include/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@ enum bind_actions {
BIND_EMULATOR_SCREENSHOT,
BIND_EMULATOR_QUICKSAVE,
BIND_EMULATOR_QUICKLOAD,
BIND_EMULATOR_PAUSE,
BIND_EMULATOR_RESET,

BIND_MAX,
BIND_MIN = BIND_GBA_A,

BIND_GBA_MIN = BIND_GBA_A,
BIND_GBA_MAX = BIND_GBA_SELECT,
BIND_EMULATOR_MIN = BIND_EMULATOR_SPEED_X1,
BIND_EMULATOR_MAX = BIND_EMULATOR_QUICKLOAD,
BIND_EMULATOR_MAX = BIND_EMULATOR_RESET,
};

extern char const * const binds_pretty_name[];
Expand Down Expand Up @@ -205,6 +207,11 @@ struct app {
/* ImGui internal stuff */
struct ImGuiIO *ioptr;

struct {
struct ImFont *normal;
struct ImFont *big;
} fonts;

/* High resolution */
float dpi;
uint32_t scale;
Expand Down
3 changes: 3 additions & 0 deletions source/gui/sdl/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ gui_sdl_setup_default_binds(
app->binds.keyboard[BIND_EMULATOR_SCREENSHOT] = SDL_GetKeyFromName("F2");
app->binds.keyboard[BIND_EMULATOR_QUICKSAVE] = SDL_GetKeyFromName("F5");
app->binds.keyboard[BIND_EMULATOR_QUICKLOAD] = SDL_GetKeyFromName("F8");
app->binds.keyboard[BIND_EMULATOR_PAUSE] = SDL_GetKeyFromName("F3");

app->binds.keyboard_alt[BIND_GBA_UP] = SDL_GetKeyFromName("Up");
app->binds.keyboard_alt[BIND_GBA_DOWN] = SDL_GetKeyFromName("Down");
Expand Down Expand Up @@ -172,6 +173,8 @@ gui_sdl_handle_bind(
case BIND_EMULATOR_SCREENSHOT: app_game_screenshot(app); break;
case BIND_EMULATOR_QUICKSAVE: app_game_quicksave(app, 0); break;
case BIND_EMULATOR_QUICKLOAD: app_game_quickload(app, 0); break;
case BIND_EMULATOR_PAUSE: app->emulation.is_running ? app_game_pause(app) : app_game_run(app); break;
case BIND_EMULATOR_RESET: app_game_reset(app); app_game_run(app); break;
default: break;
}
}
Expand Down
16 changes: 10 additions & 6 deletions source/gui/sdl/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ void
gui_sdl_video_init(
struct app *app
) {
int err;
SDL_DisplayMode mode;
char const *glsl_version;
SDL_DisplayMode mode;
ImFontConfig *cfg;
int err;

memset(&mode, 0, sizeof(mode));

Expand Down Expand Up @@ -133,13 +134,16 @@ gui_sdl_video_init(
app->ui.ioptr->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
app->ui.ioptr->IniFilename = NULL;

/* Setup ImGui font size */
ImFontConfig *cfg;

cfg = ImFontConfig_ImFontConfig();
cfg->SizePixels = 13.f * app->ui.scale;
cfg->GlyphOffset.y = 13.f * app->ui.scale;
ImFontAtlas_AddFontDefault(app->ui.ioptr->Fonts, cfg);
app->ui.fonts.normal = ImFontAtlas_AddFontDefault(app->ui.ioptr->Fonts, cfg);

cfg = ImFontConfig_ImFontConfig();
cfg->SizePixels = 13.f * app->ui.scale * 3.;
cfg->GlyphOffset.y = 13.f * app->ui.scale * 3.;
app->ui.fonts.big = ImFontAtlas_AddFontDefault(app->ui.ioptr->Fonts, cfg);

ImGuiStyle_ScaleAllSizes(igGetStyle(), app->ui.scale);

ImGui_ImplSDL2_InitForOpenGL(app->sdl.window, app->gfx.gl_context);
Expand Down
35 changes: 34 additions & 1 deletion source/gui/windows/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@
#include "app.h"
#include "gui/gui.h"

static
void
gui_win_game_pause_text(
struct app *app
) {
igPushFont(app->ui.fonts.big);
{
char const *text;
ImVec2 text_size;
ImVec2 text_pos;
ImVec2 win_size;

text = "- GAME PAUSED -";

igGetWindowSize(&win_size);
igCalcTextSize(&text_size, text, NULL, false, -1.0f);

text_pos.x = (win_size.x - text_size.x) / 2.f;
text_pos.y = (win_size.y - text_size.y) / 2.f;
igSetCursorPos(text_pos);
igText(text);
}
igPopFont();
}

void
gui_win_game(
struct app *app
Expand All @@ -23,6 +48,10 @@ gui_win_game(
float game_pos_y;
float game_size_x;
float game_size_y;
float tint;

// Adjust the tint if the game is paused
tint = app->emulation.is_running ? 1.0 : 0.1;

if (!app->gfx.active_programs_length) {
// If there's no shaders loaded, we shortcut the pipeline and simply load the game's framebuffer in the corresponding texture
Expand Down Expand Up @@ -111,10 +140,14 @@ gui_win_game(
(ImVec2){.x = game_size_x, .y = game_size_y},
(ImVec2){.x = 0, .y = 0},
(ImVec2){.x = 1, .y = 1},
(ImVec4){.x = 1, .y = 1, .z = 1, .w = 1},
(ImVec4){.x = tint, .y = tint, .z = tint, .w = 1},
(ImVec4){.x = 0, .y = 0, .z = 0, .w = 0}
);

if (!app->emulation.is_running) {
gui_win_game_pause_text(app);
}

igEnd();

igPopStyleVar(2);
Expand Down
4 changes: 4 additions & 0 deletions source/gui/windows/keybinds.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ char const * const binds_pretty_name[] = {
[BIND_EMULATOR_SCREENSHOT] = "Screenshot",
[BIND_EMULATOR_QUICKSAVE] = "Quicksave",
[BIND_EMULATOR_QUICKLOAD] = "Quickload",
[BIND_EMULATOR_PAUSE] = "Pause",
[BIND_EMULATOR_RESET] = "Reset",
};

char const * const binds_slug[] = {
Expand All @@ -59,6 +61,8 @@ char const * const binds_slug[] = {
[BIND_EMULATOR_SCREENSHOT] = "screenshot",
[BIND_EMULATOR_QUICKSAVE] = "quicksave",
[BIND_EMULATOR_QUICKLOAD] = "quickload",
[BIND_EMULATOR_PAUSE] = "pause",
[BIND_EMULATOR_RESET] = "reset",
};

void
Expand Down

0 comments on commit 4f02773

Please sign in to comment.