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>
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/misc/sg_path.hxx>
44 #include <simgear/screen/screen-dump.hxx>
45 #include <simgear/structure/event_mgr.hxx>
46 #include <simgear/props/props_io.hxx>
48 #include <Cockpit/panel.hxx>
49 #include <Main/globals.hxx>
50 #include <Main/fg_props.hxx>
51 #include <Main/fg_os.hxx>
52 #include <Main/renderer.hxx>
53 #include <Main/viewmgr.hxx>
54 #include <Main/WindowSystemAdapter.hxx>
55 #include <Main/CameraGroup.hxx>
56 #include <GUI/new_gui.hxx>
60 # include <shellapi.h>
68 #if defined( TR_HIRES_SNAP)
69 #include <simgear/screen/tr.h>
70 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
71 GLfloat x_end, GLfloat y_end );
75 const __fg_gui_fn_t __fg_gui_fn[] = {
77 {"dumpHiResSnapShot", fgHiResDumpWrapper},
79 {"dumpSnapShot", fgDumpSnapShotWrapper},
83 // Structure termination
88 /* ================ General Purpose Functions ================ */
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)
96 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
99 SGPropertyNode *master = gui->getDialogProperties("message");
103 const int maxdialogs = 5;
105 SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
107 for (i = 0; i < maxdialogs; i++) {
108 std::ostringstream s;
109 s << "message-" << i;
112 if (!msg->getNode(name.c_str(), false))
115 if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
116 SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
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());
128 gui->showDialog(name.c_str());
131 // Message Box to report an error.
132 void guiErrorMessage (const char *txt)
134 SG_LOG(SG_GENERAL, SG_ALERT, txt);
138 // Message Box to report a throwable (usually an exception).
139 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
143 msg += throwable.getFormattedMessage();
144 if (!std::strlen(throwable.getOrigin()) != 0) {
145 msg += "\n (reported by ";
146 msg += throwable.getOrigin();
149 SG_LOG(SG_GENERAL, SG_ALERT, msg);
150 mkDialog(msg.c_str());
155 /* -----------------------------------------------------------------------
156 the Gui callback functions
157 ____________________________________________________________________*/
160 // Hier Neu :-) This is my newly added code
161 // Added by David Findlay <nedz@bigpond.com>
162 // on Sunday 3rd of December
169 SGPath path( globals->get_fg_root() );
170 path.append( "Docs/index.html" );
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());
179 command += " " + path.str();
182 system( command.c_str() );
186 // Look for favorite browser
187 char Dummy[1024], ExecName[1024], browserParameter[1024];
188 char win32_name[1024];
190 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
192 strncpy(win32_name,path.c_str(), 1024);
195 FindExecutable(win32_name, Dummy, ExecName);
196 snprintf(browserParameter, 1024, "file:///%s", win32_name);
197 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
202 mkDialog ("Help started in your web browser window.");
205 #if defined( TR_HIRES_SNAP)
210 bool menu_status = fgGetBool("/sim/menubar/visibility");
211 char *filename = new char [24];
212 static int count = 1;
214 static const SGPropertyNode *master_freeze
215 = fgGetNode("/sim/freeze/master");
217 bool freeze = master_freeze->getBoolValue();
219 fgSetBool("/sim/freeze/master", true);
222 fgSetBool("/sim/menubar/visibility", false);
223 int mouse = fgGetMouseCursor();
224 fgSetMouseCursor(MOUSE_CURSOR_NONE);
226 FGRenderer *renderer = globals->get_renderer();
228 renderer->resize( fgGetInt("/sim/startup/xsize"),
229 fgGetInt("/sim/startup/ysize") );
231 // we need two render frames here to clear the menu and cursor
232 // ... not sure why but doing an extra fgRenderFrame() shouldn't
234 //renderer->update( true );
235 //renderer->update( true );
237 // This ImageSize stuff is a temporary hack
238 // should probably use 128x128 tile size and
239 // support any image size
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");
246 /* allocate buffer large enough to store one tile */
247 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
250 printf("Malloc of tile buffer failed!\n");
254 int imageWidth = multiplier*width;
255 int imageHeight = multiplier*height;
257 /* allocate buffer to hold a row of tiles */
259 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
263 printf("Malloc of tile row buffer failed!\n");
266 TRcontext *tr = trNew();
267 trTileSize(tr, width, height, 0);
268 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
269 trImageSize(tr, imageWidth, imageHeight);
270 trRowOrder(tr, TR_TOP_TO_BOTTOM);
272 // sgFrustum *frustum = ssgGetFrustum();
274 // frustum->getLeft(), frustum->getRight(),
275 // frustum->getBot(), frustum->getTop(),
276 // frustum->getNear(), frustum->getFar());
278 /* Prepare ppm output file */
279 while (count < 1000) {
280 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
281 if ( (f = fopen(filename, "r")) == NULL )
285 f = fopen(filename, "wb");
287 printf("Couldn't open image file: %s\n", filename);
294 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
295 fprintf(f,"%i %i\n", imageWidth, imageHeight);
298 /* just to be safe... */
299 glPixelStorei(GL_PACK_ALIGNMENT, 1);
303 /* Because the HUD and Panel change the ViewPort we will
304 * need to handle some lowlevel stuff ourselves */
305 int ncols = trGet(tr, TR_COLUMNS);
306 int nrows = trGet(tr, TR_ROWS);
308 bool do_hud = fgGetBool("/sim/hud/visibility");
309 GLfloat hud_col_step = 640.0 / ncols;
310 GLfloat hud_row_step = 480.0 / nrows;
312 bool do_panel = fgPanelVisible();
313 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
314 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
316 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
317 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
318 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
319 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
320 glHint(GL_FOG_HINT, GL_NICEST);
326 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
327 // int curRow = trGet(tr, TR_CURRENT_ROW);
329 renderer->update( false );
332 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
333 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
336 // globals->get_current_panel()->update(
337 // curColumn*panel_col_step, panel_col_step,
338 // curRow*panel_row_step, panel_row_step );
339 more = trEndTile(tr);
341 /* save tile into tile row buffer*/
342 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
343 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
344 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
345 int xOffset = curColumn * bytesPerTileRow;
346 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
348 for (i=0;i<height;i++) {
349 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
350 tile + i*bytesPerTileRow, /* Src */
351 bytesPerCurrentTileRow); /* Byte count*/
354 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
355 /* write this buffered row of tiles to the file */
356 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
357 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
359 for (i=0;i<curTileHeight;i++) {
360 /* Remember, OpenGL images are bottom to top. Have to reverse. */
361 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
362 fwrite(rowPtr, 1, imageWidth*3, f);
368 renderer->resize( width, height );
372 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
373 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
374 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
375 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
376 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
377 (!fgGetBool("/sim/rendering/shading"))) {
378 // if fastest fog requested, or if flat shading force fastest
379 glHint ( GL_FOG_HINT, GL_FASTEST );
380 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
381 glHint ( GL_FOG_HINT, GL_DONT_CARE );
386 message = "Snapshot saved to \"";
389 mkDialog (message.c_str());
396 fgSetMouseCursor(mouse);
397 fgSetBool("/sim/menubar/visibility", menu_status);
400 fgSetBool("/sim/freeze/master", false);
403 #endif // #if defined( TR_HIRES_SNAP)
405 void fgDumpSnapShotWrapper () {
410 void fgHiResDumpWrapper () {
416 using namespace flightgear;
418 class GUISnapShotOperation :
419 public GraphicsContextOperation
423 // start new snap shot
426 // allow only one snapshot at a time
427 if (_snapShotOp.valid())
429 _snapShotOp = new GUISnapShotOperation();
430 /* register with graphics context so actual snap shot is done
431 * in the graphics context (thread) */
432 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
433 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
434 osg::GraphicsContext* gc = 0;
436 gc = guiCamera->getGraphicsContext();
438 gc->add(_snapShotOp.get());
440 wsa->windows[0]->gc->add(_snapShotOp.get());
446 // constructor to be executed in main loop's thread
447 GUISnapShotOperation() :
448 flightgear::GraphicsContextOperation(std::string("GUI snap shot")),
449 _master_freeze(fgGetNode("/sim/freeze/master", true)),
450 _freeze(_master_freeze->getBoolValue()),
452 _mouse(fgGetMouseCursor())
455 _master_freeze->setBoolValue(true);
457 fgSetMouseCursor(MOUSE_CURSOR_NONE);
459 string dir = fgGetString("/sim/paths/screenshot-dir");
461 dir = fgGetString("/sim/fg-current");
463 _path.set(dir + '/');
464 if (_path.create_dir( 0755 )) {
465 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
466 << dir << "'. Trying home directory.");
467 dir = fgGetString("/sim/fg-home");
471 static int count = 1;
472 while (count < 1000) {
473 snprintf(filename, 24, "fgfs-screen-%03d.png", count++);
483 _xsize = fgGetInt("/sim/startup/xsize");
484 _ysize = fgGetInt("/sim/startup/ysize");
486 FGRenderer *renderer = globals->get_renderer();
487 renderer->resize(_xsize, _ysize);
488 globals->get_event_mgr()->addTask("SnapShotTimer",
489 this, &GUISnapShotOperation::timerExpired,
493 // to be executed in graphics context (maybe separate thread)
494 void run(osg::GraphicsContext* gc)
496 _result = sg_glDumpWindow(_path.c_str(),
501 // timer method, to be executed in main loop's thread
502 virtual void timerExpired()
506 globals->get_event_mgr()->removeTask("SnapShotTimer");
508 fgSetString("/sim/paths/screenshot-last", _path.c_str());
509 fgSetBool("/sim/signals/screenshot", _result);
511 fgSetMouseCursor(_mouse);
514 _master_freeze->setBoolValue(false);
520 static osg::ref_ptr<GUISnapShotOperation> _snapShotOp;
521 SGPropertyNode_ptr _master_freeze;
531 osg::ref_ptr<GUISnapShotOperation> GUISnapShotOperation::_snapShotOp;
533 // do a screen snap shot
534 bool fgDumpSnapShot ()
537 // start snap shot operation, while needs to be executed in
539 return GUISnapShotOperation::start();
541 // obsolete code => remove when new code is stable
542 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
544 bool freeze = master_freeze->getBoolValue();
546 fgSetBool("/sim/freeze/master", true);
549 int mouse = fgGetMouseCursor();
550 fgSetMouseCursor(MOUSE_CURSOR_NONE);
552 fgSetBool("/sim/signals/screenshot", true);
554 FGRenderer *renderer = globals->get_renderer();
555 renderer->resize( fgGetInt("/sim/startup/xsize"),
556 fgGetInt("/sim/startup/ysize") );
558 // we need two render frames here to clear the menu and cursor
559 // ... not sure why but doing an extra fgRenderFrame() shouldn't
561 renderer->update( true );
562 renderer->update( true );
564 string dir = fgGetString("/sim/paths/screenshot-dir");
566 dir = fgGetString("/sim/fg-current");
568 SGPath path(dir + '/');
569 if (path.create_dir( 0755 )) {
570 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
571 << dir << "'. Trying home directory.");
572 dir = fgGetString("/sim/fg-home");
576 static int count = 1;
577 while (count < 1000) {
578 snprintf(filename, 24, "fgfs-screen-%03d.png", count++);
588 bool result = sg_glDumpWindow(path.c_str(),
589 fgGetInt("/sim/startup/xsize"),
590 fgGetInt("/sim/startup/ysize"));
592 fgSetString("/sim/paths/screenshot-last", path.c_str());
593 fgSetBool("/sim/signals/screenshot", false);
595 fgSetMouseCursor(mouse);
598 fgSetBool("/sim/freeze/master", false);
604 // do an entire scenegraph dump
605 void fgDumpSceneGraph()
607 char *filename = new char [24];
609 static int count = 1;
611 static const SGPropertyNode *master_freeze
612 = fgGetNode("/sim/freeze/master");
614 bool freeze = master_freeze->getBoolValue();
616 fgSetBool("/sim/freeze/master", true);
619 while (count < 1000) {
621 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
622 if ( (fp = fopen(filename, "r")) == NULL )
627 if ( fgDumpSceneGraphToFile(filename)) {
628 message = "Entire scene graph saved to \"";
632 message = "Failed to save to \"";
637 mkDialog (message.c_str());
642 fgSetBool("/sim/freeze/master", false);
647 // do an terrain branch dump
648 void fgDumpTerrainBranch()
650 char *filename = new char [24];
652 static int count = 1;
654 static const SGPropertyNode *master_freeze
655 = fgGetNode("/sim/freeze/master");
657 bool freeze = master_freeze->getBoolValue();
659 fgSetBool("/sim/freeze/master", true);
662 while (count < 1000) {
664 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
665 if ( (fp = fopen(filename, "r")) == NULL )
670 if ( fgDumpTerrainBranchToFile(filename)) {
671 message = "Terrain graph saved to \"";
675 message = "Failed to save to \"";
680 mkDialog (message.c_str());
685 fgSetBool("/sim/freeze/master", false);
689 void fgPrintVisibleSceneInfoCommand()
691 static const SGPropertyNode *master_freeze
692 = fgGetNode("/sim/freeze/master");
694 bool freeze = master_freeze->getBoolValue();
696 fgSetBool("/sim/freeze/master", true);
699 flightgear::printVisibleSceneInfo(globals->get_renderer());
702 fgSetBool("/sim/freeze/master", false);