Debian Codegraph @

prboom2/src/dsda/hud_components/ready_ammo_text.c

raw
1 //
2 // Copyright(C) 2022 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA Ready Ammo Text HUD Component
16 //
17
18 #include "base.h"
19
20 #include "ready_ammo_text.h"
21
22 typedef struct {
23 dsda_text_t component;
24 } local_component_t;
25
26 static local_component_t* local;
27
28 int dsda_AmmoColor(player_t* player) {
29 int ammo_percent;
30
31 ammo_percent = P_AmmoPercent(player, player->readyweapon);
32
33 if (ammo_percent < hud_ammo_red)
34 return dsda_tc_exhud_ammo_bad;
35 else if (ammo_percent < hud_ammo_yellow)
36 return dsda_tc_exhud_ammo_warning;
37 else if (ammo_percent < 100)
38 return dsda_tc_exhud_ammo_ok;
39 else
40 return dsda_tc_exhud_ammo_full;
41 }
42
43 static void dsda_UpdateComponentText(char* str, size_t max_size) {
44 player_t* player;
45
46 player = &players[displayplayer];
47
48 if (hexen) {
49 snprintf(str, max_size, "%sAMM %s%3d %s%3d",
50 dsda_TextColor(dsda_tc_exhud_ammo_label),
51 dsda_TextColor(dsda_tc_exhud_ammo_mana1), player->ammo[0],
52 dsda_TextColor(dsda_tc_exhud_ammo_mana2), player->ammo[1]);
53 }
54 else {
55 ammotype_t ammo_type;
56
57 ammo_type = weaponinfo[player->readyweapon].ammo;
58
59 if (ammo_type == am_noammo || !player->maxammo[ammo_type])
60 snprintf(str, max_size, "%sAMM %sN/A",
61 dsda_TextColor(dsda_tc_exhud_ammo_label),
62 dsda_TextColor(dsda_tc_exhud_ammo_value));
63 else
64 snprintf(str, max_size, "%sAMM %s%3d",
65 dsda_TextColor(dsda_tc_exhud_ammo_label),
66 dsda_TextColor(dsda_AmmoColor(player)), player->ammo[ammo_type]);
67 }
68 }
69
70 void dsda_InitReadyAmmoTextHC(int x_offset, int y_offset, int vpt, int* args, int arg_count, void** data) {
71 *data = Z_Calloc(1, sizeof(local_component_t));
72 local = *data;
73
74 dsda_InitTextHC(&local->component, x_offset, y_offset, vpt);
75 }
76
77 void dsda_UpdateReadyAmmoTextHC(void* data) {
78 local = data;
79
80 dsda_UpdateComponentText(local->component.msg, sizeof(local->component.msg));
81 dsda_RefreshHudText(&local->component);
82 }
83
84 void dsda_DrawReadyAmmoTextHC(void* data) {
85 local = data;
86
87 dsda_DrawBasicText(&local->component);
88 }