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