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