Debian Codegraph @

similar/main/hud.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /*
8 *
9 * Routines for displaying HUD messages...
10 *
11 */
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include "hudmsg.h"
18 #include "pstypes.h"
19 #include "u_mem.h"
20 #include "strutil.h"
21 #include "console.h"
22 #include "object.h"
23 #include "inferno.h"
24 #include "game.h"
25 #include "gauges.h"
26 #include "newdemo.h"
27 #include "player.h"
28 #include "gamefont.h"
29 #include "text.h"
30 #include "playsave.h"
31 #include "countarray.h"
32 #include "d_levelstate.h"
33
34 int HUD_toolong;
35 namespace {
36 constexpr std::integral_constant<unsigned, 150> HUD_MESSAGE_LENGTH{};
37
38 struct hudmsg
39 {
40 fix time;
41 ntstring<HUD_MESSAGE_LENGTH> message;
42 hudmsg(const fix &t, auto &&m) :
43 time(t)
44 {
45 message.copy_if(m);
46 }
47 };
48
49 struct hudmsg_array_t : public count_array_t<hudmsg, HUD_MAX_NUM_STOR> {};
50
51 static hudmsg_array_t HUD_messages;
52
53 static int HUD_color = -1;
54 static int HUD_init_message_literal_worth_showing(int class_flag, const char *message);
55 }
56
57 void HUD_clear_messages()
58 {
59 HUD_messages.clear();
60 HUD_toolong = 0;
61 HUD_color = -1;
62 }
63
64 namespace dsx {
65 // ----------------------------------------------------------------------------
66 // Writes a message on the HUD and checks its timer.
67 void HUD_render_message_frame(grs_canvas &canvas)
68 {
69 int y;
70
71 HUD_toolong = 0;
72
73 if (HUD_messages.empty())
74 return;
75
76 auto expired = [](hudmsg &h) -> int {
77 if (h.time <= FrameTime)
78 return 1;
79 h.time -= FrameTime;
80 return 0;
81 };
82 HUD_messages.erase_if(expired);
83
84 // display last $HUD_MAX_NUM_DISP messages on the list
85 if (!HUD_messages.empty())
86 {
87 if (HUD_color == -1)
88 HUD_color = BM_XRGB(0,28,0);
89 gr_set_fontcolor(canvas, HUD_color, -1);
90 y = FSPACY(1);
91
92 auto &game_font = *GAME_FONT;
93 const auto &&line_spacing = LINE_SPACING(game_font, game_font);
94 #if DXX_BUILD_DESCENT == 2
95 if (PlayerCfg.GuidedInBigWindow &&
96 LevelUniqueObjectState.Guided_missile.get_player_active_guided_missile(LevelUniqueObjectState.get_objects().vmptr, Player_num) != nullptr)
97 y += line_spacing;
98 #endif
99
100 hudmsg_array_t::iterator i, e = HUD_messages.end();
101 if (HUD_messages.size() < HUD_MAX_NUM_DISP)
102 i = HUD_messages.begin();
103 else
104 i = e - HUD_MAX_NUM_DISP;
105 if (strlen(i->message) > 38)
106 HUD_toolong = 1;
107 for (; i != e; ++i ) {
108 gr_string(canvas, game_font, 0x8000, y, &i->message[0u]);
109 y += line_spacing;
110 }
111 }
112 }
113 }
114
115 namespace {
116
117 static int is_worth_showing(int class_flag)
118 {
119 if (PlayerCfg.NoRedundancy && (class_flag & HM_REDUNDANT))
120 return 0;
121
122 if (PlayerCfg.MultiMessages && (Game_mode & GM_MULTI) && !(class_flag & HM_MULTI))
123 return 0;
124 return 1;
125 }
126
127 }
128
129 // Call to flash a message on the HUD. Returns true if message drawn.
130 // (message might not be drawn if previous message was same)
131 int HUD_init_message_va(int class_flag, const char * format, va_list args)
132 {
133 if (!is_worth_showing(class_flag))
134 return 0;
135
136 std::array<char, HUD_MESSAGE_LENGTH> message;
137 const auto written = std::max(vsnprintf(std::data(message), std::size(message), format, args), 0);
138 int r = HUD_init_message_literal_worth_showing(class_flag, std::data(message));
139 if (r)
140 con_puts(CON_HUD, std::span<char>(std::data(message), written));
141 return r;
142 }
143
144 namespace {
145
146 static int HUD_init_message_literal_worth_showing(int class_flag, const char *message)
147 {
148 // check if message is already in list and bail out if so
149 if (!HUD_messages.empty())
150 {
151 hudmsg_array_t::iterator i, e = HUD_messages.end();
152 // if "normal" message, only check if it's the same at the most recent one, if marked as "may duplicate" check whole list
153 if (class_flag & HM_MAYDUPL)
154 i = HUD_messages.begin();
155 else
156 i = e - 1;
157 for (; i != e; ++i)
158 {
159 if (!d_stricmp(message, i->message))
160 {
161 i->time = F1_0*2; // keep redundant message in list
162 if (std::distance(i, e) < HUD_MAX_NUM_DISP) // if redundant message on display, update them all
163 {
164 if (HUD_messages.size() < HUD_MAX_NUM_DISP)
165 i = HUD_messages.begin();
166 else
167 i = HUD_messages.end() - HUD_MAX_NUM_DISP;
168 for (unsigned j = 1; i != e; ++i, j++)
169 i->time = F1_0*(j*2);
170 }
171 return 0;
172 }
173 }
174 }
175
176 if (HUD_messages.size() >= HUD_MAX_NUM_STOR)
177 {
178 std::move(HUD_messages.begin() + 1, HUD_messages.end(), HUD_messages.begin());
179 HUD_messages.pop_back();
180 }
181 fix t;
182 if (HUD_messages.size() + 1 < HUD_MAX_NUM_DISP)
183 t = F1_0*3; // one message - display 3 secs
184 else
185 {
186 hudmsg_array_t::iterator e = HUD_messages.end(), i = e - HUD_MAX_NUM_DISP;
187 for (unsigned j = 1; ++i != e; j++) // multiple messages - display 2 seconds each
188 i->time = F1_0*(j*2);
189 t = F1_0 * ((HUD_MAX_NUM_DISP + 1) * 2);
190 }
191 HUD_messages.emplace_back(t, message);
192
193 if (HUD_color == -1)
194 HUD_color = BM_XRGB(0,28,0);
195
196 if (Newdemo_state == ND_STATE_RECORDING )
197 newdemo_record_hud_message( message );
198
199 return 1;
200 }
201
202 }
203
204 int (HUD_init_message)(int class_flag, const char * format, ... )
205 {
206 int ret;
207 va_list args;
208
209 va_start(args, format);
210 ret = HUD_init_message_va(class_flag, format, args);
211 va_end(args);
212
213 return ret;
214 }
215
216 int HUD_init_message_literal(int class_flag, const std::span<const char> str)
217 {
218 if (!is_worth_showing(class_flag))
219 return 0;
220 int r = HUD_init_message_literal_worth_showing(class_flag, str.data());
221 if (r)
222 con_puts(CON_HUD, str);
223 return r;
224 }
225
226 void player_dead_message(grs_canvas &canvas)
227 {
228 if (Player_dead_state == player_dead_state::exploded)
229 {
230 if (get_local_player().lives == 1)
231 {
232 int x, y;
233 auto &huge_font = *HUGE_FONT;
234 const auto &&[gw, gh] = gr_get_string_size(huge_font, TXT_GAME_OVER);
235 const auto w = gw + 20;
236 const auto h = gh + 8;
237 x = (canvas.cv_bitmap.bm_w - w ) / 2;
238 y = (canvas.cv_bitmap.bm_h - h ) / 2;
239
240 gr_settransblend(canvas, gr_fade_level{14}, gr_blend::normal);
241 const uint8_t color = BM_XRGB(0, 0, 0);
242 gr_rect(canvas, x, y, x + w, y + h, color);
243 gr_settransblend(canvas, GR_FADE_OFF, gr_blend::normal);
244
245 gr_string(canvas, huge_font, 0x8000, (canvas.cv_bitmap.bm_h - h) / 2 + h / 8, TXT_GAME_OVER, gw, gh);
246 }
247
248 if (HUD_color == -1)
249 HUD_color = BM_XRGB(0,28,0);
250 gr_set_fontcolor(canvas, HUD_color, -1);
251 auto &game_font = *GAME_FONT;
252 gr_string(canvas, game_font, 0x8000, canvas.cv_bitmap.bm_h - LINE_SPACING(game_font, game_font), PlayerCfg.RespawnMode == RespawnPress::Any ? TXT_PRESS_ANY_KEY : "Press fire key or button to continue...");
253 }
254 }