]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
Added a "Presets" menu.
[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/glut.h>
44 #include <GL/gl.h>
45
46 #if defined(FX) && defined(XMESA)
47 #  include <GL/xmesa.h>
48 #endif
49
50 #include STL_FSTREAM
51 #include STL_STRING
52
53 #include <stdlib.h>
54 #include <string.h>
55
56 // for help call back
57 #ifdef WIN32
58 # include <shellapi.h>
59 # ifdef __CYGWIN__
60 #  include <sys/cygwin.h>
61 # endif
62 #endif
63
64 #include <simgear/constants.h>
65 #include <simgear/debug/logstream.hxx>
66 #include <simgear/misc/sg_path.hxx>
67 #include <simgear/screen/screen-dump.hxx>
68
69 #include <Include/general.hxx>
70 #include <Aircraft/aircraft.hxx>
71 #include <Airports/simple.hxx>
72 #include <Autopilot/auto_gui.hxx>
73 #include <Autopilot/newauto.hxx>
74 #include <Cockpit/panel.hxx>
75 #include <Controls/controls.hxx>
76 #include <FDM/flight.hxx>
77 #include <Main/fg_init.hxx>
78 #include <Main/fg_io.hxx>
79 #include <Main/globals.hxx>
80 #include <Main/fg_props.hxx>
81 #include <Main/viewmgr.hxx>
82
83 #ifdef FG_NETWORK_OLK
84 #include <NetworkOLK/network.h>
85 #endif
86    
87 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
88 #  include <simgear/screen/win32-printer.h>
89 #  include <simgear/screen/GlBitmaps.h>
90 #endif
91
92 #include "gui.h"
93 #include "gui_local.hxx"
94 #include "net_dlg.hxx"
95 #include "preset_dlg.hxx"
96 #include "prop_picker.hxx"
97 #include "sgVec3Slider.hxx"
98
99 SG_USING_STD(string);
100
101 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
102 SG_USING_STD(cout);
103 #endif
104
105 // main.cxx hack, should come from an include someplace
106 extern void fgInitVisuals( void );
107 extern void fgReshape( int width, int height );
108 extern void fgRenderFrame( void );
109
110 extern void fgHUDalphaAdjust( puObject * );
111
112 // from cockpit.cxx
113 extern void fgLatLonFormatToggle( puObject *);
114
115 #ifdef FG_NETWORK_OLK
116 extern void net_fgd_scan(puObject *cb);
117 #endif // #ifdef FG_NETWORK_OLK
118
119 #if defined( TR_HIRES_SNAP)
120 #include <simgear/screen/tr.h>
121 extern void trRenderFrame( void );
122 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
123                          GLfloat x_end, GLfloat y_end );
124 #endif
125
126 extern puMenuBar    *mainMenuBar;
127
128 puDialogBox  *dialogBox = 0;
129 puFrame      *dialogFrame = 0;
130 puText       *dialogBoxMessage = 0;
131 puOneShot    *dialogBoxOkButton = 0;
132
133 puDialogBox  *YNdialogBox = 0;
134 puFrame      *YNdialogFrame = 0;
135 puText       *YNdialogBoxMessage = 0;
136 puOneShot    *YNdialogBoxOkButton = 0;
137 puOneShot    *YNdialogBoxNoButton = 0;
138
139 char *gui_msg_OK;     // "OK"
140 char *gui_msg_NO;     // "NO"
141 char *gui_msg_YES;    // "YES"
142 char *gui_msg_CANCEL; // "CANCEL"
143 char *gui_msg_RESET;  // "RESET"
144
145 char msg_OK[]     = "OK";
146 char msg_NO[]     = "NO";
147 char msg_YES[]    = "YES";
148 char msg_CANCEL[] = "Cancel";
149 char msg_RESET[]  = "Reset";
150
151 char global_dialog_string[256];
152
153 const __fg_gui_fn_t __fg_gui_fn[] = {
154
155         // File
156         {"saveFlight", saveFlight},
157         {"loadFlight", loadFlight},
158         {"reInit", reInit},
159 #ifdef TR_HIRES_SNAP
160         {"dumpHiResSnapShot", dumpHiResSnapShot},
161 #endif
162         {"dumpSnapShot", dumpSnapShot},
163 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
164         {"printScreen", printScreen},
165 #endif
166         {"MayBeGoodBye", MayBeGoodBye},
167
168         //View
169         {"guiTogglePanel", guiTogglePanel},
170         {"PilotOffsetAdjust", PilotOffsetAdjust},
171         {"fgHUDalphaAdjust", fgHUDalphaAdjust},
172         {"prop_pickerView", prop_pickerView},
173
174         // Environment
175         {"fgPresetAirport", fgPresetAirport},
176         {"fgPresetRunway", fgPresetRunway},
177         {"fgPresetOffsetDistance", fgPresetOffsetDistance},
178         {"fgPresetAltitude", fgPresetAltitude},
179         {"fgPresetGlideslope", fgPresetGlideslope},
180         {"fgPresetAirspeed", fgPresetAirspeed},
181         {"fgPresetCommit", fgPresetCommit},
182
183         // Network
184 #ifdef FG_NETWORK_OLK
185         {"net_display_toggle", net_display_toggle},
186         {"NewCallSign", NewCallSign},
187         {"net_fgd_scan", net_fgd_scan},
188         {"net_register", net_register},
189         {"net_unregister", net_unregister},
190 #endif
191
192         // Autopilot
193         {"NewAltitude", NewAltitude},
194         {"NewHeading", NewHeading},
195         {"AddWayPoint", AddWayPoint},
196         {"PopWayPoint", PopWayPoint},
197         {"ClearRoute", ClearRoute},
198         {"fgAPAdjust", fgAPAdjust},
199         {"fgLatLonFormatToggle", fgLatLonFormatToggle},
200
201         // Help
202         {"helpCb", helpCb},
203
204         // Structure termination
205         {"", NULL}
206 };
207
208
209 /* ================ General Purpose Functions ================ */
210
211 void initDialog(void) {
212     // Initialize our GLOBAL GUI STRINGS
213     gui_msg_OK     = msg_OK;     // "OK"
214     gui_msg_NO     = msg_NO;     // "NO"
215     gui_msg_YES    = msg_YES;    // "YES"
216     gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
217     gui_msg_RESET  = msg_RESET;  // "RESET"
218 }
219
220 // General Purpose Message Box
221 void mkDialog (const char *txt)
222 {
223     strncpy(global_dialog_string, txt, 256);
224     dialogBoxMessage->setLabel(global_dialog_string);
225     FG_PUSH_PUI_DIALOG( dialogBox );
226 }
227
228 // Message Box to report an error.
229 void guiErrorMessage (const char *txt)
230 {
231     SG_LOG(SG_GENERAL, SG_ALERT, txt);
232     if (dialogBox != 0)
233       mkDialog(txt);
234 }
235
236 // Message Box to report a throwable (usually an exception).
237 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
238 {
239     string msg = txt;
240     msg += '\n';
241     msg += throwable.getFormattedMessage();
242     if (!throwable.getOrigin().empty()) {
243       msg += "\n (reported by ";
244       msg += throwable.getOrigin();
245       msg += ')';
246     }
247     SG_LOG(SG_GENERAL, SG_ALERT, msg);
248     if (dialogBox != 0)
249       mkDialog(msg.c_str());
250 }
251
252 // Toggle the Menu and Mouse display state
253 void guiToggleMenu(void)
254 {
255     if( gui_menu_on ) {
256         // printf("Hiding Menu\n");
257         mainMenuBar->hide  ();
258 #if defined(WIN32_CURSOR_TWEAKS_OFF)
259         if( mouse_mode == MOUSE_POINTER )
260             TurnCursorOff();
261 #endif // WIN32_CURSOR_TWEAKS_OFF
262     } else {
263         // printf("Showing Menu\n");
264         mainMenuBar->reveal();
265 #ifdef WIN32
266         TurnCursorOn();
267 #endif // WIN32
268     }
269     gui_menu_on = ~gui_menu_on;
270 }
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 //void MenuHideMenuCb(puObject *cb)
432 void hideMenuCb (puObject *cb)
433 {
434     guiToggleMenu();
435 }
436
437 void goodBye(puObject *)
438 {
439     // SG_LOG( SG_INPUT, SG_ALERT,
440     //      "Program exiting normally at user request." );
441     cout << "Program exiting normally at user request." << endl;
442
443 #ifdef FG_NETWORK_OLK    
444     if ( fgGetBool("/sim/networking/network-olk") ) {
445         if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
446     }
447 #endif
448
449     // close all external I/O connections
450     globals->get_io()->shutdown_all();
451
452     exit(0);
453 }
454
455
456 void goAwayCb (puObject *me)
457 {
458     FG_POP_PUI_DIALOG( dialogBox );
459 }
460
461 void mkDialogInit (void)
462 {
463     //  printf("mkDialogInit\n");
464     int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
465     int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
466     dialogBox = new puDialogBox (x, y); // 150, 50
467     {
468         dialogFrame = new puFrame (0,0,400,100);
469         dialogBoxMessage  =  new puText         (10, 70);
470         dialogBoxMessage  -> setLabel           ("");
471         dialogBoxOkButton =  new puOneShot      (180, 10, 240, 50);
472         dialogBoxOkButton -> setLegend          (gui_msg_OK);
473         dialogBoxOkButton -> makeReturnDefault  (TRUE );
474         dialogBoxOkButton -> setCallback        (goAwayCb);
475     }
476     FG_FINALIZE_PUI_DIALOG( dialogBox );
477 }
478
479 void MayBeGoodBye(puObject *)
480 {
481     ConfirmExitDialog(); 
482 }
483
484 void goAwayYesNoCb(puObject *me)
485 {
486     FG_POP_PUI_DIALOG( YNdialogBox);
487 }
488
489 void ConfirmExitDialogInit(void)
490 {
491     char msg[] = "Really Quit";
492     char *s;
493
494     //  printf("ConfirmExitDialogInit\n");
495     int len = 200 - puGetDefaultLabelFont().getStringWidth ( msg ) / 2;
496
497     int x = (fgGetInt("/sim/startup/xsize")/2 - 400/2);
498     int y = (fgGetInt("/sim/startup/ysize")/2 - 100/2);
499         
500     YNdialogBox = new puDialogBox (x, y); // 150, 50
501     {
502         YNdialogFrame = new puFrame (0,0,400, 100);
503         
504         YNdialogBoxMessage  =  new puText         (len, 70);
505         YNdialogBoxMessage  -> setDefaultValue    (msg);
506         YNdialogBoxMessage  -> getDefaultValue    (&s);
507         YNdialogBoxMessage  -> setLabel           (s);
508         
509         YNdialogBoxOkButton =  new puOneShot      (100, 10, 160, 50);
510         YNdialogBoxOkButton -> setLegend          (gui_msg_OK);
511         YNdialogBoxOkButton -> makeReturnDefault  (TRUE );
512         YNdialogBoxOkButton -> setCallback        (goodBye);
513         
514         YNdialogBoxNoButton =  new puOneShot      (240, 10, 300, 50);
515         YNdialogBoxNoButton -> setLegend          (gui_msg_NO);
516         YNdialogBoxNoButton -> setCallback        (goAwayYesNoCb);
517     }
518     FG_FINALIZE_PUI_DIALOG( YNdialogBox );
519 }
520
521 void notCb (puObject *)
522 {
523     mkDialog ("This function isn't implemented yet");
524 }
525
526 void helpCb (puObject *)
527 {
528     string command;
529         
530 #if defined(FX) && !defined(WIN32)
531 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
532     if ( globals->get_fullscreen() ) {
533         globals->set_fullscreen(false);
534         XMesaSetFXmode( XMESA_FX_WINDOW );
535     }
536 #  endif
537 #endif
538         
539     SGPath path( globals->get_fg_root() );
540     path.append( "Docs/index.html" );
541         
542 #if !defined(WIN32)
543
544     string help_app = fgGetString("/sim/startup/browser-app");
545
546     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
547         command = help_app + " -remote \"openURL(" + path.str() + ")\"";
548     } else {
549         command = help_app + " " + path.str();
550     }
551     command += " &";
552     system( command.c_str() );
553
554 #else // WIN32
555
556     // Look for favorite browser
557     char Dummy[1024], ExecName[1024], browserParameter[1024];
558     char win32_name[1024];
559 # ifdef __CYGWIN__
560     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
561 # else
562     strcpy(win32_name,path.c_str());
563 # endif
564     Dummy[0] = 0;
565     FindExecutable(win32_name, Dummy, ExecName);
566     sprintf(browserParameter, "file:///%s", win32_name);
567     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
568                    SW_SHOWNORMAL ) ;
569
570 #endif
571         
572     mkDialog ("Help started in your web browser window.");
573 }
574
575 #if defined( TR_HIRES_SNAP)
576 void fgHiResDump()
577 {
578     FILE *f;
579     string message;
580     bool show_pu_cursor = false;
581     bool show_menu = false;
582     char *filename = new char [24];
583     static int count = 1;
584
585     static const SGPropertyNode *master_freeze
586         = fgGetNode("/sim/freeze/master");
587
588     bool freeze = master_freeze->getBoolValue();
589     if ( !freeze ) {
590         fgSetBool("/sim/freeze/master", true);
591     }
592
593     if(gui_menu_on) {
594         show_menu = true;
595         guiToggleMenu();
596     }
597         
598     if ( !puCursorIsHidden() ) {
599         show_pu_cursor = true;
600         puHideCursor();
601     }
602
603     fgInitVisuals();
604     fgReshape( fgGetInt("/sim/startup/xsize"),
605                fgGetInt("/sim/startup/ysize") );
606
607     // we need two render frames here to clear the menu and cursor
608     // ... not sure why but doing an extra fgRenderFrame() shouldn't
609     // hurt anything
610     fgRenderFrame();
611     fgRenderFrame();
612
613     // Make sure we have SSG projection primed for current view
614     glMatrixMode(GL_MODELVIEW);
615     glLoadIdentity();
616     ssgSetCamera( (sgVec4 *)globals->get_current_view()->get_VIEW() );
617     ssgSetFOV( globals->get_current_view()->get_h_fov(),
618                globals->get_current_view()->get_v_fov() );
619     // ssgSetNearFar( 10.0f, 120000.0f );
620     ssgSetNearFar( 0.5f, 1200000.0f );
621
622         
623     // This ImageSize stuff is a temporary hack
624     // should probably use 128x128 tile size and
625     // support any image size
626
627     // This should be a requester to get multiplier from user
628     int multiplier = 3;
629     int width = fgGetInt("/sim/startup/xsize");
630     int height = fgGetInt("/sim/startup/ysize");
631         
632     /* allocate buffer large enough to store one tile */
633     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
634     if (!tile) {
635         printf("Malloc of tile buffer failed!\n");
636         return;
637     }
638
639     int imageWidth  = multiplier*width;
640     int imageHeight = multiplier*height;
641
642     /* allocate buffer to hold a row of tiles */
643     GLubyte *buffer
644         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
645     if (!buffer) {
646         free(tile);
647         printf("Malloc of tile row buffer failed!\n");
648         return;
649     }
650     TRcontext *tr = trNew();
651     trTileSize(tr, width, height, 0);
652     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
653     trImageSize(tr, imageWidth, imageHeight);
654     trRowOrder(tr, TR_TOP_TO_BOTTOM);
655     sgFrustum *frustum = ssgGetFrustum();
656     trFrustum(tr,
657               frustum->getLeft(), frustum->getRight(),
658               frustum->getBot(),  frustum->getTop(), 
659               frustum->getNear(), frustum->getFar());
660         
661     /* Prepare ppm output file */
662     while (count < 1000) {
663         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
664         if ( (f = fopen(filename, "r")) == NULL )
665             break;
666         fclose(f);
667     }
668     f = fopen(filename, "wb");
669     if (!f) {
670         printf("Couldn't open image file: %s\n", filename);
671         free(buffer);
672         free(tile);
673         return;
674     }
675     fprintf(f,"P6\n");
676     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
677     fprintf(f,"%i %i\n", imageWidth, imageHeight);
678     fprintf(f,"255\n");
679
680     /* just to be safe... */
681     glPixelStorei(GL_PACK_ALIGNMENT, 1);
682
683     /* Because the HUD and Panel change the ViewPort we will
684      * need to handle some lowlevel stuff ourselves */
685     int ncols = trGet(tr, TR_COLUMNS);
686     int nrows = trGet(tr, TR_ROWS);
687
688     bool do_hud = fgGetBool("/sim/hud/visibility");
689     GLfloat hud_col_step = 640.0 / ncols;
690     GLfloat hud_row_step = 480.0 / nrows;
691         
692     bool do_panel = fgPanelVisible();
693     GLfloat panel_col_step = current_panel->getWidth() / ncols;
694     GLfloat panel_row_step = current_panel->getHeight() / nrows;
695         
696     /* Draw tiles */
697     int more = 1;
698     while (more) {
699         trBeginTile(tr);
700         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
701         int curRow =  trGet(tr, TR_CURRENT_ROW);
702         trRenderFrame();
703         if ( do_hud )
704             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
705                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
706         if (do_panel)
707             current_panel->update( curColumn*panel_col_step, panel_col_step,
708                                    curRow*panel_row_step,    panel_row_step );
709         more = trEndTile(tr);
710
711         /* save tile into tile row buffer*/
712         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
713         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
714         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
715         int xOffset = curColumn * bytesPerTileRow;
716         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
717         int i;
718         for (i=0;i<height;i++) {
719             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
720                    tile + i*bytesPerTileRow,              /* Src */
721                    bytesPerCurrentTileRow);               /* Byte count*/
722         }
723
724         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
725             /* write this buffered row of tiles to the file */
726             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
727             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
728             int i;
729             for (i=0;i<curTileHeight;i++) {
730                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
731                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
732                 fwrite(rowPtr, 1, imageWidth*3, f);
733             }
734         }
735
736     }
737
738     fgReshape( width, height );
739
740     trDelete(tr);
741
742     fclose(f);
743
744     message = "Snap shot saved to ";
745     message += filename;
746     mkDialog (message.c_str());
747
748     free(tile);
749     free(buffer);
750
751     //  message = "Snap shot saved to ";
752     //  message += filename;
753     //  mkDialog (message.c_str());
754
755     delete [] filename;
756
757     if( show_menu )
758         guiToggleMenu();
759
760     if ( show_pu_cursor ) {
761         puShowCursor();
762     }
763
764     if ( !freeze ) {
765         fgSetBool("/sim/freeze/master", false);
766     }
767 }
768 #endif // #if defined( TR_HIRES_SNAP)
769
770
771 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
772
773 void rotateView( double roll, double pitch, double yaw )
774 {
775         // rotate view
776 }
777
778 GlBitmap *b1 = NULL;
779 GLubyte *hiResScreenCapture( int multiplier )
780 {
781     float oldfov = fgGetDouble("/sim/current-view/field-of-view");
782     float fov = oldfov / multiplier;
783     FGViewer *v = globals->get_current_view();
784     fgSetDouble("/sim/current-view/field-of-view", fov);
785     fgInitVisuals();
786     int cur_width = fgGetInt("/sim/startup/xsize");
787     int cur_height = fgGetInt("/sim/startup/ysize");
788     if (b1) delete( b1 );
789     // New empty (mostly) bitmap
790     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
791     int x,y;
792     for ( y = 0; y < multiplier; y++ ) {
793         for ( x = 0; x < multiplier; x++ ) {
794             fgReshape( cur_width, cur_height );
795             // pan to tile
796             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
797             fgRenderFrame();
798             // restore view
799             GlBitmap b2;
800             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
801         }
802     }
803     fgSetDouble("/sim/current-view/field-of-view", oldfov);
804     return b1->getBitmap();
805 }
806 #endif
807
808 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
809 // win32 print screen function
810 void printScreen ( puObject *obj ) {
811     bool show_pu_cursor = false;
812     TurnCursorOff();
813     if ( !puCursorIsHidden() ) {
814         show_pu_cursor = true;
815         puHideCursor();
816     }
817     // BusyCursor( 0 );
818     mainMenuBar->hide();
819
820     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
821     int cur_width = fgGetInt("/sim/startup/xsize");
822     int cur_height = fgGetInt("/sim/startup/ysize");
823     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
824         p.End( hiResScreenCapture(3) );
825
826     if( gui_menu_on ) {
827         mainMenuBar->reveal();
828     }
829     // BusyCursor(1);
830     if ( show_pu_cursor ) {
831         puShowCursor();
832     }
833     TurnCursorOn();
834 }
835 #endif // #ifdef WIN32
836
837
838 void dumpSnapShot ( puObject *obj ) {
839     fgDumpSnapShot();
840 }
841
842
843 void dumpHiResSnapShot ( puObject *obj ) {
844     fgHiResDump();
845 }
846
847
848 // do a screen snap shot
849 void fgDumpSnapShot () {
850     bool show_pu_cursor = false;
851     char *filename = new char [24];
852     string message;
853     static int count = 1;
854
855     static const SGPropertyNode *master_freeze
856         = fgGetNode("/sim/freeze/master");
857
858     bool freeze = master_freeze->getBoolValue();
859     if ( !freeze ) {
860         fgSetBool("/sim/freeze/master", true);
861     }
862
863     mainMenuBar->hide();
864     TurnCursorOff();
865     if ( !puCursorIsHidden() ) {
866         show_pu_cursor = true;
867         puHideCursor();
868     }
869
870     fgInitVisuals();
871     fgReshape( fgGetInt("/sim/startup/xsize"),
872                fgGetInt("/sim/startup/ysize") );
873
874     // we need two render frames here to clear the menu and cursor
875     // ... not sure why but doing an extra fgRenderFrame() shouldn't
876     // hurt anything
877     fgRenderFrame();
878     fgRenderFrame();
879
880     while (count < 1000) {
881         FILE *fp;
882         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
883         if ( (fp = fopen(filename, "r")) == NULL )
884             break;
885         fclose(fp);
886     }
887
888     if ( sg_glDumpWindow( filename,
889                           fgGetInt("/sim/startup/xsize"), 
890                           fgGetInt("/sim/startup/ysize")) ) {
891         message = "Snap shot saved to ";
892         message += filename;
893     } else {
894         message = "Failed to save to ";
895         message += filename;
896     }
897
898     mkDialog (message.c_str());
899
900     delete [] filename;
901
902     if ( show_pu_cursor ) {
903         puShowCursor();
904     }
905
906     TurnCursorOn();
907     if( gui_menu_on ) {
908         mainMenuBar->reveal();
909     }
910
911     if ( !freeze ) {
912         fgSetBool("/sim/freeze/master", false);
913     }
914 }
915
916 #ifdef FG_NETWORK_OLK
917 void net_display_toggle( puObject *cb)
918 {
919         net_hud_display = (net_hud_display) ? 0 : 1;
920         printf("Toggle net_hud_display : %d\n", net_hud_display);
921 }
922
923 void net_register( puObject *cb)
924 {
925         fgd_send_com( "1", FGFS_host );
926         net_is_registered = 0;
927         printf("Registering to deamon\n");
928 }
929
930 void net_unregister( puObject *cb)
931 {
932         fgd_send_com( "8", FGFS_host );
933         net_is_registered = -1;
934         printf("Unregistering from deamon\n");
935 }
936
937 #endif // #ifdef FG_NETWORK_OLK