Debian Codegraph @

editor/gameeditor.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/editor/gameeditor.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * Game to editor/editor to Game interface
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 103 9/24/99 12:01p Samir
30 * use directinput when -alternatejoy is selected (like 1.0)
31 *
32 * 102 8/18/99 12:05p 3dsmax
33 * Joystick fix (from Samir)
34 *
35 * 101 4/29/99 10:01p Samir
36 * use emulation mode of joystick first.
37 *
38 * 100 4/26/99 9:11p Matt
39 * Use small font instead of old UI font.
40 *
41 * 99 4/09/99 12:02p Samir
42 * joystick changes (Win32 DirectInput support)
43 *
44 * 98 3/27/99 7:22p Jeff
45 * fixed cinematics when going from one cut to another immediatly. Added
46 * start transition
47 *
48 * 97 3/04/99 6:09p Samir
49 * initialize slowkey for game.
50 *
51 * 96 2/27/99 6:24p Sean
52 * may fix sequencing problems with rendering windows going in and out of
53 * editor.
54 *
55 * 95 2/20/99 3:43a Jeff
56 * removed useless call to free pilot mission data
57 *
58 * 94 2/15/99 7:47p Jeff
59 * new pilot file class and read/write system checked in...should be more
60 * robust than old
61 *
62 * 93 2/11/99 7:24p Nate
63 * Added hiding of Dallas when going into game, and restoring of Dallas
64 * when returning to editor
65 *
66 * 92 2/10/99 3:44p Dan
67 * pause editor while reloading level
68 *
69 * 91 2/01/99 10:35a Matt
70 * Fixed the initial player position in the game when playing from the
71 * editor.
72 *
73 * 90 1/25/99 6:47p Samir
74 * allow slow keyboard
75 *
76 * 89 1/25/99 11:02a Samir
77 * revamped mouse and key controls.
78 *
79 * 88 1/22/99 3:19p Jason
80 * added 256x256 textures to help with terrain skies
81 *
82 * 87 1/19/99 12:14p Matt
83 * Fixed problem if there was no player object when coming back from the
84 * game
85 *
86 * 86 1/15/99 7:52p Chris
87 * Updated ObjSetPos() to include a f_update_attach_children flag
88 *
89 * 85 1/11/99 11:12a Jeff
90 * handle untitled levels and scripts
91 *
92 * 84 1/08/99 2:56p Samir
93 * Ripped out OSIRIS1.
94 *
95 * 83 1/07/99 12:28p Samir
96 * Call to InitD3Music added parameter.
97 *
98 * 82 12/31/98 7:33p Jeff
99 * reset osiris timers when going editor->game
100 *
101 * 81 12/17/98 12:08p Jeff
102 * first checkin of new implementation of OSIRIS (old OSIRIS no longer
103 * works)
104 *
105 * 80 12/15/98 4:29p Jeff
106 * reset pilot mission data when we go to load in a pilot
107 *
108 * 79 11/13/98 2:28p Samir
109 * new music system.
110 *
111 * 78 11/06/98 12:45p Chris
112 *
113 * 77 11/03/98 6:43p Jeff
114 * new low-level & high level Force Feedback system implemented, handles
115 * window losing focus, etc.
116 *
117 * 76 11/02/98 6:15p Chris
118 * Room AABBs get saved with the level and the sort_face and dec_sort_face
119 * list s have been removed
120 *
121 * 75 10/15/98 1:36p Jeff
122 * handle the case where the default pilot gets deleted...forcing a new
123 * pilot to be made
124 *
125 * 74 10/13/98 3:14p Samir
126 * maybe hacked temp fix to one editor restore window problem that keeps
127 * coming up but NEVER happens/happened on my machine under 95 or NT.
128 *
129 * 73 9/30/98 10:25a Jason
130 * another attempt to the get the OpenGL window thing working reliably...
131 *
132 * 72 9/29/98 6:22p Samir
133 * this may fix things.
134 *
135 * 71 9/24/98 1:13p Samir
136 * maybe fix weird editor problem.
137 *
138 * 70 9/22/98 10:32a Jason
139 * made preferred renderer default to OpenGL
140 *
141 * 69 9/18/98 7:38p Jeff
142 * creation of low-level forcefeedback and beginning of high-level
143 * forcefeedback
144 *
145 * 68 9/18/98 2:55p Matt
146 * Fixed bug resetting viewer position when returning to the editor after
147 * playing from the main menu.
148 *
149 * 67 9/18/98 1:27p Jason
150 * cleaned up renderer initting
151 *
152 * 66 9/08/98 3:25p Matt
153 * Fixed, hopefully, problems with getting the the player position &
154 * orientation back to the editor when playing from the editor.
155 *
156 * 65 8/25/98 11:37p Matt
157 * Fixed (I think) problem setting the viewer from the player when going
158 * from the game to the editor after the player has been deleted.
159 *
160 * 64 8/15/98 5:16p Matt
161 * Added new Base_directory variable. Got rid of D3_LOCAL check and
162 * 'local directory' registry variable.
163 *
164 * 63 8/07/98 2:44p Jeff
165 * reset player ship permissions when going game->editor
166 *
167 * 62 7/28/98 5:40p Samir
168 * music init and close within editor.
169 *
170 * 61 7/08/98 6:27p Samir
171 * stream library integrated with highlevel sound system.
172 *
173 * 60 7/01/98 4:59p Samir
174 * set mouse mode when entering and leaving game.
175 *
176 * 59 6/30/98 4:20p Samir
177 * added ddio_Close as standalone. ddio_Init will no longer close
178 * itself.
179 *
180 * 58 6/29/98 6:43p Samir
181 * IO system initializes using Descent object always (especially for
182 * fullscreen debug!)
183 *
184 * 57 6/22/98 12:00p Chris
185 * Working on sound system to make it in a nice shape.
186 *
187 * 56 6/16/98 3:48p Chris
188 * Updated the sound system and added the start of sound streaming
189 *
190 * 55 6/12/98 5:09p Samir
191 * resize window later.
192 *
193 * 54 5/11/98 4:40p Chris
194 * AI info is now a malloc'ed pointer
195 *
196 * 53 5/06/98 4:32p Samir
197 * moved ui init back to it's proper place.
198 *
199 * 52 5/05/98 5:16p Samir
200 * took out calls to init UI system (that's done now in SetScreenMode)
201 *
202 * 51 5/01/98 1:54p Jeff
203 * sound lib inits before mainmenu when going editor->game
204 *
205 * 50 4/21/98 2:29p Matt
206 * Added mprintf()s
207 *
208 * 49 3/20/98 1:20p Jeff
209 * Save/restore Default_pilot when switching from game<->editor
210 *
211 * 48 3/19/98 11:27a Samir
212 * Better error checking.
213 *
214 * 47 3/13/98 5:04p Jason
215 * fixed problem with preferred renderer
216 *
217 * 46 3/13/98 1:22p Jason
218 * Moved UseHardware flag to the renderer lib where it belongs
219 *
220 * 45 3/12/98 9:16p Matt
221 * When going from the game to the editor, only copy the player's position
222 * to the viewer if was playing directly from the editor, and not if went
223 * from the editor to the main menu.
224 *
225 * 44 3/10/98 5:16p Samir
226 * Got debug callbacks working when you hit an Int3.
227 *
228 * 43 3/10/98 12:50p Samir
229 * Initialize NewUI system at startup.
230 *
231 * 42 3/06/98 2:22p Samir
232 * Added fullscreen windowed version.
233 *
234 * 41 3/05/98 6:39p Samir
235 * Initialize ui system with UI_FONT
236 *
237 * 40 2/26/98 1:00p Samir
238 * Hopefully keystrokes only reach the game.
239 *
240 * 39 2/23/98 6:48p Samir
241 * Make sure to initialize new window objects.
242 *
243 * 38 2/11/98 5:55p Samir
244 * Now InitD3XDebug and CloseD3XDebug are called when you enter and leave
245 * the game. This as well as ui_Init and ui_Close
246 *
247 * 37 2/05/98 2:57p Matt
248 * Changed code to use ObjSetPos()
249 *
250 * 36 2/04/98 6:09p Matt
251 * Changed object room number to indicate a terrain cell via a flag. Got
252 * rid of the object flag which used to indicate terrain.
253 *
254 * 35 1/06/98 2:44p Jason
255 * another pass for multiplayer
256 *
257 * 34 1/06/98 1:26p Matt
258 * Cleaned up interfaces to rendering routines, deleted unused code, etc.
259 *
260 * 33 1/02/98 6:39p Matt
261 * Got rid of Render_viewport
262 *
263 * 32 1/02/98 5:29p Samir
264 * Set PROGRAM(editor) to 0 when going to game, and 1 when in editor.
265 *
266 * 31 1/02/98 5:32p Chris
267 * More radical changes to the sound system
268 *
269 * 30 12/29/97 5:46p Samir
270 * Implemented new video and font changes
271 *
272 * 29 12/19/97 12:21p Jason
273 * changes for 2d/3d integration
274 *
275 * 28 12/19/97 11:25a Samir
276 * g3_StartFrame and g3_EndFrame replaced by EditorStartFrame and
277 * EditorEndFrame
278 *
279 * 27 12/18/97 3:49p Jason
280 * a new first pass at networking!
281 *
282 * 26 11/05/97 3:45p Samir
283 * After ddio_Init, recall InitControls.
284 *
285 * 25 10/17/97 5:43p Samir
286 * Hopefully this fixes things.
287 *
288 * 24 10/17/97 5:26p Samir
289 * Move window after screen res change.
290 *
291 * 23 10/17/97 5:18p Samir
292 * ddio_Init now does mouse opening and closing, where it should be.
293 *
294 * 22 10/17/97 4:26p Samir
295 * More modifications for debug mode.
296 *
297 * 21 10/17/97 3:14p Samir
298 * Hopefully when debugging, you can get back into debugger.
299 *
300 * 20 10/16/97 7:35p Samir
301 * For fullscreen game from editor, hide all user interface elements of
302 * the editor window and use the client area for our display. It's REALLY
303 * hacked, but seems to be the cleanest method of playing the game in
304 * fullscreen. Old method of blanking window doesn't work.
305 *
306 * 19 10/16/97 4:41p Samir
307 * Don't set game window to be active. It fucks something up under NT
308 * (and only happens in the editor.)
309 *
310 * 18 10/16/97 2:31p Samir
311 * Reinitialize ddio library when going into game.
312 *
313 * 17 10/02/97 12:35p Samir
314 * Free script when loading in GameSave.d3l.
315 *
316 * 16 9/05/97 1:25p Samir
317 * Moved some code to InitThisLevel for game initialization.
318 *
319 * 15 8/26/97 11:38a Samir
320 * Added Editor_active global to determine whether editor is currently
321 * active (useful for any window using the gr library.)
322 *
323 * 14 8/22/97 5:09p Jason
324 * save/restore game editor view when appropriate
325 *
326 * 13 8/12/97 1:13p Chris
327 * Added AABBs.
328 *
329 * 12 8/11/97 5:55p Chris
330 * Got rid of a Chrishack. :) Added MakeBoa in a correct manner.
331 *
332 * 11 8/01/97 11:59a Samir
333 * Added a preference for debugging in fullscreen.
334 *
335 * 10 7/25/97 12:02p Samir
336 * Funky window resizings gone because we're using the GDI-DirectX
337 * graphics system for running the game.
338 *
339 * 9 7/24/97 6:34p Jeff
340 * Turned on mouse by default
341 *
342 * 8 7/22/97 7:07p Matt
343 * Cleaned up D3EditState, moving some vars in and some out, and renaming
344 * and changing a few others
345 *
346 * 7 7/15/97 4:57p Mark
347 * Corrected usage of UseHardware flag
348 *
349 * 41 6/16/97 4:46p Samir
350 * OpenGL works in window too.
351 *
352 * 40 6/16/97 2:34p Jason
353 * added 3dfx support
354 *
355 * 39 6/13/97 4:11p Jason
356 * took out redundant call to rend_Close
357 *
358 * 38 6/12/97 6:35p Samir
359 * Implented new changes to GR system
360 *
361 * 37 6/11/97 2:40p Samir
362 * Adjusted to new gameos system.
363 *
364 * 36 6/09/97 4:15p Samir
365 * Hide cursor while in game.
366 *
367 * 35 5/29/97 3:25p Chris
368 *
369 * 34 5/27/97 7:23p Matt
370 * Fixed (hopefully for real this time) the same problem I tried to fix
371 * last time.
372 *
373 * 33 5/27/97 5:41p Jason
374 * added toggling of opengl renderer
375 *
376 * 32 5/27/97 5:22p Matt
377 * Fixed editor->game->editor code to save & load gamesave.d3l in same
378 * directory.
379 *
380 * 31 5/27/97 4:39p Jason
381 * changes to support OpenGL
382 *
383 * 30 5/22/97 4:03p Samir
384 * HACK! Assigned hOpenGLWnd.
385 *
386 * 29 5/16/97 3:40p Matt
387 * When going from game->editor, only copy player to viewer if the levels
388 * are the same, which we determine by checking the mission name & level
389 * number (though I think it would be better to check the filename.)
390 *
391 * 28 5/15/97 5:14p Matt
392 * When going from game to editor, set terrain/mine editing based on where
393 * the player is
394 *
395 * 27 5/15/97 3:20p Samir
396 * Fixed problem with editor being show during gameplay.
397 *
398 * 26 5/15/97 2:07p Samir
399 * minimize editor when going from editor to game.
400 *
401 * 25 5/15/97 1:59a Chris
402 *
403 * 24 5/06/97 4:11p Matt
404 * Fixed a bunch of code that deals with the player and viewer objects
405 * when going between the game and editor.
406 *
407 * 23 5/06/97 1:17p Jason
408 * checked in for matt
409 *
410 * 22 5/01/97 6:08p Matt
411 * When switching between editor & game, save & load level to temp file.
412 *
413 * 21 4/24/97 1:33p Chris
414 * Sound initializations are all now editor friendly. Fullscreen,
415 * windowed, and editor.
416 *
417 * 20 4/23/97 6:06p Jason
418 * made player ship and weapons work correctly together (first pass)
419 *
420 * 19 4/03/97 8:37p Chris
421 * Added the Main_OS object pass in the sound library
422 * initialization code.
423 *
424 * 18 4/03/97 5:45p Jason
425 * make player/viewer object switch flags if over terrain
426 *
427 * 17 4/01/97 11:00p Matt
428 * Changed editor to keep a viewer object (type camera) seperate from the
429 * player object. This camera, and not the player, is now moved by
430 * slewing, the C key, etc. When going into the game, the viewer position
431 * & orientation are copied to the player. When going back to the editor,
432 * the player position is copied to the viewer, and the player object is
433 * reset to its start location.
434 *
435 * 16 3/31/97 1:23p Samir
436 * Fixed problem when going to fullscreen mode when last mode was GDI,
437 * which then caused a crash. Fixed this by moving graphics and sound
438 * initialization to after new window is created and processed.
439 *
440 * 15 3/28/97 11:46a Chris
441 * Make sound code use editor's window handle
442 *
443 * 14 3/27/97 10:48a Chris
444 * Removed some bad code
445 *
446 * 13 3/17/97 5:12p Chris
447 * Allowed new sound code to be demo'ed.
448 *
449 * 12 3/17/97 11:54a Chris
450 * Forced a valid mass and drag (hack). But, I did use chrishack so I
451 * will remove it later. This fixes the problem with imported D2 levels
452 * have invalid player data.
453 *
454 * 11 3/12/97 6:31p Chris
455 * Added player flight controls while in game
456 *
457 * 10 2/28/97 11:03a Samir
458 * Changes to reflect newer gameos interface.
459 *
460 * 9 2/11/97 3:48p Samir
461 * Added ddio.h
462 *
463 * 8 2/11/97 2:45p Samir
464 * flushed keyboard when entering or leaving game from editor.
465 *
466 * 7 2/05/97 12:17p Samir
467 * Set window size for both fullscreen and windowed versions.
468 *
469 * 6 2/04/97 6:23p Samir
470 * Don't make windowed game window topmost.
471 *
472 * 5 2/04/97 5:33p Samir
473 * Always assert EDITOR_MODE when leaving MainLoop.
474 *
475 * 4 2/04/97 5:25p Samir
476 * Support entering either menu or game from editor.
477 *
478 * 3 1/30/97 6:43p Samir
479 * stupid forgetting an argument
480 *
481 * 2 1/30/97 6:21p Samir
482 * New gameos.h implementation
483 *
484 * $NoKeywords: $
485 */
486
487 #include "stdafx.h"
488 #include "editor.h"
489 #include "MainFrm.h"
490
491 #include "ui.h"
492 #include "render.h"
493 #include "pserror.h"
494 #include "gr.h"
495 #include "mono.h"
496 #include "init.h"
497 #include "game.h"
498 #include "descent.h"
499 #include "Application.h"
500 #include "AppDatabase.h"
501 #include "program.h"
502 #include "ddio.h"
503 #include "object.h"
504 #include "slew.h"
505 #include "hlsoundlib.h" //chrishack
506 #include "ds3dlib.h" // chrishack
507 #include "HView.h"
508 #include "player.h"
509 #include "loadlevel.h"
510 #include "renderer.h"
511 #include "BOA.h"
512 //@@#include "d3x.h"
513 //@@#include "d3xdebug.h"
514 #include "ObjScript.h"
515 #include "controls.h"
516 #include "multi.h"
517 #include "3d.h"
518 #include "ddvid.h"
519 #include "gamefont.h"
520 #include "newui.h"
521 #include "pilot.h"
522 #include "streamaudio.h"
523 #include "D3ForceFeedback.h"
524 #include "d3music.h"
525 #include "osiris_dll.h"
526 #include "d3edit.h"
527 #include "editordoc.h"
528 #include "args.h"
529 #include "terrain.h"
530 #include "mem.h"
531 #include "DallasMainDlg.h"
532 #include "gamecinematics.h"
533
534 // ---------------------------------------------------------------------------
535 // local globals
536 // ---------------------------------------------------------------------------
537 static oeApplication *Saved_editor_app = NULL; // the editor's app object
538 static char Editor_dir[_MAX_PATH]; // old editor directory
539 static RECT Editor_wndrect; // editor window rect.
540 unsigned hGameWnd; // used for hack. window handle to draw to.
541 static HMENU hEditorMenu;
542 static DWORD EditorWndStyle;
543 static DWORD EditorBackBrush;
544 static WINDOWPLACEMENT EditorWndPlacement;
545
546 static bool Temp_level_saved = 0; // do we have a current temp level saved?
547 extern bool Disable_editor_rendering;
548
549 int Just_returned_from_game = 0;
550 float Just_returned_time;
551
552 // ---------------------------------------------------------------------------
553 // Prototypes internal
554 // ---------------------------------------------------------------------------
555
556 void GameToEditor(bool); // deinit game, reinit editor
557 void EditorToGame(); // deinit editor, reinit game
558
559 void InitGameEditSystems();
560 void InitEditGameSystems();
561
562 void DeswInitMainWindow(HINSTANCE hinst);
563 void DeswCloseMainWindow();
564
565 LRESULT WINAPI DescentWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
566
567 // ---------------------------------------------------------------------------
568 // Function mode switching functions
569 // ---------------------------------------------------------------------------
570
571 void RunGameFromEditor() {
572 int game_mode = GetFunctionMode(); // save the mode we're going to (menu or editor_game)
573 renderer_type old_rend = PreferredRenderer;
574
575 // Run the damn game
576 mprintf(0, "Entering the game...\n");
577
578 Editor_active = false;
579 PROGRAM(editor) = 0;
580 if (PROGRAM(windowed))
581 PreferredRenderer = RENDERER_OPENGL;
582
583 EditorToGame();
584 MainLoop();
585 GameToEditor(game_mode == EDITOR_GAME_MODE); // if was playing from editor, copy player pos to viewer
586 PROGRAM(editor) = 1;
587 PreferredRenderer = old_rend;
588 Editor_active = true;
589 SetFunctionMode(EDITOR_MODE); // ASSERT WE ARE IN EDITOR MODE!
590
591 mprintf(0, "Returning to editor...\n");
592 }
593
594 // Vars for getting the viewer position back from the game to the editor
595 vector editor_player_pos;
596 matrix editor_player_orient;
597 int editor_player_roomnum;
598
599 uint16_t *Save_sky_data = NULL;
600
601 void GameToEditor(bool set_viewer_from_player) {
602 /* We have to reinitialize the DDGR system for windowed mode.
603 This will annihilate all previous DDGR settings.
604 */
605 Cinematic_Close();
606
607 ddio_init_info io_info;
608
609 theApp.pause();
610 // Sleep to prevent race conditions with the display drivers
611 Sleep(2000);
612
613 // Close down whatever renderer we were using and restore the software renderer
614 rend_Init(RENDERER_OPENGL, Saved_editor_app, NULL);
615
616 if (Game_screen)
617 delete Game_screen;
618
619 // Restore saved game if one exists
620 if (Temp_level_saved) {
621 char filename[_MAX_PATH];
622
623 ddio_MakePath(filename, Base_directory_write, "GameSave.D3L", NULL); // make explicit path
624 LoadLevel(filename);
625 Temp_level_saved = 0;
626 }
627
628 // if no player vars back from the game, use the viewer from the file
629 if (editor_player_roomnum == -1) {
630 set_viewer_from_player = 0;
631 editor_player_roomnum = Player_object->roomnum;
632 }
633
634 // Set editor to mine or terrain mode based on current player position
635 Editor_view_mode = ROOMNUM_OUTSIDE(editor_player_roomnum) ? VM_TERRAIN : VM_MINE;
636
637 // Make sure there's a viewer object
638 SetEditorViewer();
639
640 // Move the viewer to the player's (pre-restore) position, if wasn't playing from the editor
641 if (set_viewer_from_player)
642 ObjSetPos(Viewer_object, &editor_player_pos, editor_player_roomnum, &editor_player_orient, false);
643
644 // Reset previous working directory
645 ddio_SetWorkingDir(Editor_dir);
646
647 // close down video system only!
648 ddvid_Close();
649
650 ASSERT((OBJECT_OUTSIDE(Viewer_object) != 0) == (Editor_view_mode == VM_TERRAIN));
651
652 SlewStop(Viewer_object);
653
654 // Close forcefeedback
655 ForceClose();
656
657 // restore Current OS object.
658 ddio_Close();
659 CloseD3Music();
660
661 if (PROGRAM(windowed)) {
662 // reinitialize IO system for editor
663 delete Descent;
664 Descent = Saved_editor_app;
665 io_info.obj = Descent;
666 } else if (D3EditState.fullscreen_debug_state) {
667 // reinitialize IO system for editor
668 delete Descent;
669 Descent = Saved_editor_app;
670 io_info.obj = Descent;
671
672 theApp.main_frame->SetMenu(CMenu::FromHandle(hEditorMenu));
673 } else {
674 Descent = Saved_editor_app;
675 io_info.obj = Descent;
676 theApp.main_frame->SetMenu(CMenu::FromHandle(hEditorMenu));
677 SetClassLong(theApp.main_frame->m_hWnd, GCL_HBRBACKGROUND, EditorBackBrush);
678 SetWindowLong(theApp.main_frame->m_hWnd, GWL_STYLE, EditorWndStyle);
679 }
680
681 // this uses information defined above
682 io_info.key_emulation = true; //(bool)(FindArg("-slowkey")!=0);
683 io_info.joy_emulation = (bool)((FindArg("-alternatejoy") == 0) && (FindArg("-directinput") == 0));
684
685 ddio_Init(&io_info);
686
687 Cinematic_Init();
688
689 // Init force feedback effects
690 ForceInit();
691
692 // This line is needed to disable OpenGL for ddgr
693 if (!ddgr_Init(Descent, "GDI32", false)) {
694 Error("Graphics initialization failed.\n");
695 }
696
697 // theApp.main_frame->SetWindowPos(&CWnd::wndNoTopMost, Editor_wndrect.left, Editor_wndrect.top,
698 // Editor_wndrect.right-Editor_wndrect.left,
699 // Editor_wndrect.bottom -
700 //Editor_wndrect.top, SWP_SHOWWINDOW);
701
702 // Restore sky if possible
703 if (Save_sky_data) {
704 int dome_bm = Terrain_sky.dome_texture;
705 if (bm_w(dome_bm, 0) != 256) {
706 // It was changed during rendering
707 bm_ChangeSize(dome_bm, 256, 256);
708 uint16_t *dest_data = (uint16_t *)bm_data(dome_bm, 0);
709 memcpy(dest_data, Save_sky_data, 256 * 256 * 2);
710 }
711
712 mem_free(Save_sky_data);
713 Save_sky_data = NULL;
714 }
715
716 // initialize systems back to editor.
717 InitGameEditSystems();
718 theApp.resume();
719
720 // If Dallas is running, restore it to where it was
721 if (theApp.m_DallasModelessDlgPtr != NULL)
722 theApp.m_DallasModelessDlgPtr->ShowWindow(SW_SHOWNA);
723
724 Disable_editor_rendering = false;
725 }
726
727 char Editor_quickplay_levelname[_MAX_PATH];
728
729 void EditorToGame() {
730 Disable_editor_rendering = true;
731
732 // Hide Dallas (if it's running) so it doesn't get any keypresses
733 if (theApp.m_DallasModelessDlgPtr != NULL)
734 theApp.m_DallasModelessDlgPtr->ShowWindow(SW_HIDE);
735
736 /* Create a new window and display it. Initialize a new GameOS Object and use this
737 for our game
738 */
739 tLnxAppInfo app_info;
740 bool paged = true;
741 char subsystem[8];
742 ddio_init_info io_info;
743
744 // Save the sky in case it gets changed
745 int dome_bm = GetTextureBitmap(Terrain_sky.dome_texture, 0);
746 if (bm_w(dome_bm, 0) == 256) {
747 Save_sky_data = (uint16_t *)mem_malloc(256 * 256 * 2);
748 ASSERT(Save_sky_data);
749
750 uint16_t *src_data = bm_data(dome_bm, 0);
751 memcpy(Save_sky_data, src_data, 256 * 256 * 2);
752 }
753
754 /* We have to reinitialize the DDGR system for full screen mode.
755 This will annihilate all previous DDGR settings.
756 */
757
758 // theApp.main_frame->ShowWindow(SW_SHOWMINNOACTIVE);
759
760 // set game working directory
761 bool set_size = false;
762 ddio_GetWorkingDir(Editor_dir, sizeof(Editor_dir));
763 ddio_SetWorkingDir(Base_directory);
764
765 Osiris_ResetAllTimers();
766
767 // continue
768 SlewStop(Viewer_object);
769
770 // Save the player positions
771 FindPlayerStarts();
772
773 // Save the level to restore when go back to editor
774 mprintf(0, "Make temp copy of level...");
775 SaveLevel("GameSave.D3L");
776 mprintf(0, "done\n");
777 Temp_level_saved = 1; // say that we have a level saved
778
779 // Save the viewer position for the game to use
780 editor_player_pos = Viewer_object->pos;
781 editor_player_orient = Viewer_object->orient;
782 editor_player_roomnum = Viewer_object->roomnum;
783
784 // Set up the player
785 SetObjectControlType(Player_object, CT_FLYING); // don't think we really need this
786 Viewer_object = Player_object;
787
788 // create a new OS object, hence a new window (The Descent III window)
789 unsigned flags;
790
791 Saved_editor_app = Descent;
792 Saved_editor_app->get_info(&app_info);
793 theApp.main_frame->GetWindowRect(&Editor_wndrect);
794
795 // close down ddgr system only.
796 ddgr_Close();
797
798 if (PROGRAM(windowed))
799 strcpy(subsystem, "GDI");
800 else if (Renderer_type == RENDERER_OPENGL)
801 strcpy(subsystem, "GDIF");
802 else
803 strcpy(subsystem, "GDIX");
804
805 if (theApp.main_doc) {
806 CString path = theApp.main_doc->GetPathName();
807 strcpy(Editor_quickplay_levelname, path.GetBuffer(0));
808 if (Editor_quickplay_levelname[0] == '\0')
809 strcpy(Editor_quickplay_levelname, "Untitled.d3l");
810 } else {
811 strcpy(Editor_quickplay_levelname, "Untitled.d3l");
812 }
813
814 // close forcefeedback
815 ForceClose();
816
817 // occurs after io initialization
818 ddio_Close();
819 CloseD3Music();
820
821 // create window and start game.
822 if (PROGRAM(windowed)) {
823 flags = OEAPP_WINDOWED;
824 Descent = new oeLnxApplication(flags);
825 Descent->init();
826 set_size = true;
827 Descent->get_info(&app_info);
828 // TODO: retrieve hwnd from SDL window handle
829 // hGameWnd = app_info.hwnd;
830 // SetForegroundWindow((HWND)hGameWnd);
831 Saved_editor_app->defer();
832 Descent->defer();
833
834 // Initialize graphics and sound this must be done after initialization of new window and
835 // after deferring to OS.
836 if (!ddvid_Init(Descent, subsystem))
837 Error("Graphics initialization failed.\n");
838 } else if (D3EditState.fullscreen_debug_state) {
839 CMenu *menu = theApp.main_frame->GetMenu();
840
841 hEditorMenu = menu->m_hMenu;
842 theApp.main_frame->SetMenu(NULL);
843
844 flags = OEAPP_FULLSCREEN;
845 Descent = new oeLnxApplication(flags);
846 Descent->init();
847 set_size = true;
848 Descent->get_info(&app_info);
849 // TODO: get hwnd from SDL Window
850 // hGameWnd = app_info.hwnd;
851 // SetForegroundWindow((HWND)hGameWnd);
852 Saved_editor_app->defer();
853 Descent->defer();
854
855 // Initialize graphics and sound this must be done after initialization of new window and
856 // after deferring to OS.
857 if (!ddvid_Init(Saved_editor_app, subsystem))
858 Error("Graphics initialization failed.\n");
859
860 // force application to think it's active.
861 SetActiveWindow((HWND)hGameWnd);
862 // Descent->activate();
863 } else {
864 CMenu *menu = theApp.main_frame->GetMenu();
865
866 hGameWnd = (unsigned)theApp.main_frame->m_hWnd;
867 hEditorMenu = menu->m_hMenu;
868 theApp.main_frame->SetMenu(NULL);
869 EditorWndStyle = GetWindowLong(theApp.main_frame->m_hWnd, GWL_STYLE);
870 EditorBackBrush = GetClassLong(theApp.main_frame->m_hWnd, GCL_HBRBACKGROUND);
871 SetWindowLong(theApp.main_frame->m_hWnd, GWL_STYLE, EditorWndStyle & ~(WS_CAPTION | WS_THICKFRAME | WS_BORDER));
872 SetClassLong(theApp.main_frame->m_hWnd, GCL_HBRBACKGROUND, (DWORD)GetStockObject(HOLLOW_BRUSH));
873
874 // Initialize graphics and sound this must be done after initialization of new window and
875 // after deferring to OS.
876 if (!ddvid_Init(Descent, subsystem))
877 Error("Graphics initialization failed.\n");
878 }
879
880 // Initialize IO System for child window
881 io_info.obj = Descent;
882 io_info.key_emulation = true; //(bool)(FindArg("-slowkey")!=0);
883 io_info.joy_emulation = (bool)((FindArg("-alternatejoy") == 0) && (FindArg("-directinput") == 0));
884 ddio_Init(&io_info);
885
886 // Init force feedback
887 ForceInit();
888
889 // Create game screen and clear it.
890 ddvid_SetVideoHandle(hGameWnd);
891
892 // Initialize everything else.
893 InitEditGameSystems();
894
895 // maybe we should CLEAR HERE
896 if (set_size) {
897 ((oeLnxApplication *)Descent)->set_sizepos(0, 0, 640, 480);
898 }
899
900 Just_returned_from_game = 1;
901 Just_returned_time = timer_GetTime();
902
903 // if (D3EditState.fullscreen_debug_state)
904 // theApp.main_frame->MoveWindow(-50,-50,20,20);
905 }
906
907 void InitEditGameSystems() {
908 tUIInitInfo uiinit;
909 #if defined(RELEASE)
910 SetDebugBreakHandlers(NULL, NULL);
911 #else
912 SetDebugBreakHandlers(D3DebugStopHandler, D3DebugResumeHandler);
913 #endif
914
915 Descent->set_defer_handler(D3DeferHandler);
916
917 LoadAllFonts();
918 InitControls();
919 ddio_KeyFlush();
920 ddio_MouseMode(MOUSE_EXCLUSIVE_MODE);
921
922 // Sound initialization
923 InitD3Music(FindArg("-nomusic") ? false : true);
924 Sound_system.InitSoundLib(Descent, Sound_mixer, Sound_quality, false);
925
926 // UI init.
927 uiinit.window_font = SMALL_FONT;
928 uiinit.w = 640;
929 uiinit.h = 480;
930 ui_Init(Descent, &uiinit);
931 ui_UseCursor("StdCursor.ogf");
932
933 NewUIInit();
934
935 // Load up the pilot selected by default
936 if (cfexist(Default_pilot)) {
937 mprintf(0, "Loading up Default Pilot: %s\n", Default_pilot);
938 Current_pilot.set_filename(Default_pilot);
939 PltReadFile(&Current_pilot, true, true);
940 } else {
941 strcpy(Default_pilot, " ");
942 Current_pilot.set_filename("");
943 }
944 }
945
946 void InitGameEditSystems() {
947 // UnLoad the pilot and fill in the default
948 char p_fname[_MAX_FNAME];
949
950 Current_pilot.get_filename(p_fname);
951 if (p_fname[0] != '\0')
952 strcpy(Default_pilot, p_fname);
953 else
954 strcpy(Default_pilot, " ");
955
956 // reset ship permissions
957 PlayerResetShipPermissions(-1, true);
958
959 CloseControls();
960 ddio_KeyFlush();
961 ddio_MouseMode(MOUSE_STANDARD_MODE);
962
963 // Sound initialization for editor
964 InitD3Music(FindArg("-nomusic") ? false : true);
965 Sound_system.InitSoundLib(Descent, Sound_mixer, Sound_quality, false);
966
967 NewUIClose();
968 ui_Close();
969
970 SetDebugBreakHandlers(NULL, NULL);
971 }
972
973 // THESE FUNCTIONS DEAL WITH INTERFACING EDITOR AND GAME 3D system
974
975 static grViewport *Editor_g3_vp = NULL;
976
977 void StartEditorFrame(grViewport *vp, vector *viewer_eye, matrix *viewer_orient, float zoom) {
978 grSurface *surf;
979
980 ASSERT(vp != NULL);
981
982 Editor_g3_vp = vp;
983 surf = Editor_g3_vp->lock();
984 if (!surf)
985 Int3(); // This surface should be locked. bad?
986
987 rend_StartFrame(0, 0, surf->width(), surf->height());
988 g3_StartFrame(viewer_eye, viewer_orient, zoom);
989 }
990
991 void EndEditorFrame() {
992 Editor_g3_vp->unlock();
993 Editor_g3_vp = NULL;
994 g3_EndFrame();
995 rend_EndFrame();
996 }