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