Debian Codegraph @

src/cdogs/hud/hud.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2017, 2019-2020, 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 "hud.h"
29
30 #include <assert.h>
31 #include <math.h>
32
33 #include "actors.h"
34 #include "automap.h"
35 #include "draw/draw.h"
36 #include "draw/draw_actor.h"
37 #include "draw/drawtools.h"
38 #include "events.h"
39 #include "font.h"
40 #include "game_events.h"
41 #include "hud_defs.h"
42 #include "mission.h"
43 #include "pic_manager.h"
44 #include "player.h"
45 #include "player_hud.h"
46
47 void HUDInit(HUD *hud, GraphicsDevice *device, struct MissionOptions *mission)
48 {
49 memset(hud, 0, sizeof *hud);
50 hud->mission = mission;
51 strcpy(hud->message, "");
52 hud->messageTicks = 0;
53 hud->device = device;
54 FPSCounterInit(&hud->fpsCounter);
55 WallClockInit(&hud->clock);
56 HUDNumPopupsInit(&hud->numPopups, mission);
57 for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
58 {
59 HUDPlayerInit(&hud->hudPlayers[i]);
60 }
61 hud->showExit = false;
62 }
63 void HUDTerminate(HUD *hud)
64 {
65 HUDNumPopupsTerminate(&hud->numPopups);
66 }
67
68 void HUDDisplayMessage(HUD *hud, const char *msg, int ticks)
69 {
70 strcpy(hud->message, msg);
71 hud->messageTicks = ticks;
72 }
73
74 void HUDUpdate(HUD *hud, const int ms)
75 {
76 if (hud->messageTicks >= 0)
77 {
78 hud->messageTicks -= ms;
79 if (hud->messageTicks < 0)
80 {
81 hud->messageTicks = 0;
82 }
83 }
84 FPSCounterUpdate(&hud->fpsCounter, ms);
85 WallClockUpdate(&hud->clock, ms);
86 HUDPopupsUpdate(&hud->numPopups, ms);
87
88 for (int i = 0; i < hud->DrawData.NumScreens; i++)
89 {
90 const PlayerData *p = hud->DrawData.Players[i];
91 HUDPlayerUpdate(&hud->hudPlayers[i], p, ms);
92 }
93 }
94
95 HUDDrawData HUDGetDrawData(void)
96 {
97 HUDDrawData drawData;
98 memset(&drawData, 0, sizeof drawData);
99 CA_FOREACH(const PlayerData, p, gPlayerDatas)
100 if (!IsPlayerScreen(p))
101 {
102 continue;
103 }
104 drawData.Players[drawData.NumScreens] = p;
105 drawData.NumScreens++;
106 }
107 return drawData;
108 }
109
110 static void DrawSharedRadar(GraphicsDevice *device, bool showExit)
111 {
112 int w = device->cachedConfig.Res.x;
113 struct vec2i pos = svec2i(w / 2 - AUTOMAP_SIZE / 2, AUTOMAP_PADDING);
114 const struct vec2i playerMidpoint = Vec2ToTile(PlayersGetMidpoint());
115 AutomapDrawRegion(
116 device->gameWindow.renderer, &gMap, pos,
117 svec2i(AUTOMAP_SIZE, AUTOMAP_SIZE), playerMidpoint, AUTOMAP_FLAGS_MASK,
118 showExit);
119 }
120
121 static void DrawPlayerAreas(HUD *hud, const int numViews);
122 static void DrawDeathmatchScores(HUD *hud);
123 static void DrawMissionState(HUD *hud);
124 static void DrawHUDMessage(HUD *hud);
125 static void DrawKeycards(HUD *hud);
126 static void DrawMissionTime(HUD *hud);
127 static void DrawObjectiveCounts(HUD *hud);
128 void HUDDraw(HUD *hud, const int numViews, const bool paused)
129 {
130 if (ConfigGetBool(&gConfig, "Graphics.ShowHUD"))
131 {
132 DrawPlayerAreas(hud, numViews);
133
134 DrawDeathmatchScores(hud);
135 DrawHUDMessage(hud);
136 if (ConfigGetBool(&gConfig, "Interface.ShowFPS"))
137 {
138 FPSCounterDraw(&hud->fpsCounter);
139 }
140 if (ConfigGetBool(&gConfig, "Interface.ShowTime"))
141 {
142 WallClockDraw(&hud->clock);
143 }
144 DrawKeycards(hud);
145 DrawMissionTime(hud);
146 if (HasObjectives(gCampaign.Entry.Mode))
147 {
148 DrawObjectiveCounts(hud);
149 }
150 }
151
152 if (!paused)
153 {
154 DrawMissionState(hud);
155 }
156 }
157
158 static void DrawPlayerAreas(HUD *hud, const int numViews)
159 {
160 int flags = 0;
161
162 Rect2i r = Rect2iNew(svec2i_zero(), hud->device->cachedConfig.Res);
163 if (hud->DrawData.NumScreens <= 1)
164 {
165 // Do nothing
166 }
167 else if (hud->DrawData.NumScreens == 2)
168 {
169 r.Size.x /= 2;
170 }
171 else if (hud->DrawData.NumScreens == 3 || hud->DrawData.NumScreens == 4)
172 {
173 r.Size.x /= 2;
174 r.Size.y /= 2;
175 }
176 else
177 {
178 CASSERT(false, "not implemented");
179 }
180
181 if (hud->DrawData.NumScreens <= 1)
182 {
183 // Do nothing
184 }
185 else if (
186 hud->DrawData.NumScreens > 1 &&
187 ConfigGetEnum(&gConfig, "Interface.Splitscreen") == SPLITSCREEN_NEVER)
188 {
189 flags |= HUDFLAGS_SHARE_SCREEN;
190 }
191 else if (hud->DrawData.NumScreens == 2)
192 {
193 flags |= HUDFLAGS_HALF_SCREEN;
194 }
195 else if (hud->DrawData.NumScreens == 3 || hud->DrawData.NumScreens == 4)
196 {
197 flags |= HUDFLAGS_QUARTER_SCREEN;
198 }
199 else
200 {
201 CASSERT(false, "not implemented");
202 }
203
204 for (int i = 0; i < hud->DrawData.NumScreens; i++)
205 {
206 const PlayerData *p = hud->DrawData.Players[i];
207 int drawFlags = flags;
208 r.Pos = svec2i_zero();
209 if (i & 1)
210 {
211 r.Pos.x = r.Size.x;
212 drawFlags |= HUDFLAGS_PLACE_RIGHT;
213 }
214 if (i >= 2)
215 {
216 r.Pos.y = r.Size.y;
217 drawFlags |= HUDFLAGS_PLACE_BOTTOM;
218 }
219 DrawPlayerHUD(hud, p, drawFlags, i, r, numViews);
220 }
221
222 // Only draw radar once if shared
223 if (ConfigGetBool(&gConfig, "Interface.ShowHUDMap") &&
224 (flags & HUDFLAGS_SHARE_SCREEN) &&
225 IsAutoMapEnabled(gCampaign.Entry.Mode))
226 {
227 DrawSharedRadar(hud->device, hud->showExit);
228 }
229 }
230
231 static void DrawDeathmatchScores(HUD *hud)
232 {
233 // Only draw deathmatch scores if single screen and non-local players exist
234 if (gCampaign.Entry.Mode != GAME_MODE_DEATHMATCH ||
235 hud->DrawData.NumScreens != 1 ||
236 GetNumPlayers(PLAYER_ANY, false, false) <= 1)
237 {
238 return;
239 }
240 FontOpts opts = FontOptsNew();
241 opts.Area = hud->device->cachedConfig.Res;
242 opts.HAlign = ALIGN_END;
243 opts.Mask = colorPurple;
244 const int nameColumn = 45;
245 const int livesColumn = 25;
246 const int killsColumn = 5;
247 int y = 5;
248 opts.Pad.x = nameColumn;
249 FontStrOpt("Player", svec2i(0, y), opts);
250 opts.Pad.x = livesColumn;
251 FontStrOpt("Lives", svec2i(0, y), opts);
252 opts.Pad.x = killsColumn;
253 FontStrOpt("Kills", svec2i(0, y), opts);
254 y += FontH();
255 // Find the player(s) with the most lives and kills
256 int maxLives = 0;
257 int maxKills = 0;
258 CA_FOREACH(const PlayerData, p, gPlayerDatas)
259 maxLives = MAX(maxLives, p->Lives);
260 maxKills = MAX(maxKills, (int)p->Stats.Kills);
261 CA_FOREACH_END()
262 CA_FOREACH(const PlayerData, p, gPlayerDatas)
263 // Player name; red if dead
264 opts.Mask = p->Lives > 0 ? colorWhite : colorRed;
265 opts.Pad.x = nameColumn;
266 FontStrOpt(p->name, svec2i(0, y), opts);
267
268 // lives; cyan if most lives
269 opts.Mask = p->Lives == maxLives ? colorCyan : colorWhite;
270 opts.Pad.x = livesColumn;
271 char buf[32];
272 sprintf(buf, "%d", p->Lives);
273 FontStrOpt(buf, svec2i(0, y), opts);
274
275 // kills; cyan if most kills
276 opts.Mask = (int)p->Stats.Kills == maxKills ? colorCyan : colorWhite;
277 opts.Pad.x = killsColumn;
278 sprintf(buf, "%d", p->Stats.Kills);
279 FontStrOpt(buf, svec2i(0, y), opts);
280 y += FontH();
281 CA_FOREACH_END()
282 }
283
284 static void DrawMissionState(HUD *hud)
285 {
286 char s[50];
287 const int numPlayersAlive =
288 GetNumPlayers(PLAYER_ALIVE_OR_DYING, false, false);
289
290 switch (hud->mission->state)
291 {
292 case MISSION_STATE_WAITING:
293 FontStrCenter("Waiting for players...");
294 break;
295 case MISSION_STATE_PLAY:
296 if (numPlayersAlive == 0 && AreAllPlayersDeadAndNoLives())
297 {
298 if (gPlayerDatas.size == 0)
299 {
300 FontStrCenter("Waiting for players...");
301 }
302 else if (!IsPVP(gCampaign.Entry.Mode))
303 {
304 FontStrCenter("Game Over!");
305 }
306 else
307 {
308 FontStrCenter("All Kill!");
309 }
310 }
311 else if (MissionNeedsMoreRescuesInExit(&gMission))
312 {
313 FontStrCenter("More rescues needed");
314 }
315 break;
316 case MISSION_STATE_PICKUP: {
317 int timeLeft = gMission.pickupTime + PICKUP_LIMIT - gMission.time;
318 sprintf(
319 s, "Pickup in %d seconds\n",
320 (timeLeft + (FPS_FRAMELIMIT - 1)) / FPS_FRAMELIMIT);
321 FontStrCenter(s);
322 }
323 break;
324 default:
325 CASSERT(false, "unknown mission state");
326 break;
327 }
328 }
329
330 static void DrawHUDMessage(HUD *hud)
331 {
332 if (hud->messageTicks > 0 || hud->messageTicks == -1)
333 {
334 // Draw the message centered, and just below the automap
335 struct vec2i pos = svec2i(
336 (hud->device->cachedConfig.Res.x - FontStrW(hud->message)) / 2,
337 AUTOMAP_SIZE + AUTOMAP_PADDING + AUTOMAP_PADDING);
338 const HSV tint = {-1.0, 1.0, Pulse256(hud->mission->time) / 256.0};
339 const color_t mask = ColorTint(colorCyan, tint);
340 FontStrMask(hud->message, pos, mask);
341 }
342 }
343
344 static void DrawKeycards(HUD *hud)
345 {
346 int keyFlags[] = {
347 FLAGS_KEYCARD_YELLOW, FLAGS_KEYCARD_GREEN, FLAGS_KEYCARD_BLUE,
348 FLAGS_KEYCARD_RED};
349 int i;
350 int xOffset = -30;
351 int xOffsetIncr = 20;
352 int yOffset = 20;
353 for (i = 0; i < 4; i++)
354 {
355 if (hud->mission->KeyFlags & keyFlags[i])
356 {
357 CPicDraw(
358 hud->device,
359 &KeyPickupClass(hud->mission->missionData->KeyStyle, i)->Pic,
360 svec2i(CenterX(8) - xOffset, yOffset), NULL);
361 }
362 xOffset += xOffsetIncr;
363 }
364 }
365
366 static void DrawMissionTime(HUD *hud)
367 {
368 char s[50];
369 // Draw elapsed mission time as MM:SS
370 int missionTimeSeconds = gMission.time / FPS_FRAMELIMIT;
371 sprintf(s, "%d:%02d", missionTimeSeconds / 60, missionTimeSeconds % 60);
372
373 FontOpts opts = FontOptsNew();
374 opts.HAlign = ALIGN_CENTER;
375 opts.Area = hud->device->cachedConfig.Res;
376 opts.Pad.y = AUTOMAP_PADDING;
377 FontStrOpt(s, svec2i_zero(), opts);
378 }
379
380 static void DrawObjectiveCounts(HUD *hud)
381 {
382 int x = 45;
383 int y = hud->device->cachedConfig.Res.y - 22;
384 CA_FOREACH(const Objective, o, hud->mission->missionData->Objectives)
385 // Don't draw anything for optional objectives
386 if (!ObjectiveIsRequired(o))
387 {
388 continue;
389 }
390
391 // Objective color dot
392 DrawRectangle(
393 hud->device, svec2i(x, y + 3), svec2i(2, 2), o->color, false);
394
395 x += 5;
396 char s[32];
397 const int itemsLeft = o->Required - o->done;
398 if (itemsLeft > 0)
399 {
400 if (!(o->Flags & OBJECTIVE_UNKNOWNCOUNT))
401 {
402 sprintf(s, "%s: %d", ObjectiveTypeStr(o->Type), itemsLeft);
403 }
404 else
405 {
406 sprintf(s, "%s: ?", ObjectiveTypeStr(o->Type));
407 }
408 }
409 else
410 {
411 strcpy(s, "Done");
412 }
413 FontStr(s, svec2i(x, y));
414
415 HUDNumPopupsDrawObjective(
416 &hud->numPopups, _ca_index, svec2i(x + FontStrW(s) - 8, y));
417
418 x += 40;
419 CA_FOREACH_END()
420 }