]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
FGIO::shutdown_all() is called from the FGIO destructor so we don't want
[flightgear.git] / src / GUI / gui_funcs.cxx
1 /**************************************************************************
2  * gui_funcs.cxx
3  *
4  * Based on gui.cxx and renamed on 2002/08/13 by Erik Hofman.
5  *
6  * Written 1998 by Durk Talsma, started Juni, 1998.  For the flight gear
7  * project.
8  *
9  * Additional mouse supported added by David Megginson, 1999.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * $Id$
26  **************************************************************************/
27
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include <simgear/compiler.h>
34
35 #ifdef SG_MATH_EXCEPTION_CLASH
36 #  include <math.h>
37 #endif
38
39 #ifdef HAVE_WINDOWS_H
40 #  include <windows.h>
41 #endif
42
43 #include SG_GL_H
44
45 #if defined(FX) && defined(XMESA)
46 #  include <GL/xmesa.h>
47 #endif
48
49 #include STL_FSTREAM
50 #include STL_STRING
51
52 #include <stdlib.h>
53 #include <string.h>
54
55 // for help call back
56 #ifdef WIN32
57 # include <shellapi.h>
58 # ifdef __CYGWIN__
59 #  include <sys/cygwin.h>
60 # endif
61 #endif
62
63 #include <plib/ssg.h>
64
65 #include <simgear/constants.h>
66 #include <simgear/debug/logstream.hxx>
67 #include <simgear/misc/sg_path.hxx>
68 #include <simgear/screen/screen-dump.hxx>
69
70 #include <Include/general.hxx>
71 #include <Aircraft/aircraft.hxx>
72 #include <Airports/simple.hxx>
73 #include <Autopilot/auto_gui.hxx>
74 #include <Cockpit/panel.hxx>
75 #include <Controls/controls.hxx>
76 #include <FDM/flight.hxx>
77 #include <Main/main.hxx>
78 #include <Main/fg_init.hxx>
79 #include <Main/fg_io.hxx>
80 #include <Main/globals.hxx>
81 #include <Main/fg_props.hxx>
82 #include <Main/viewmgr.hxx>
83
84 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
85 #  include <simgear/screen/win32-printer.h>
86 #  include <simgear/screen/GlBitmaps.h>
87 #endif
88
89 #include "gui.h"
90 #include "gui_local.hxx"
91 #include "preset_dlg.hxx"
92 #include "prop_picker.hxx"
93 #include "sgVec3Slider.hxx"
94
95 SG_USING_STD(string);
96 SG_USING_STD(cout);
97
98 extern void fgHUDalphaAdjust( puObject * );
99
100 // from cockpit.cxx
101 extern void fgLatLonFormatToggle( puObject *);
102
103 #if defined( TR_HIRES_SNAP)
104 #include <simgear/screen/tr.h>
105 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
106                          GLfloat x_end, GLfloat y_end );
107 #endif
108
109 puDialogBox  *dialogBox = 0;
110 puFrame      *dialogFrame = 0;
111 puText       *dialogBoxMessage = 0;
112 puOneShot    *dialogBoxOkButton = 0;
113
114 puDialogBox  *YNdialogBox = 0;
115 puFrame      *YNdialogFrame = 0;
116 puText       *YNdialogBoxMessage = 0;
117 puOneShot    *YNdialogBoxOkButton = 0;
118 puOneShot    *YNdialogBoxNoButton = 0;
119
120 char *gui_msg_OK;     // "OK"
121 char *gui_msg_NO;     // "NO"
122 char *gui_msg_YES;    // "YES"
123 char *gui_msg_CANCEL; // "CANCEL"
124 char *gui_msg_RESET;  // "RESET"
125
126 char msg_OK[]     = "OK";
127 char msg_NO[]     = "NO";
128 char msg_YES[]    = "YES";
129 char msg_CANCEL[] = "Cancel";
130 char msg_RESET[]  = "Reset";
131
132 char global_dialog_string[256];
133
134 const __fg_gui_fn_t __fg_gui_fn[] = {
135
136         // File
137         {"saveFlight", saveFlight},
138         {"loadFlight", loadFlight},
139         {"reInit", reInit},
140 #ifdef TR_HIRES_SNAP
141         {"dumpHiResSnapShot", fgHiResDumpWrapper},
142 #endif
143         {"dumpSnapShot", fgDumpSnapShotWrapper},
144 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
145         {"printScreen", printScreen},
146 #endif
147         {"MayBeGoodBye", MayBeGoodBye},
148
149         //View
150         {"guiTogglePanel", guiTogglePanel},
151         {"PilotOffsetAdjust", PilotOffsetAdjust},
152         {"fgHUDalphaAdjust", fgHUDalphaAdjust},
153         {"prop_pickerView", prop_pickerView},
154
155         // Environment
156         {"fgPresetAirport", fgPresetAirport},
157         {"fgPresetRunway", fgPresetRunway},
158         {"fgPresetOffsetDistance", fgPresetOffsetDistance},
159         {"fgPresetAltitude", fgPresetAltitude},
160         {"fgPresetGlideslope", fgPresetGlideslope},
161         {"fgPresetAirspeed", fgPresetAirspeed},
162         {"fgPresetCommit", fgPresetCommit},
163
164         // Autopilot
165         {"NewAltitude", NewAltitude},
166         {"NewHeading", NewHeading},
167         {"AddWayPoint", AddWayPoint},
168         {"PopWayPoint", PopWayPoint},
169         {"ClearRoute", ClearRoute},
170         {"fgLatLonFormatToggle", fgLatLonFormatToggle},
171
172         // Help
173         {"helpCb", helpCb},
174
175         // Structure termination
176         {"", NULL}
177 };
178
179
180 /* ================ General Purpose Functions ================ */
181
182 void initDialog(void) {
183     // Initialize our GLOBAL GUI STRINGS
184     gui_msg_OK     = msg_OK;     // "OK"
185     gui_msg_NO     = msg_NO;     // "NO"
186     gui_msg_YES    = msg_YES;    // "YES"
187     gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
188     gui_msg_RESET  = msg_RESET;  // "RESET"
189 }
190
191 // General Purpose Message Box
192 void mkDialog (const char *txt)
193 {
194     strncpy(global_dialog_string, txt, 256);
195     dialogBoxMessage->setLabel(global_dialog_string);
196     FG_PUSH_PUI_DIALOG( dialogBox );
197 }
198
199 // Message Box to report an error.
200 void guiErrorMessage (const char *txt)
201 {
202     SG_LOG(SG_GENERAL, SG_ALERT, txt);
203     if (dialogBox != 0)
204       mkDialog(txt);
205 }
206
207 // Message Box to report a throwable (usually an exception).
208 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
209 {
210     string msg = txt;
211     msg += '\n';
212     msg += throwable.getFormattedMessage();
213     if (!throwable.getOrigin().empty()) {
214       msg += "\n (reported by ";
215       msg += throwable.getOrigin();
216       msg += ')';
217     }
218     SG_LOG(SG_GENERAL, SG_ALERT, msg);
219     if (dialogBox != 0)
220       mkDialog(msg.c_str());
221 }
222
223 // Intercept the Escape Key
224 void ConfirmExitDialog(void)
225 {
226     FG_PUSH_PUI_DIALOG( YNdialogBox );
227 }
228
229
230
231 /* -----------------------------------------------------------------------
232 the Gui callback functions 
233 ____________________________________________________________________*/
234
235
236 // Hier Neu :-) This is my newly added code
237 // Added by David Findlay <nedz@bigpond.com>
238 // on Sunday 3rd of December
239
240 // Start new Save Dialog Box
241 puDialogBox     *SaveDialog = 0;
242 puFrame         *SaveDialogFrame = 0;
243 puText          *SaveDialogMessage = 0;
244 puInput         *SaveDialogInput = 0;
245
246 puOneShot       *SaveDialogOkButton = 0;
247 puOneShot       *SaveDialogCancelButton = 0;
248 // static puOneShot       *SaveDialogResetButton = 0;
249
250 // Default save filename
251 char saveFile[256] = "fgfs.sav";
252
253 // Cancel Button
254 void SaveDialogCancel(puObject *) {
255     FG_POP_PUI_DIALOG( SaveDialog );
256 }
257
258 // If press OK do this
259 void SaveDialogOk(puObject*) {
260
261     FG_POP_PUI_DIALOG( SaveDialog );
262
263     char *s;
264     SaveDialogInput->getValue(&s);
265
266     ofstream output(s);
267     // cout << saveFile << endl;
268     if (output.good() && fgSaveFlight(output)) {
269         output.close();
270         mkDialog("Saved flight");
271         SG_LOG(SG_INPUT, SG_INFO, "Saved flight");
272     } else {
273         mkDialog("Cannot save flight");
274         SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight");
275     }
276 }
277
278 // Create Dialog
279 void saveFlight(puObject *cv) {
280     SaveDialog = new puDialogBox (150, 50);
281     {
282         SaveDialogFrame   = new puFrame           (0,0,350, 150);
283         SaveDialogMessage
284             = new puText( (150 - puGetDefaultLabelFont().getStringWidth( "File Name:" ) / 2), 110 );
285         SaveDialogMessage ->    setLabel          ("File Name:");
286
287         SaveDialogInput   = new puInput           (50, 70, 300, 100);
288         SaveDialogInput   ->    setValue          (saveFile);
289         SaveDialogInput   ->    acceptInput();
290
291         SaveDialogOkButton     =  new puOneShot   (50, 10, 110, 50);
292         SaveDialogOkButton     ->     setLegend   (gui_msg_OK);
293         SaveDialogOkButton     ->     setCallback ( SaveDialogOk );
294         SaveDialogOkButton     ->     makeReturnDefault(TRUE);
295
296         SaveDialogCancelButton =  new puOneShot   (140, 10, 210, 50);
297         SaveDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
298         SaveDialogCancelButton ->     setCallback ( SaveDialogCancel );
299     }
300     FG_FINALIZE_PUI_DIALOG( SaveDialog );
301    
302     SaveDialog -> reveal();
303 }
304
305 // Load Dialog Start
306 puDialogBox     *LoadDialog = 0;
307 puFrame         *LoadDialogFrame = 0;
308 puText          *LoadDialogMessage = 0;
309 puInput         *LoadDialogInput = 0;
310
311 puOneShot       *LoadDialogOkButton = 0;
312 puOneShot       *LoadDialogCancelButton = 0;
313 // static puOneShot       *LoadDialogResetButton = 0;
314
315 // Default load filename
316 char loadFile[256] = "fgfs.sav";
317
318 // Do this if the person click okay
319 void LoadDialogOk(puObject *) {
320
321     FG_POP_PUI_DIALOG( LoadDialog );
322
323     char *l;
324     LoadDialogInput->getValue(&l);
325
326     ifstream input(l);
327     if (input.good() && fgLoadFlight(input)) {
328         input.close();
329         mkDialog("Loaded flight");
330         SG_LOG(SG_INPUT, SG_INFO, "Restored flight");
331     } else {
332         mkDialog("Failed to load flight");
333         SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight");
334     }
335 }
336
337 // Do this if the person presses cancel
338 void LoadDialogCancel(puObject *) {
339     FG_POP_PUI_DIALOG( LoadDialog );
340 }
341
342 // Create Load Dialog
343 void loadFlight(puObject *cb)
344 {
345     LoadDialog = new puDialogBox (150, 50);
346     {
347         LoadDialogFrame   = new puFrame           (0,0,350, 150);
348         LoadDialogMessage
349             = new puText( (150 - puGetDefaultLabelFont().getStringWidth( "File Name:" ) / 2), 110 );
350         LoadDialogMessage ->    setLabel          ("File Name:");
351
352         LoadDialogInput   = new puInput           (50, 70, 300, 100);
353         LoadDialogInput   ->    setValue          (loadFile);
354         LoadDialogInput   ->    acceptInput();
355
356         LoadDialogOkButton     =  new puOneShot   (50, 10, 110, 50);
357         LoadDialogOkButton     ->     setLegend   (gui_msg_OK);
358         LoadDialogOkButton     ->     setCallback ( LoadDialogOk );
359         LoadDialogOkButton     ->     makeReturnDefault(TRUE);
360
361         LoadDialogCancelButton =  new puOneShot   (140, 10, 210, 50);
362         LoadDialogCancelButton ->     setLegend   (gui_msg_CANCEL);
363         LoadDialogCancelButton ->     setCallback ( LoadDialogCancel );
364     }
365     FG_FINALIZE_PUI_DIALOG( LoadDialog );
366    
367     LoadDialog -> reveal();
368 }
369
370 // This is the accessor function
371 void guiTogglePanel(puObject *cb)
372 {
373   if (fgGetBool("/sim/panel/visibility"))
374     fgSetBool("/sim/panel/visibility", false);
375   else
376     fgSetBool("/sim/panel/visibility", true);
377
378   globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
379                                   fgGetInt("/sim/startup/ysize"));
380 }
381
382 void goodBye(puObject *)
383 {
384     // SG_LOG( SG_INPUT, SG_ALERT,
385     //      "Program exiting normally at user request." );
386     cout << "Program exiting normally at user request." << endl;
387
388     exit(0);
389 }
390
391
392 void goAwayCb (puObject *me)
393 {
394     FG_POP_PUI_DIALOG( dialogBox );
395 }
396
397 void mkDialogInit (void)
398 {
399     //  printf("mkDialogInit\n");
400     int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
401     int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
402     dialogBox = new puDialogBox (x, y); // 150, 50
403     {
404         dialogFrame = new puFrame (0,0,400,100);
405         dialogBoxMessage  =  new puText         (10, 70);
406         dialogBoxMessage  -> setLabel           ("");
407         dialogBoxOkButton =  new puOneShot      (180, 10, 240, 50);
408         dialogBoxOkButton -> setLegend          (gui_msg_OK);
409         dialogBoxOkButton -> makeReturnDefault  (TRUE );
410         dialogBoxOkButton -> setCallback        (goAwayCb);
411     }
412     FG_FINALIZE_PUI_DIALOG( dialogBox );
413 }
414
415 void MayBeGoodBye(puObject *)
416 {
417     ConfirmExitDialog(); 
418 }
419
420 void goAwayYesNoCb(puObject *me)
421 {
422     FG_POP_PUI_DIALOG( YNdialogBox);
423 }
424
425 void ConfirmExitDialogInit(void)
426 {
427     char msg[] = "Really Quit";
428     char *s;
429
430     //  printf("ConfirmExitDialogInit\n");
431     int len = 200 - puGetDefaultLabelFont().getStringWidth ( msg ) / 2;
432
433     int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
434     int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
435         
436     YNdialogBox = new puDialogBox (x, y); // 150, 50
437     {
438         YNdialogFrame = new puFrame (0,0,400, 100);
439         
440         YNdialogBoxMessage  =  new puText         (len, 70);
441         YNdialogBoxMessage  -> setDefaultValue    (msg);
442         YNdialogBoxMessage  -> getDefaultValue    (&s);
443         YNdialogBoxMessage  -> setLabel           (s);
444         
445         YNdialogBoxOkButton =  new puOneShot      (100, 10, 160, 50);
446         YNdialogBoxOkButton -> setLegend          (gui_msg_OK);
447         YNdialogBoxOkButton -> makeReturnDefault  (TRUE );
448         YNdialogBoxOkButton -> setCallback        (goodBye);
449         
450         YNdialogBoxNoButton =  new puOneShot      (240, 10, 300, 50);
451         YNdialogBoxNoButton -> setLegend          (gui_msg_NO);
452         YNdialogBoxNoButton -> setCallback        (goAwayYesNoCb);
453     }
454     FG_FINALIZE_PUI_DIALOG( YNdialogBox );
455 }
456
457 void notCb (puObject *)
458 {
459     mkDialog ("This function isn't implemented yet");
460 }
461
462 void helpCb (puObject *)
463 {
464     string command;
465         
466 #if defined(FX) && !defined(WIN32)
467 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
468     if ( globals->get_fullscreen() ) {
469         globals->set_fullscreen(false);
470         XMesaSetFXmode( XMESA_FX_WINDOW );
471     }
472 #  endif
473 #endif
474         
475     SGPath path( globals->get_fg_root() );
476     path.append( "Docs/index.html" );
477         
478 #if !defined(WIN32)
479
480     string help_app = fgGetString("/sim/startup/browser-app");
481
482     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
483         command = help_app + " -remote \"openURL(" + path.str() + ")\"";
484     } else {
485         command = help_app + " " + path.str();
486     }
487     command += " &";
488     system( command.c_str() );
489
490 #else // WIN32
491
492     // Look for favorite browser
493     char Dummy[1024], ExecName[1024], browserParameter[1024];
494     char win32_name[1024];
495 # ifdef __CYGWIN__
496     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
497 # else
498     strcpy(win32_name,path.c_str());
499 # endif
500     Dummy[0] = 0;
501     FindExecutable(win32_name, Dummy, ExecName);
502     sprintf(browserParameter, "file:///%s", win32_name);
503     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
504                    SW_SHOWNORMAL ) ;
505
506 #endif
507         
508     mkDialog ("Help started in your web browser window.");
509 }
510
511 #if defined( TR_HIRES_SNAP)
512 void fgHiResDump()
513 {
514     FILE *f;
515     string message;
516     bool show_pu_cursor = false;
517     char *filename = new char [24];
518     static int count = 1;
519
520     static const SGPropertyNode *master_freeze
521         = fgGetNode("/sim/freeze/master");
522
523     bool freeze = master_freeze->getBoolValue();
524     if ( !freeze ) {
525         fgSetBool("/sim/freeze/master", true);
526     }
527
528     TurnCursorOff();
529     if ( !puCursorIsHidden() ) {
530         show_pu_cursor = true;
531         puHideCursor();
532     }
533
534     FGRenderer *renderer = globals->get_renderer();
535     renderer->init();
536     renderer->resize( fgGetInt("/sim/startup/xsize"),
537                       fgGetInt("/sim/startup/ysize") );
538
539     // we need two render frames here to clear the menu and cursor
540     // ... not sure why but doing an extra fgRenderFrame() shouldn't
541     // hurt anything
542     renderer->update( true );
543     renderer->update( true );
544
545     // This ImageSize stuff is a temporary hack
546     // should probably use 128x128 tile size and
547     // support any image size
548
549     // This should be a requester to get multiplier from user
550     int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
551     int width = fgGetInt("/sim/startup/xsize");
552     int height = fgGetInt("/sim/startup/ysize");
553         
554     /* allocate buffer large enough to store one tile */
555     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
556     if (!tile) {
557         printf("Malloc of tile buffer failed!\n");
558         return;
559     }
560
561     int imageWidth  = multiplier*width;
562     int imageHeight = multiplier*height;
563
564     /* allocate buffer to hold a row of tiles */
565     GLubyte *buffer
566         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
567     if (!buffer) {
568         free(tile);
569         printf("Malloc of tile row buffer failed!\n");
570         return;
571     }
572     TRcontext *tr = trNew();
573     trTileSize(tr, width, height, 0);
574     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
575     trImageSize(tr, imageWidth, imageHeight);
576     trRowOrder(tr, TR_TOP_TO_BOTTOM);
577     sgFrustum *frustum = ssgGetFrustum();
578     trFrustum(tr,
579               frustum->getLeft(), frustum->getRight(),
580               frustum->getBot(),  frustum->getTop(), 
581               frustum->getNear(), frustum->getFar());
582         
583     /* Prepare ppm output file */
584     while (count < 1000) {
585         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
586         if ( (f = fopen(filename, "r")) == NULL )
587             break;
588         fclose(f);
589     }
590     f = fopen(filename, "wb");
591     if (!f) {
592         printf("Couldn't open image file: %s\n", filename);
593         free(buffer);
594         free(tile);
595         return;
596     }
597     fprintf(f,"P6\n");
598     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
599     fprintf(f,"%i %i\n", imageWidth, imageHeight);
600     fprintf(f,"255\n");
601
602     /* just to be safe... */
603     glPixelStorei(GL_PACK_ALIGNMENT, 1);
604
605     /* Because the HUD and Panel change the ViewPort we will
606      * need to handle some lowlevel stuff ourselves */
607     int ncols = trGet(tr, TR_COLUMNS);
608     int nrows = trGet(tr, TR_ROWS);
609
610     bool do_hud = fgGetBool("/sim/hud/visibility");
611     GLfloat hud_col_step = 640.0 / ncols;
612     GLfloat hud_row_step = 480.0 / nrows;
613         
614     bool do_panel = fgPanelVisible();
615     GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
616     GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
617         
618     /* Draw tiles */
619     int more = 1;
620     while (more) {
621         trBeginTile(tr);
622         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
623         int curRow =  trGet(tr, TR_CURRENT_ROW);
624         renderer->update( false );
625         if ( do_hud )
626             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
627                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
628         if (do_panel)
629             globals->get_current_panel()->update(
630                                    curColumn*panel_col_step, panel_col_step,
631                                    curRow*panel_row_step,    panel_row_step );
632         more = trEndTile(tr);
633
634         /* save tile into tile row buffer*/
635         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
636         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
637         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
638         int xOffset = curColumn * bytesPerTileRow;
639         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
640         int i;
641         for (i=0;i<height;i++) {
642             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
643                    tile + i*bytesPerTileRow,              /* Src */
644                    bytesPerCurrentTileRow);               /* Byte count*/
645         }
646
647         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
648             /* write this buffered row of tiles to the file */
649             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
650             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
651             int i;
652             for (i=0;i<curTileHeight;i++) {
653                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
654                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
655                 fwrite(rowPtr, 1, imageWidth*3, f);
656             }
657         }
658
659     }
660
661     renderer->resize( width, height );
662
663     trDelete(tr);
664
665     fclose(f);
666
667     message = "Snap shot saved to ";
668     message += filename;
669     mkDialog (message.c_str());
670
671     free(tile);
672     free(buffer);
673
674     //  message = "Snap shot saved to ";
675     //  message += filename;
676     //  mkDialog (message.c_str());
677
678     delete [] filename;
679
680     if ( show_pu_cursor ) {
681         puShowCursor();
682     }
683
684     TurnCursorOn();
685
686     if ( !freeze ) {
687         fgSetBool("/sim/freeze/master", false);
688     }
689 }
690 #endif // #if defined( TR_HIRES_SNAP)
691
692
693 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
694
695 void rotateView( double roll, double pitch, double yaw )
696 {
697         // rotate view
698 }
699
700 GlBitmap *b1 = NULL;
701 GLubyte *hiResScreenCapture( int multiplier )
702 {
703     float oldfov = fgGetDouble("/sim/current-view/field-of-view");
704     float fov = oldfov / multiplier;
705     FGViewer *v = globals->get_current_view();
706     fgSetDouble("/sim/current-view/field-of-view", fov);
707     globals->get_renderer()->init();
708     int cur_width = fgGetInt("/sim/startup/xsize");
709     int cur_height = fgGetInt("/sim/startup/ysize");
710     if (b1) delete( b1 );
711     // New empty (mostly) bitmap
712     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
713     int x,y;
714     for ( y = 0; y < multiplier; y++ ) {
715         for ( x = 0; x < multiplier; x++ ) {
716             globals->get_renderer()->resize( cur_width, cur_height );
717             // pan to tile
718             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
719             globals->get_renderer()->update( false );
720             // restore view
721             GlBitmap b2;
722             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
723         }
724     }
725     fgSetDouble("/sim/current-view/field-of-view", oldfov);
726     return b1->getBitmap();
727 }
728 #endif
729
730 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
731 // win32 print screen function
732 void printScreen ( puObject *obj ) {
733     bool show_pu_cursor = false;
734     TurnCursorOff();
735     if ( !puCursorIsHidden() ) {
736         show_pu_cursor = true;
737         puHideCursor();
738     }
739     // BusyCursor( 0 );
740
741     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
742     int cur_width = fgGetInt("/sim/startup/xsize");
743     int cur_height = fgGetInt("/sim/startup/ysize");
744     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
745     p.End( hiResScreenCapture(3) );
746
747     // BusyCursor(1);
748     if ( show_pu_cursor ) {
749         puShowCursor();
750     }
751     TurnCursorOn();
752 }
753 #endif // #ifdef WIN32
754
755
756 void fgDumpSnapShotWrapper ( puObject *obj ) {
757     fgDumpSnapShot();
758 }
759
760
761 void fgHiResDumpWrapper ( puObject *obj ) {
762     fgHiResDump();
763 }
764
765
766 // do a screen snap shot
767 void fgDumpSnapShot () {
768     bool show_pu_cursor = false;
769     char *filename = new char [24];
770     string message;
771     static int count = 1;
772
773     static const SGPropertyNode *master_freeze
774         = fgGetNode("/sim/freeze/master");
775
776     bool freeze = master_freeze->getBoolValue();
777     if ( !freeze ) {
778         fgSetBool("/sim/freeze/master", true);
779     }
780
781     TurnCursorOff();
782     if ( !puCursorIsHidden() ) {
783         show_pu_cursor = true;
784         puHideCursor();
785     }
786
787     FGRenderer *renderer = globals->get_renderer();
788     renderer->init();
789     renderer->resize( fgGetInt("/sim/startup/xsize"),
790                         fgGetInt("/sim/startup/ysize") );
791
792     // we need two render frames here to clear the menu and cursor
793     // ... not sure why but doing an extra fgRenderFrame() shouldn't
794     // hurt anything
795     renderer->update( true );
796     renderer->update( true );
797
798     while (count < 1000) {
799         FILE *fp;
800         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
801         if ( (fp = fopen(filename, "r")) == NULL )
802             break;
803         fclose(fp);
804     }
805
806     if ( sg_glDumpWindow( filename,
807                           fgGetInt("/sim/startup/xsize"), 
808                           fgGetInt("/sim/startup/ysize")) ) {
809         message = "Snap shot saved to ";
810         message += filename;
811     } else {
812         message = "Failed to save to ";
813         message += filename;
814     }
815
816     mkDialog (message.c_str());
817
818     delete [] filename;
819
820     if ( show_pu_cursor ) {
821         puShowCursor();
822     }
823
824     TurnCursorOn();
825
826     if ( !freeze ) {
827         fgSetBool("/sim/freeze/master", false);
828     }
829 }
830