]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
Attached patches remove BORLANDC, and hence SG_MATH_EXCEPTION_CLASH and SG_INCOM
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 HAVE_WINDOWS_H
36 #  include <windows.h>
37 #endif
38
39 #include SG_GL_H
40
41 #if defined(FX) && defined(XMESA)
42 #  include <GL/xmesa.h>
43 #endif
44
45 #include STL_FSTREAM
46 #include STL_STRING
47
48 #include <stdlib.h>
49 #include <string.h>
50
51 // for help call back
52 #ifdef WIN32
53 # include <shellapi.h>
54 # ifdef __CYGWIN__
55 #  include <sys/cygwin.h>
56 # endif
57 #endif
58
59 #include <sstream>
60
61 #include <simgear/constants.h>
62 #include <simgear/debug/logstream.hxx>
63 #include <simgear/misc/sg_path.hxx>
64 #include <simgear/screen/screen-dump.hxx>
65
66 #include <Include/general.hxx>
67 #include <Aircraft/aircraft.hxx>
68 #include <Aircraft/controls.hxx>
69 #include <Airports/simple.hxx>
70 #include <Cockpit/panel.hxx>
71 #include <FDM/flight.hxx>
72 #include <Main/main.hxx>
73 #include <Main/fg_init.hxx>
74 #include <Main/fg_io.hxx>
75 #include <Main/globals.hxx>
76 #include <Main/fg_props.hxx>
77 #include <Main/renderer.hxx>
78 #include <Main/viewmgr.hxx>
79 #include <GUI/new_gui.hxx>
80
81 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
82 #  include <simgear/screen/win32-printer.h>
83 #  include <simgear/screen/GlBitmaps.h>
84 #endif
85
86 #include "gui.h"
87
88 SG_USING_STD(string);
89 SG_USING_STD(cout);
90
91
92 #if defined( TR_HIRES_SNAP)
93 #include <simgear/screen/tr.h>
94 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
95                          GLfloat x_end, GLfloat y_end );
96 #endif
97
98
99 const __fg_gui_fn_t __fg_gui_fn[] = {
100 #ifdef TR_HIRES_SNAP
101         {"dumpHiResSnapShot", fgHiResDumpWrapper},
102 #endif
103         {"dumpSnapShot", fgDumpSnapShotWrapper},
104 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
105         {"printScreen", printScreen},
106 #endif
107         // Help
108         {"helpCb", helpCb},
109
110         // Structure termination
111         {"", NULL}
112 };
113
114
115 /* ================ General Purpose Functions ================ */
116
117 // General Purpose Message Box. Makes sure no more than 5 different
118 // messages are displayed at the same time, and none of them are
119 // duplicates. (5 is a *lot*, but this will hardly ever be reached
120 // and we don't want to miss any, either.)
121 void mkDialog (const char *txt)
122 {
123     NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
124     if (!gui)
125         return;
126     SGPropertyNode *master = gui->getDialogProperties("message");
127     if (!master)
128         return;
129
130     const int maxdialogs = 5;
131     string name;
132     SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
133     int i;
134     for (i = 0; i < maxdialogs; i++) {
135         std::ostringstream s;
136         s << "message-" << i;
137         name = s.str();
138
139         if (!msg->getNode(name.c_str(), false))
140             break;
141
142         if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
143             SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
144             return;
145         }
146     }
147     if (i == maxdialogs)
148         return;
149     msg = msg->getNode(name.c_str(), true);
150     msg->setStringValue("message", txt);
151     msg = msg->getNode("dialog", true);
152     copyProperties(master, msg);
153     msg->setStringValue("name", name.c_str());
154     gui->newDialog(msg);
155     gui->showDialog(name.c_str());
156 }
157
158 // Message Box to report an error.
159 void guiErrorMessage (const char *txt)
160 {
161     SG_LOG(SG_GENERAL, SG_ALERT, txt);
162     mkDialog(txt);
163 }
164
165 // Message Box to report a throwable (usually an exception).
166 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
167 {
168     string msg = txt;
169     msg += '\n';
170     msg += throwable.getFormattedMessage();
171     if (!throwable.getOrigin().empty()) {
172         msg += "\n (reported by ";
173         msg += throwable.getOrigin();
174         msg += ')';
175     }
176     SG_LOG(SG_GENERAL, SG_ALERT, msg);
177     mkDialog(msg.c_str());
178 }
179
180
181
182 /* -----------------------------------------------------------------------
183 the Gui callback functions 
184 ____________________________________________________________________*/
185
186
187 // Hier Neu :-) This is my newly added code
188 // Added by David Findlay <nedz@bigpond.com>
189 // on Sunday 3rd of December
190
191
192 void helpCb ()
193 {
194     string command;
195         
196 #if defined(FX) && !defined(WIN32)
197 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
198     if ( globals->get_fullscreen() ) {
199         globals->set_fullscreen(false);
200         XMesaSetFXmode( XMESA_FX_WINDOW );
201     }
202 #  endif
203 #endif
204         
205     SGPath path( globals->get_fg_root() );
206     path.append( "Docs/index.html" );
207         
208 #if !defined(WIN32)
209
210     command = globals->get_browser();
211     string::size_type pos;
212     if ((pos = command.find("%u", 0)) != string::npos)
213         command.replace(pos, 2, path.str());
214     else
215         command += " " + path.str();
216
217     command += " &";
218     system( command.c_str() );
219
220 #else // WIN32
221
222     // Look for favorite browser
223     char Dummy[1024], ExecName[1024], browserParameter[1024];
224     char win32_name[1024];
225 # ifdef __CYGWIN__
226     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
227 # else
228     strncpy(win32_name,path.c_str(), 1024);
229 # endif
230     Dummy[0] = 0;
231     FindExecutable(win32_name, Dummy, ExecName);
232     snprintf(browserParameter, 1024, "file:///%s", win32_name);
233     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
234                    SW_SHOWNORMAL ) ;
235
236 #endif
237         
238     mkDialog ("Help started in your web browser window.");
239 }
240
241 #if defined( TR_HIRES_SNAP)
242 void fgHiResDump()
243 {
244     FILE *f;
245     string message;
246     bool show_pu_cursor = false;
247     bool menu_status = fgGetBool("/sim/menubar/visibility");
248     char *filename = new char [24];
249     static int count = 1;
250
251     static const SGPropertyNode *master_freeze
252         = fgGetNode("/sim/freeze/master");
253
254     bool freeze = master_freeze->getBoolValue();
255     if ( !freeze ) {
256         fgSetBool("/sim/freeze/master", true);
257     }
258
259     fgSetBool("/sim/menubar/visibility", false);
260     TurnCursorOff();
261     if ( !puCursorIsHidden() ) {
262         show_pu_cursor = true;
263         puHideCursor();
264     }
265
266     FGRenderer *renderer = globals->get_renderer();
267 //     renderer->init();
268     renderer->resize( fgGetInt("/sim/startup/xsize"),
269                       fgGetInt("/sim/startup/ysize") );
270
271     // we need two render frames here to clear the menu and cursor
272     // ... not sure why but doing an extra fgRenderFrame() shouldn't
273     // hurt anything
274     //renderer->update( true );
275     //renderer->update( true );
276
277     // This ImageSize stuff is a temporary hack
278     // should probably use 128x128 tile size and
279     // support any image size
280
281     // This should be a requester to get multiplier from user
282     int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
283     int width = fgGetInt("/sim/startup/xsize");
284     int height = fgGetInt("/sim/startup/ysize");
285         
286     /* allocate buffer large enough to store one tile */
287     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
288     if (!tile) {
289         printf("Malloc of tile buffer failed!\n");
290         return;
291     }
292
293     int imageWidth  = multiplier*width;
294     int imageHeight = multiplier*height;
295
296     /* allocate buffer to hold a row of tiles */
297     GLubyte *buffer
298         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
299     if (!buffer) {
300         free(tile);
301         printf("Malloc of tile row buffer failed!\n");
302         return;
303     }
304     TRcontext *tr = trNew();
305     trTileSize(tr, width, height, 0);
306     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
307     trImageSize(tr, imageWidth, imageHeight);
308     trRowOrder(tr, TR_TOP_TO_BOTTOM);
309     // OSGFIXME
310 //     sgFrustum *frustum = ssgGetFrustum();
311 //     trFrustum(tr,
312 //               frustum->getLeft(), frustum->getRight(),
313 //               frustum->getBot(),  frustum->getTop(), 
314 //               frustum->getNear(), frustum->getFar());
315         
316     /* Prepare ppm output file */
317     while (count < 1000) {
318         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
319         if ( (f = fopen(filename, "r")) == NULL )
320             break;
321         fclose(f);
322     }
323     f = fopen(filename, "wb");
324     if (!f) {
325         printf("Couldn't open image file: %s\n", filename);
326         free(buffer);
327         free(tile);
328         return;
329     }
330     fprintf(f,"P6\n");
331     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
332     fprintf(f,"%i %i\n", imageWidth, imageHeight);
333     fprintf(f,"255\n");
334
335     /* just to be safe... */
336     glPixelStorei(GL_PACK_ALIGNMENT, 1);
337
338     /* Because the HUD and Panel change the ViewPort we will
339      * need to handle some lowlevel stuff ourselves */
340     int ncols = trGet(tr, TR_COLUMNS);
341     int nrows = trGet(tr, TR_ROWS);
342
343     bool do_hud = fgGetBool("/sim/hud/visibility");
344     GLfloat hud_col_step = 640.0 / ncols;
345     GLfloat hud_row_step = 480.0 / nrows;
346         
347     bool do_panel = fgPanelVisible();
348     GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
349     GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
350
351     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
352     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
353     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
354     glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
355     glHint(GL_FOG_HINT, GL_NICEST);
356         
357     /* Draw tiles */
358     int more = 1;
359     while (more) {
360         trBeginTile(tr);
361         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
362         int curRow =  trGet(tr, TR_CURRENT_ROW);
363
364         renderer->update( false );
365         // OSGFIXME
366 //         if ( do_hud )
367 //             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
368 //                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
369         // OSGFIXME
370 //         if (do_panel)
371 //             globals->get_current_panel()->update(
372 //                                    curColumn*panel_col_step, panel_col_step,
373 //                                    curRow*panel_row_step,    panel_row_step );
374         more = trEndTile(tr);
375
376         /* save tile into tile row buffer*/
377         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
378         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
379         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
380         int xOffset = curColumn * bytesPerTileRow;
381         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
382         int i;
383         for (i=0;i<height;i++) {
384             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
385                    tile + i*bytesPerTileRow,              /* Src */
386                    bytesPerCurrentTileRow);               /* Byte count*/
387         }
388
389         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
390             /* write this buffered row of tiles to the file */
391             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
392             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
393             int i;
394             for (i=0;i<curTileHeight;i++) {
395                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
396                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
397                 fwrite(rowPtr, 1, imageWidth*3, f);
398             }
399         }
400
401     }
402
403     renderer->resize( width, height );
404
405     trDelete(tr);
406
407     glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
408     glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
409     glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
410     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
411     if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
412          (!fgGetBool("/sim/rendering/shading"))) {
413         // if fastest fog requested, or if flat shading force fastest
414         glHint ( GL_FOG_HINT, GL_FASTEST );
415     } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
416         glHint ( GL_FOG_HINT, GL_DONT_CARE );
417     }
418
419     fclose(f);
420
421     message = "Snapshot saved to \"";
422     message += filename;
423     message += "\".";
424     mkDialog (message.c_str());
425
426     free(tile);
427     free(buffer);
428
429     delete [] filename;
430
431     if ( show_pu_cursor ) {
432         puShowCursor();
433     }
434
435     TurnCursorOn();
436     fgSetBool("/sim/menubar/visibility", menu_status);
437
438     if ( !freeze ) {
439         fgSetBool("/sim/freeze/master", false);
440     }
441 }
442 #endif // #if defined( TR_HIRES_SNAP)
443
444
445 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
446
447 void rotateView( double roll, double pitch, double yaw )
448 {
449         // rotate view
450 }
451
452 GlBitmap *b1 = NULL;
453 GLubyte *hiResScreenCapture( int multiplier )
454 {
455     float oldfov = fgGetDouble("/sim/current-view/field-of-view");
456     float fov = oldfov / multiplier;
457     FGViewer *v = globals->get_current_view();
458     fgSetDouble("/sim/current-view/field-of-view", fov);
459 //     globals->get_renderer()->init();
460     int cur_width = fgGetInt("/sim/startup/xsize");
461     int cur_height = fgGetInt("/sim/startup/ysize");
462     delete( b1 );
463     // New empty (mostly) bitmap
464     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
465     int x,y;
466     for ( y = 0; y < multiplier; y++ ) {
467         for ( x = 0; x < multiplier; x++ ) {
468             globals->get_renderer()->resize( cur_width, cur_height );
469             // pan to tile
470             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
471             globals->get_renderer()->update( false );
472             // restore view
473             GlBitmap b2;
474             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
475         }
476     }
477     fgSetDouble("/sim/current-view/field-of-view", oldfov);
478     return b1->getBitmap();
479 }
480 #endif
481
482 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
483 // win32 print screen function
484 void printScreen () {
485     bool show_pu_cursor = false;
486     TurnCursorOff();
487     if ( !puCursorIsHidden() ) {
488         show_pu_cursor = true;
489         puHideCursor();
490     }
491
492     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
493     int cur_width = fgGetInt("/sim/startup/xsize");
494     int cur_height = fgGetInt("/sim/startup/ysize");
495     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
496     p.End( hiResScreenCapture(3) );
497
498     if ( show_pu_cursor ) {
499         puShowCursor();
500     }
501     TurnCursorOn();
502 }
503 #endif // #ifdef WIN32
504
505
506 void fgDumpSnapShotWrapper () {
507     fgDumpSnapShot();
508 }
509
510
511 void fgHiResDumpWrapper () {
512     fgHiResDump();
513 }
514
515
516 // do a screen snap shot
517 bool fgDumpSnapShot () {
518     bool show_pu_cursor = false;
519     static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
520
521     bool freeze = master_freeze->getBoolValue();
522     if ( !freeze ) {
523         fgSetBool("/sim/freeze/master", true);
524     }
525
526     TurnCursorOff();
527     if ( !puCursorIsHidden() ) {
528         show_pu_cursor = true;
529         puHideCursor();
530     }
531     fgSetBool("/sim/signals/screenshot", true);
532
533     FGRenderer *renderer = globals->get_renderer();
534     renderer->resize( fgGetInt("/sim/startup/xsize"),
535                       fgGetInt("/sim/startup/ysize") );
536
537     // we need two render frames here to clear the menu and cursor
538     // ... not sure why but doing an extra fgRenderFrame() shouldn't
539     // hurt anything
540     renderer->update( true );
541     renderer->update( true );
542
543     string dir = fgGetString("/sim/paths/screenshot-dir", fgGetString("/sim/fg-current"));
544     SGPath path(dir + '/');
545     if (path.create_dir( 0755 )) {
546         SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
547                 << dir << "'. Trying home directory.");
548         dir = fgGetString("/sim/fg-home");
549     }
550
551     char filename[24];
552     static int count = 1;
553     while (count < 1000) {
554         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
555
556         SGPath p(dir);
557         p.append(filename);
558         if (!p.exists()) {
559             path.set(p.str());
560             break;
561         }
562     }
563
564     int result = sg_glDumpWindow(path.c_str(),
565                                  fgGetInt("/sim/startup/xsize"),
566                                  fgGetInt("/sim/startup/ysize"));
567
568     fgSetString("/sim/paths/screenshot-last", path.c_str());
569     fgSetBool("/sim/signals/screenshot", false);
570
571     if ( show_pu_cursor ) {
572         puShowCursor();
573     }
574
575     TurnCursorOn();
576
577     if ( !freeze ) {
578         fgSetBool("/sim/freeze/master", false);
579     }
580     return result != 0;
581 }
582
583 // do an entire scenegraph dump
584 void fgDumpSceneGraph()
585 {
586     char *filename = new char [24];
587     string message;
588     static int count = 1;
589
590     FGRenderer *renderer = globals->get_renderer();
591
592     static const SGPropertyNode *master_freeze
593         = fgGetNode("/sim/freeze/master");
594
595     bool freeze = master_freeze->getBoolValue();
596     if ( !freeze ) {
597         fgSetBool("/sim/freeze/master", true);
598     }
599
600     while (count < 1000) {
601         FILE *fp;
602         snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
603         if ( (fp = fopen(filename, "r")) == NULL )
604             break;
605         fclose(fp);
606     }
607
608     if ( fgDumpSceneGraphToFile(filename)) {
609         message = "Entire scene graph saved to \"";
610         message += filename;
611         message += "\".";
612     } else {
613         message = "Failed to save to \"";
614         message += filename;
615         message += "\".";
616     }
617
618     mkDialog (message.c_str());
619
620     delete [] filename;
621
622     if ( !freeze ) {
623         fgSetBool("/sim/freeze/master", false);
624     }
625 }
626
627     
628 // do an terrain branch dump
629 void fgDumpTerrainBranch()
630 {
631     char *filename = new char [24];
632     string message;
633     static int count = 1;
634
635     FGRenderer *renderer = globals->get_renderer();
636
637     static const SGPropertyNode *master_freeze
638         = fgGetNode("/sim/freeze/master");
639
640     bool freeze = master_freeze->getBoolValue();
641     if ( !freeze ) {
642         fgSetBool("/sim/freeze/master", true);
643     }
644
645     while (count < 1000) {
646         FILE *fp;
647         snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
648         if ( (fp = fopen(filename, "r")) == NULL )
649             break;
650         fclose(fp);
651     }
652
653     if ( fgDumpTerrainBranchToFile(filename)) {
654         message = "Terrain graph saved to \"";
655         message += filename;
656         message += "\".";
657     } else {
658         message = "Failed to save to \"";
659         message += filename;
660         message += "\".";
661     }
662
663     mkDialog (message.c_str());
664
665     delete [] filename;
666
667     if ( !freeze ) {
668         fgSetBool("/sim/freeze/master", false);
669     }
670 }
671
672     
673