Debian Codegraph @

prboom2/src/dsda/hud_components/coordinate_display.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 Coordinate Display HUD Component
16 //
17
18 #include "base.h"
19
20 #include "coordinate_display.h"
21
22 #define THRESHOLD_1V 15.11
23 #define THRESHOLD_2V 19.35
24 #define THRESHOLD_3V 21.37
25
26 #define THRESHOLD_1D 16.67
27 #define THRESHOLD_2D 21.35
28 #define THRESHOLD_3D 23.58
29
30 typedef struct {
31 dsda_text_t dsda_x_display;
32 dsda_text_t dsda_y_display;
33 dsda_text_t dsda_z_display;
34 dsda_text_t dsda_a_display;
35 dsda_text_t dsda_v_display;
36 dsda_text_t dsda_vx_display;
37 dsda_text_t dsda_vy_display;
38 dsda_text_t dsda_d_display;
39 dsda_text_t dsda_dx_display;
40 dsda_text_t dsda_dy_display;
41 } local_component_t;
42
43 static local_component_t* local;
44
45 static const char* dsda_coordinate_color;
46 static const char* dsda_velocity_color;
47 static const char* dsda_distance_color;
48
49 static double dsda_CalculateVelocity(void) {
50 double vx, vy;
51
52 vx = (double) players[displayplayer].mo->momx / FRACUNIT;
53 vy = (double) players[displayplayer].mo->momy / FRACUNIT;
54
55 return sqrt(vx * vx + vy * vy);
56 }
57
58 static double dsda_CalculateDistance(void) {
59 double vx, vy;
60 mobj_t* mo;
61
62 mo = players[displayplayer].mo;
63
64 vx = (double) (mo->x - mo->PrevX) / FRACUNIT;
65 vy = (double) (mo->y - mo->PrevY) / FRACUNIT;
66
67 return sqrt(vx * vx + vy * vy);
68 }
69
70 static void dsda_WriteCoordinate(dsda_text_t* text, fixed_t x, const char* ch) {
71 char str[FIXED_STRING_LENGTH];
72
73 dsda_FixedToString(str, x);
74
75 snprintf(text->msg, sizeof(text->msg), "%s%s: %s", dsda_coordinate_color, ch, str);
76
77 dsda_RefreshHudText(text);
78 }
79
80 static void dsda_WriteAngle(dsda_text_t* text, angle_t x, const char* ch) {
81 dsda_angle_t value;
82
83 value = dsda_SplitAngle(x);
84
85 if (value.frac)
86 snprintf(text->msg, sizeof(text->msg), "%s%s: %i.%03i",
87 dsda_coordinate_color, ch, value.base, value.frac);
88 else
89 snprintf(text->msg, sizeof(text->msg), "%s%s: %i",
90 dsda_coordinate_color, ch, value.base);
91
92 dsda_RefreshHudText(text);
93 }
94
95 static void dsda_WriteCoordinateSimple(dsda_text_t* text, fixed_t x, const char* ch, const char* color) {
96 dsda_fixed_t value;
97
98 value = dsda_SplitFixed(x);
99
100 if (value.frac) {
101 if (value.negative && !value.base)
102 snprintf(text->msg, sizeof(text->msg), "%s%s: -%i.%03i",
103 color, ch, value.base, 1000 * value.frac / 0xffff);
104 else
105 snprintf(text->msg, sizeof(text->msg), "%s%s: %i.%03i",
106 color, ch, value.base, 1000 * value.frac / 0xffff);
107 }
108 else
109 snprintf(text->msg, sizeof(text->msg), "%s%s: %i", color, ch, value.base);
110
111 dsda_RefreshHudText(text);
112 }
113
114 static void dsda_WriteVelocity(dsda_text_t* text) {
115 double v;
116
117 v = dsda_CalculateVelocity();
118
119 dsda_velocity_color =
120 v >= THRESHOLD_3V ?
121 dsda_TextColor(dsda_tc_exhud_coords_fast) :
122 v >= THRESHOLD_2V ?
123 dsda_TextColor(dsda_tc_exhud_coords_sr50) :
124 v >= THRESHOLD_1V ?
125 dsda_TextColor(dsda_tc_exhud_coords_sr40) :
126 dsda_TextColor(dsda_tc_exhud_coords_mf50);
127
128 if (v)
129 snprintf(text->msg, sizeof(text->msg), "%sV: %.3f", dsda_velocity_color, v);
130 else
131 snprintf(text->msg, sizeof(text->msg), "%sV: 0", dsda_velocity_color);
132
133 dsda_RefreshHudText(text);
134 }
135
136 static void dsda_WriteDistance(dsda_text_t* text) {
137 double v;
138
139 v = dsda_CalculateDistance();
140
141 dsda_distance_color =
142 v >= THRESHOLD_3D ?
143 dsda_TextColor(dsda_tc_exhud_coords_fast) :
144 v >= THRESHOLD_2D ?
145 dsda_TextColor(dsda_tc_exhud_coords_sr50) :
146 v >= THRESHOLD_1D ?
147 dsda_TextColor(dsda_tc_exhud_coords_sr40) :
148 dsda_TextColor(dsda_tc_exhud_coords_mf50);
149
150 if (v)
151 snprintf(text->msg, sizeof(text->msg), "%sD: %.3f", dsda_distance_color, v);
152 else
153 snprintf(text->msg, sizeof(text->msg), "%sD: 0", dsda_distance_color);
154
155 dsda_RefreshHudText(text);
156 }
157
158 void dsda_InitCoordinateDisplayHC(int x_offset, int y_offset, int vpt, int* args, int arg_count, void** data) {
159 *data = Z_Calloc(1, sizeof(local_component_t));
160 local = *data;
161
162 dsda_coordinate_color = dsda_TextColor(dsda_tc_exhud_coords_base);
163
164 dsda_InitTextHC(&local->dsda_x_display, x_offset, y_offset, vpt);
165 dsda_InitTextHC(&local->dsda_y_display, x_offset, y_offset + 8, vpt);
166 dsda_InitTextHC(&local->dsda_z_display, x_offset, y_offset + 16, vpt);
167 dsda_InitTextHC(&local->dsda_a_display, x_offset, y_offset + 24, vpt);
168 dsda_InitTextHC(&local->dsda_v_display, x_offset, y_offset + 40, vpt);
169 dsda_InitTextHC(&local->dsda_vx_display, x_offset, y_offset + 48, vpt);
170 dsda_InitTextHC(&local->dsda_vy_display, x_offset, y_offset + 56, vpt);
171 dsda_InitTextHC(&local->dsda_d_display, x_offset, y_offset + 72, vpt);
172 dsda_InitTextHC(&local->dsda_dx_display, x_offset, y_offset + 80, vpt);
173 dsda_InitTextHC(&local->dsda_dy_display, x_offset, y_offset + 88, vpt);
174 }
175
176 void dsda_UpdateCoordinateDisplayHC(void* data) {
177 mobj_t* mo;
178
179 local = data;
180
181 mo = players[displayplayer].mo;
182
183 dsda_WriteCoordinate(&local->dsda_x_display, mo->x, "X");
184 dsda_WriteCoordinate(&local->dsda_y_display, mo->y, "Y");
185 dsda_WriteCoordinate(&local->dsda_z_display, mo->z, "Z");
186 dsda_WriteAngle(&local->dsda_a_display, mo->angle, "A");
187 dsda_WriteVelocity(&local->dsda_v_display);
188 dsda_WriteCoordinateSimple(&local->dsda_vx_display, mo->momx, "X", dsda_velocity_color);
189 dsda_WriteCoordinateSimple(&local->dsda_vy_display, mo->momy, "Y", dsda_velocity_color);
190 dsda_WriteDistance(&local->dsda_d_display);
191 dsda_WriteCoordinateSimple(&local->dsda_dx_display, mo->x - mo->PrevX, "X", dsda_distance_color);
192 dsda_WriteCoordinateSimple(&local->dsda_dy_display, mo->y - mo->PrevY, "Y", dsda_distance_color);
193
194 dsda_RefreshHudText(&local->dsda_x_display);
195 dsda_RefreshHudText(&local->dsda_y_display);
196 dsda_RefreshHudText(&local->dsda_z_display);
197 dsda_RefreshHudText(&local->dsda_a_display);
198 dsda_RefreshHudText(&local->dsda_v_display);
199 dsda_RefreshHudText(&local->dsda_vx_display);
200 dsda_RefreshHudText(&local->dsda_vy_display);
201 dsda_RefreshHudText(&local->dsda_d_display);
202 dsda_RefreshHudText(&local->dsda_dx_display);
203 dsda_RefreshHudText(&local->dsda_dy_display);
204 }
205
206 void dsda_DrawCoordinateDisplayHC(void* data) {
207 local = data;
208
209 dsda_DrawBasicText(&local->dsda_x_display);
210 dsda_DrawBasicText(&local->dsda_y_display);
211 dsda_DrawBasicText(&local->dsda_z_display);
212 dsda_DrawBasicText(&local->dsda_a_display);
213 dsda_DrawBasicText(&local->dsda_v_display);
214 dsda_DrawBasicText(&local->dsda_vx_display);
215 dsda_DrawBasicText(&local->dsda_vy_display);
216 dsda_DrawBasicText(&local->dsda_d_display);
217 dsda_DrawBasicText(&local->dsda_dx_display);
218 dsda_DrawBasicText(&local->dsda_dy_display);
219 }