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