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