1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 #ifndef __HUD_H__
11 #define __HUD_H__
12 
13 #include "hud/hudgauges.h"
14 #include "graphics/2d.h"
15 #include "hud/hudparse.h"
16 #include "globalincs/vmallocator.h"
17 #include "graphics/font.h"
18 
19 class object;
20 struct cockpit_display;
21 
22 typedef struct hud_anim {
23 	char filename[MAX_FILENAME_LEN];
24 	int first_frame;	// the bitmap id for the first frame in the animation... note that
25 						// all bitmap id's following this frame are numbered sequentially
26 	int num_frames;		// number of frames in the animation
27 	int sx, sy;			// screen (x,y) of top-left corner of animation
28 	float total_time;	// total time in seconds for the animation (depends on animation fps)
29 	float time_elapsed;	// time that has elapsed (in seconds) since animation started playing
30 } hud_anim;
31 
32 typedef struct hud_frames {
33 	int	first_frame;
34 	int	num_frames;
35 } hud_frames;
36 
37 // Objective display
38 typedef struct objective_display_info
39 {
40 	int display_timer;
41 	int goal_type;
42 	int goal_status;
43 	int goal_ntotal;
44 	int goal_nresolved;
45 
46 } objective_display_info;
47 
48 // used to track how player subsystems are getting damaged
49 typedef struct hud_subsys_info
50 {
51 	float	last_str;
52 	int	flash_duration_timestamp;
53 } hud_subsys_info;
54 
55 // used for the display of damaged subsystems
56 typedef struct hud_subsys_damage
57 {
58 	int	str;
59 	int	type;
60 	char	*name;
61 } hud_subsys_damage;
62 
63 extern int HUD_draw;
64 extern int HUD_contrast;
65 
66 #define HUD_NUM_COLOR_LEVELS	16
67 extern color HUD_color_defaults[HUD_NUM_COLOR_LEVELS];
68 
69 // extern globals that will control the color of the HUD gauges
70 #define HUD_COLOR_ALPHA_USER_MAX		13		// max user-settable alpha, absolute max is 15
71 #define HUD_COLOR_ALPHA_USER_MIN		3		// min user-settable alpha, absolute min is 0
72 
73 #define HUD_COLOR_ALPHA_MAX			15
74 #define HUD_COLOR_ALPHA_DEFAULT		8
75 
76 #define HUD_BRIGHT_DELTA				7		// Level added to HUD_color_alpha to make brightness used for flashing
77 
78 // hud macro for maybe flickering all gauges
79 #define GR_AABITMAP(a, b, c)						{ int jx, jy; if(emp_should_blit_gauge()) { gr_set_bitmap(a); jx = b; jy = c; emp_hud_jitter(&jx, &jy); gr_aabitmap(jx, jy); } }
80 #define GR_AABITMAP_EX(a, b, c, d, e, f, g)	{ int jx, jy; if(emp_should_blit_gauge()) { gr_set_bitmap(a); jx = b; jy = c; emp_hud_jitter(&jx, &jy); gr_aabitmap_ex(jx, jy, d, e, f, g); } }
81 
82 // radar target identification flags
83 #define RTIF_CROSSHAIRS	(1<<0)		// draw crosshairs
84 #define RTIF_BLINK		(1<<1)		// make targeted blip blink
85 #define RTIF_PULSATE	(1<<2)		// make targeted blips size pulsate
86 #define RTIF_ENLARGE	(1<<3)		// make targeted blip appear larger than the other blips
87 
88 extern int radar_target_id_flags;
89 
90 extern int HUD_color_red;
91 extern int HUD_color_green;
92 extern int HUD_color_blue;
93 extern int HUD_color_alpha;
94 
95 extern color HUD_color_debug;
96 
97 // Values used "wiggle" the HUD.  In the 2D HUD case, the clip region accounts
98 // for these, but for the 3d-type hud stuff, you need to add these in manually.
99 extern float HUD_offset_x;
100 extern float HUD_offset_y;
101 
102 // the offset of the player's view vector and the ship forward vector in pixels (Swifty)
103 extern int HUD_nose_x;
104 extern int HUD_nose_y;
105 // Global: integrity of player's target
106 extern float Pl_target_integrity;
107 extern float Player_rearm_eta;
108 
109 extern int Hud_max_targeting_range;
110 
111 void HUD_init_colors();
112 void HUD_init();
113 void hud_close();
114 void hud_level_close();
115 void hud_update_frame(float frametime);		// updates hud systems not dependant on rendering
116 void hud_render_preprocess(float frametime);			// renders 3d dependant gauges
117 void hud_render_all();
118 void hud_render_gauges(int cockpit_display_num = -1);
119 void hud_stop_looped_engine_sounds();
120 
121 // set the offset values for this render frame
122 void HUD_set_offsets(object *viewer_obj, int wiggedy_wack, matrix *eye_orient);
123 // returns the offset between the player's view vector and the forward vector of the ship in pixels (Swifty)
124 void HUD_get_nose_coordinates(int *x, int *y);
125 
126 // Basically like gr_reset_clip only it accounts for hud jittering
127 void HUD_reset_clip();
128 void hud_save_restore_camera_data(int save);
129 
130 // Basically like gr_set_clip only it accounts for hud jittering
131 void HUD_set_clip(int x, int y, int w, int h);
132 
133 // do flashing text gauge
134 void hud_start_text_flash(const char *txt, int t, int interval = 200);
135 
136 // convert a string to use mono spaced numbers
137 void hud_num_make_mono(char *num_str, int font_num = FONT1);
138 
139 // functions for handling hud animations
140 void hud_anim_init(hud_anim *ha, int sx, int sy, const char *filename);
141 void hud_frames_init(hud_frames *hf);
142 int	hud_anim_render(hud_anim *ha, float frametime, int draw_alpha=0, int loop=1, int hold_last=0, int reverse=0,int resize_mode=GR_RESIZE_FULL, bool mirror = false);
143 int	hud_anim_load(hud_anim *ha);
144 
145 // functions for displaying the support view popup
146 void hud_support_view_start();
147 void hud_support_view_stop(int stop_now=0);
148 void hud_support_view_abort();
149 void hud_support_view_update();
150 
151 void HUD_init_hud_color_array();
152 
153 // setting HUD colors
154 void hud_set_default_color();
155 void hud_set_iff_color(object *objp, int is_bright=0);
156 color* hud_get_iff_color(object *objp, int is_bright=0);
157 void hud_set_bright_color();
158 void hud_set_dim_color();
159 
160 // HUD gauge functions
161 #define HUD_C_NONE				-4
162 #define HUD_C_BRIGHT			-3
163 #define HUD_C_DIM				-2
164 #define HUD_C_NORMAL			-1
165 void	hud_set_gauge_color(int gauge_index, int bright_index = HUD_C_NONE);
166 int	hud_gauge_active(int gauge_index);
167 void	hud_gauge_start_flash(int gauge_index);
168 int	hud_gauge_maybe_flash(int gauge_index);
169 
170 // popup gauges
171 void	hud_gauge_popup_start(int gauge_index, int time=4000);
172 int	hud_gauge_is_popup(int gauge_index);
173 
174 // objective status gauge
175 void hud_update_objective_message();
176 void hud_add_objective_messsage(int type, int status);
177 
178 int	hud_get_dock_time( object *docker_objp );
179 void	hud_show_target_model();
180 void	hud_show_voice_status();
181 
182 void	hud_subspace_notify_abort();
183 
184 // render multiplayer text message currently being entered if any
185 void hud_maybe_render_multi_text();
186 
187 int hud_get_draw();
188 void hud_toggle_draw();
189 int	hud_disabled();
190 int hud_support_find_closest( int objnum );
191 
192 // Goober5000
193 void hud_set_draw(int draw);
194 void hud_disable_except_messages(int disable);
195 int hud_disabled_except_messages();
196 
197 // contrast stuff
198 void hud_toggle_contrast();
199 void hud_set_contrast(int high);
200 
201 class HudGauge
202 {
203 protected:
204 	int position[2];
205 	int base_w, base_h;
206 	color gauge_color;
207 	int gauge_config;
208 	int gauge_object;
209 
210 	int font_num;
211 
212 	bool lock_color;
213 	bool sexp_lock_color;
214 	bool reticle_follow;
215 	bool active;
216 	bool off_by_default;
217 	bool sexp_override;
218 	bool pop_up;
219 	int popup_timer;
220 	bool message_gauge;
221 	int disabled_views;
222 
223 	int flash_duration;
224 	int flash_next;
225 	bool flash_status;
226 
227 	// custom gauge specific stuff
228 	bool custom_gauge;
229 	hud_frames custom_frame;
230 	int custom_frame_offset;
231 	int textoffset_x, textoffset_y;
232 	char custom_name[NAME_LENGTH];
233 	SCP_string custom_text;
234 
235 	SCP_string default_text;
236 
237 	// Render to texture stuff
238 	char texture_target_fname[MAX_FILENAME_LEN];
239 	int texture_target;
240 	int canvas_w, canvas_h;
241 	int target_w, target_h;
242 	int target_x, target_y;
243 	int display_offset_x, display_offset_y;
244 public:
245 	// constructors
246 	HudGauge();
247 	HudGauge(int _gauge_object, int _gauge_config, bool _slew, bool _message, int _disabled_views, int r, int g, int b);
248 	// constructor for custom gauges
249 	HudGauge(int _gauge_config, bool _slew, int r, int g, int b, char* _custom_name, char* _custom_text, char* frame_fname, int txtoffset_x, int txtoffset_y);
250 
251 	void initPosition(int x, int y);
252 	void initBaseResolution(int w, int h);
253 	void initSlew(bool slew);
254 	void initFont(int font_num);
255 	void initCockpitTarget(const char* display_name, int _target_x, int _target_y, int _target_w, int _target_h, int _canvas_w, int _canvas_h);
256 	void initRenderStatus(bool render);
257 
258 	int getConfigType();
259 	int getObjectType();
260 	void getPosition(int *x, int *y);
261 	bool isOffbyDefault();
262 	bool isActive();
263 
264 	void updateColor(int r, int g, int b, int a = 255);
265 	void lockConfigColor(bool lock);
266 	void sexpLockConfigColor(bool lock);
267 	void updateActive(bool show);
268 	void updatePopUp(bool pop_up_flag);
269 	void updateSexpOverride(bool sexp);
270 
271 
272 	// SEXP interfacing functions
273 	// For flashing gauges in training missions
274 	void startFlashSexp();
275 	int maybeFlashSexp();
276 	bool flashExpiredSexp();
277 	void resetTimers();
278 
279 	// For updating custom gauges
280 	const char* getCustomGaugeName();
281 	void updateCustomGaugeCoords(int _x, int _y);
282 	void updateCustomGaugeFrame(int frame_offset);
283 	void updateCustomGaugeText(const char* txt);
284 	void updateCustomGaugeText(const SCP_string& txt);
285 	const char* getCustomGaugeText();
286 
287 	void startPopUp(int time=4000);
288 	int popUpActive();
289 
290 	virtual void preprocess();
291 	virtual void render(float frametime);
292 	virtual bool canRender();
293 	virtual void pageIn();
294 	virtual void initialize();
295 	virtual void onFrame(float frametime);
296 
297 	bool setupRenderCanvas(int render_target = -1);
298 	void setCockpitTarget(const cockpit_display *display);
299 	void resetCockpitTarget();
300 
301 	void setFont();
302 	void setGaugeColor(int bright_index = -4);
303 
304 	// rendering functions
305 	void renderBitmap(int x, int y);
306 	void renderBitmap(int frame, int x, int y);
307 	void renderBitmapColor(int frame, int x, int y);
308 	void renderBitmapUv(int frame, int x, int y, int w, int h, float u0, float v0, float u1, float v1);
309 	void renderBitmapEx(int frame, int x, int y, int w, int h, int sx, int sy);
310 	void renderString(int x, int y, const char *str);
311 	void renderString(int x, int y, int gauge_id, const char *str);
312 	void renderStringAlignCenter(int x, int y, int area_width, const char *s);
313 	void renderPrintf(int x, int y, const char* format, ...);
314 	void renderPrintf(int x, int y, int gauge_id, const char* format, ...);
315 	void renderLine(int x1, int y1, int x2, int y2);
316 	void renderGradientLine(int x1, int y1, int x2, int y2);
317 	void renderRect(int x, int y, int w, int h);
318 	void renderCircle(int x, int y, int diameter);
319 
320 	void unsize(int *x, int *y);
321 	void unsize(float *x, float *y);
322 	void resize(int *x, int *y);
323 	void resize(float *x, float *y);
324 	void setClip(int x, int y, int w, int h);
325 	void resetClip();
326 };
327 
328 class HudGaugeMissionTime: public HudGauge // HUD_MISSION_TIME
329 {
330 	hud_frames time_gauge;
331 
332 	int time_text_offsets[2]; // Mission_time_text_coords[gr_screen.res]
333 	int time_val_offsets[2]; // Mission_time_text_val_coords[gr_screen.res]
334 public:
335 	HudGaugeMissionTime();
336 	void initBitmaps(const char *fname);
337 	void initTextOffsets(int x, int y);
338 	void initValueOffsets(int x, int y);
339 	void render(float frametime);
340 	void pageIn();
341 };
342 
343 class HudGaugeTextWarnings: public HudGauge // HUD_TEXT_FLASH
344 {
345 	int next_flash;
346 	bool flash_flags;
347 public:
348 	HudGaugeTextWarnings();
349 	void render(float frametime);
350 	void initialize();
351 	int maybeTextFlash();
352 };
353 
354 class HudGaugeKills: public HudGauge
355 {
356 	hud_frames Kills_gauge;
357 
358 	int text_offsets[2];
359 	int text_value_offsets[2];
360 public:
361 	HudGaugeKills();
362 	void initBitmaps(const char *fname);
363 	void initTextOffsets(int x, int y);
364 	void initTextValueOffsets(int x, int y);
365 	void render(float frametime);
366 	void pageIn();
367 };
368 
369 class HudGaugeLag: public HudGauge
370 {
371 	hud_frames Netlag_icon;
372 
373 	int flash_timer[2];
374 	bool flash_flag;
375 public:
376 	HudGaugeLag();
377 	void initBitmaps(const char *fname);
378 	void render(float frametime);
379 	void pageIn();
380 
381 	void startFlashLag(int duration = 1400);
382 	bool maybeFlashLag(bool flash_fast = false);
383 };
384 
385 class HudGaugeObjectiveNotify: public HudGauge
386 {
387 protected:
388 	hud_frames	Objective_display_gauge;
389 
390 	int Objective_text_offset_y;
391 	int Objective_text_val_offset_y;
392 	int Subspace_text_offset_y;
393 	int Subspace_text_val_offset_y;
394 	int Red_text_offset_y;
395 	int Red_text_val_offset_y;
396 
397 	int flash_timer[2];
398 	bool flash_flag;
399 public:
400 	HudGaugeObjectiveNotify();
401 	void initBitmaps(const char *fname);
402 	void initObjTextOffsetY(int y);
403 	void initObjValueOffsetY(int y);
404 	void initSubspaceTextOffsetY(int y);
405 	void initSubspaceValueOffsetY(int y);
406 	void initRedAlertTextOffsetY(int y);
407 	void initRedAlertValueOffsetY(int y);
408 
409 	virtual void render(float frametime);
410 	void startFlashNotify(int duration = 1400);
411 	bool maybeFlashNotify(bool flash_fast = false);
412 	void renderObjective();
413 	void renderRedAlert();
414 	void renderSubspace();
415 	void pageIn();
416 	void initialize();
417 };
418 
419 class HudGaugeDamage: public HudGauge
420 {
421 protected:
422 	hud_frames damage_top;
423 	hud_frames damage_middle;
424 	hud_frames damage_bottom;
425 
426 	int header_offsets[2];
427 	int hull_integ_offsets[2];
428 	int hull_integ_val_offset_x;
429 	int middle_frame_start_offset_y;
430 	int subsys_integ_start_offsets[2];
431 	int subsys_integ_val_offset_x;
432 	int bottom_bg_offset;
433 	int line_h;
434 
435 	int Damage_flash_timer;
436 	bool Damage_flash_bright;
437 
438 	int next_flash;
439 	bool flash_status;
440 public:
441 	HudGaugeDamage();
442 	void initBitmaps(const char *fname_top, const char *fname_middle, const char *fname_bottom);
443 	void initHeaderOffsets(int x, int y);
444 	void initHullIntegOffsets(int x, int y);
445 	void initHullIntegValueOffsetX(int x);
446 	void initMiddleFrameStartOffsetY(int y);
447 	void initSubsysIntegStartOffsets(int x, int y);
448 	void initSubsysIntegValueOffsetX(int x);
449 	void initBottomBgOffset(int offset);
450 	void initLineHeight(int h);
451 	void render(float frametime);
452 	void pageIn();
453 	void initialize();
454 };
455 
456 class HudGaugeSupport: public HudGauge
457 {
458 protected:
459 	hud_frames background;
460 
461 	int Header_offsets[2];
462 	int text_val_offset_y;
463 	int text_dock_offset_x;
464 	int text_dock_val_offset_x;
465 public:
466 	HudGaugeSupport();
467 	void initBitmaps(const char *fname);
468 	void initHeaderOffsets(int x, int y);
469 	void initTextValueOffsetY(int y);
470 	void initTextDockOffsetX(int x);
471 	void initTextDockValueOffsetX(int x);
472 	void render(float frametime);
473 	void pageIn();
474 };
475 
476 class HudGaugeMultiMsg: public HudGauge
477 {
478 protected:
479 public:
480 	HudGaugeMultiMsg();
481 	bool canRender();
482 	void render(float frametime);
483 };
484 
485 class HudGaugeVoiceStatus: public HudGauge
486 {
487 protected:
488 public:
489 	HudGaugeVoiceStatus();
490 	void render(float frametime);
491 };
492 
493 class HudGaugePing: public HudGauge
494 {
495 protected:
496 public:
497 	HudGaugePing();
498 	void render(float frametime);
499 };
500 
501 class HudGaugeSupernova: public HudGauge
502 {
503 public:
504 	HudGaugeSupernova();
505 	void render(float frametime);
506 };
507 
508 class HudGaugeFlightPath: public HudGauge
509 {
510 	hud_frames Marker;
511 
512 	int Marker_half[2];
513 public:
514 	HudGaugeFlightPath();
515 	void initBitmap(const char *fname);
516 	void initHalfSize(int w, int h);
517 	void render(float frametime);
518 };
519 
520 HudGauge* hud_get_gauge(const char* name);
521 
522 extern SCP_vector<HudGauge*> default_hud_gauges;
523 
524 extern flag_def_list Hud_gauge_types[];
525 extern int Num_hud_gauge_types;
526 
527 #endif	/* __HUD_H__ */
528 
529