Skip to content

Commit

Permalink
Refactor pause menu #284
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Mar 12, 2024
1 parent 577bcc0 commit f371790
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ install_manifest_Runtime.txt
__pycache__/
.cmake/
compile_commands.json
CMakeSettings.json

# Generated files
src/cdogs/sys_config.h
Expand Down Expand Up @@ -62,6 +61,7 @@ Win32/
*.tmp
*.VC.*
.vs/
CMakeSettings.json
VSInheritEnvironments.txt

# XCode
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(CDOGS_SDL_SOURCES
menu_utils.c
namegen.c
password.c
pause_menu.c
player_select_menus.c
prep.c
prep_equip.c
Expand All @@ -57,6 +58,7 @@ set(CDOGS_SDL_HEADERS
menu_utils.h
namegen.h
password.h
pause_menu.h
player_select_menus.h
prep.h
prep_equip.h
Expand Down
88 changes: 7 additions & 81 deletions src/cdogs/hud/hud.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2013-2017, 2019-2020 Cong Xu
Copyright (c) 2013-2017, 2019-2020, 2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -120,16 +120,12 @@ static void DrawSharedRadar(GraphicsDevice *device, bool showExit)

static void DrawPlayerAreas(HUD *hud, const int numViews);
static void DrawDeathmatchScores(HUD *hud);
static void DrawStateMessage(
HUD *hud, const input_device_e pausingDevice,
const bool controllerUnplugged);
static void DrawMissionState(HUD *hud);
static void DrawHUDMessage(HUD *hud);
static void DrawKeycards(HUD *hud);
static void DrawMissionTime(HUD *hud);
static void DrawObjectiveCounts(HUD *hud);
void HUDDraw(
HUD *hud, const input_device_e pausingDevice,
const bool controllerUnplugged, const int numViews)
void HUDDraw(HUD *hud, const int numViews, const bool paused)
{
if (ConfigGetBool(&gConfig, "Graphics.ShowHUD"))
{
Expand All @@ -153,7 +149,10 @@ void HUDDraw(
}
}

DrawStateMessage(hud, pausingDevice, controllerUnplugged);
if (!paused)
{
DrawMissionState(hud);
}
}

static void DrawPlayerAreas(HUD *hud, const int numViews)
Expand Down Expand Up @@ -282,79 +281,6 @@ static void DrawDeathmatchScores(HUD *hud)
CA_FOREACH_END()
}

static void DrawMissionState(HUD *hud);
static void DrawStateMessage(
HUD *hud, const input_device_e pausingDevice,
const bool controllerUnplugged)
{
if (controllerUnplugged || pausingDevice != INPUT_DEVICE_UNSET)
{
// Draw a background overlay
color_t overlay = colorBlack;
overlay.a = 128;
DrawRectangle(
hud->device, svec2i_zero(), hud->device->cachedConfig.Res, overlay,
true);
}
if (controllerUnplugged)
{
struct vec2i pos = svec2i_scale_divide(
svec2i_subtract(
gGraphicsDevice.cachedConfig.Res,
FontStrSize(ARROW_LEFT
"Paused" ARROW_RIGHT
"\nFoobar\nPlease reconnect controller")),
2);
const int x = pos.x;
FontStr(ARROW_LEFT "Paused" ARROW_RIGHT, pos);

pos.y += FontH();
pos = FontStr("Press ", pos);
char buf[256];
color_t c = colorWhite;
InputGetButtonNameColor(pausingDevice, 0, CMD_ESC, buf, &c);
pos = FontStrMask(buf, pos, c);
FontStr(" to quit", pos);

pos.x = x;
pos.y += FontH();
FontStr("Please reconnect controller", pos);
}
else if (pausingDevice != INPUT_DEVICE_UNSET)
{
struct vec2i pos = svec2i_scale_divide(
svec2i_subtract(
gGraphicsDevice.cachedConfig.Res,
FontStrSize("Foo\nPress foo or bar to unpause\nBaz")),
2);
const int x = pos.x;
FontStr(ARROW_LEFT "Paused" ARROW_RIGHT, pos);

pos.y += FontH();
pos = FontStr("Press ", pos);
char buf[256];
color_t c = colorWhite;
InputGetButtonNameColor(pausingDevice, 0, CMD_ESC, buf, &c);
pos = FontStrMask(buf, pos, c);
FontStr(" again to quit", pos);

pos.x = x;
pos.y += FontH();
pos = FontStr("Press ", pos);
c = colorWhite;
InputGetButtonNameColor(pausingDevice, 0, CMD_BUTTON1, buf, &c);
pos = FontStrMask(buf, pos, c);
pos = FontStr(" or ", pos);
c = colorWhite;
InputGetButtonNameColor(pausingDevice, 0, CMD_BUTTON2, buf, &c);
pos = FontStrMask(buf, pos, c);
FontStr(" to unpause", pos);
}
else
{
DrawMissionState(hud);
}
}
static void DrawMissionState(HUD *hud)
{
char s[50];
Expand Down
4 changes: 1 addition & 3 deletions src/cdogs/hud/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,4 @@ HUDDrawData HUDGetDrawData(void);

void HUDUpdate(HUD *hud, const int ms);

void HUDDraw(
HUD *hud, const input_device_e pausingDevice,
const bool controllerUnplugged, const int numViews);
void HUDDraw(HUD *hud, const int numViews, const bool paused);
94 changes: 17 additions & 77 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ static void PlayerSpecialCommands(TActor *actor, const int cmd)
}
}
else if (
!Button2(actor->lastCmd) && Button2(cmd) &&
!actor->specialCmdDir && !actor->CanPickupSpecial &&
!Button2(actor->lastCmd) && Button2(cmd) && !actor->specialCmdDir &&
!actor->CanPickupSpecial &&
!(ConfigGetEnum(&gConfig, "Game.SwitchMoveStyle") ==
SWITCHMOVE_SLIDE &&
CMD_HAS_DIRECTION(cmd)))
Expand Down Expand Up @@ -216,6 +216,8 @@ static void RunGameOnEnter(GameLoopData *data)
}
}

PauseMenuInit(&rData->pm, &gEventHandlers, &gGraphicsDevice);

rData->m->state = MISSION_STATE_WAITING;
rData->m->isDone = false;
rData->m->DoneCounter = 0;
Expand Down Expand Up @@ -264,6 +266,7 @@ static void RunGameOnExit(GameLoopData *data)
CA_FOREACH_END()
CArrayTerminate(&rData->ammoSpawners);
CameraTerminate(&rData->Camera);
PauseMenuTerminate(&rData->pm);

// Draw background
GrafxRedrawBackground(&gGraphicsDevice, rData->Camera.lastPosition);
Expand Down Expand Up @@ -311,17 +314,14 @@ static void RunGameInput(GameLoopData *data)
}
memset(rData->cmds, 0, sizeof rData->cmds);
int cmdAll = 0;
int idx = 0;
input_device_e pausingDevice = INPUT_DEVICE_UNSET;
input_device_e firstPausingDevice = INPUT_DEVICE_UNSET;
if (GetNumPlayers(PLAYER_ANY, false, true) == 0)
{
// If no players, allow default keyboard to control camera
rData->cmds[0] = GetKeyboardCmd(&gEventHandlers.keyboard, 0, false);
firstPausingDevice = INPUT_DEVICE_KEYBOARD;
}
else
{
int idx = 0;
for (int i = 0; i < (int)gPlayerDatas.size; i++, idx++)
{
const PlayerData *p = CArrayGet(&gPlayerDatas, i);
Expand All @@ -330,87 +330,29 @@ static void RunGameInput(GameLoopData *data)
idx--;
continue;
}
if (firstPausingDevice == INPUT_DEVICE_UNSET)
{
firstPausingDevice = p->inputDevice;
}
rData->cmds[idx] = GetGameCmd(&gEventHandlers, p);
cmdAll |= rData->cmds[idx];

// Only allow the first player to escape
// Use keypress otherwise the player will quit immediately
if (idx == 0 && (rData->cmds[idx] & CMD_ESC) &&
!(rData->lastCmds[idx] & CMD_ESC))
{
pausingDevice = p->inputDevice;
}
}
}
if (KeyIsPressed(&gEventHandlers.keyboard, SDL_SCANCODE_ESCAPE))
{
pausingDevice = INPUT_DEVICE_KEYBOARD;
}

// Check if any controllers are unplugged
rData->controllerUnplugged = false;
CA_FOREACH(const PlayerData, p, gPlayerDatas)
if (p->inputDevice == INPUT_DEVICE_UNSET && p->IsLocal)
{
rData->controllerUnplugged = true;
break;
}
CA_FOREACH_END()

// If in Superhot(tm) Mode, don't update unless there was an input in this
// or the last frame
data->SkipNextFrame = data->SuperhotMode && !lastCmdAll;

// Check if:
// - escape was pressed, or
// - window lost focus
// - controller unplugged
// If the game is paused, unpause if a button is released
// If the game was not paused, enter pause mode
// If the game was paused and escape was pressed, exit the game
if (rData->pausingDevice != INPUT_DEVICE_UNSET && AnyButton(lastCmdAll) &&
!AnyButton(cmdAll))
// Update and check if we want to quit
if (PauseMenuUpdate(&rData->pm, rData->cmds, rData->lastCmds))
{
rData->pausingDevice = INPUT_DEVICE_UNSET;
// Need to unpause to process the quit
data->SkipNextFrame = true;
}
else if (rData->controllerUnplugged || gEventHandlers.HasLostFocus)

// Don't show map if pause menu is shown
if (PauseMenuIsShown(&rData->pm))
{
// Pause the game
rData->pausingDevice = firstPausingDevice;
rData->isMap = false;
SoundPlay(&gSoundDevice, StrSound("menu_error"));
}
else if (pausingDevice != INPUT_DEVICE_UNSET)
{
if (rData->pausingDevice != INPUT_DEVICE_UNSET)
{
// Already paused; exit
GameEvent e = GameEventNew(GAME_EVENT_MISSION_END);
e.u.MissionEnd.IsQuit = true;
GameEventsEnqueue(&gGameEvents, e);
// Need to unpause to process the quit
rData->pausingDevice = INPUT_DEVICE_UNSET;
rData->controllerUnplugged = false;
// Don't skip exiting the game
data->SkipNextFrame = false;
SoundPlay(&gSoundDevice, StrSound("menu_back"));
}
else
{
// Pause the game
rData->pausingDevice = pausingDevice;
rData->isMap = false;
SoundPlay(&gSoundDevice, StrSound("menu_back"));
}
}

const bool paused = rData->pausingDevice != INPUT_DEVICE_UNSET ||
rData->controllerUnplugged;
if (!paused)
if (!PauseMenuIsShown(&rData->pm))
{
// Check if automap key is pressed by any player
// Toggle
Expand Down Expand Up @@ -464,8 +406,7 @@ static GameLoopResult RunGameUpdate(GameLoopData *data, LoopRunner *l)
// If we're not hosting a net game,
// don't update if the game has paused or has automap shown
// Important: don't consider paused if we are trying to quit
const bool paused = rData->pausingDevice != INPUT_DEVICE_UNSET ||
rData->controllerUnplugged || rData->isMap;
const bool paused = PauseMenuIsShown(&rData->pm) || rData->isMap;
if (!gCampaign.IsClient && !ConfigGetBool(&gConfig, "StartServer") &&
paused && !gEventHandlers.HasQuit)
{
Expand Down Expand Up @@ -700,9 +641,8 @@ static void RunGameDraw(GameLoopData *data)
// Draw HUD layer
BlitClearBuf(&gGraphicsDevice);
CameraDrawMode(&rData->Camera);
HUDDraw(
&rData->Camera.HUD, rData->pausingDevice, rData->controllerUnplugged,
rData->Camera.NumViews);
HUDDraw(&rData->Camera.HUD, rData->Camera.NumViews, PauseMenuIsShown(&rData->pm));
PauseMenuDraw(&rData->pm);
// Draw automap if enabled
if (rData->isMap)
{
Expand Down
7 changes: 3 additions & 4 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2015, 2017, 2020-2021 Cong Xu
Copyright (c) 2015, 2017, 2020-2021, 2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -58,6 +58,7 @@
#include <cdogs/powerup.h>

#include "game_loop.h"
#include "pause_menu.h"

GameLoopData *RunGame(Campaign *co, struct MissionOptions *m, Map *map);

Expand All @@ -67,9 +68,7 @@ typedef struct
struct MissionOptions *m;
Map *map;
Camera Camera;
// TODO: turn the following into a screen system?
input_device_e pausingDevice; // INPUT_DEVICE_UNSET if not paused
bool controllerUnplugged;
PauseMenu pm;
bool isMap;
int cmds[MAX_LOCAL_PLAYERS];
int lastCmds[MAX_LOCAL_PLAYERS];
Expand Down
Loading

0 comments on commit f371790

Please sign in to comment.