Debian Codegraph @

prboom2/src/dsda/hud_components/keys.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 Keys HUD Component
16 //
17
18 #include "base.h"
19
20 #include "keys.h"
21
22 #define PATCH_DELTA 10
23
24 typedef struct {
25 dsda_patch_component_t component;
26 dboolean horizontal;
27 } local_component_t;
28
29 static local_component_t* local;
30
31 static int key_patch_num[NUMCARDS];
32
33 static const char* dsda_Key1Name(player_t* player) {
34 if (heretic) {
35 if (player->cards[key_yellow])
36 return "ykeyicon";
37 }
38 else {
39 if (player->cards[0] && player->cards[3])
40 return "STKEYS6";
41 else if (player->cards[0])
42 return "STKEYS0";
43 else if (player->cards[3])
44 return "STKEYS3";
45 }
46
47 return NULL;
48 }
49
50 static const char* dsda_Key2Name(player_t* player) {
51 if (heretic) {
52 if (player->cards[key_green])
53 return "gkeyicon";
54 }
55 else {
56 if (player->cards[1] && player->cards[4])
57 return "STKEYS7";
58 else if (player->cards[1])
59 return "STKEYS1";
60 else if (player->cards[4])
61 return "STKEYS4";
62 }
63
64 return NULL;
65 }
66
67 static const char* dsda_Key3Name(player_t* player) {
68 if (heretic) {
69 if (player->cards[key_blue])
70 return "bkeyicon";
71 }
72 else {
73 if (player->cards[2] && player->cards[5])
74 return "STKEYS8";
75 else if (player->cards[2])
76 return "STKEYS2";
77 else if (player->cards[5])
78 return "STKEYS5";
79 }
80
81 return NULL;
82 }
83
84 void drawKey(player_t* player, int* x, int* y, const char* (*key)(player_t*)) {
85 const char* name;
86
87 name = key(player);
88
89 if (name)
90 V_DrawNamePatch(*x, *y, FG, name, CR_DEFAULT, local->component.vpt);
91
92 if (local->horizontal)
93 *x += PATCH_DELTA;
94 else
95 *y += PATCH_DELTA;
96 }
97
98 static void dsda_DrawComponent(void) {
99 player_t* player;
100 int x, y;
101
102 player = &players[displayplayer];
103
104 x = local->component.x;
105 y = local->component.y;
106
107 if (hexen) {
108 int i;
109
110 for (i = 0; i < NUMCARDS; ++i)
111 if (player->cards[i]) {
112 V_DrawNumPatch(x, y, 0, key_patch_num[i], CR_DEFAULT, local->component.vpt);
113 x += R_NumPatchWidth(key_patch_num[i]) + 4;
114 }
115
116 return;
117 }
118
119 drawKey(player, &x, &y, dsda_Key1Name);
120 drawKey(player, &x, &y, dsda_Key2Name);
121 drawKey(player, &x, &y, dsda_Key3Name);
122 }
123
124 void dsda_InitKeysHC(int x_offset, int y_offset, int vpt, int* args, int arg_count, void** data) {
125 *data = Z_Calloc(1, sizeof(local_component_t));
126 local = *data;
127
128 local->horizontal = arg_count > 0 ? !!args[0] : false;
129
130 if (hexen) {
131 int i;
132
133 for (i = 0; i < NUMCARDS; ++i)
134 key_patch_num[i] = R_NumPatchForSpriteIndex(HEXEN_SPR_KEY1 + i);
135 }
136
137 dsda_InitPatchHC(&local->component, x_offset, y_offset, vpt);
138 }
139
140 void dsda_UpdateKeysHC(void* data) {
141 local = data;
142 }
143
144 void dsda_DrawKeysHC(void* data) {
145 local = data;
146
147 dsda_DrawComponent();
148 }