1 /**************************************************************************
4 * Based on gui.cxx and renamed on 2002/08/13 by Erik Hofman.
6 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
9 * Additional mouse supported added by David Megginson, 1999.
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.
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.
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.
26 **************************************************************************/
33 #include <simgear/compiler.h>
35 #ifdef SG_MATH_EXCEPTION_CLASH
45 #if defined(FX) && defined(XMESA)
46 # include <GL/xmesa.h>
57 # include <shellapi.h>
59 # include <sys/cygwin.h>
65 #include <simgear/constants.h>
66 #include <simgear/debug/logstream.hxx>
67 #include <simgear/misc/sg_path.hxx>
68 #include <simgear/screen/screen-dump.hxx>
70 #include <Include/general.hxx>
71 #include <Aircraft/aircraft.hxx>
72 #include <Aircraft/controls.hxx>
73 #include <Airports/simple.hxx>
74 #include <Cockpit/panel.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/renderer.hxx>
82 #include <Main/viewmgr.hxx>
83 #include <GUI/new_gui.hxx>
85 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
86 # include <simgear/screen/win32-printer.h>
87 # include <simgear/screen/GlBitmaps.h>
91 #include "gui_local.hxx"
92 #include "sgVec3Slider.hxx"
98 #if defined( TR_HIRES_SNAP)
99 #include <simgear/screen/tr.h>
100 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
101 GLfloat x_end, GLfloat y_end );
105 const __fg_gui_fn_t __fg_gui_fn[] = {
110 {"dumpHiResSnapShot", fgHiResDumpWrapper},
112 {"dumpSnapShot", fgDumpSnapShotWrapper},
113 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
114 {"printScreen", printScreen},
119 // Structure termination
124 /* ================ General Purpose Functions ================ */
126 // General Purpose Message Box. Makes sure no more than 5 different
127 // messages are displayed at the same time, and none of them are
128 // duplicates. (5 is a *lot*, but this will hardly ever be reached
129 // and we don't want to miss any, either.)
130 void mkDialog (const char *txt)
132 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
135 SGPropertyNode *master = gui->getDialogProperties("message");
139 const int maxdialogs = 5;
141 SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
143 for (i = 0; i < maxdialogs; i++) {
144 std::ostringstream s;
145 s << "message-" << i;
148 if (!msg->getNode(name.c_str(), false))
151 if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
152 SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
158 msg = msg->getNode(name.c_str(), true);
159 msg->setStringValue("message", txt);
160 msg = msg->getNode("dialog", true);
161 copyProperties(master, msg);
162 msg->setStringValue("name", name.c_str());
164 gui->showDialog(name.c_str());
167 // Message Box to report an error.
168 void guiErrorMessage (const char *txt)
170 SG_LOG(SG_GENERAL, SG_ALERT, txt);
174 // Message Box to report a throwable (usually an exception).
175 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
179 msg += throwable.getFormattedMessage();
180 if (!throwable.getOrigin().empty()) {
181 msg += "\n (reported by ";
182 msg += throwable.getOrigin();
185 SG_LOG(SG_GENERAL, SG_ALERT, msg);
186 mkDialog(msg.c_str());
191 /* -----------------------------------------------------------------------
192 the Gui callback functions
193 ____________________________________________________________________*/
196 // Hier Neu :-) This is my newly added code
197 // Added by David Findlay <nedz@bigpond.com>
198 // on Sunday 3rd of December
201 void helpCb (puObject *)
205 #if defined(FX) && !defined(WIN32)
206 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
207 if ( globals->get_fullscreen() ) {
208 globals->set_fullscreen(false);
209 XMesaSetFXmode( XMESA_FX_WINDOW );
214 SGPath path( globals->get_fg_root() );
215 path.append( "Docs/index.html" );
219 string help_app = fgGetString("/sim/startup/browser-app");
221 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
222 command = help_app + " -remote \"openURL(" + path.str() + ")\"";
224 command = help_app + " " + path.str();
227 system( command.c_str() );
231 // Look for favorite browser
232 char Dummy[1024], ExecName[1024], browserParameter[1024];
233 char win32_name[1024];
235 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
237 strncpy(win32_name,path.c_str(), 1024);
240 FindExecutable(win32_name, Dummy, ExecName);
241 snprintf(browserParameter, 1024, "file:///%s", win32_name);
242 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
247 mkDialog ("Help started in your web browser window.");
250 #if defined( TR_HIRES_SNAP)
255 bool show_pu_cursor = false;
256 bool menu_status = fgGetBool("/sim/menubar/visibility");
257 char *filename = new char [24];
258 static int count = 1;
260 static const SGPropertyNode *master_freeze
261 = fgGetNode("/sim/freeze/master");
263 bool freeze = master_freeze->getBoolValue();
265 fgSetBool("/sim/freeze/master", true);
268 fgSetBool("/sim/menubar/visibility", false);
270 if ( !puCursorIsHidden() ) {
271 show_pu_cursor = true;
275 FGRenderer *renderer = globals->get_renderer();
277 renderer->resize( fgGetInt("/sim/startup/xsize"),
278 fgGetInt("/sim/startup/ysize") );
280 // we need two render frames here to clear the menu and cursor
281 // ... not sure why but doing an extra fgRenderFrame() shouldn't
283 //renderer->update( true );
284 //renderer->update( true );
286 // This ImageSize stuff is a temporary hack
287 // should probably use 128x128 tile size and
288 // support any image size
290 // This should be a requester to get multiplier from user
291 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
292 int width = fgGetInt("/sim/startup/xsize");
293 int height = fgGetInt("/sim/startup/ysize");
295 /* allocate buffer large enough to store one tile */
296 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
298 printf("Malloc of tile buffer failed!\n");
302 int imageWidth = multiplier*width;
303 int imageHeight = multiplier*height;
305 /* allocate buffer to hold a row of tiles */
307 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
310 printf("Malloc of tile row buffer failed!\n");
313 TRcontext *tr = trNew();
314 trTileSize(tr, width, height, 0);
315 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
316 trImageSize(tr, imageWidth, imageHeight);
317 trRowOrder(tr, TR_TOP_TO_BOTTOM);
319 // sgFrustum *frustum = ssgGetFrustum();
321 // frustum->getLeft(), frustum->getRight(),
322 // frustum->getBot(), frustum->getTop(),
323 // frustum->getNear(), frustum->getFar());
325 /* Prepare ppm output file */
326 while (count < 1000) {
327 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
328 if ( (f = fopen(filename, "r")) == NULL )
332 f = fopen(filename, "wb");
334 printf("Couldn't open image file: %s\n", filename);
340 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
341 fprintf(f,"%i %i\n", imageWidth, imageHeight);
344 /* just to be safe... */
345 glPixelStorei(GL_PACK_ALIGNMENT, 1);
347 /* Because the HUD and Panel change the ViewPort we will
348 * need to handle some lowlevel stuff ourselves */
349 int ncols = trGet(tr, TR_COLUMNS);
350 int nrows = trGet(tr, TR_ROWS);
352 bool do_hud = fgGetBool("/sim/hud/visibility");
353 GLfloat hud_col_step = 640.0 / ncols;
354 GLfloat hud_row_step = 480.0 / nrows;
356 bool do_panel = fgPanelVisible();
357 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
358 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
360 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
361 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
362 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
363 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
364 glHint(GL_FOG_HINT, GL_NICEST);
370 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
371 int curRow = trGet(tr, TR_CURRENT_ROW);
373 renderer->update( false );
376 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
377 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
380 // globals->get_current_panel()->update(
381 // curColumn*panel_col_step, panel_col_step,
382 // curRow*panel_row_step, panel_row_step );
383 more = trEndTile(tr);
385 /* save tile into tile row buffer*/
386 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
387 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
388 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
389 int xOffset = curColumn * bytesPerTileRow;
390 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
392 for (i=0;i<height;i++) {
393 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
394 tile + i*bytesPerTileRow, /* Src */
395 bytesPerCurrentTileRow); /* Byte count*/
398 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
399 /* write this buffered row of tiles to the file */
400 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
401 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
403 for (i=0;i<curTileHeight;i++) {
404 /* Remember, OpenGL images are bottom to top. Have to reverse. */
405 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
406 fwrite(rowPtr, 1, imageWidth*3, f);
412 renderer->resize( width, height );
416 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
417 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
418 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
419 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
420 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
421 (!fgGetBool("/sim/rendering/shading"))) {
422 // if fastest fog requested, or if flat shading force fastest
423 glHint ( GL_FOG_HINT, GL_FASTEST );
424 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
425 glHint ( GL_FOG_HINT, GL_DONT_CARE );
430 message = "Snapshot saved to \"";
433 mkDialog (message.c_str());
440 if ( show_pu_cursor ) {
445 fgSetBool("/sim/menubar/visibility", menu_status);
448 fgSetBool("/sim/freeze/master", false);
451 #endif // #if defined( TR_HIRES_SNAP)
454 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
456 void rotateView( double roll, double pitch, double yaw )
462 GLubyte *hiResScreenCapture( int multiplier )
464 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
465 float fov = oldfov / multiplier;
466 FGViewer *v = globals->get_current_view();
467 fgSetDouble("/sim/current-view/field-of-view", fov);
468 // globals->get_renderer()->init();
469 int cur_width = fgGetInt("/sim/startup/xsize");
470 int cur_height = fgGetInt("/sim/startup/ysize");
472 // New empty (mostly) bitmap
473 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
475 for ( y = 0; y < multiplier; y++ ) {
476 for ( x = 0; x < multiplier; x++ ) {
477 globals->get_renderer()->resize( cur_width, cur_height );
479 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
480 globals->get_renderer()->update( false );
483 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
486 fgSetDouble("/sim/current-view/field-of-view", oldfov);
487 return b1->getBitmap();
491 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
492 // win32 print screen function
493 void printScreen ( puObject *obj ) {
494 bool show_pu_cursor = false;
496 if ( !puCursorIsHidden() ) {
497 show_pu_cursor = true;
501 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
502 int cur_width = fgGetInt("/sim/startup/xsize");
503 int cur_height = fgGetInt("/sim/startup/ysize");
504 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
505 p.End( hiResScreenCapture(3) );
507 if ( show_pu_cursor ) {
512 #endif // #ifdef WIN32
515 void fgDumpSnapShotWrapper ( puObject *obj ) {
520 void fgHiResDumpWrapper ( puObject *obj ) {
525 // do a screen snap shot
526 bool fgDumpSnapShot () {
527 bool show_pu_cursor = false;
528 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
530 bool freeze = master_freeze->getBoolValue();
532 fgSetBool("/sim/freeze/master", true);
536 if ( !puCursorIsHidden() ) {
537 show_pu_cursor = true;
540 fgSetBool("/sim/signals/screenshot", true);
542 FGRenderer *renderer = globals->get_renderer();
543 renderer->resize( fgGetInt("/sim/startup/xsize"),
544 fgGetInt("/sim/startup/ysize") );
546 // we need two render frames here to clear the menu and cursor
547 // ... not sure why but doing an extra fgRenderFrame() shouldn't
549 renderer->update( true );
550 renderer->update( true );
552 string dir = fgGetString("/sim/paths/screenshot-dir", fgGetString("/sim/fg-current"));
553 SGPath path(dir + '/');
554 if (path.create_dir( 0755 )) {
555 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
556 << dir << "'. Trying home directory.");
557 dir = fgGetString("/sim/fg-home");
561 static int count = 1;
562 while (count < 1000) {
563 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
573 int result = sg_glDumpWindow(path.c_str(),
574 fgGetInt("/sim/startup/xsize"),
575 fgGetInt("/sim/startup/ysize"));
577 fgSetString("/sim/paths/screenshot-last", path.c_str());
578 fgSetBool("/sim/signals/screenshot", false);
580 if ( show_pu_cursor ) {
587 fgSetBool("/sim/freeze/master", false);
592 // do an entire scenegraph dump
593 void fgDumpSceneGraph()
595 char *filename = new char [24];
597 static int count = 1;
599 FGRenderer *renderer = globals->get_renderer();
601 static const SGPropertyNode *master_freeze
602 = fgGetNode("/sim/freeze/master");
604 bool freeze = master_freeze->getBoolValue();
606 fgSetBool("/sim/freeze/master", true);
609 while (count < 1000) {
611 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
612 if ( (fp = fopen(filename, "r")) == NULL )
617 if ( fgDumpSceneGraphToFile(filename)) {
618 message = "Entire scene graph saved to \"";
622 message = "Failed to save to \"";
627 mkDialog (message.c_str());
632 fgSetBool("/sim/freeze/master", false);
637 // do an terrain branch dump
638 void fgDumpTerrainBranch()
640 char *filename = new char [24];
642 static int count = 1;
644 FGRenderer *renderer = globals->get_renderer();
646 static const SGPropertyNode *master_freeze
647 = fgGetNode("/sim/freeze/master");
649 bool freeze = master_freeze->getBoolValue();
651 fgSetBool("/sim/freeze/master", true);
654 while (count < 1000) {
656 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
657 if ( (fp = fopen(filename, "r")) == NULL )
662 if ( fgDumpTerrainBranchToFile(filename)) {
663 message = "Terrain graph saved to \"";
667 message = "Failed to save to \"";
672 mkDialog (message.c_str());
677 fgSetBool("/sim/freeze/master", false);