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