Debian Codegraph @

Descent3/SmallViews.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/SmallViews.cpp $
21 * $Revision: 33 $
22 * $Date: 4/19/00 5:07p $
23 * $Author: Matt $
24 *
25 * System to draw small 3D views on the screen
26 *
27 * $Log: /DescentIII/main/SmallViews.cpp $
28 *
29 * 33 4/19/00 5:07p Matt
30 * From Duane for 1.4
31 * Added checks, asserts, and fixes for bad return values
32 *
33 * 32 4/06/00 9:25a Matt
34 * Fixed a screen clear problem on at ATI Rage Fury Maxx in dual-chip mode
35 * by forcing the screen to clear four times (instead of three).
36 *
37 * 31 5/17/99 11:19a Samir
38 * scale small views in full hud when shrinking screen.
39 *
40 * 30 5/08/99 1:06a Samir
41 * shrink font according to screen size.
42 *
43 * 29 5/01/99 12:41a Matt
44 * Added static when a small viewer object dies.
45 *
46 * 28 4/29/99 2:00a Jason
47 * fixed guided missile zbuffer problem
48 *
49 * 27 4/19/99 1:31p Matt
50 * Made guided missile small view use title and crosshairs flag, instead
51 * of doing special check for guided missile.
52 *
53 * 26 4/14/99 3:57a Jeff
54 * fixed case mismatch in #includes
55 *
56 * 25 4/12/99 1:10a Matt
57 * Added labels for small views.
58 *
59 * 24 3/31/99 3:01p Jason
60 * fixed bug with guided missile
61 *
62 * 23 3/22/99 4:26p Samir
63 * added toggles for guided missile view and reticle.
64 *
65 * 22 3/04/99 2:56p Samir
66 * clear out flags for displaying small views for cockpit in
67 * ResetSmallViews.
68 *
69 * 21 2/27/99 5:09p Jason
70 * clear screen 3 times with z buffer
71 *
72 * 20 2/10/99 1:47p Matt
73 * Changed object handle symbolic constants
74 *
75 * 19 2/08/99 5:37p Jason
76 * added comment
77 *
78 * 18 1/31/99 7:26p Matt
79 * Renamed a bunch of functions to have HUD capitalized
80 *
81 * 17 1/13/99 12:42p Matt
82 * Added code to close a popup view
83 *
84 * 16 1/08/99 4:34p Matt
85 * Added an Int3()
86 *
87 * 15 12/08/98 1:47p Matt
88 * If the main view is showing the rear view, make the small rear views
89 * show the forward view (got it?)
90 *
91 * 14 10/23/98 12:05p Matt
92 * Position the small views in the cockpit relative to the window
93 * position.
94 *
95 * 13 10/21/98 10:18a Matt
96 * Don't do small views in letterbox mode
97 *
98 * 12 10/20/98 9:23p Matt
99 * When rendering the small views, set Viewer_object to the local viewer,
100 * to keep the player ship from rendering in the small views when using
101 * the external camera.
102 *
103 * 11 10/20/98 12:42p Matt
104 * Made the small views work on the cockpit.
105 *
106 * 10 10/20/98 12:28a Matt
107 * When turning off a small view, redraw the background screen.
108 *
109 * 9 10/14/98 6:01p Jason
110 * fixed small views going too high on the terrain
111 *
112 * 8 10/08/98 5:59p Jason
113 * fixed object and room popping with small views
114 *
115 * 7 5/26/98 11:36a Matt
116 * Changed small view system to allow the popup window in any of the three
117 * positions, to allow any window to be the "bigger" size, and to restore
118 * the old view when a popup view goes away.
119 *
120 * 6 4/30/98 6:46p Jason
121 * more framerate testing
122 *
123 * 5 2/09/98 3:19p Matt
124 * Added function to return the viewer object for a small view
125 *
126 * 4 2/04/98 6:09p Matt
127 * Changed object room number to indicate a terrain cell via a flag. Got
128 * rid of the object flag which used to indicate terrain.
129 *
130 * 3 2/04/98 12:50a Matt
131 * Added ability to view from a gun point in small views.
132 * Made pop-up view separate from and bigger than other small views.
133 * Added real keys to set view mode in the three small views.
134 * Changed rendering functions a bit to accommodate smallview changes.
135 *
136 * 2 1/30/98 2:55p Matt
137 * Added SmallViews system
138 *
139 * 1 1/29/98 11:15a Matt
140 *
141 */
142
143 #include "SmallViews.h"
144
145 #include "descent.h"
146 #include "pserror.h"
147
148 #include "game.h"
149 #include "object.h"
150 #include "terrain.h"
151 #include "render.h"
152 #include "gameloop.h"
153 #include "weapon.h"
154 #include "findintersection.h"
155 #include "config.h"
156 #include "terrain.h"
157 #include "gauges.h"
158 #include "cockpit.h"
159 #include "player.h"
160 #include "grtext.h"
161 #include "stringtable.h"
162 #include "gamefont.h"
163
164 // How many small views
165 #define NUM_SMALL_VIEWS 3
166
167 // How many chars to allow for the window label
168 #define LABEL_LEN 19
169
170 // Struct to keep data for small views
171 struct small_view {
172 int objhandle; // the object we're viewing from, or OBJECT_HANDLE_NONE if view not active
173 int flags; // various flags
174 float timer; // how much longer time window stays alive, if SVF_TIMED flag is set
175 float zoom; // the zoom for this window
176 int gun_num; // which gun to view from, or -1 if none
177 char label[LABEL_LEN + 1]; // the label for the window
178 };
179
180 // The array of small views
181 static small_view Small_views[NUM_SMALL_VIEWS];
182
183 // Saved copies of small views for while a pop-up is active
184 static small_view Small_views_save[NUM_SMALL_VIEWS];
185
186 // guided missile in small view? if so, then true.
187 bool Guided_missile_smallview = false;
188 static int Guided_missile_objhandle = OBJECT_HANDLE_NONE;
189
190 // Returns the viewer object for the specified small view.
191 // If the view isn't active, returns OBJECT_HANDLE_NONE.
192 int GetSmallViewer(int window) {
193 ASSERT((window >= 0) && (window < NUM_SMALL_VIEWS));
194
195 return Small_views[window].objhandle;
196 }
197
198 // Create a new small view. If there is already a view in the given window, the old view gets blown away.
199 // Parameters: window - which window to open. See constants in SmallViews.h
200 // objhandle - handle for the object to view from
201 // flags - various view attributes. See defines in header file.
202 // time - how long to keep the window up. If 0, keep up indefinitely
203 // zoom - the zoom for this window. If 0, use the default zoom
204 // gun_num - which gun to view from. if -1, use viewer's center and orientation.
205 // label - the label for the window
206 // Returns: which window was opened, or -1 if window couldn't be created
207 int CreateSmallView(int window, int objhandle, int flags, float time, float zoom, int gun_num, const char *label) {
208 small_view *svp;
209
210 // Check for valid window
211 if ((window < 0) || (window >= NUM_SMALL_VIEWS)) {
212 Int3();
213 window = SVW_LEFT;
214 }
215
216 // Make sure the object exists
217 if (!ObjGet(objhandle))
218 return -1;
219
220 svp = &Small_views[window];
221
222 // If this is a pop-up, and there was a non-popup active, save previous view
223 if ((flags & SVF_POPUP) && (svp->objhandle != OBJECT_HANDLE_NONE) && !(svp->flags & SVF_POPUP))
224 Small_views_save[window] = *svp;
225
226 // Set vars for new view
227 svp->objhandle = objhandle;
228 svp->zoom = zoom;
229 svp->gun_num = gun_num;
230 svp->flags = flags;
231 svp->timer = time;
232 if (time != 0.0)
233 svp->flags |= SVF_TIMED;
234
235 strncpy(svp->label, label ? label : "", LABEL_LEN);
236 svp->label[LABEL_LEN] = 0; // strncpy won't terminate if at max len
237
238 if (window == SVW_LEFT)
239 Disable_primary_monitor = 1;
240 else if (window == SVW_RIGHT)
241 Disable_secondary_monitor = 1;
242
243 // if guided view, set flag to true
244 if (ObjGet(objhandle) == Players[Player_num].guided_obj) {
245 Guided_missile_smallview = true;
246 Guided_missile_objhandle = objhandle;
247 }
248
249 return window;
250 }
251
252 // Called to get rid of all the small views & init system
253 void ResetSmallViews() {
254 for (int v = 0; v < NUM_SMALL_VIEWS; v++) {
255 Small_views[v].objhandle = OBJECT_HANDLE_NONE;
256 Small_views_save[v].objhandle = OBJECT_HANDLE_NONE;
257 }
258 Disable_primary_monitor = 0;
259 Disable_secondary_monitor = 0;
260 Guided_missile_smallview = false;
261 Guided_missile_objhandle = OBJECT_HANDLE_NONE;
262 }
263
264 #include "vclip.h"
265
266 // How long the static is visbile
267 #define STATIC_TIME 0.25f
268
269 // Render into one of the small windows
270 void RenderSmallWindow(int left, int top, int right, int bot, object *viewer, vector *viewer_eye, int viewer_roomnum,
271 matrix *viewer_orient, int flags, float zoom, ddgr_color outline_color, char *label,
272 float timer) {
273 int width = right - left, height = bot - top;
274
275 // Set up for rendering
276 StartFrame(left, top, right, bot);
277
278 // scale font according to screen size
279 float font_aspect_x = (float)Game_window_w / Max_window_w;
280
281 if (font_aspect_x <= 0.60f) {
282 grtext_SetFontScale(0.60f);
283 } else if (font_aspect_x <= 0.80f) {
284 grtext_SetFontScale(0.80f);
285 } else {
286 grtext_SetFontScale(1.0f);
287 }
288
289 // Draw either static or 3D
290 if (flags & SVF_STATIC) {
291 // draw static
292 static int handle = -1;
293 if (handle == -1)
294 handle = FindTextureName("Static");
295
296 ASSERT(handle != -1); // DAJ -1FIX
297
298 PageInVClip(GameTextures[handle].bm_handle);
299 vclip *vc = &GameVClips[GameTextures[handle].bm_handle];
300
301 int frame = vc->num_frames * (STATIC_TIME - timer) / STATIC_TIME;
302 if (frame == vc->num_frames)
303 frame--;
304 int bm_handle = vc->frames[frame];
305
306 rend_SetAlphaType(AT_CONSTANT_TEXTURE);
307 rend_SetAlphaValue(255);
308 rend_SetLighting(LS_NONE);
309 rend_SetColorModel(CM_MONO);
310 rend_SetOverlayType(OT_NONE);
311 rend_SetWrapType(WT_CLAMP);
312 rend_SetFiltering(0);
313
314 rend_DrawScaledBitmap(0, 0, width - 1, height - 1, bm_handle, 0.0, 0.0, 1.0, 1.0);
315 } else {
316 // draw 3D view
317 ASSERT(viewer != NULL);
318 ASSERT(viewer_eye != NULL);
319 object *save_viewer_object = Viewer_object;
320 bool rear_view = ((flags & SVF_REARVIEW) != 0);
321
322 // Reset facings for mine stuff
323 ResetFacings();
324
325 // Set the viewer for this render
326 Viewer_object = viewer;
327
328 // Render the world
329 GameRenderWorld(viewer, viewer_eye, viewer_roomnum, viewer_orient, zoom, rear_view);
330
331 // Restore the viewer
332 Viewer_object = save_viewer_object;
333 }
334
335 // Draw window outline
336 if (outline_color != -1) {
337 rend_SetFlatColor(outline_color);
338 rend_DrawLine(0, 0, width - 1, 0);
339 rend_DrawLine(width - 1, 0, width - 1, height - 1);
340 rend_DrawLine(width - 1, height - 1, 0, height - 1);
341 rend_DrawLine(0, height - 1, 0, 0);
342 }
343
344 // Draw the label
345 if (label[0]) {
346 int w = grtext_GetLineWidth(label);
347 grtext_SetFont(HUD_FONT);
348 grtext_SetColor(HUD_COLOR);
349 grtext_SetAlpha(255);
350 grtext_SetFlags(0);
351 grtext_Puts((width - w) / 2, 2, label);
352 grtext_Flush();
353 }
354
355 // Draw the crosshairs
356 if (flags & SVF_CROSSHAIRS) {
357 int cx = width / 2, cy = height / 2;
358 int line_w = width / 50, line_h = height / 50;
359 rend_SetZBufferState(0);
360 rend_SetFlatColor(GR_GREEN);
361 rend_DrawLine(cx - line_w, cy, cx + line_w, cy);
362 rend_DrawLine(cx, cy - line_h, cx, cy + line_h);
363 rend_SetZBufferState(1);
364 }
365
366 // Done rendering
367 EndFrame();
368 }
369
370 // Find the room number of a gun point
371 int GetGunRoom(vector *p0, int roomnum, int objnum, vector *p1) {
372 fvi_query fq;
373 fvi_info hit_data;
374 int fate;
375
376 fq.p0 = p0;
377 fq.startroom = roomnum;
378 fq.p1 = p1;
379 fq.rad = 0;
380 fq.thisobjnum = objnum;
381 fq.ignore_obj_list = NULL;
382 fq.flags = 0;
383
384 fate = fvi_FindIntersection(&fq, &hit_data);
385 if (fate != HIT_NONE)
386 Int3();
387
388 return hit_data.hit_room;
389 }
390
391 #define SMALL_WINDOW_W 120
392 #define SMALL_WINDOW_H 120
393 #define SPACE_BETWEEN_WINDOWS ((640 - 3 * SMALL_WINDOW_W) / 3)
394
395 extern int Point_visible_last_frame;
396
397 // Draw all the active small views
398 void DrawSmallViews() {
399 int v;
400 small_view *svp;
401 int left, top, right, bot;
402 ddgr_color outline_color;
403 vector *viewer_eye, gun_pos;
404 matrix *viewer_orient, gun_orient;
405 matrix save_orient;
406 vector save_eye;
407 int viewer_roomnum;
408
409 // if letterbox, don't draw the small views
410 if (GetHUDMode() == HUD_LETTERBOX)
411 return;
412
413 bool lsave = Detail_settings.Dynamic_lighting;
414
415 g3_GetUnscaledMatrix(&save_orient);
416 g3_GetViewPosition(&save_eye);
417
418 Detail_settings.Dynamic_lighting = false;
419
420 for (v = 0, svp = Small_views; v < NUM_SMALL_VIEWS; v++, svp++) {
421 if (svp->objhandle == OBJECT_HANDLE_NONE)
422 continue;
423
424 object *viewer = ObjGet(svp->objhandle);
425 bool kill_viewer = false;
426
427 // Bail if object not around any more, or if its gone too high
428 if (!viewer || (viewer->type == OBJ_DUMMY))
429 kill_viewer = true;
430
431 if (viewer && OBJECT_OUTSIDE(viewer) && viewer->pos.y >= MAX_TERRAIN_HEIGHT)
432 kill_viewer = true;
433
434 // If not view & not already static, switch to static
435 if (kill_viewer && !(svp->flags & SVF_STATIC)) {
436 // Viewer is dead, so switch to static
437 svp->flags |= SVF_STATIC | SVF_TIMED;
438 svp->timer = STATIC_TIME;
439 svp->gun_num = -1;
440 }
441
442 if (v == SVW_CENTER) // no center window for now
443 Int3();
444
445 if (CockpitState() == COCKPIT_STATE_QUASI) {
446 continue;
447 }
448
449 else if (CockpitState() == COCKPIT_STATE_FUNCTIONAL) {
450 // draw on the cockpit
451 if (!GetCockpitWindowCoords((v == SVW_LEFT) ? 0 : 1, &left, &top, &right, &bot)) {
452 continue;
453 }
454
455 // Move relative to the current window
456 left += Game_window_x;
457 right += Game_window_x;
458 top += Game_window_y;
459 bot += Game_window_y;
460
461 outline_color = -1;
462 } else {
463 // draw windows on HUD based on the screen size
464 ASSERT(CockpitState() == COCKPIT_STATE_DORMANT);
465
466 // Compute size of window
467 int x, y, w, h, spacing;
468 w = Game_window_h / 4;
469 spacing = ((Game_window_w - (NUM_SMALL_VIEWS)*w) / 3);
470 x = ((spacing + w) / 2) + (v * (spacing + w)) + Game_window_x;
471 h = w;
472 y = Game_window_y + (Game_window_h - h / 2 - Game_window_h / 24);
473
474 // If bigger window, increase size
475 if (svp->flags & SVF_BIGGER) {
476 w += w / 4;
477 h += h / 4;
478 }
479
480 left = x - (w / 2);
481 top = y - (h / 2);
482 right = x + (w / 2);
483 bot = y + (h / 2);
484
485 // Set window color
486 outline_color = (svp->flags & SVF_POPUP) ? GR_RGB(255, 0, 0) : GR_RGB(0, 0, 255);
487 }
488
489 // Get view info from the gun or from the object itself
490 if (svp->gun_num != -1) {
491 // using a gun
492 vector gun_vector;
493 WeaponCalcGun(&gun_pos, &gun_vector, viewer, svp->gun_num);
494 vm_VectorToMatrix(&gun_orient, &gun_vector, NULL, NULL);
495 viewer_eye = &gun_pos;
496 viewer_orient = &gun_orient;
497 viewer_roomnum = GetGunRoom(&viewer->pos, viewer->roomnum, OBJNUM(viewer), &gun_pos);
498 } else if (viewer) {
499 // no gun, so just use the object
500 viewer_eye = &viewer->pos;
501 viewer_orient = &viewer->orient;
502 viewer_roomnum = viewer->roomnum;
503 } else {
504 ASSERT(svp->flags & SVF_STATIC);
505 viewer_eye = NULL;
506 viewer_orient = NULL;
507 viewer_roomnum = -1;
508 }
509
510 // Determine whether to draw rear view. If view has rear view set and the viewer is
511 // a player and the player has rear view on, then draw forward view
512 int flags = svp->flags;
513 if ((flags & SVF_REARVIEW) && (viewer == Viewer_object) && (viewer->type == OBJ_PLAYER) &&
514 (Players[viewer->id].flags & PLAYER_FLAGS_REARVIEW))
515 flags &= ~SVF_REARVIEW;
516
517 // Let's render
518 Point_visible_last_frame = -1;
519 RenderSmallWindow(left, top, right, bot, viewer, viewer_eye, viewer_roomnum, viewer_orient, flags, svp->zoom,
520 outline_color, svp->label, svp->timer);
521 Point_visible_last_frame = -1;
522
523 // Update time for this view
524 if (svp->flags & SVF_TIMED) {
525 svp->timer -= Frametime;
526 if (svp->timer < 0.0) {
527 CloseSmallView(v);
528 }
529 }
530 }
531
532 Detail_settings.Dynamic_lighting = lsave;
533
534 // Resets aspect ratio
535 g3_StartFrame(&save_eye, &save_orient, Render_zoom);
536 g3_EndFrame();
537 }
538
539 // Parameters: window - the window to get rid of
540 void CloseSmallView(int window) {
541 // Mark the window as inactive
542 if (Small_views[window].objhandle == Guided_missile_objhandle) {
543 // mark guided missile small view as inactive.
544 Guided_missile_objhandle = OBJECT_HANDLE_NONE;
545 Guided_missile_smallview = false;
546 }
547
548 Small_views[window].objhandle = OBJECT_HANDLE_NONE;
549
550 // Restore old view if there was one
551 if (Small_views_save[window].objhandle != OBJECT_HANDLE_NONE) {
552 Small_views[window] = Small_views_save[window];
553 Small_views_save[window].objhandle = OBJECT_HANDLE_NONE;
554 } else {
555 Clear_screen = 4; // force background redraw
556
557 if (window == SVW_LEFT)
558 Disable_primary_monitor = 0;
559 else if (window == SVW_RIGHT)
560 Disable_secondary_monitor = 0;
561 }
562 }
563
564 // Get rid of a small view if it's a popup window
565 // Parameters: window - the window to get rid of
566 void ClosePopupView(int window) {
567 if (Small_views[window].flags & SVF_POPUP)
568 CloseSmallView(window);
569 }