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