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