Debian Codegraph @

src/pause_menu.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2024 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "pause_menu.h"
29
30 #include <cdogs/draw/drawtools.h>
31 #include <cdogs/font.h>
32
33 void PauseMenuInit(
34 PauseMenu *pm, EventHandlers *handlers, GraphicsDevice *g,
35 void (*gfxChangeCallback)(void *, const bool), void *gfxChangeData)
36 {
37 memset(pm, 0, sizeof *pm);
38 MenuSystemInit(&pm->ms, handlers, g, svec2i_zero(), g->cachedConfig.Res);
39 pm->ms.current = pm->ms.root =
40 MenuCreateNormal("", "", MENU_TYPE_NORMAL, 0);
41 // A bit counterintuitive, but QUIT means we want to quit this menu, also
42 // this item will get auto selected when pressing escape
43 MenuAddSubmenu(pm->ms.root, MenuCreate("Resume", MENU_TYPE_QUIT));
44 MenuAddSubmenu(pm->ms.root, MenuCreateSeparator(""));
45 pm->oData.config = &gConfig;
46 pm->oData.gfxChangeCallback = gfxChangeCallback;
47 pm->oData.gfxChangeData = gfxChangeData;
48 pm->oData.ms = &pm->ms;
49 MenuAddSubmenu(pm->ms.root, MenuCreateOptions("Options...", &pm->oData));
50 MenuAddSubmenu(pm->ms.root, MenuCreateSeparator(""));
51 MenuAddSubmenu(pm->ms.root, MenuCreateReturn("Quit", 0));
52 MenuAddExitType(&pm->ms, MENU_TYPE_RETURN);
53 pm->ms.allowAborts = true;
54 pm->handlers = handlers;
55 pm->g = g;
56 pm->pausingDevice = INPUT_DEVICE_UNSET;
57 }
58 void PauseMenuTerminate(PauseMenu *pm)
59 {
60 MenuSystemTerminate(&pm->ms);
61 }
62
63 bool PauseMenuUpdate(
64 PauseMenu *pm, const int cmds[MAX_LOCAL_PLAYERS],
65 const int lastCmds[MAX_LOCAL_PLAYERS])
66 {
67 input_device_e pausingDevice = INPUT_DEVICE_UNSET;
68 input_device_e firstPausingDevice = INPUT_DEVICE_UNSET;
69 if (GetNumPlayers(PLAYER_ANY, false, true) == 0)
70 {
71 // If no players, allow default keyboard
72 firstPausingDevice = INPUT_DEVICE_KEYBOARD;
73 }
74 else
75 {
76 int idx = 0;
77 for (int i = 0; i < (int)gPlayerDatas.size; i++, idx++)
78 {
79 const PlayerData *p = CArrayGet(&gPlayerDatas, i);
80 if (!p->IsLocal)
81 {
82 idx--;
83 continue;
84 }
85 if (firstPausingDevice == INPUT_DEVICE_UNSET)
86 {
87 firstPausingDevice = p->inputDevice;
88 }
89
90 // Only allow the first player to escape
91 // Use keypress otherwise the player will quit immediately
92 if (idx == 0 && (cmds[idx] & CMD_ESC) &&
93 !(lastCmds[idx] & CMD_ESC))
94 {
95 pausingDevice = p->inputDevice;
96 }
97 }
98 }
99 if (KeyIsPressed(&gEventHandlers.keyboard, SDL_SCANCODE_ESCAPE))
100 {
101 pausingDevice = INPUT_DEVICE_KEYBOARD;
102 }
103
104 // Check if any controllers are unplugged
105 pm->controllerUnplugged = false;
106 CA_FOREACH(const PlayerData, p, gPlayerDatas)
107 if (p->inputDevice == INPUT_DEVICE_UNSET && p->IsLocal)
108 {
109 pm->controllerUnplugged = true;
110 break;
111 }
112 CA_FOREACH_END()
113
114 // Check if:
115 // - escape was pressed, or
116 // - window lost focus
117 // - controller unplugged
118 // If the game is paused, update the pause menu
119 // If the game was not paused, enter pause mode
120 // If the game was paused and escape was pressed, exit the game
121 if (pm->pausingDevice != INPUT_DEVICE_UNSET)
122 {
123 const GameLoopResult result = MenuUpdate(&pm->ms);
124 if (result == UPDATE_RESULT_OK || pm->ms.current->type == MENU_TYPE_QUIT)
125 {
126 // Unpause
127 pm->pausingDevice = INPUT_DEVICE_UNSET;
128 if (pm->ms.current->type == MENU_TYPE_RETURN)
129 {
130 // Quit
131 GameEvent e = GameEventNew(GAME_EVENT_MISSION_END);
132 e.u.MissionEnd.IsQuit = true;
133 GameEventsEnqueue(&gGameEvents, e);
134 // Need to unpause to process the quit
135 pm->pausingDevice = INPUT_DEVICE_UNSET;
136 pm->controllerUnplugged = false;
137 SoundPlay(&gSoundDevice, StrSound("menu_back"));
138 // Return true to say we want to quit
139 return true;
140 }
141 MenuReset(&pm->ms);
142 }
143 }
144 else if (pm->controllerUnplugged || gEventHandlers.HasLostFocus)
145 {
146 // Pause the game
147 pm->pausingDevice = firstPausingDevice;
148 SoundPlay(&gSoundDevice, StrSound("menu_error"));
149 }
150 else if (pausingDevice != INPUT_DEVICE_UNSET)
151 {
152 // Pause the game
153 pm->pausingDevice = pausingDevice;
154 SoundPlay(&gSoundDevice, StrSound("menu_back"));
155 }
156 return false;
157 }
158 void PauseMenuDraw(const PauseMenu *pm)
159 {
160 if (!PauseMenuIsShown(pm))
161 {
162 return;
163 }
164
165 // Draw a background overlay
166 color_t overlay = colorBlack;
167 overlay.a = 128;
168 DrawRectangle(
169 pm->g, svec2i_zero(), pm->g->cachedConfig.Res, overlay, true);
170
171 if (pm->controllerUnplugged)
172 {
173 struct vec2i pos = svec2i_scale_divide(
174 svec2i_subtract(
175 pm->g->cachedConfig.Res,
176 FontStrSize(ARROW_LEFT
177 "Paused" ARROW_RIGHT
178 "\nFoobar\nPlease reconnect controller")),
179 2);
180 const int x = pos.x;
181 FontStr(ARROW_LEFT "Paused" ARROW_RIGHT, pos);
182
183 pos.y += FontH();
184 pos = FontStr("Press ", pos);
185 char buf[256];
186 color_t c = colorWhite;
187 InputGetButtonNameColor(pm->pausingDevice, 0, CMD_ESC, buf, &c);
188 pos = FontStrMask(buf, pos, c);
189 FontStr(" to quit", pos);
190
191 pos.x = x;
192 pos.y += FontH();
193 FontStr("Please reconnect controller", pos);
194 }
195 else if (pm->pausingDevice != INPUT_DEVICE_UNSET)
196 {
197 MenuDraw(&pm->ms);
198 }
199 }
200
201 bool PauseMenuIsShown(const PauseMenu *pm)
202 {
203 return pm->pausingDevice != INPUT_DEVICE_UNSET || pm->controllerUnplugged;
204 }