Debian Codegraph @

src/cdogs/hud/player_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-2021, 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 "player_hud.h"
29
30 #include "actors.h"
31 #include "automap.h"
32 #include "draw/draw_actor.h"
33 #include "draw/nine_slice.h"
34 #include "hud/gauge.h"
35 #include "hud_defs.h"
36
37 #define SCORE_WIDTH 26
38 #define GRENADES_WIDTH 30
39 #define AMMO_WIDTH 27
40 #define GUN_ICON_WIDTH 14
41 #define PLAYER_ICON_WIDTH 20
42 #define BAR_HEIGHT 13
43 #define GUN_CHANGE_FLASH_MS 500
44 #define FLASH_PERIOD_MS 100
45
46 void HUDPlayerInit(HUDPlayer *h)
47 {
48 memset(h, 0, sizeof *h);
49 HealthGaugeInit(&h->healthGauge);
50 }
51
52 static const TActor *GetActor(const PlayerData *p)
53 {
54 if (!IsPlayerAlive(p))
55 {
56 return NULL;
57 }
58 const TActor *a = ActorGetByUID(p->ActorUID);
59 if (a != NULL)
60 {
61 if (a->vehicleUID != -1)
62 {
63 // Draw HUD for vehicle if piloting
64 a = ActorGetByUID(a->vehicleUID);
65 }
66 }
67 return a;
68 }
69
70 void HUDPlayerUpdate(HUDPlayer *h, const PlayerData *p, const int ms)
71 {
72 const TActor *a = GetActor(p);
73 if (a == NULL)
74 return;
75
76 // Health
77 HealthGaugeUpdate(&h->healthGauge, a, ms);
78
79 // Initialise guns
80 const Weapon *gun = ACTOR_GET_GUN(a);
81 const WeaponClass *gunWc = gun->Gun;
82 if (h->gunWc == NULL)
83 {
84 h->gunWc = gunWc;
85 }
86 h->gunChangeMs = MAX(0, h->gunChangeMs - ms);
87 if (h->gunWc != gunWc)
88 {
89 // Gun has changed
90 h->gunWc = gunWc;
91 h->gunChangeMs = GUN_CHANGE_FLASH_MS;
92 }
93 const Weapon *grenade = ACTOR_GET_GRENADE(a);
94 const WeaponClass *grenadeWc = grenade->Gun;
95 const int grenadeAmount = ActorWeaponGetAmmo(a, grenadeWc, 0);
96 if (h->grenadeWc == NULL)
97 {
98 h->grenadeWc = grenadeWc;
99 h->grenadeAmount = grenadeAmount;
100 }
101 h->grenadeChangeMs = MAX(0, h->grenadeChangeMs - ms);
102 if (h->grenadeWc != grenadeWc || h->grenadeAmount != grenadeAmount)
103 {
104 // Grenade has changed
105 h->grenadeWc = grenadeWc;
106 h->grenadeAmount = grenadeAmount;
107 h->grenadeChangeMs = GUN_CHANGE_FLASH_MS;
108 }
109 }
110
111 static void DrawPlayerStatus(
112 HUD *hud, const PlayerData *data, const TActor *p, const int flags,
113 const HUDPlayer *h, const Rect2i r);
114 static void DrawPlayerObjectiveCompass(
115 const HUD *hud, const TActor *a, const int hudPlayerIndex,
116 const int numViews);
117 void DrawPlayerHUD(
118 HUD *hud, const PlayerData *p, const int drawFlags,
119 const int hudPlayerIndex, const Rect2i r, const int numViews)
120 {
121 const TActor *a = GetActor(p);
122 DrawPlayerStatus(
123 hud, p, a, drawFlags, &hud->hudPlayers[hudPlayerIndex], r);
124 HUDNumPopupsDrawPlayer(&hud->numPopups, hudPlayerIndex, drawFlags, r);
125 DrawPlayerObjectiveCompass(hud, a, hudPlayerIndex, numViews);
126 }
127
128 static void DrawPlayerIcon(
129 const TActor *a, GraphicsDevice *g, const PicManager *pm, const int flags,
130 const SDL_RendererFlip flip, const color_t mask);
131 static void DrawScore(
132 GraphicsDevice *g, const PicManager *pm, const TActor *a, const int score,
133 const int flags, const Rect2i r, const color_t mask);
134 static void DrawLives(
135 const GraphicsDevice *device, const PlayerData *player,
136 const FontAlign hAlign, const FontAlign vAlign);
137 static void DrawWeaponStatus(
138 GraphicsDevice *g, const PicManager *pm, const TActor *actor,
139 const int flags, const Rect2i r, const color_t mask);
140 static void DrawGunIcons(
141 GraphicsDevice *g, const TActor *actor, const int flags,
142 const HUDPlayer *h, const Rect2i r);
143 static void DrawGrenadeStatus(
144 GraphicsDevice *g, const TActor *a, const int flags, const HUDPlayer *h,
145 const Rect2i r);
146 static void DrawRadar(
147 GraphicsDevice *device, const TActor *p, const int flags,
148 const bool showExit);
149 static void DrawHealth(
150 GraphicsDevice *g, const TActor *a, const int flags, const HUDPlayer *h,
151 const Rect2i r, const color_t mask);
152 // Draw player's score, health etc.
153 static void DrawPlayerStatus(
154 HUD *hud, const PlayerData *data, const TActor *p, const int flags,
155 const HUDPlayer *h, const Rect2i r)
156 {
157 const color_t mask = data->Char.Colors.Body;
158 SDL_RendererFlip flip = SDL_FLIP_NONE;
159 if (flags & HUDFLAGS_PLACE_RIGHT)
160 {
161 flip |= SDL_FLIP_HORIZONTAL;
162 }
163 if (flags & HUDFLAGS_PLACE_BOTTOM)
164 {
165 flip |= SDL_FLIP_VERTICAL;
166 }
167
168 DrawPlayerIcon(p, hud->device, &gPicManager, flags, flip, mask);
169
170 // Draw back bar, stretched across the screen
171 const Pic *backBar = PicManagerGetPic(&gPicManager, "hud/back_bar");
172 const int barWidth = r.Size.x - 22;
173 struct vec2i barPos = svec2i(22, 0);
174 if (flags & HUDFLAGS_PLACE_RIGHT)
175 {
176 barPos.x = r.Pos.x;
177 }
178 if (flags & HUDFLAGS_PLACE_BOTTOM)
179 {
180 barPos.y = hud->device->cachedConfig.Res.y - backBar->size.y;
181 }
182 Draw9Slice(
183 hud->device, backBar, Rect2iNew(barPos, svec2i(barWidth, BAR_HEIGHT)),
184 0, 0, 0, 0, true, mask, flip);
185
186 FontOpts opts = FontOptsNew();
187 if (flags & HUDFLAGS_PLACE_RIGHT)
188 {
189 opts.HAlign = ALIGN_END;
190 }
191 if (flags & HUDFLAGS_PLACE_BOTTOM)
192 {
193 opts.VAlign = ALIGN_END;
194 }
195 opts.Area = gGraphicsDevice.cachedConfig.Res;
196
197 DrawScore(hud->device, &gPicManager, p, data->Stats.Score, flags, r, mask);
198 DrawGrenadeStatus(hud->device, p, flags, h, r);
199 DrawWeaponStatus(hud->device, &gPicManager, p, flags, r, mask);
200 DrawGunIcons(hud->device, p, flags, h, r);
201 DrawLives(hud->device, data, opts.HAlign, opts.VAlign);
202 DrawHealth(hud->device, p, flags, h, r, mask);
203
204 // Name
205 opts.Pad = svec2i(23, 2);
206 if (flags & HUDFLAGS_PLACE_BOTTOM)
207 {
208 opts.Pad.y += 1;
209 }
210 char buf[256];
211 if (p != NULL && p->pilotUID != p->uid)
212 {
213 // Actor is a vehicle
214 sprintf(buf, "%s (%s)", data->name, ActorGetCharacter(p)->Class->Name);
215 }
216 else
217 {
218 strcpy(buf, data->name);
219 }
220 FontStrOpt(buf, svec2i_zero(), opts);
221
222 if (ConfigGetBool(&gConfig, "Interface.ShowHUDMap") &&
223 !(flags & HUDFLAGS_SHARE_SCREEN) &&
224 IsAutoMapEnabled(gCampaign.Entry.Mode))
225 {
226 DrawRadar(hud->device, p, flags, hud->showExit);
227 }
228 }
229 static void DrawPlayerIcon(
230 const TActor *a, GraphicsDevice *g, const PicManager *pm, const int flags,
231 const SDL_RendererFlip flip, const color_t mask)
232 {
233 const Pic *framePic = PicManagerGetPic(pm, "hud/player_frame");
234 const Pic *underlayPic = PicManagerGetPic(pm, "hud/player_frame_underlay");
235 struct vec2i picPos = svec2i_zero();
236 if (flags & HUDFLAGS_PLACE_RIGHT)
237 {
238 picPos.x = g->cachedConfig.Res.x - framePic->size.x;
239 }
240 if (flags & HUDFLAGS_PLACE_BOTTOM)
241 {
242 picPos.y = g->cachedConfig.Res.y - framePic->size.y;
243 }
244
245 PicRender(
246 underlayPic, g->gameWindow.renderer, picPos, colorWhite, 0,
247 svec2_one(), flip, Rect2iZero());
248 if (a)
249 {
250 ActorPics pics = GetCharacterPicsFromActor(a);
251 pics.ShadowMask = colorTransparent;
252 const struct vec2i offset = svec2i(-2, 10);
253 struct vec2i pos = svec2i(framePic->size.x / 2, framePic->size.y / 2);
254 if (flags & HUDFLAGS_PLACE_RIGHT)
255 {
256 pos.x = g->cachedConfig.Res.x - pos.x + 4;
257 }
258 if (flags & HUDFLAGS_PLACE_BOTTOM)
259 {
260 pos.y = g->cachedConfig.Res.y - pos.y + 2;
261 }
262 DrawActorPics(
263 &pics, svec2i_add(pos, offset),
264 Rect2iNew(svec2i(-12, -12), svec2i(12, 5)));
265 }
266 PicRender(
267 framePic, g->gameWindow.renderer, picPos, mask, 0, svec2_one(), flip,
268 Rect2iZero());
269 }
270 static void DrawScore(
271 GraphicsDevice *g, const PicManager *pm, const TActor *a, const int score,
272 const int flags, const Rect2i r, const color_t mask)
273 {
274 // Score aligned to the right
275 struct vec2i backPos = svec2i(r.Size.x - SCORE_WIDTH, 1);
276 if (flags & HUDFLAGS_PLACE_RIGHT)
277 {
278 backPos.x = r.Pos.x;
279 }
280 if (flags & HUDFLAGS_PLACE_BOTTOM)
281 {
282 backPos.y = g->cachedConfig.Res.y - BAR_HEIGHT + backPos.y;
283 }
284
285 HUDDrawGauge(g, pm, backPos, SCORE_WIDTH, 0, colorTransparent, mask);
286
287 if (a == NULL)
288 {
289 return;
290 }
291
292 char s[50];
293 if (IsScoreNeeded(gCampaign.Entry.Mode))
294 {
295 if (gCampaign.Setting.Ammo)
296 {
297 // Display money instead of ammo
298 sprintf(s, "$%d", score);
299 }
300 else
301 {
302 sprintf(s, "%d", score);
303 }
304 }
305 else
306 {
307 s[0] = 0;
308 }
309
310 const FontOpts opts = PlayerHUDGetScorePos(flags, r);
311 FontStrOpt(s, r.Pos, opts);
312 }
313 static void DrawLives(
314 const GraphicsDevice *device, const PlayerData *player,
315 const FontAlign hAlign, const FontAlign vAlign)
316 {
317 const struct vec2i pos = svec2i(PLAYER_ICON_WIDTH + 4, 11);
318 const int xStep = (hAlign == ALIGN_START ? 1 : -1) * 8;
319 const struct vec2i offset = svec2i(2, 5);
320 struct vec2i drawPos = svec2i_add(pos, offset);
321 if (hAlign == ALIGN_END)
322 {
323 const int w = device->cachedConfig.Res.x;
324 drawPos.x = w - drawPos.x - offset.x;
325 }
326 if (vAlign == ALIGN_END)
327 {
328 const int h = device->cachedConfig.Res.y;
329 drawPos.y = h - drawPos.y - 1;
330 }
331 for (int i = 0; i < player->Lives; i++)
332 {
333 DrawHead(
334 device->gameWindow.renderer, &player->Char, DIRECTION_DOWN,
335 drawPos);
336 drawPos.x += xStep;
337 }
338 }
339
340 static void DrawWeaponStatus(
341 GraphicsDevice *g, const PicManager *pm, const TActor *actor,
342 const int flags, const Rect2i r, const color_t mask)
343 {
344 // TODO: draw as gauge
345 const Pic *backPic = PicManagerGetPic(pm, "hud/gauge_small_back");
346 const struct vec2i backPicSize = svec2i(AMMO_WIDTH - 1, backPic->size.y);
347
348 // Aligned to the right
349 const int right = AMMO_WIDTH + SCORE_WIDTH + GRENADES_WIDTH;
350 struct vec2i pos = svec2i(r.Size.x - right, 2);
351 if (flags & HUDFLAGS_PLACE_RIGHT)
352 {
353 pos.x = r.Pos.x + SCORE_WIDTH + GRENADES_WIDTH;
354 }
355 if (flags & HUDFLAGS_PLACE_BOTTOM)
356 {
357 pos.y = g->cachedConfig.Res.y - BAR_HEIGHT + pos.y;
358 }
359
360 Draw9Slice(
361 g, backPic, Rect2iNew(pos, backPicSize), 0, 2, 0, 4, false, mask,
362 SDL_FLIP_NONE);
363
364 if (actor == NULL)
365 {
366 return;
367 }
368
369 const Weapon *weapon = ACTOR_GET_WEAPON(actor);
370 const WeaponClass *wc = weapon->Gun;
371 if (wc == NULL)
372 {
373 return;
374 }
375 // TODO: support drawing multi weapons in HUD
376 const int barrel = 0;
377 wc = WeaponClassGetBarrel(wc, barrel);
378
379 // Draw gauge and ammo counter if ammo used
380 if (gCampaign.Setting.Ammo && wc->u.Normal.AmmoId >= 0)
381 {
382 const Ammo *ammo = AmmoGetById(&gAmmo, wc->u.Normal.AmmoId);
383 const int amount = ActorWeaponGetAmmo(actor, wc, barrel);
384 FontOpts opts = FontOptsNew();
385 opts.Area = g->cachedConfig.Res;
386 opts.Pad = svec2i(pos.x + AMMO_WIDTH / 2, pos.y);
387
388 // Draw ammo level as inner fill
389 if (amount > 0)
390 {
391 const Pic *fillPic = PicManagerGetPic(pm, "hud/gauge_small_inner");
392 const int ammoMax = ammo->Max ? ammo->Max : amount;
393 const struct vec2i fillPicSize = svec2i(
394 MAX(1, (AMMO_WIDTH - 1) * amount / ammoMax), fillPic->size.y);
395 Draw9Slice(
396 g, fillPic, Rect2iNew(svec2i(pos.x, pos.y), fillPicSize), 1, 1,
397 1, 1, false, colorBlue, SDL_FLIP_NONE);
398 }
399
400 // Include ammo counter
401 char buf[128];
402 sprintf(buf, "%d", amount);
403
404 // If low / no ammo, draw text with different colours, flashing
405 const int fps = ConfigGetInt(&gConfig, "Game.FPS");
406 if (amount == 0)
407 {
408 // No ammo; fast flashing
409 const int pulsePeriod = fps / 4;
410 if ((gMission.time % pulsePeriod) < (pulsePeriod / 2))
411 {
412 opts.Mask = colorRed;
413 }
414 }
415 else if (AmmoIsLow(ammo, amount))
416 {
417 // Low ammo; slow flashing
418 const int pulsePeriod = fps / 2;
419 if ((gMission.time % pulsePeriod) < (pulsePeriod / 2))
420 {
421 opts.Mask = colorOrange;
422 }
423 }
424 FontStrOpt(buf, svec2i_zero(), opts);
425 }
426
427 // Draw reload balls
428 for (int i = 0; i < WeaponClassNumBarrels(weapon->Gun); i++)
429 {
430 if (weapon->barrels[i].lock > 0)
431 {
432 const int maxLock = weapon->Gun->Lock;
433 const Pic *ballPic = PicManagerGetPic(pm, "hud/gauge_small_ball");
434 const int ballAreaWidth = AMMO_WIDTH - 6;
435 const struct vec2i ballPos = svec2i(
436 pos.x +
437 MAX(0, ballAreaWidth *
438 (maxLock - weapon->barrels[i].lock) / maxLock),
439 pos.y + 1);
440 PicRender(
441 ballPic, g->gameWindow.renderer, ballPos, colorWhite, 0.0,
442 svec2_one(), SDL_FLIP_NONE, Rect2iZero());
443 }
444 }
445 }
446
447 static void DrawGunIcons(
448 GraphicsDevice *g, const TActor *actor, const int flags,
449 const HUDPlayer *h, const Rect2i r)
450 {
451 if (actor == NULL)
452 {
453 return;
454 }
455
456 const Weapon *weapon = ACTOR_GET_WEAPON(actor);
457 const WeaponClass *wc = weapon->Gun;
458 if (wc == NULL)
459 {
460 return;
461 }
462
463 // Aligned right
464 const int right =
465 GUN_ICON_WIDTH + AMMO_WIDTH + SCORE_WIDTH + GRENADES_WIDTH;
466 struct vec2i pos = svec2i(r.Size.x - right, 0);
467 if (flags & HUDFLAGS_PLACE_RIGHT)
468 {
469 pos.x = r.Pos.x + AMMO_WIDTH + SCORE_WIDTH + GRENADES_WIDTH;
470 }
471 if (flags & HUDFLAGS_PLACE_BOTTOM)
472 {
473 pos.y = g->cachedConfig.Res.y - BAR_HEIGHT + pos.y;
474 }
475
476 // Ammo icon
477 // TODO: support multi ammo guns
478 const int ammoId = WC_BARREL_ATTR(*wc, AmmoId, 0);
479 if (gCampaign.Setting.Ammo && ammoId >= 0)
480 {
481 const Ammo *ammo = AmmoGetById(&gAmmo, ammoId);
482 const struct vec2i ammoPos = svec2i_add(pos, svec2i(6, 5));
483 CPicDraw(g, &ammo->Pic, ammoPos, NULL);
484 }
485
486 // Gun icon
487 const color_t gunIconMask =
488 h->gunChangeMs > 0 &&
489 (h->gunChangeMs % FLASH_PERIOD_MS) < FLASH_PERIOD_MS / 2
490 ? colorDarkGray
491 : colorWhite;
492 PicRender(
493 wc->Icon, g->gameWindow.renderer, pos, gunIconMask, 0.0, svec2_one(),
494 SDL_FLIP_NONE, Rect2iZero());
495 }
496
497 static void DrawGrenadeIcons(
498 GraphicsDevice *g, const HUDPlayer *h, const Pic *icon,
499 const struct vec2i pos, const int width, const int amount);
500 static void DrawGrenadeStatus(
501 GraphicsDevice *g, const TActor *a, const int flags, const HUDPlayer *h,
502 const Rect2i r)
503 {
504 if (a == NULL)
505 {
506 return;
507 }
508
509 const Weapon *grenade = ACTOR_GET_GRENADE(a);
510 const WeaponClass *wc = grenade->Gun;
511 if (wc == NULL)
512 {
513 return;
514 }
515 CASSERT(wc->Type == GUNTYPE_GRENADE, "unexpected gun type");
516
517 // Aligned to the right
518 const int right = GRENADES_WIDTH + SCORE_WIDTH;
519 struct vec2i pos = svec2i(r.Size.x - right, 1);
520 if (flags & HUDFLAGS_PLACE_RIGHT)
521 {
522 pos.x = r.Pos.x + SCORE_WIDTH;
523 }
524 if (flags & HUDFLAGS_PLACE_BOTTOM)
525 {
526 pos.y = g->cachedConfig.Res.y - BAR_HEIGHT + pos.y;
527 }
528
529 // Draw number of grenade icons; if there are too many draw one with the
530 // amount as text
531 const bool useAmmo = gCampaign.Setting.Ammo && wc->u.Normal.AmmoId >= 0;
532 const int amount = useAmmo ? ActorWeaponGetAmmo(a, wc, 0) : -1;
533 const Pic *icon = wc->Icon;
534 if (useAmmo && amount > 0 && amount <= MAX_GRENADE_ICONS)
535 {
536 DrawGrenadeIcons(g, h, icon, pos, GRENADES_WIDTH, amount);
537 }
538 else
539 {
540 DrawGrenadeIcons(g, h, icon, pos, GRENADES_WIDTH, 1);
541 char buf[256];
542 if (amount >= 0)
543 {
544 sprintf(buf, " x %d", amount);
545 }
546 else
547 {
548 sprintf(buf, " x 99");
549 }
550 FontOpts opts = FontOptsNew();
551 opts.HAlign = ALIGN_END;
552 FontStrOpt(buf, svec2i(pos.x + GRENADES_WIDTH - 5, pos.y + 1), opts);
553 }
554 }
555 static void DrawGrenadeIcons(
556 GraphicsDevice *g, const HUDPlayer *h, const Pic *icon,
557 const struct vec2i pos, const int width, const int amount)
558 {
559 const int dx = width / MAX_GRENADE_ICONS;
560 const color_t grenadeIconMask =
561 h->grenadeChangeMs > 0 &&
562 (h->grenadeChangeMs % FLASH_PERIOD_MS) < FLASH_PERIOD_MS / 2
563 ? colorDarkGray
564 : colorWhite;
565 for (int i = 0; i < amount; i++)
566 {
567 const struct vec2i drawPos = svec2i(pos.x + i * dx, pos.y);
568 PicRender(
569 icon, g->gameWindow.renderer, drawPos, grenadeIconMask, 0.0,
570 svec2_one(), SDL_FLIP_NONE, Rect2iZero());
571 }
572 }
573
574 static void DrawRadar(
575 GraphicsDevice *device, const TActor *p, const int flags,
576 const bool showExit)
577 {
578 struct vec2i pos = svec2i_zero();
579 int w = device->cachedConfig.Res.x;
580 int h = device->cachedConfig.Res.y;
581
582 if (!p)
583 {
584 return;
585 }
586
587 // Possible map positions:
588 // top-right (player 1 only)
589 // top-left (player 2 only)
590 // bottom-right (player 3 only)
591 // bottom-left (player 4 only)
592 // top-left-of-middle (player 1 when two players)
593 // top-right-of-middle (player 2 when two players)
594 // bottom-left-of-middle (player 3)
595 // bottom-right-of-middle (player 4)
596 // top (shared screen)
597 if (flags & HUDFLAGS_HALF_SCREEN)
598 {
599 // two players
600 pos.y = AUTOMAP_PADDING;
601 if (flags & HUDFLAGS_PLACE_RIGHT)
602 {
603 // player 2
604 pos.x = w / 2 + AUTOMAP_PADDING;
605 }
606 else
607 {
608 // player 1
609 pos.x = w / 2 - AUTOMAP_SIZE - AUTOMAP_PADDING;
610 }
611 }
612 else if (flags & HUDFLAGS_QUARTER_SCREEN)
613 {
614 // four players
615 if (flags & HUDFLAGS_PLACE_RIGHT)
616 {
617 // player 2 or 4
618 pos.x = w / 2 + AUTOMAP_PADDING;
619 }
620 else
621 {
622 // player 1 or 3
623 pos.x = w / 2 - AUTOMAP_SIZE - AUTOMAP_PADDING;
624 }
625
626 if (flags & HUDFLAGS_PLACE_BOTTOM)
627 {
628 // player 3 or 4
629 pos.y = h - AUTOMAP_SIZE - AUTOMAP_PADDING;
630 }
631 else
632 {
633 // player 1 or 2
634 pos.y = AUTOMAP_PADDING;
635 }
636 }
637 else
638 {
639 // one player
640 if (flags & HUDFLAGS_PLACE_RIGHT)
641 {
642 // player 2 or 4
643 pos.x = AUTOMAP_PADDING;
644 }
645 else
646 {
647 // player 1 or 3
648 pos.x = w - AUTOMAP_SIZE - AUTOMAP_PADDING;
649 }
650
651 if (flags & HUDFLAGS_PLACE_BOTTOM)
652 {
653 // player 3 or 4
654 pos.y = h - AUTOMAP_SIZE - AUTOMAP_PADDING;
655 }
656 else
657 {
658 // player 1 or 2
659 pos.y = AUTOMAP_PADDING;
660 }
661 }
662
663 if (!svec2i_is_zero(pos))
664 {
665 const struct vec2i playerPos = Vec2ToTile(p->thing.Pos);
666 AutomapDrawRegion(
667 device->gameWindow.renderer, &gMap, pos,
668 svec2i(AUTOMAP_SIZE, AUTOMAP_SIZE), playerPos, AUTOMAP_FLAGS_MASK,
669 showExit);
670 }
671 }
672
673 static void DrawHealth(
674 GraphicsDevice *g, const TActor *a, const int flags, const HUDPlayer *h,
675 const Rect2i r, const color_t mask)
676 {
677 const int right =
678 SCORE_WIDTH + GRENADES_WIDTH + AMMO_WIDTH + GUN_ICON_WIDTH;
679 const int width = r.Size.x - right - PLAYER_ICON_WIDTH;
680
681 struct vec2i backPos = svec2i(PLAYER_ICON_WIDTH + 1, 1);
682 FontOpts opts = FontOptsNew();
683 opts.HAlign = ALIGN_END;
684 if (flags & HUDFLAGS_PLACE_RIGHT)
685 {
686 backPos.x = r.Pos.x + right;
687 opts.HAlign = ALIGN_START;
688 }
689 if (flags & HUDFLAGS_PLACE_BOTTOM)
690 {
691 backPos.y = g->cachedConfig.Res.y - 10 - 1;
692 }
693
694 HealthGaugeDraw(&h->healthGauge, g, a, backPos, width, opts, mask);
695 }
696
697 static void DrawObjectiveCompass(
698 GraphicsDevice *g, const struct vec2 playerPos, const Rect2i r,
699 const bool showExit);
700 static void DrawPlayerObjectiveCompass(
701 const HUD *hud, const TActor *a, const int hudPlayerIndex,
702 const int numViews)
703 {
704 // Draw objective compass
705 if (a == NULL)
706 {
707 return;
708 }
709 // deviding the screen by 4 and 2, which draws the arrows half way from the
710 // screen edge to be centered
711 Rect2i r = Rect2iNew(
712 svec2i(
713 hud->device->cachedConfig.Res.x / 4,
714 hud->device->cachedConfig.Res.y / 4),
715 svec2i(
716 hud->device->cachedConfig.Res.x / 2,
717 hud->device->cachedConfig.Res.y / 2));
718 if (hudPlayerIndex & 1)
719 {
720 r.Pos.x = r.Size.x;
721 }
722 if (hudPlayerIndex >= 2)
723 {
724 r.Pos.y = r.Size.y;
725 }
726 if (numViews == 1)
727 {
728 // No change
729 }
730 else if (numViews == 2)
731 {
732 r.Size.x /= 2;
733 }
734 else if (numViews == 3 || numViews == 4)
735 {
736 r.Size.x /= 2;
737 r.Size.y /= 2;
738 }
739 else
740 {
741 CASSERT(false, "not implemented");
742 }
743 DrawObjectiveCompass(hud->device, a->Pos, r, hud->showExit);
744 }
745
746 static void DrawCompassArrow(
747 GraphicsDevice *g, const Rect2i r, const struct vec2 pos,
748 const struct vec2 playerPos, const color_t mask, const char *label);
749 static void DrawObjectiveCompass(
750 GraphicsDevice *g, const struct vec2 playerPos, const Rect2i r,
751 const bool showExit)
752 {
753 // Draw exit positions
754 if (showExit)
755 {
756 CA_FOREACH(const Exit, e, gMap.exits)
757 if (e->Hidden)
758 continue;
759 DrawCompassArrow(
760 g, r, MapGetExitPos(&gMap, _ca_index), playerPos, colorGreen,
761 "Exit");
762 CA_FOREACH_END()
763 }
764
765 // Draw objectives
766 Map *map = &gMap;
767 struct vec2i tilePos;
768 for (tilePos.y = 0; tilePos.y < map->Size.y; tilePos.y++)
769 {
770 for (tilePos.x = 0; tilePos.x < map->Size.x; tilePos.x++)
771 {
772 Tile *tile = MapGetTile(map, tilePos);
773 CA_FOREACH(ThingId, tid, tile->things)
774 Thing *ti = ThingIdGetThing(tid);
775 if (!(ti->flags & THING_OBJECTIVE))
776 {
777 continue;
778 }
779 const int objective = ObjectiveFromThing(ti->flags);
780 const Objective *o =
781 CArrayGet(&gMission.missionData->Objectives, objective);
782 if (o->Flags & OBJECTIVE_HIDDEN)
783 {
784 continue;
785 }
786 if (!(o->Flags & OBJECTIVE_POSKNOWN) && !tile->isVisited)
787 {
788 continue;
789 }
790 DrawCompassArrow(g, r, ti->Pos, playerPos, o->color, NULL);
791 CA_FOREACH_END()
792 }
793 }
794 }
795
796 #define COMP_SATURATE_DIST 350
797 static void DrawCompassArrow(
798 GraphicsDevice *g, const Rect2i r, const struct vec2 pos,
799 const struct vec2 playerPos, const color_t mask, const char *label)
800 {
801 const struct vec2 compassV = svec2_subtract(
802 pos,
803 // Offset a little so the arrow points exactly at center of objective
804 svec2(playerPos.x - TILE_WIDTH / 2, playerPos.y));
805 // Don't draw if objective is on screen
806 if (fabsf(compassV.x) < r.Size.x && fabsf(compassV.y) < r.Size.y)
807 {
808 return;
809 }
810 // Saturate according to dist from screen edge
811 int xDist = (int)fabsf(compassV.x) - r.Size.x / 2;
812 int yDist = (int)fabsf(compassV.y) - r.Size.y / 2;
813 int lDist;
814 xDist > yDist ? lDist = xDist : (lDist = yDist);
815 HSV hsv = {
816 -1.0, 1.0,
817 2.0 - 1.5 * MIN(lDist, COMP_SATURATE_DIST) / COMP_SATURATE_DIST};
818 const color_t tintedMask = ColorTint(mask, hsv);
819 struct vec2i textPos = svec2i_zero();
820 struct vec2i drawPos = svec2i_zero();
821 const Pic *p = PicManagerGetPic(&gPicManager, "hud/arrow");
822 // Find which edge of screen is the best
823 bool draw = false;
824 if (compassV.x != 0)
825 {
826 double sx = r.Size.x / 2.0 / compassV.x;
827 int yInt = (int)floor(fabs(sx) * compassV.y + 0.5);
828 if (yInt >= -r.Size.y / 2 && yInt <= r.Size.y / 2)
829 {
830 draw = true;
831 // Intercepts either left or right side
832 if (compassV.x > 0)
833 {
834 // right edge
835 textPos =
836 svec2i(r.Pos.x + r.Size.x, r.Pos.y + r.Size.y / 2 + yInt);
837 drawPos =
838 svec2i(textPos.x - p->size.x, textPos.y - p->size.y / 2);
839 }
840 else
841 {
842 // left edge
843 textPos = svec2i(r.Pos.x, r.Pos.y + r.Size.y / 2 + yInt);
844 drawPos = svec2i(textPos.x, textPos.y - p->size.y / 2);
845 }
846 }
847 }
848 if (!draw && compassV.y != 0)
849 {
850 double sy = r.Size.y / 2.0 / compassV.y;
851 int xInt = (int)floor(fabs(sy) * compassV.x + 0.5);
852 if (xInt >= -r.Size.x / 2 && xInt <= r.Size.x / 2)
853 {
854 draw = true;
855 // Intercepts either top or bottom side
856 if (compassV.y > 0)
857 {
858 // bottom edge
859 textPos =
860 svec2i(r.Pos.x + r.Size.x / 2 + xInt, r.Pos.y + r.Size.y);
861 drawPos =
862 svec2i(textPos.x - p->size.x / 2, textPos.y - p->size.y);
863 }
864 else
865 {
866 // top edge
867 textPos = svec2i(r.Pos.x + r.Size.x / 2 + xInt, r.Pos.y);
868 drawPos = svec2i(textPos.x - p->size.x / 2, textPos.y);
869 }
870 }
871 }
872 if (draw)
873 {
874 PicRender(
875 p, g->gameWindow.renderer, drawPos, tintedMask,
876 svec2_angle(compassV) + MPI_2, svec2_one(), SDL_FLIP_NONE,
877 Rect2iZero());
878 if (label && strlen(label) > 0)
879 {
880 struct vec2i textSize = FontStrSize(label);
881 // Center the text around the target position
882 textPos.x -= textSize.x / 2;
883 textPos.y -= textSize.y / 2;
884 // Make sure the text is inside the screen
885 int padding = 8;
886 textPos.x = MAX(textPos.x, r.Pos.x + padding);
887 textPos.x =
888 MIN(textPos.x, r.Pos.x + r.Size.x - textSize.x - padding);
889 textPos.y = MAX(textPos.y, r.Pos.y + padding);
890 textPos.y =
891 MIN(textPos.y, r.Pos.y + r.Size.y - textSize.y - padding);
892 FontStrMask(label, textPos, tintedMask);
893 }
894 }
895 }
896
897 FontOpts PlayerHUDGetScorePos(const int flags, const Rect2i r)
898 {
899 FontOpts opts = FontOptsNew();
900 opts.Area = r.Size;
901 opts.Pad = svec2i(2, 2);
902 // Score aligned to the right
903 opts.HAlign = ALIGN_END;
904 if (flags & HUDFLAGS_PLACE_RIGHT)
905 {
906 opts.HAlign = ALIGN_START;
907 }
908 if (flags & HUDFLAGS_PLACE_BOTTOM)
909 {
910 opts.VAlign = ALIGN_END;
911 opts.Pad.y += 1;
912 }
913 return opts;
914 }