]> icculus.org git repositories - taylor/freespace2.git/blob - src/fred2/fred.cpp
Initial revision
[taylor/freespace2.git] / src / fred2 / fred.cpp
1 /*
2  * $Logfile: /Freespace2/code/Fred2/FRED.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * FRED.cpp : Defines the class behaviors for the application.
8  * Global editor dialog box classes are instantiated here, initializes the
9  * application (MFC level at least), processes the INI file.
10  *
11  * $Log$
12  * Revision 1.1  2002/05/03 03:28:08  root
13  * Initial revision
14  *
15  * 
16  * 6     5/19/99 4:07p Dave
17  * Moved versioning code into a nice isolated common place. Fixed up
18  * updating code on the pxo screen. Fixed several stub problems.
19  * 
20  * 5     3/26/99 4:49p Dave
21  * Made cruisers able to dock with stuff. Made docking points and paths
22  * visible in fred.
23  * 
24  * 4     3/20/99 5:09p Dave
25  * Fixed release build fred warnings and unhandled exception.
26  * 
27  * 3     10/29/98 10:41a Dave
28  * Change the way cfile initializes exe directory.
29  * 
30  * 2     10/07/98 6:28p Dave
31  * Initial checkin. Renamed all relevant stuff to be Fred2 instead of
32  * Fred. Globalized mission and campaign file extensions. Removed Silent
33  * Threat specific code.
34  * 
35  * 1     10/07/98 3:01p Dave
36  * 
37  * 1     10/07/98 3:00p Dave
38  * 
39  * 71    7/02/98 12:17p Hoffoss
40  * Load and save options 'anti-aliased gridlines' and 'double fine
41  * gridlines'.
42  * 
43  * 70    5/22/98 11:02a Allender
44  * Added error conditions on compile and new MAX_SHIPS limits for when
45  * FRED is defined when building
46  * 
47  * 69    5/22/98 1:06a Hoffoss
48  * Made Fred not use OLE.
49  * 
50  * 68    4/17/98 1:41p Allender
51  * took out function calls in NDEBUG mode
52  * 
53  * 67    3/25/98 4:14p Hoffoss
54  * Split ship editor up into ship editor and a misc dialog, which tracks
55  * flags and such.
56  * 
57  * 66    3/23/98 4:04p Hoffoss
58  * Fixed dialog window initialization so it looks better at startup (they
59  * don't flash on for a second).
60  * 
61  * 65    3/19/98 11:41a Hoffoss
62  * Fixed problems with rendering and reading of flying controls in Fred.
63  * 
64  * 64    3/19/98 11:11a Allender
65  * made Fred actually be able to move camera again -- Hoffos is going to
66  * fix the code right.
67  * 
68  * 63    3/18/98 3:10p Allender
69  * fixed update problems in OnIdle routine
70  * 
71  * 62    2/06/98 4:39p Hoffoss
72  * Added better checking for whether Fred is in the foreground or
73  * background.
74  * 
75  * 61    8/19/97 5:46p Hoffoss
76  * Changed font used in Fred, and added display to show current eye
77  * position.
78  * 
79  * 60    8/17/97 10:22p Hoffoss
80  * Fixed several bugs in Fred with Undo feature.  In the process, recoded
81  * a lot of CFile.cpp.
82  * 
83  * 59    8/14/97 2:32p Hoffoss
84  * fixed bug where controlling an object doesn't cause screen updates, and
85  * added a number of cool features to viewpoint/control object code.
86  * 
87  * 58    8/13/97 1:38p Hoffoss
88  * Added ability to update multiple times, which in needed in one case to
89  * brute force redraw so deleted ships actually do get removed from the
90  * display.
91  * 
92  * 57    8/12/97 6:32p Hoffoss
93  * Added code to allow hiding of arrival and departure cues in editors.
94  * 
95  * 56    8/10/97 4:52p Hoffoss
96  * Made Fred startup with main window having focus.
97  * 
98  * 55    8/06/97 7:55p Hoffoss
99  * Fixed bug with objects not seeming to be where they are drawn (due to
100  * new briefing clip render stuff).  This caused rendering problems,
101  * though.  So I fixed those next.
102  * 
103  * 54    8/05/97 1:29p Hoffoss
104  * show sexp help saved between sessions.
105  * 
106  * 53    8/01/97 3:10p Hoffoss
107  * Made Sexp help hidable.
108  * 
109  * 52    8/01/97 12:49p Hoffoss
110  * Changed recently used files total to 9 instead of 4.
111  * 
112  * 51    6/18/97 11:46a Hoffoss
113  * Fixed initial order object reference updating and added briefing dialog
114  * window tracking data.
115  * 
116  * 50    6/03/97 10:18a Allender
117  * make showCmd default to maximized since 0 will make the window hidden
118  * by default
119  * 
120  * 49    6/02/97 5:18p Hoffoss
121  * Fixed bug in error checker and improved startup code.
122  * 
123  * 48    6/02/97 11:52a Hoffoss
124  * Custom cursors displayed when over objects in different modes.
125  * 
126  * 47    5/12/97 12:27p John
127  * Restructured Graphics Library to add support for multiple renderers.
128  * 
129  * 46    5/09/97 9:45a Hoffoss
130  * Added OLE initialization. (needed for drag and drop functionality)
131  * 
132  * 45    5/06/97 10:34a Hoffoss
133  * Fixed bug where dialog boxes that change size (in the code) still
134  * retain their old size from the INI stored values.  They are resized now
135  * automatically.
136  * 
137  * 44    5/05/97 9:41a Hoffoss
138  * Campaign editor begun.
139  * 
140  * 43    5/02/97 8:39a Allender
141  * turn of heap allocation debuging
142  * 
143  * 42    4/29/97 1:58p Hoffoss
144  * Added some debugging to Fred to try and track down sexp corruption
145  * causes.
146  * 
147  * 41    4/28/97 3:43p Hoffoss
148  * Show_waypoints defaults to on for Fred, and is saved as a preference
149  * now.
150  * 
151  * 40    4/17/97 2:01p Hoffoss
152  * All dialog box window states are saved between sessions now.
153  * 
154  * 39    4/03/97 11:35a Hoffoss
155  * Fixed bugs: viewpoint didn't reset, initial orders not updated when
156  * referenced ship is renamed or deleted.
157  * 
158  * 38    4/01/97 11:43p Mike
159  * Resolve link errors due to addition of SystemVars.cpp.
160  * 
161  * 37    3/26/97 12:44p Hoffoss
162  * Took out the debugging 3d compass on/off toggling I put in to test
163  * rerendering with.
164  * 
165  * 36    3/20/97 3:55p Hoffoss
166  * Major changes to how dialog boxes initialize (load) and update (save)
167  * their internal data.  This should simplify things and create less
168  * problems.
169  * 
170  * 35    3/13/97 12:08p Hoffoss
171  * Waypoint path editor finished (apparently I didn't get around to
172  * completing it before).
173  * 
174  * 34    3/12/97 12:39p Hoffoss
175  * Fixed bugs in wing object management functions, several small additions
176  * and rearrangements.
177  * 
178  * 33    3/06/97 3:35p Hoffoss
179  * Added Show_outline stuff, moved show options to the view menu, fixed a
180  * bug in message dialog editor.
181  * 
182  * 32    2/28/97 11:31a Hoffoss
183  * Implemented modeless dialog saving and restoring, and changed some
184  * variables names.
185  * 
186  * 31    2/27/97 5:54p Hoffoss
187  * Implemented support for saving and restoring window positions.
188  * 
189  * 30    2/25/97 6:10p Hoffoss
190  * Fixed bug with modeless dialog box errors on update.
191  * 
192  * 29    2/24/97 12:50p Hoffoss
193  * First attempt at non-continuous redrawing.
194  * 
195  * 28    2/17/97 5:28p Hoffoss
196  * Checked RCS headers, added them were missing, changing description to
197  * something better, etc where needed.
198  * 
199  * 27    2/12/97 12:25p Hoffoss
200  * Expanded on global error checker, added initial orders conflict
201  * checking and warning, added waypoint editor dialog and code.
202  * 
203  * 26    1/30/97 2:24p Hoffoss
204  * Added remaining mission file structures and implemented load/save of
205  * them.
206  *
207  * $NoKeywords: $
208  */
209
210 #include "stdafx.h"
211 #include "fred.h"
212
213 #include "mainfrm.h"
214 #include "freddoc.h"
215 #include "fredview.h"
216 #include "fredrender.h"
217 #include "management.h"
218
219 #include "2d.h"
220 #include "key.h"
221 #include "object.h"
222 #include "editor.h"
223 #include "campaigntreewnd.h"
224 #include "campaigntreeview.h"
225 #include "campaigneditordlg.h"
226
227 #ifdef NDEBUG
228 #ifndef FRED
229 #error macro FRED is not defined when trying to build release Fred.  Please define FRED macro in build settings in all Fred projects
230 #endif
231 #endif
232
233 #ifdef _DEBUG
234 #define new DEBUG_NEW
235 #undef THIS_FILE
236 static char THIS_FILE[] = __FILE__;
237 #endif
238
239 int     Fred_running = 1;
240 int     User_interface = HOFFOSS_INTERFACE;
241 int     FrameCount = 0;
242 int     Fred_active = 1;
243 int     Update_window = 1;
244 HCURSOR h_cursor_move, h_cursor_rotate;
245
246 CWnd *Prev_window;
247 CShipEditorDlg  Ship_editor_dialog;
248 wing_editor             Wing_editor_dialog;
249 waypoint_path_dlg       Waypoint_editor_dialog;
250 bg_bitmap_dlg   *Bg_bitmap_dialog = NULL;
251 briefing_editor_dlg     *Briefing_dialog = NULL;
252
253 window_data Main_wnd_data;
254 window_data Ship_wnd_data;
255 window_data Wing_wnd_data;
256 window_data Object_wnd_data;
257 window_data Mission_goals_wnd_data;
258 window_data Messages_wnd_data;
259 window_data Player_wnd_data;
260 window_data Events_wnd_data;
261 window_data Bg_wnd_data;
262 window_data Briefing_wnd_data;
263 window_data Reinforcement_wnd_data;
264 window_data Waypoint_wnd_data;
265 window_data Starfield_wnd_data;
266 window_data Asteroid_wnd_data;
267 window_data Mission_notes_wnd_data;
268
269 /////////////////////////////////////////////////////////////////////////////
270 // CFREDApp
271
272 BEGIN_MESSAGE_MAP(CFREDApp, CWinApp)
273         //{{AFX_MSG_MAP(CFREDApp)
274         ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
275                 // NOTE - the ClassWizard will add and remove mapping macros here.
276                 //    DO NOT EDIT what you see in these blocks of generated code!
277         //}}AFX_MSG_MAP
278         // Standard file based document commands
279         ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
280         ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
281         // Standard print setup command
282         ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
283 END_MESSAGE_MAP()
284
285 /////////////////////////////////////////////////////////////////////////////
286 // CFREDApp construction
287
288 CFREDApp::CFREDApp()
289 {
290         app_init = 0;
291
292         #ifndef NDEBUG
293         outwnd_init();
294         #endif
295         // TODO: add construction code here,
296         // Place all significant initialization in InitInstance
297 }
298
299 /////////////////////////////////////////////////////////////////////////////
300 // The one and only CFREDApp object
301
302 CFREDApp theApp;
303
304 /////////////////////////////////////////////////////////////////////////////
305 // CFREDApp initialization
306 char *c;
307 char *tok = "whee";
308 BOOL CFREDApp::InitInstance()
309 {       
310         // disable the debug memory stuff
311         _CrtSetDbgFlag(~(_CRTDBG_ALLOC_MEM_DF) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
312
313         // Standard initialization
314         // If you are not using these features and wish to reduce the size
315         //  of your final executable, you should remove from the following
316         //  the specific initialization routines you do not need.
317
318 #ifdef _AFXDLL
319         Enable3dControls();                     // Call this when using MFC in a shared DLL
320 #else
321         Enable3dControlsStatic();       // Call this when linking to MFC statically
322 #endif
323
324         LoadStdProfileSettings(9);  // Load standard INI file options (including MRU)
325         User_interface = GetProfileInt("Preferences", "User interface", User_interface);
326         Show_stars = GetProfileInt("Preferences", "Show stars", Show_stars);
327         Show_grid_positions = GetProfileInt("Preferences", "Show grid positions", Show_grid_positions);
328         Show_coordinates = GetProfileInt("Preferences", "Show coordinates", Show_coordinates);
329         Show_compass = GetProfileInt("Preferences", "Show compass", Show_compass);
330         Show_ship_models = GetProfileInt("Preferences", "Show ship models", Show_ship_models);
331         Show_ship_info = GetProfileInt("Preferences", "Show ship info", Show_ship_info);
332         Show_outlines = GetProfileInt("Preferences", "Show outlines", Show_outlines);
333         Show_waypoints = GetProfileInt("Preferences", "Show waypoints", Show_waypoints);
334         Show_sexp_help = GetProfileInt("Preferences", "Show sexp help", Show_sexp_help);
335         physics_speed = GetProfileInt("Preferences", "Physics speed", physics_speed);
336         physics_rot = GetProfileInt("Preferences", "Physics rotation", physics_rot);
337         Hide_ship_cues = GetProfileInt("Preferences", "Hide ship cues", Hide_ship_cues);
338         Hide_wing_cues = GetProfileInt("Preferences", "Hide wing cues", Hide_wing_cues);
339         Autosave_disabled = GetProfileInt("Preferences", "Autosave disabled", Autosave_disabled);
340         double_fine_gridlines = GetProfileInt("Preferences", "Double fine gridlines", double_fine_gridlines);
341         Aa_gridlines = GetProfileInt("Preferences", "Anti aliased gridlines", Aa_gridlines);
342         Show_dock_points = GetProfileInt("Preferences", "Show dock points", Show_dock_points);
343         Show_paths_fred = GetProfileInt("Preferences", "Show paths", Show_paths_fred);
344         read_window("Main window", &Main_wnd_data);
345         read_window("Ship window", &Ship_wnd_data);
346         read_window("Wing window", &Wing_wnd_data);
347         read_window("Waypoint window", &Waypoint_wnd_data);
348         read_window("Object window", &Object_wnd_data);
349         read_window("Mission goals window", &Mission_goals_wnd_data);
350         read_window("Messages window", &Messages_wnd_data);
351         read_window("Player window", &Player_wnd_data);
352         read_window("Events window", &Events_wnd_data);
353         read_window("Bg window", &Bg_wnd_data);
354         read_window("Briefing window", &Briefing_wnd_data);
355         read_window("Reinforcement window", &Reinforcement_wnd_data);
356         read_window("Starfield window", &Starfield_wnd_data);
357         read_window("Asteroid window", &Asteroid_wnd_data);
358         read_window("Mission notes window", &Mission_notes_wnd_data);
359         write_ini_file(1);
360
361         // Register the application's document templates.  Document templates
362         //  serve as the connection between documents, frame windows and views.
363
364         CSingleDocTemplate* pDocTemplate;
365         pDocTemplate = new CSingleDocTemplate(
366                 IDR_MAINFRAME,
367                 RUNTIME_CLASS(CFREDDoc),
368                 RUNTIME_CLASS(CMainFrame),       // main SDI frame window
369                 RUNTIME_CLASS(CFREDView));
370         AddDocTemplate(pDocTemplate);
371
372         // Enable DDE Execute open
373         EnableShellOpen();
374         RegisterShellFileTypes(TRUE);   
375
376         // setup the fred exe directory so CFILE can init properly
377         /*
378         c = GetCommandLine();
379         Assert(c != NULL);
380         if(c == NULL){
381                 return FALSE;
382         } 
383         tok = strtok(c, " \n");
384         Assert(tok != NULL);
385         if(tok == NULL){
386                 return FALSE;           
387         }
388         // Fred_exe_dir = strdup(c);            
389         strcpy(Fred_exe_dir, tok);
390         */
391         strcpy(Fred_exe_dir, __argv[0]);
392
393         // Parse command line for standard shell commands, DDE, file open
394         CCommandLineInfo cmdInfo;
395         ParseCommandLine(cmdInfo);      
396
397         m_nCmdShow = Main_wnd_data.p.showCmd;
398         OnFileNew();
399
400         // Enable drag/drop open
401         m_pMainWnd->DragAcceptFiles();
402
403         h_cursor_move = LoadCursor(IDC_CURSOR1);
404         h_cursor_rotate = LoadCursor(IDC_CURSOR2);
405         return TRUE;
406 }
407
408 /////////////////////////////////////////////////////////////////////////////
409 // CAboutDlg dialog used for App About
410
411 class CAboutDlg : public CDialog
412 {
413 public:
414         CAboutDlg();
415
416 // Dialog Data
417         //{{AFX_DATA(CAboutDlg)
418         enum { IDD = IDD_ABOUTBOX };
419         //}}AFX_DATA
420
421         // ClassWizard generated virtual function overrides
422         //{{AFX_VIRTUAL(CAboutDlg)
423         protected:
424         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
425         //}}AFX_VIRTUAL
426
427 // Implementation
428 protected:
429         //{{AFX_MSG(CAboutDlg)
430         //}}AFX_MSG
431         DECLARE_MESSAGE_MAP()
432 };
433
434 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
435 {
436         //{{AFX_DATA_INIT(CAboutDlg)
437         //}}AFX_DATA_INIT
438 }
439
440 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
441 {
442         CDialog::DoDataExchange(pDX);
443         //{{AFX_DATA_MAP(CAboutDlg)
444         //}}AFX_DATA_MAP
445 }
446
447 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
448         //{{AFX_MSG_MAP(CAboutDlg)
449         //}}AFX_MSG_MAP
450 END_MESSAGE_MAP()
451
452 // App command to run the dialog
453 void CFREDApp::OnAppAbout()
454 {
455         CAboutDlg aboutDlg;
456         aboutDlg.DoModal();
457 }
458
459 /////////////////////////////////////////////////////////////////////////////
460 // CFREDApp commands
461
462 char *edit_mode_text[] = {
463         "Ships",
464         "Waypoints",
465 };
466 /*      "Grid",
467         "Wing",
468         "Object Relative"
469 };
470 */
471
472 char *control_mode_text[] = {
473         "Camera",
474         "Object"
475 };
476
477 void show_control_mode(void)
478 {
479         CString str;
480
481         CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
482         CStatusBar* pStatus = &pFrame->m_wndStatusBar;
483         //CStatusBarCtrl pStatusBarCtrl;
484
485         if (pStatus) {
486 //              pStatus->GetStatusBarCtrl().SetParts(NUM_STATUS_PARTS, parts);
487
488                 if (Marked)
489                         str.Format("Marked: %d", Marked);
490                 else
491                         str = _T("");
492                 pStatus->SetPaneText(1, str);
493
494                 if (viewpoint)
495                         str.Format("Viewpoint: %s", object_name(view_obj));
496                 else
497                         str.Format("Viewpoint: Camera");
498
499                 pStatus->SetPaneText(2, str);
500                 
501                 if (FREDDoc_ptr->IsModified())
502                         pStatus->SetPaneText(3, "MODIFIED");
503                 else
504                         pStatus->SetPaneText(3, "");
505
506                 str.Format("Units = %.1f Meters", The_grid->square_size);
507                 pStatus->SetPaneText(4, str);
508
509 //              pStatus->SetPaneText(4, "abcdefg");
510 //              pStatus->SetPaneText(4, "1234567890!");
511         }
512         
513 }
514
515 #define MAX_PENDING_MESSAGES    16
516
517 typedef struct {
518         int     frame_to_process, hwnd, id, wparam, lparam;
519 } pending_message;
520
521 pending_message Pending_messages[MAX_PENDING_MESSAGES];
522
523 //      Process messages that needed to wait until a frame had gone by.
524 void process_pending_messages(void)
525 {
526         int     i;
527
528         for (i=0; i<MAX_PENDING_MESSAGES; i++)
529                 if (Pending_messages[i].frame_to_process != -1)
530                         if (Pending_messages[i].frame_to_process <= FrameCount) {
531                                 pending_message *pmp = &Pending_messages[i];
532                                 PostMessage((HWND) pmp->hwnd, pmp->id, pmp->wparam, pmp->lparam);
533                                 Pending_messages[i].frame_to_process = -1;
534                         }
535 }
536
537 //      Add a message to be processed to a buffer.
538 //      Wait skip_count frames before processing.
539 void add_pending_message(HWND hwnd, int id, int wparam, int lparam, int skip_count)
540 {
541         int     i;
542
543         for (i=0; i<MAX_PENDING_MESSAGES; i++)
544                 if (Pending_messages[i].frame_to_process == -1) {
545                         Pending_messages[i].hwnd = (int) hwnd;
546                         Pending_messages[i].id = id;
547                         Pending_messages[i].wparam = wparam;
548                         Pending_messages[i].lparam = lparam;
549                         Pending_messages[i].frame_to_process = FrameCount + skip_count;
550                 }
551 }
552
553 void init_pending_messages(void)
554 {
555         int     i;
556
557         for (i=0; i<MAX_PENDING_MESSAGES; i++)
558                 Pending_messages[i].frame_to_process = -1;
559 }
560
561 // void win32_blit(HDC hSrcDC, HPALETTE hPalette, int x, int y, int w, int h )
562 #if 0
563 void win32_blit(void *xx, void *yy, int x, int y, int w, int h )
564 {
565         HPALETTE hOldPalette = NULL;
566         HDC hdc = GetDC(hwndApp);
567
568         if ( !hdc )     return;
569         if ( !fAppActive ) return;
570
571         if (hPalette)   {
572                 hOldPalette = SelectPalette(hdc, hPalette, FALSE);
573                 RealizePalette( hdc );
574         }
575
576         BitBlt(hdc, 0, 0, w, h, hSrcDC, x, y, SRCCOPY);
577         
578         if ( hOldPalette )      
579                 SelectPalette(hdc, hOldPalette, FALSE);
580
581         ReleaseDC( hwndApp, hdc );
582 }
583 #endif
584
585 void draw_render_window(CDC* pDC)
586 {
587         int w, h;
588
589         w = gr_screen.clip_width;
590         h = gr_screen.clip_height;
591         gr_screen.clip_width = gr_screen.max_w;
592         gr_screen.clip_height = gr_screen.max_h;
593         gr_flip_window((uint) pDC->m_hDC, 0, 0, gr_screen.max_w, gr_screen.max_h);
594         gr_screen.clip_width = w;
595         gr_screen.clip_height = h;
596 }
597
598 BOOL CFREDApp::OnIdle(LONG lCount)
599 {
600         int adjust = 0;
601         CWnd *top, *wnd;
602
603         if (!Show_sexp_help)
604                 adjust = -SEXP_HELP_BOX_SIZE;
605
606         if (!app_init) {
607                 app_init = 1;
608                 theApp.init_window(&Ship_wnd_data, &Ship_editor_dialog, adjust, 1);
609                 theApp.init_window(&Wing_wnd_data, &Wing_editor_dialog, adjust, 1);
610                 theApp.init_window(&Waypoint_wnd_data, &Waypoint_editor_dialog, 0, 1);
611                 init_window(&Main_wnd_data, Fred_main_wnd);
612                 Fred_main_wnd->SetWindowPos(&CWnd::wndTop, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
613
614                 Ship_editor_dialog.calc_cue_height();
615                 Wing_editor_dialog.calc_cue_height();
616         }
617
618         CWinApp::OnIdle(lCount);
619         internal_integrity_check();
620         if (Update_ship) {
621                 Ship_editor_dialog.initialize_data(1);
622                 Update_ship = 0;
623         }
624
625         if (Update_wing) {
626                 Wing_editor_dialog.initialize_data(1);
627                 Update_wing = 0;
628         }
629
630         Prev_window = CFREDView::GetActiveWindow();
631
632         // Find the root window of the active window
633         wnd = top = Prev_window;
634         while (wnd) {
635                 top = wnd;
636                 wnd = wnd->GetParent();
637         }
638
639         // See if the active window is a child of Fred
640         if (Prev_window)
641                 Fred_active = ( (top == Fred_main_wnd) || (top == Campaign_wnd) );
642         else
643                 Fred_active = 0;
644
645         if (!Fred_active)
646                 return FALSE;  // if fred isn't active, don't waste any time with it.
647
648         game_do_frame();  // do stuff that needs doing, whether we render or not.
649         show_control_mode();
650
651         if (!Update_window)
652                 return FALSE;
653
654         render_frame(); // "do the rendering!"  Renders image to offscreen buffer
655
656         CFREDView* pFV = CFREDView::GetView();
657         CDC* pDC = pFV->GetDC();
658
659         // gr_surface_flip();
660
661         // if you hit the next Assert, find Hoffoss or Allender.  If neither here, then comment it out.
662         Assert( Update_window >= 0 );
663         if (Update_window) {
664                 draw_render_window(pDC);  // this actually copies the offscreen buffer to the screen
665                 Update_window--;
666         }
667
668         process_pending_messages();
669         pFV->ReleaseDC(pDC);
670
671         FrameCount++;
672         return TRUE;
673 }
674
675 void update_map_window()
676 {
677         if (Fred_active) {
678                 Update_window++;  // on idle will handle the drawing already.
679                 return;
680         }
681
682         if (!Fred_main_wnd)
683                 return;
684
685         CFREDView* pFV = CFREDView::GetView();
686         if (!pFV)
687                 return;
688
689         render_frame(); // "do the rendering!"
690
691         CDC* pDC = pFV->GetDC();
692         Assert(pDC);
693
694         draw_render_window(pDC);
695         if ( Update_window > 0 )
696                 Update_window--;
697
698         show_control_mode();
699         process_pending_messages();
700         pFV->ReleaseDC(pDC);
701
702         FrameCount++;
703 }
704
705 void CFREDApp::write_ini_file(int degree)
706 {
707         WriteProfileInt("Preferences", "User interface", User_interface);
708         WriteProfileInt("Preferences", "Show stars", Show_stars);
709         WriteProfileInt("Preferences", "Show grid positions", Show_grid_positions);
710         WriteProfileInt("Preferences", "Show coordinates", Show_coordinates);
711         WriteProfileInt("Preferences", "Show compass", Show_compass);
712         WriteProfileInt("Preferences", "Show ship models", Show_ship_models);
713         WriteProfileInt("Preferences", "Show ship info", Show_ship_info);
714         WriteProfileInt("Preferences", "Show outlines", Show_outlines);
715         WriteProfileInt("Preferences", "Physics speed", physics_speed);
716         WriteProfileInt("Preferences", "Physics rotation", physics_rot);
717         WriteProfileInt("Preferences", "Show waypoints", Show_waypoints);
718         WriteProfileInt("Preferences", "Show sexp help", Show_sexp_help);
719         WriteProfileInt("Preferences", "Hide ship cues", Hide_ship_cues);
720         WriteProfileInt("Preferences", "Hide wing cues", Hide_wing_cues);
721         WriteProfileInt("Preferences", "Autosave disabled", Autosave_disabled);
722         WriteProfileInt("Preferences", "Double fine gridlines", double_fine_gridlines);
723         WriteProfileInt("Preferences", "Anti aliased gridlines", Aa_gridlines);
724         WriteProfileInt("Preferences", "Show dock points",      Show_dock_points);
725         WriteProfileInt("Preferences", "Show paths",    Show_paths_fred);
726
727         if (!degree) {
728                 record_window_data(&Waypoint_wnd_data, &Waypoint_editor_dialog);
729                 record_window_data(&Wing_wnd_data, &Wing_editor_dialog);
730                 record_window_data(&Ship_wnd_data, &Ship_editor_dialog);
731                 record_window_data(&Main_wnd_data, Fred_main_wnd);
732
733                 write_window("Main window", &Main_wnd_data);
734                 write_window("Ship window", &Ship_wnd_data);
735                 write_window("Wing window", &Wing_wnd_data);
736                 write_window("Waypoint window", &Waypoint_wnd_data);
737                 write_window("Object window", &Object_wnd_data);
738                 write_window("Mission goals window", &Mission_goals_wnd_data);
739                 write_window("Messages window", &Messages_wnd_data);
740                 write_window("Player window", &Player_wnd_data);
741                 write_window("Events window", &Events_wnd_data);
742                 write_window("Bg window", &Bg_wnd_data);
743                 write_window("Briefing window", &Briefing_wnd_data);
744                 write_window("Reinforcement window", &Reinforcement_wnd_data);
745                 write_window("Starfield window", &Starfield_wnd_data);
746                 write_window("Asteroid window", &Asteroid_wnd_data);
747                 write_window("Mission notes window", &Mission_notes_wnd_data);
748         }
749 }
750
751 void CFREDApp::write_window(char *name, window_data *wndd)
752 {
753         WriteProfileInt(name, "valid", wndd->valid);
754         WriteProfileInt(name, "length", wndd->p.length);
755         WriteProfileInt(name, "flags", wndd->p.flags);
756         WriteProfileInt(name, "showCmd", wndd->p.showCmd);
757         WriteProfileInt(name, "ptMinPosition.x", wndd->p.ptMinPosition.x);
758         WriteProfileInt(name, "ptMinPosition.y", wndd->p.ptMinPosition.y);
759         WriteProfileInt(name, "ptMaxPosition.x", wndd->p.ptMaxPosition.x);
760         WriteProfileInt(name, "ptMaxPosition.y", wndd->p.ptMaxPosition.y);
761         WriteProfileInt(name, "rcNormalPosition.left", wndd->p.rcNormalPosition.left);
762         WriteProfileInt(name, "rcNormalPosition.top", wndd->p.rcNormalPosition.top);
763         WriteProfileInt(name, "rcNormalPosition.right", wndd->p.rcNormalPosition.right);
764         WriteProfileInt(name, "rcNormalPosition.bottom", wndd->p.rcNormalPosition.bottom);
765         WriteProfileInt(name, "Visible", wndd->visible);
766 }
767
768 void CFREDApp::read_window(char *name, window_data *wndd)
769 {
770         wndd->processed = 0;
771         wndd->valid = GetProfileInt(name, "valid", FALSE);
772         wndd->p.length = GetProfileInt(name, "length", 0);
773         wndd->p.flags = GetProfileInt(name, "flags", 0);
774         wndd->p.showCmd = GetProfileInt(name, "showCmd", SW_SHOWMAXIMIZED);
775         wndd->p.ptMinPosition.x = GetProfileInt(name, "ptMinPosition.x", 0);
776         wndd->p.ptMinPosition.y = GetProfileInt(name, "ptMinPosition.y", 0);
777         wndd->p.ptMaxPosition.x = GetProfileInt(name, "ptMaxPosition.x", 0);
778         wndd->p.ptMaxPosition.y = GetProfileInt(name, "ptMaxPosition.y", 0);
779         wndd->p.rcNormalPosition.left = GetProfileInt(name, "rcNormalPosition.left", 0);
780         wndd->p.rcNormalPosition.top = GetProfileInt(name, "rcNormalPosition.top", 0);
781         wndd->p.rcNormalPosition.right = GetProfileInt(name, "rcNormalPosition.right", 0);
782         wndd->p.rcNormalPosition.bottom = GetProfileInt(name, "rcNormalPosition.bottom", 0);
783         wndd->visible = GetProfileInt(name, "Visible", 1);
784 }
785
786 int CFREDApp::init_window(window_data *wndd, CWnd *wnd, int adjust, int pre)
787 {
788         int width, height;
789         WINDOWPLACEMENT p;
790
791         if (pre && !wndd->visible)
792                 return -1;
793
794         if (wndd->processed)
795                 return -2;
796
797         Assert(wnd->GetSafeHwnd());
798         wnd->GetWindowPlacement(&p);
799         width = p.rcNormalPosition.right - p.rcNormalPosition.left;
800         height = p.rcNormalPosition.bottom - p.rcNormalPosition.top + adjust;
801         wndd->p.rcNormalPosition.right = wndd->p.rcNormalPosition.left + width;
802         wndd->p.rcNormalPosition.bottom = wndd->p.rcNormalPosition.top + height;
803
804         if (wndd->valid) {
805                 wnd->SetWindowPlacement(&wndd->p);
806 //              if (!wndd->visible)
807 //                      wnd->ShowWindow(SW_SHOW);
808 //              else
809 //                      wnd->ShowWindow(SW_HIDE);
810         }
811
812         record_window_data(wndd, wnd);
813         wndd->processed = 1;
814         return 0;
815 }
816
817 void CFREDApp::record_window_data(window_data *wndd, CWnd *wnd)
818 {
819         wnd->GetWindowPlacement(&wndd->p);
820         wndd->visible = wnd->IsWindowVisible();
821         wndd->valid = TRUE;
822 }