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