Debian Codegraph @

Descent3/huddisplay.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/huddisplay.cpp $
21 * $Revision: 59 $
22 * $Date: 4/19/00 5:16p $
23 * $Author: Matt $
24 *
25 * Hud message and stat display file.
26 *
27 * $Log: /DescentIII/main/huddisplay.cpp $
28 *
29 * 59 4/19/00 5:16p Matt
30 * From Duane for 1.4
31 * Removed Mac OpenGL hack
32 *
33 * 58 10/21/99 6:39p Matt
34 * Mac merge
35 * This change is mostly setting & clearing the variable
36 * use_opengl_1555_format around some blocks of code. Duane says this
37 * won't affect the Windows version.
38 *
39 * 57 7/28/99 4:11p Kevin
40 * Mac
41 *
42 * 56 6/10/99 9:07p Samir
43 * fixed weapon string name localization issues.
44 *
45 * 55 5/20/99 12:38p Matt
46 * Fixed centering of the timer HUD display.
47 *
48 * 54 5/20/99 2:26a Matt
49 * Don't use sprintf() for custom_text2 type items, so imbedded percents
50 * work.
51 *
52 * 53 5/19/99 1:23p Matt
53 * Moved the timer display down a bit, and made the time round up.
54 *
55 * 52 5/19/99 11:25a Matt
56 * Added multisafe functions & Dallas actions for showing a timer on the
57 * screen and adding custom HUD messages.
58 *
59 * 51 5/13/99 7:00p Samir
60 * print score in co-op or robo-anarchy
61 *
62 * 50 5/10/99 10:22p Ardussi
63 * changes to compile on Mac
64 *
65 * 49 5/09/99 6:21a Jeff
66 * added missing restore of ZBuffer state
67 *
68 * 48 5/05/99 5:46p Samir
69 * fixed RenderHUDGetTextWidth. Hopefully didn't break for higher
70 * resolutions.
71 *
72 * 47 4/29/99 5:08p Jason
73 * fixed wrapping bug with RenderHudQuad
74 *
75 * 46 4/28/99 5:23a Jeff
76 * use new inventory function that gets a list of inventory items to
77 * display inventory and countermeasure on the hud (fixes weird selection
78 * bug)
79 *
80 * 45 4/24/99 10:38p Samir
81 * cleaned up hud text problems in 'small mode'
82 *
83 * 44 4/24/99 8:43p Samir
84 * when shrinking screen hud messages get rendered in black region.
85 *
86 * 43 4/22/99 11:19a Samir
87 * render hud text functions shouldn't nest formatted text.
88 *
89 * 42 4/21/99 9:29p Samir
90 * don't draw score in multiplayer.
91 *
92 * 41 4/20/99 3:06p Jeff
93 * fixed inventory displaying of non-usable items
94 *
95 * 40 4/20/99 1:14p Samir
96 * non usable hud inventory.
97 *
98 * 39 4/20/99 11:47a Samir
99 * shrinking hud fixes.
100 *
101 * 38 4/16/99 10:54p Jeff
102 * fixed min() for Linux
103 *
104 * 37 4/06/99 6:02p Matt
105 * Added score system
106 *
107 * 36 4/06/99 11:39a Samir
108 * added more formatting options for hud items (added two other full
109 * screen hud infs for different ships)
110 *
111 * 35 3/02/99 6:26p Samir
112 * hires font madness.
113 *
114 * 34 3/01/99 12:55a Matt
115 * Deleted some obsolete strings from the string table, and moved some
116 * formatting info from the table to the code.
117 *
118 * 33 2/06/99 6:59p Jeff
119 * created RenderHUDGetTextLineWidth and RenderHUDGetTextHeight
120 *
121 * 32 1/31/99 7:26p Matt
122 * Renamed a bunch of functions to have HUD capitalized
123 *
124 * 31 1/29/99 2:08p Jeff
125 * localization
126 *
127 * 30 11/01/98 1:57a Jeff
128 * converted the vsprintf calls to use the Pvsprintf, which is a safe
129 * vsprintf, no buffer overflows allowed
130 *
131 * 29 10/22/98 11:30p Matt
132 * Centered the names of non-ammo weapon names with respect to the weapon
133 * icon.
134 *
135 * 28 10/20/98 1:46p Samir
136 * made text version of inventory.
137 *
138 * 27 10/14/98 4:27p Samir
139 * improved HUD_ITEM_CUSTOMTEXT
140 *
141 * 26 10/13/98 4:16p Matt
142 * Fixed stupid bug
143 *
144 * 25 10/13/98 12:56p Matt
145 * Finished (hopefully) with the ammo system. Added support for napalm
146 * fuel.
147 *
148 * 24 9/30/98 4:34p Samir
149 * hack to prevent display of lock warning at Gametime = 0.
150 *
151 * 23 8/25/98 7:08p Samir
152 * hud primary weapon ammo.
153 *
154 * 22 8/07/98 12:54p Jeff
155 * removed () in countermeasures :)
156 *
157 * 21 8/07/98 12:54p Jeff
158 * inventory doesn't show count if count==1 countermeasure have () around
159 * count
160 *
161 * 20 7/06/98 7:34p Samir
162 * added countermeasures.
163 *
164 * 19 6/24/98 7:38p Samir
165 * redid graphical/text/cockpit hud item management.
166 *
167 * 18 6/17/98 6:31p Samir
168 * Added anti-grav warning when dying.
169 *
170 * 17 6/16/98 11:10a Jeff
171 * fixed merges goofs
172 *
173 * 16 6/16/98 10:54a Jeff
174 *
175 * 15 6/15/98 6:54p Samir
176 * added invulnerability and cloak effect.
177 *
178 * 14 6/15/98 2:14p Samir
179 * afterburner hud image scale modified.
180 *
181 * 13 6/10/98 4:03p Samir
182 * restored old hud config.
183 *
184 * 12 5/26/98 8:01p Samir
185 * don't display placeholder small icon for weapon.
186 *
187 * 11 5/26/98 5:06p Samir
188 * repositioned shield and energy sub hud items due to new design.
189 *
190 * 10 5/25/98 8:31p Samir
191 * added call to render hud text with cerntering options.
192 *
193 * 9 5/25/98 6:38p Matt
194 * Added needed include.
195 *
196 * 8 5/22/98 6:25p Samir
197 * implemented a lot of hud items.
198 *
199 * 7 5/07/98 12:23p Samir
200 * RenderHUDText functions added to hud.h
201 *
202 * 6 5/05/98 6:28p Samir
203 * moved hud_x and hud_y macros to hud.h.
204 *
205 * 5 4/27/98 1:14p Jason
206 * cleaned up afterburner stuff
207 *
208 * 4 4/24/98 5:32p Samir
209 * added saturation option to DrawHudQuad.
210 *
211 * 3 4/24/98 8:02a Samir
212 * display weapon names properly.
213 *
214 * 2 4/23/98 4:13a Samir
215 * new hud system.
216 *
217 * 1 4/23/98 4:05a Samir
218 * initial revision.
219 *
220 * $NoKeywords: $
221 */
222
223 #include <cstdarg>
224 #include <cstdio>
225
226 #include "hud.h"
227 #include "grtext.h"
228 #include "gamefont.h"
229 #include "renderer.h"
230 #include "pserror.h"
231 #include "player.h"
232 #include "game.h"
233 #include "weapon.h"
234 #include "gametexture.h"
235 #include "stringtable.h"
236 #include "ship.h"
237 #include "config.h"
238 #include "multi.h"
239 #include "render.h"
240
241 #include <algorithm>
242
243 //////////////////////////////////////////////////////////////////////////////
244 // Data
245
246 // renders the description for the current inventory item
247 static void RenderHUDInventory(tHUDItem *item);
248
249 // renders the shield rating for the ship
250 static void RenderHUDShieldValue(tHUDItem *item);
251
252 // renders the energy rating for the ship
253 static void RenderHUDEnergyValue(tHUDItem *item);
254
255 // draws the afterburner hud gauge.
256 static void RenderHUDAfterburner(tHUDItem *item);
257
258 // draws the primary weapon current in.
259 static void RenderHUDPrimary(tHUDItem *item);
260
261 // draw secondary weapon current in.
262 static void RenderHUDSecondary(tHUDItem *item);
263
264 // renders ship status.
265 static void RenderHUDShipStatus(tHUDItem *item);
266
267 // renders warnings like system failures or missile locks.
268 static void RenderHUDWarnings(tHUDItem *item);
269
270 // render hud countermeasures
271 static void RenderHUDCountermeasures(tHUDItem *item);
272
273 // returns the weapon's icon.
274 static inline int get_weapon_icon(int player, int type) {
275 player_weapon *pw = &Players[player].weapon[type];
276 weapon *wpn = GetWeaponFromIndex(player, pw->index);
277 int bmp;
278
279 if (wpn) {
280 bmp = GetTextureBitmap(wpn->icon_handle, 0);
281 if (bmp > 0)
282 return bmp;
283 }
284
285 return HUD_resources.wpn_bmp;
286 }
287
288 static void RenderHUDTextFlagsNoFormat(int flags, ddgr_color col, uint8_t alpha, int sat_count, int x, int y, const char *str);
289
290 //////////////////////////////////////////////////////////////////////////////
291 // Hud item display routines.
292
293 void HudDisplayRouter(tHUDItem *item) {
294 grtext_SetAlpha(192);
295 grtext_SetFlags(0);
296
297 switch (item->type) {
298 case HUD_ITEM_PRIMARY:
299 RenderHUDPrimary(item);
300 break;
301
302 case HUD_ITEM_SECONDARY:
303 RenderHUDSecondary(item);
304 break;
305
306 case HUD_ITEM_SHIELD:
307 RenderHUDShieldValue(item);
308 break;
309
310 case HUD_ITEM_ENERGY:
311 RenderHUDEnergyValue(item);
312 break;
313
314 case HUD_ITEM_AFTERBURNER:
315 RenderHUDAfterburner(item);
316 break;
317
318 case HUD_ITEM_INVENTORY:
319 RenderHUDInventory(item);
320 break;
321
322 case HUD_ITEM_SHIPSTATUS:
323 RenderHUDShipStatus(item);
324 break;
325
326 case HUD_ITEM_WARNINGS:
327 RenderHUDWarnings(item);
328 break;
329
330 case HUD_ITEM_CNTRMEASURE:
331 RenderHUDCountermeasures(item);
332 break;
333
334 case HUD_ITEM_CUSTOMTEXT:
335 if (item->data.text) {
336 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x, item->y, item->data.text);
337 }
338 break;
339
340 case HUD_ITEM_CUSTOMTEXT2:
341 if (item->data.text) {
342 RenderHUDTextFlagsNoFormat(0, item->color, item->alpha, item->saturation_count, 2, 240, item->data.text);
343 }
344 break;
345
346 case HUD_ITEM_CUSTOMIMAGE:
347 break;
348
349 default:
350 Int3();
351 }
352 }
353
354 //////////////////////////////////////////////////////////////////////////////
355 // Hud item functions.
356
357 // renders the description for the current inventory item
358 void RenderHUDInventory(tHUDItem *item) {
359 tInvenList ilist[MAX_UNIQUE_INVEN_ITEMS];
360 int cur_sel;
361 int count = Players[Player_num].inventory.GetInventoryItemList(ilist, MAX_UNIQUE_INVEN_ITEMS, &cur_sel);
362 if (!count)
363 return;
364
365 int y;
366 float img_w = 0;
367
368 if (cur_sel != -1) {
369 ASSERT(cur_sel < MAX_UNIQUE_INVEN_ITEMS);
370
371 // render currently selected item
372 if (ilist[cur_sel].hud_name) {
373 img_w = bm_w(HUD_resources.arrow_bmp, 0) * HUD_ARROW_SCALE;
374
375 RenderHUDQuad(item->x, item->y + 2, img_w, bm_h(HUD_resources.arrow_bmp, 0) * HUD_ARROW_SCALE, 0, 0, 1, 1,
376 HUD_resources.arrow_bmp, item->alpha, item->saturation_count);
377
378 if (ilist[cur_sel].amount > 1)
379 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + img_w, item->y, "%s (%d)",
380 ilist[cur_sel].hud_name, ilist[cur_sel].amount);
381 else
382 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + img_w, item->y, "%s",
383 ilist[cur_sel].hud_name);
384 }
385 }
386
387 // render non usable list.
388 if (cur_sel != -1 && ilist[cur_sel].hud_name) {
389 y = item->y + RenderHUDGetTextHeight(ilist[cur_sel].hud_name) + 6;
390 } else {
391 y = item->y + 16;
392 }
393
394 for (int i = 0; i < count; i++) {
395 if (cur_sel == i)
396 continue;
397 if (ilist[i].selectable)
398 continue;
399 if (!ilist[i].hud_name)
400 continue;
401
402 if (ilist[i].amount > 1) {
403 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + img_w, y, "%s (%d)", ilist[i].hud_name,
404 ilist[i].amount);
405 } else {
406 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + img_w, y, "%s", ilist[i].hud_name);
407 }
408
409 y += RenderHUDGetTextHeight(ilist[i].hud_name);
410 }
411
412 /*
413 Inventory *inventory = &Players[Player_num].inventory;
414
415 if(inventory->Size()) //make sure there is at least one item in the inventory
416 {
417 int size, y;
418 int current_inv_pos = inventory->GetPos();
419 float img_w = bm_w(HUD_resources.arrow_bmp,0)*HUD_ARROW_SCALE;
420 char *name = inventory->GetPosName();
421
422 if(name && inventory->IsUsable()) {
423 RenderHUDQuad(item->x, item->y+2, img_w, bm_h(HUD_resources.arrow_bmp,0)*HUD_ARROW_SCALE,
424 0,0,1,1,HUD_resources.arrow_bmp, item->alpha, item->saturation_count); int count = inventory->GetPosCount();
425 if(count>1)
426 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x+img_w, item->y, "%s
427 (%d)", name,count); else RenderHUDText(item->color, item->alpha, item->saturation_count, item->x+img_w, item->y, "%s",
428 name);
429 }
430
431
432 // render non usable list.
433 inventory->ResetPos();
434 size = inventory->Size();
435 if (name) {
436 y = item->y + RenderHUDGetTextHeight(name) + 6;
437 }
438 else {
439 y = item->y + 16;
440 }
441 while (size)
442 {
443 if (!inventory->IsUsable()) {
444 name = inventory->GetPosName();
445 if(name) {
446 int count = inventory->GetPosCount();
447 if(count>1) {
448 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x+img_w,
449 y, "%s (%d)", name,count);
450 }
451 else {
452 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x+img_w,
453 y, "%s", name);
454 }
455 y+= RenderHUDGetTextHeight(name);
456 }
457 }
458 inventory->NextPos(true);
459 size--;
460 }
461
462 // restore inventory position
463 inventory->GotoPos(current_inv_pos);
464 }
465 */
466 }
467
468 // renders the shield rating for the ship
469 void RenderHUDShieldValue(tHUDItem *item) {
470 const int SHIELD_NUM_X = 86, SHIELD_NUM_Y = 14;
471 const int SHIELD_IMG_X = 21, SHIELD_IMG_Y = 57;
472 const int WARNING_IMG_X = 110, WARNING_IMG_Y = 8;
473 float alpha_mod = (Objects[Players[Player_num].objnum].shields) / (float)INITIAL_SHIELDS;
474
475 if (alpha_mod > 1.0f)
476 alpha_mod = 1.0f;
477
478 if (item->stat & STAT_GRAPHICAL) {
479 // GRAPHICAL VERSION
480 // display text and warning dot
481 int img = (int)ceil((1.0f - alpha_mod - 0.1f) * NUM_SHIELD_GAUGE_FRAMES);
482 float grscalex = (item->grscalex != 0.0f) ? item->grscalex : HUD_SHIELD_SCALE;
483 float grscaley = (item->grscaley != 0.0f) ? item->grscaley : HUD_SHIELD_SCALE;
484
485 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + SHIELD_NUM_X, item->y + SHIELD_NUM_Y,
486 "%03d", (int)Objects[Players[Player_num].objnum].shields);
487
488 if (alpha_mod <= 0.20f) {
489 RenderHUDQuad(item->x + WARNING_IMG_X, item->y + WARNING_IMG_Y, bm_w(HUD_resources.dot_bmp, 0) * HUD_DOT_SCALE,
490 bm_h(HUD_resources.dot_bmp, 0) * HUD_DOT_SCALE, 0, 0, 1, 1, HUD_resources.dot_bmp, item->alpha, 1);
491 }
492
493 if (img < NUM_SHIELD_GAUGE_FRAMES) {
494 if (img < 0)
495 img = 0;
496 img = HUD_resources.shield_bmp[img];
497 RenderHUDQuad(item->x + SHIELD_IMG_X, item->y + SHIELD_IMG_Y, bm_w(img, 0) * grscalex, bm_h(img, 0) * grscaley, 0,
498 0, 1, 1, img, item->alpha, item->saturation_count);
499 }
500 } else if (item->stat & STAT_SPECIAL) {
501 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x, item->y, "%03d",
502 (int)Objects[Players[Player_num].objnum].shields);
503
504 if (alpha_mod <= 0.20f) {
505 RenderHUDQuad(item->x + 30, item->y - 10, bm_w(HUD_resources.dot_bmp, 0) * HUD_DOT_SCALE,
506 bm_h(HUD_resources.dot_bmp, 0) * HUD_DOT_SCALE, 0, 0, 1, 1, HUD_resources.dot_bmp, item->alpha, 1);
507 }
508 } else {
509 // TEXT VERSION
510 RenderHUDText(item->tcolor, item->alpha, (alpha_mod <= .20f) ? item->saturation_count + 1 : item->saturation_count,
511 item->tx, item->ty, "%s: %03d", TXT_HUD_SHIELDS, (int)Objects[Players[Player_num].objnum].shields);
512 }
513 }
514
515 // renders the energy rating for the ship
516 void RenderHUDEnergyValue(tHUDItem *item) {
517 float normalized_energy = (float)Players[Player_num].energy / (float)INITIAL_ENERGY;
518
519 // cap off energy to 1.0 normalized
520 if (normalized_energy > 1.0f)
521 normalized_energy = 1.0f;
522
523 // display text and warning dot
524 if (item->stat & STAT_GRAPHICAL) {
525 const int ENERGY_NUM_X = 15, ENERGY_NUM_Y = 14;
526 // int ENERGY_IMG_X = 3, ENERGY_IMG_Y = 74;
527 // int ENERGY_IMG_X2 = 119;
528 const int WARNING_IMG_X = 6, WARNING_IMG_Y = 8;
529 float grscalex = (item->grscalex != 0.0f) ? item->grscalex : HUD_ENERGY_SCALE;
530 float grscaley = (item->grscaley != 0.0f) ? item->grscaley : HUD_ENERGY_SCALE;
531
532 float img_w = bm_w(HUD_resources.energy_bmp, 0) * grscalex;
533 float img_h = bm_h(HUD_resources.energy_bmp, 0) * grscaley;
534 int img_energy_h;
535
536 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + ENERGY_NUM_X, item->y + ENERGY_NUM_Y,
537 "%03d", (int)Players[Player_num].energy);
538
539 if (normalized_energy <= 0.20f) {
540 RenderHUDQuad(item->x + WARNING_IMG_X, item->y + WARNING_IMG_Y, bm_w(HUD_resources.dot_bmp, 0) * HUD_DOT_SCALE,
541 bm_h(HUD_resources.dot_bmp, 0) * HUD_DOT_SCALE, 0, 0, 1, 1, HUD_resources.dot_bmp, item->alpha, 1);
542 }
543
544 img_energy_h = (int)floor((normalized_energy * img_h) + 0.5f);
545
546 // draw energy gauge, showing how much energy below 100% you have. draw energy spent faded.
547 RenderHUDQuad(item->x + item->xa, item->y + item->ya, img_w, img_h - img_energy_h, 0, 0, 1.0,
548 1.0f - normalized_energy, HUD_resources.energy_bmp, item->alpha / 4, 0);
549
550 RenderHUDQuad(item->x + item->xb - img_w, item->y + item->yb, img_w, img_h - img_energy_h, 1.0, 0, 0,
551 1.0f - normalized_energy, HUD_resources.energy_bmp, item->alpha / 4, 0);
552
553 RenderHUDQuad(item->x + item->xa, item->y + item->ya + (int)(img_h - img_energy_h), img_w, img_energy_h, 0,
554 1.0f - normalized_energy, 1, 1, HUD_resources.energy_bmp, item->alpha, item->saturation_count);
555
556 RenderHUDQuad(item->x + item->xb - img_w, item->y + item->yb + (int)(img_h - img_energy_h), img_w, img_energy_h, 1,
557 1.0f - normalized_energy, 0, 1, HUD_resources.energy_bmp, item->alpha, item->saturation_count);
558 } else if (item->stat & STAT_SPECIAL) {
559 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x, item->y, "%03d",
560 (int)Players[Player_num].energy);
561
562 if (normalized_energy <= 0.20f) {
563 RenderHUDQuad(item->x - 10, item->y - 10, bm_w(HUD_resources.dot_bmp, 0) * HUD_DOT_SCALE,
564 bm_h(HUD_resources.dot_bmp, 0) * HUD_DOT_SCALE, 0, 0, 1, 1, HUD_resources.dot_bmp, item->alpha, 1);
565 }
566 } else {
567 // TEXT VERSION
568 RenderHUDText(item->tcolor, item->alpha,
569 (normalized_energy <= .20f) ? item->saturation_count + 1 : item->saturation_count, item->tx, item->ty,
570 "%s: %03d", TXT_HUD_ENERGY, (int)Players[Player_num].energy);
571 }
572 }
573
574 // draws the afterburner hud gauge.
575 void RenderHUDAfterburner(tHUDItem *item) {
576 float val = (Players[Player_num].afterburn_time_left / AFTERBURN_TIME);
577 char str[8];
578
579 if (item->stat & STAT_GRAPHICAL) {
580 const int BURNER_NUM_X = 24;
581 float grscalex = (item->grscalex != 0.0f) ? item->grscalex : HUD_BURN_SCALE;
582 float grscaley = (item->grscaley != 0.0f) ? item->grscaley : HUD_BURN_SCALE;
583 float img_w = bm_w(HUD_resources.afterburn_bmp, 0) * grscalex;
584 float img_h = bm_h(HUD_resources.afterburn_bmp, 0) * grscaley;
585 int img_burn_w = (int)floor((val * img_w) + 0.5f);
586
587 // draw afterburn spent.
588 RenderHUDQuad(item->x + item->xa + img_burn_w, item->y + item->ya, img_w - img_burn_w, img_h, val, 0, 1, 1,
589 HUD_resources.afterburn_bmp, item->alpha / 4, 0);
590
591 // draw afterburn left
592 RenderHUDQuad(item->x + item->xa, item->y + item->ya, img_burn_w, img_h, 0, 0, val, 1, HUD_resources.afterburn_bmp,
593 item->alpha, item->saturation_count);
594
595 snprintf(str, sizeof(str), "%d%%", (int)(val * 100.0f));
596 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + BURNER_NUM_X, item->y, str);
597 } else if (item->stat & STAT_SPECIAL) {
598 snprintf(str, sizeof(str), "%d%%", (int)(val * 100.0f));
599 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x, item->y, str);
600 } else {
601 // TEXT VERSION
602 RenderHUDText(item->tcolor, item->alpha, (val <= .30f) ? item->saturation_count + 1 : item->saturation_count,
603 item->tx, item->ty, "%s: %d%%", TXT_HUD_AFTERBURNER, (int)(val * 100.0f));
604 }
605 }
606
607 // draws the primary weapon current in.
608 void RenderHUDPrimary(tHUDItem *item) {
609 int index = Players[Player_num].weapon[PW_PRIMARY].index;
610 ship *ship = &Ships[Players[Player_num].ship_index];
611 otype_wb_info *wb = &ship->static_wb[index];
612 const char *text = TXT(Static_weapon_names_msg[index]);
613 float txt_w = (int)(grtext_GetLineWidth(text) / Hud_aspect_x) + 4;
614 char ammo_string[10];
615
616 if (!wb)
617 return;
618
619 if (wb->ammo_usage) {
620 int ammo = Players[Player_num].weapon_ammo[index];
621 if (ship->fire_flags[index] & SFF_TENTHS)
622 snprintf(ammo_string, sizeof(ammo_string), "%d.%d", ammo / 10, ammo % 10);
623 else
624 snprintf(ammo_string, sizeof(ammo_string), "%d", ammo);
625 }
626
627 if (item->stat & STAT_GRAPHICAL) {
628 const int WPN_INFO_W = 100;
629 float grscalex = (item->grscalex != 0.0f) ? item->grscalex : HUD_WPN_SCALE;
630 float grscaley = (item->grscaley != 0.0f) ? item->grscaley : HUD_WPN_SCALE;
631
632 float img_w = bm_w(HUD_resources.wpn_bmp, 0) * grscalex;
633 int icon;
634
635 int y = item->y;
636
637 if (wb->ammo_usage) {
638 int w2 = (int)(grtext_GetLineWidth("00000") / Hud_aspect_x) + 2;
639 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + WPN_INFO_W - img_w - w2, y + 13,
640 ammo_string);
641 } else
642 y += 13 / 2; // If just one line, move it down
643
644 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + WPN_INFO_W - img_w - txt_w, y, "%s",
645 text);
646
647 icon = get_weapon_icon(Player_num, PW_PRIMARY);
648
649 if (icon != HUD_resources.wpn_bmp) {
650 RenderHUDQuad(item->x + WPN_INFO_W - img_w, item->y, img_w, bm_h(icon, 0) * grscaley, 0, 0, 1, 1, icon,
651 item->alpha, item->saturation_count);
652 }
653 } else if (item->stat & STAT_SPECIAL) {
654
655 } else {
656 // TEXT VERSION
657 if (wb->ammo_usage) {
658 RenderHUDText(item->tcolor, item->alpha, item->saturation_count, item->tx, item->ty, "%s %s", text, ammo_string);
659 } else {
660 RenderHUDText(item->tcolor, item->alpha, item->saturation_count, item->tx, item->ty, "%s", text);
661 }
662 }
663 }
664
665 // draw secondary weapon current in.
666 void RenderHUDSecondary(tHUDItem *item) {
667 int index = Players[Player_num].weapon[PW_SECONDARY].index;
668 ship *ship = &Ships[Players[Player_num].ship_index];
669 otype_wb_info *wb = &ship->static_wb[index];
670 weapon *wpn = GetWeaponFromIndex(Player_num, index);
671 const char *text = TXT(Static_weapon_names_msg[index]);
672 float txt_w = (int)(grtext_GetLineWidth(text) / Hud_aspect_x) + 2;
673
674 if (item->stat & STAT_GRAPHICAL) {
675 int icon;
676 float grscalex = (item->grscalex != 0.0f) ? item->grscalex : HUD_WPN_SCALE;
677 float grscaley = (item->grscaley != 0.0f) ? item->grscaley : HUD_WPN_SCALE;
678
679 float img_w = bm_w(HUD_resources.wpn_bmp, 0) * grscalex;
680
681 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + img_w + 2, item->y, "%s", text);
682 if (wb->ammo_usage) {
683 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + img_w + 16, item->y + 13, "%d",
684 Players[Player_num].weapon_ammo[Players[Player_num].weapon[PW_SECONDARY].index]);
685 }
686
687 icon = get_weapon_icon(Player_num, PW_SECONDARY);
688
689 if (icon != HUD_resources.wpn_bmp) {
690 RenderHUDQuad(item->x, item->y, img_w, bm_h(icon, 0) * grscaley, 0, 0, 1, 1, icon, item->alpha,
691 item->saturation_count);
692 }
693 } else if (item->stat & STAT_SPECIAL) {
694
695 } else {
696 // TEXT VERSION
697 RenderHUDText(item->tcolor, item->alpha, item->saturation_count, item->tx, item->ty, "%s %d", text,
698 Players[Player_num].weapon_ammo[Players[Player_num].weapon[PW_SECONDARY].index]);
699 }
700 }
701
702 void RenderHUDShipStatus(tHUDItem *item) {
703 float clk_time_frame, inv_time_frame;
704 uint8_t clk_alpha = 0, inv_alpha;
705
706 // render text status
707 if (Objects[Players[Player_num].objnum].effect_info->type_flags & EF_CLOAKED) {
708 clk_time_frame = Objects[Players[Player_num].objnum].effect_info->cloak_time;
709 if (clk_time_frame < HUD_CLOAKEND_TIME)
710 clk_alpha = 128 - 127 * FixCos(65536.0f * (clk_time_frame - (int)clk_time_frame));
711 else
712 clk_alpha = 0;
713 }
714 if (Players[Player_num].flags & PLAYER_FLAGS_INVULNERABLE) {
715 // do invulnerablity animation.
716 inv_time_frame = (Gametime - (int)Gametime);
717 inv_alpha = (255 * inv_time_frame);
718 }
719
720 if (item->stat & STAT_GRAPHICAL) {
721 const int STATUS_IMG_X = 0, STATUS_IMG_Y = 68;
722 const int STATUS_TXT_X = 8;
723 const int STATUS_TXT_Y = -1, STATUS_TXT_Y2 = 9;
724 float grscalex = (item->grscalex != 0.0f) ? item->grscalex : HUD_SHIP_SCALE;
725 float grscaley = (item->grscaley != 0.0f) ? item->grscaley : HUD_SHIP_SCALE;
726
727 int x = item->x + STATUS_IMG_X;
728 int y = item->y + STATUS_IMG_Y;
729 int w = bm_w(HUD_resources.ship_bmp, 0) * grscalex;
730 int h = bm_w(HUD_resources.ship_bmp, 0) * grscaley;
731
732 if (Objects[Players[Player_num].objnum].effect_info->type_flags & EF_CLOAKED) {
733 RenderHUDQuad(x, y, w, h, 0, 0, 1, 1, HUD_resources.ship_bmp, clk_alpha, item->saturation_count);
734 RenderHUDText(item->color, 255 - clk_alpha, item->saturation_count, item->x + STATUS_TXT_X,
735 item->y + STATUS_TXT_Y2, TXT_CLK);
736 } else {
737 // non cloaked ship
738 RenderHUDQuad(x, y, w, h, 0, 0, 1, 1, HUD_resources.ship_bmp, item->alpha, item->saturation_count);
739 }
740
741 if (Players[Player_num].flags & PLAYER_FLAGS_INVULNERABLE) {
742 x = x - (w * inv_time_frame * 0.5f);
743 y = y - (h * inv_time_frame * 0.5f);
744
745 RenderHUDQuad(x, y, (1.0f + inv_time_frame) * w, (1.0f + inv_time_frame) * h, 0, 0, 1, 1,
746 HUD_resources.invpulse_bmp, inv_alpha, item->saturation_count);
747 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x + STATUS_TXT_X + 2,
748 item->y + STATUS_TXT_Y, TXT_INV);
749 }
750 } else if (item->stat & STAT_SPECIAL) {
751
752 } else {
753 if (Players[Player_num].flags & PLAYER_FLAGS_INVULNERABLE)
754 RenderHUDText(item->tcolor, 255 - clk_alpha, item->saturation_count, item->tx, item->ty, TXT_HUD_INVULN);
755 if (Objects[Players[Player_num].objnum].effect_info->type_flags & EF_CLOAKED)
756 RenderHUDText(item->tcolor, 255 - clk_alpha, item->saturation_count, item->tx, item->ty + 12, TXT_HUD_CLOAKED);
757 }
758 }
759
760 // renders warnings like system failures or missile locks.
761 void RenderHUDWarnings(tHUDItem *item) {
762 object *playerobj = &Objects[Players[Player_num].objnum];
763
764 if (item->stat & STAT_GRAPHICAL) {
765 const int LOCK_IMG_X = 8, LOCK_IMG_Y = 2;
766 const int ANTIGRAV_X = 8, ANTIGRAV_Y = 64;
767
768 float img_w, img_h;
769 float grscalex = (item->grscalex != 0.0f) ? item->grscalex : HUD_LOCK_SCALE;
770 float grscaley = (item->grscaley != 0.0f) ? item->grscaley : HUD_LOCK_SCALE;
771
772 if (Players[Player_num].last_homing_warning_sound_time == Gametime && Gametime > 0.0f) {
773 // draw graphical rep of missile lock warning.
774 img_w = bm_w(HUD_resources.lock_bmp[0], 0) * grscalex;
775 img_h = bm_h(HUD_resources.lock_bmp[1], 0) * grscaley;
776
777 RenderHUDQuad(item->x + LOCK_IMG_X, item->y + LOCK_IMG_Y, img_w, img_h, 0, 0, 1, 1, HUD_resources.lock_bmp[0],
778 item->alpha, item->saturation_count);
779 RenderHUDQuad(item->x + LOCK_IMG_X + img_w, item->y + LOCK_IMG_Y, img_w, img_h, 0, 0, 1, 1,
780 HUD_resources.lock_bmp[1], item->alpha, item->saturation_count);
781 }
782
783 if (playerobj->mtype.phys_info.flags & PF_GRAVITY) {
784 // draw graphical rep of missile lock warning.
785 img_w = bm_w(HUD_resources.antigrav_bmp[0], 0) * grscalex;
786 img_h = bm_h(HUD_resources.antigrav_bmp[1], 0) * grscaley;
787
788 RenderHUDQuad(item->x + ANTIGRAV_X, item->y + ANTIGRAV_Y, img_w, img_h, 0, 0, 1, 1, HUD_resources.antigrav_bmp[0],
789 item->alpha, item->saturation_count);
790 RenderHUDQuad(item->x + ANTIGRAV_X + img_w, item->y + ANTIGRAV_Y, img_w, img_h, 0, 0, 1, 1,
791 HUD_resources.antigrav_bmp[1], item->alpha, item->saturation_count);
792 }
793 } else {
794 if (Players[Player_num].last_homing_warning_sound_time == Gametime)
795 RenderHUDText(item->tcolor, item->alpha, item->saturation_count, item->tx, item->ty, TXT_HUDITM_LOCK);
796 if (playerobj->mtype.phys_info.flags & PF_GRAVITY)
797 RenderHUDText(item->tcolor, item->alpha, item->saturation_count, item->tx, item->ty + 12, TXT_HUDITM_ANTIGRAV);
798 }
799 }
800
801 // render hud countermeasures
802 void RenderHUDCountermeasures(tHUDItem *item) {
803 tInvenList ilist[MAX_UNIQUE_INVEN_ITEMS];
804 int cur_sel;
805 int count = Players[Player_num].counter_measures.GetInventoryItemList(ilist, MAX_UNIQUE_INVEN_ITEMS, &cur_sel);
806 if (!count)
807 return;
808
809 if (cur_sel != -1) {
810 ASSERT(cur_sel < MAX_UNIQUE_INVEN_ITEMS);
811
812 // render currently selected item
813 if (ilist[cur_sel].hud_name) {
814 if (item->stat & STAT_GRAPHICAL)
815 RenderHUDText(item->color, item->alpha, item->saturation_count, item->x, item->y, "%s %d",
816 ilist[cur_sel].hud_name, ilist[cur_sel].amount);
817 else
818 RenderHUDText(item->color, item->alpha, item->saturation_count, item->tx, item->ty, "%s %d",
819 ilist[cur_sel].hud_name, ilist[cur_sel].amount);
820 }
821 }
822 }
823
824 //////////////////////////////////////////////////////////////////////////////
825 // Hud rendering functions
826
827 extern tFontTemplate Hud_font_template; // retain hud font template
828
829 // returns scaled line width
830 int RenderHUDGetTextLineWidth(const char *string) {
831 float aspect_x;
832 int str_width_curfont = grtext_GetLineWidth(string);
833
834 if (grtext_GetFont() == HUD_FONT) {
835 aspect_x = ((float)grtext_GetLineWidthTemplate(&Hud_font_template, string)) / ((float)str_width_curfont);
836 } else {
837 aspect_x = 1.0f;
838 }
839
840 if (aspect_x == 1.0f) {
841 aspect_x = aspect_x * DEFAULT_HUD_WIDTH / Max_window_w;
842 }
843
844 return (int)((str_width_curfont * aspect_x));
845 }
846
847 // returns scaled text height
848 int RenderHUDGetTextHeight(const char *string) {
849 float aspect_y;
850 int str_height_curfont = grtext_GetHeight(string);
851
852 if (grtext_GetFont() == HUD_FONT) {
853 aspect_y = ((float)grtext_GetHeightTemplate(&Hud_font_template, string)) / ((float)str_height_curfont);
854 } else {
855 aspect_y = 1.0f;
856 }
857
858 return (int)((str_height_curfont * aspect_y));
859 }
860
861 void RenderHUDQuad(int x, int y, int w, int h, float u0, float v0, float u1, float v1, int bm, uint8_t alpha,
862 int sat_count) {
863 int i;
864
865 rend_SetZBufferState(0);
866 rend_SetTextureType(TT_LINEAR);
867 rend_SetAlphaValue(alpha);
868 rend_SetLighting(LS_NONE);
869 rend_SetWrapType(WT_CLAMP);
870
871 if (sat_count)
872 rend_SetAlphaType(AT_SATURATE_TEXTURE);
873 else
874 rend_SetAlphaType(AT_CONSTANT_TEXTURE);
875
876 x = HUD_X(x);
877 y = HUD_Y(y);
878 w = HUD_X(w);
879 h = HUD_Y(h);
880
881 for (i = 0; i < sat_count + 1; i++) {
882 rend_DrawScaledBitmap(x, y, x + w, y + h, bm, u0, v0, u1, v1);
883 }
884
885 rend_SetZBufferState(1);
886 }
887
888 // renders text, scaled, alphaed, saturated,
889 void RenderHUDText(ddgr_color col, uint8_t alpha, int sat_count, int x, int y, const char *fmt, ...) {
890 std::va_list arglist;
891 char buf[128];
892
893 va_start(arglist, fmt);
894 std::vsnprintf(buf, 128, fmt, arglist);
895 va_end(arglist);
896
897 RenderHUDTextFlagsNoFormat(0, col, alpha, sat_count, x, y, buf);
898 }
899
900 // renders text, scaled, alphaed, saturated,
901 void RenderHUDTextFlagsNoFormat(int flags, ddgr_color col, uint8_t alpha, int sat_count, int x, int y, const char *str) {
902 int i;
903
904 grtext_SetAlpha(alpha);
905
906 if (sat_count)
907 grtext_SetFlags(GRTEXTFLAG_SATURATE);
908 else
909 grtext_SetFlags(0);
910
911 grtext_SetColor(col);
912
913 x = HUD_X(x);
914 y = HUD_Y(y);
915
916 for (i = 0; i < sat_count + 1; i++) {
917 if (flags & HUDTEXT_CENTERED)
918 grtext_CenteredPrintf(0, y, str);
919 else
920 grtext_Puts(x, y, str);
921 }
922 }
923
924 // renders text, scaled, alphaed, saturated,
925 void RenderHUDTextFlags(int flags, ddgr_color col, uint8_t alpha, int sat_count, int x, int y, const char *fmt, ...) {
926 std::va_list arglist;
927 char buf[128];
928 int i;
929
930 va_start(arglist, fmt);
931 std::vsnprintf(buf, 128, fmt, arglist);
932 va_end(arglist);
933
934 grtext_SetAlpha(alpha);
935
936 if (sat_count)
937 grtext_SetFlags(GRTEXTFLAG_SATURATE);
938 else
939 grtext_SetFlags(0);
940
941 grtext_SetColor(col);
942
943 x = HUD_X(x);
944 y = HUD_Y(y);
945
946 for (i = 0; i < sat_count + 1; i++) {
947 if (flags & HUDTEXT_CENTERED)
948 grtext_CenteredPrintf(0, y, buf);
949 else
950 grtext_Puts(x, y, buf);
951 }
952 }
953
954 // Show the score
955 void RenderHUDScore(tHUDItem *item) {
956 char buf[100];
957 int win_w;
958
959 if (Game_mode & GM_MULTI) {
960 if (!(Netgame.flags & NF_USE_ROBOTS)) {
961 return;
962 }
963 }
964
965 snprintf(buf, sizeof(buf), "%s: %d ", TXT_SCORE, Players[Player_num].score);
966
967 win_w = (Max_window_w - Game_window_w) * (Hud_aspect_x);
968 // if (Game_video_resolution==RES_512X384) { win_w = win_w + 10; }
969
970 int w = RenderHUDGetTextLineWidth(buf); // * win_w)/(Game_window_w);
971 RenderHUDText(item->color, HUD_ALPHA, 0, item->x - w - win_w, item->y, buf);
972
973 if (Score_added_timer > 0.0) {
974 int text_height = grfont_GetHeight(HUD_FONT);
975 snprintf(buf, sizeof(buf), "%d ", Score_added);
976 w = RenderHUDGetTextLineWidth(buf); // * win_w/Game_window_w;
977 uint8_t alpha = std::min<float>(HUD_ALPHA, HUD_ALPHA * 4 * Score_added_timer / SCORE_ADDED_TIME);
978 RenderHUDText(item->color, alpha, 0, item->x - w - win_w, item->y + text_height, buf);
979 Score_added_timer -= Frametime;
980 }
981 }
982
983 extern float Osiris_TimerTimeRemaining(int handle);
984
985 // Show the timer
986 void RenderHUDTimer(tHUDItem *item) {
987 float time;
988 int min, secs;
989 char buf[100];
990
991 time = Osiris_TimerTimeRemaining(item->data.timer_handle);
992
993 // If no more time, kill item
994 if (time < 0) {
995 item->stat = 0;
996 return;
997 }
998
999 // round up
1000 time = ceil(time);
1001
1002 min = time / 60;
1003 secs = time - (60 * min);
1004
1005 snprintf(buf, sizeof(buf), "T-%d:%02d", min, secs);
1006
1007 int h = grfont_GetHeight(HUD_FONT);
1008 RenderHUDTextFlagsNoFormat(HUDTEXT_CENTERED, item->color, HUD_ALPHA, 0, 0, h * 4, buf);
1009 }
1010
1011 ////////////////////////////////////////////////////////////////////////////////////////
1012 void t_dirty_rect::fill(ddgr_color col) {
1013 int i;
1014 for (i = 2; i >= 0; i--) {
1015 rend_SetAlphaValue(255);
1016 if (r[i].l >= 0 && r[i].r >= 0) {
1017 rend_FillRect(col, r[i].l, r[i].t, r[i].r, r[i].b);
1018 }
1019 if (i > 0) {
1020 r[i].l = r[i - 1].l;
1021 r[i].t = r[i - 1].t;
1022 r[i].r = r[i - 1].r;
1023 r[i].b = r[i - 1].b;
1024 }
1025 }
1026 r[0].l = -1;
1027 r[0].t = -1;
1028 r[0].r = -1;
1029 r[0].b = -1;
1030 }
1031
1032 void t_dirty_rect::reset() {
1033 int i;
1034 for (i = 0; i < 3; i++) {
1035 r[i].l = -1;
1036 r[i].t = -1;
1037 r[i].b = -1;
1038 r[i].r = -1;
1039 }
1040 }