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