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>
47 #include <Cockpit/panel.hxx>
48 #include <Main/globals.hxx>
49 #include <Main/fg_props.hxx>
50 #include <Main/fg_os.hxx>
51 #include <Main/renderer.hxx>
52 #include <Main/viewmgr.hxx>
53 #include <Main/WindowSystemAdapter.hxx>
54 #include <Main/CameraGroup.hxx>
55 #include <GUI/new_gui.hxx>
59 # include <shellapi.h>
67 #if defined( TR_HIRES_SNAP)
68 #include <simgear/screen/tr.h>
69 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
70 GLfloat x_end, GLfloat y_end );
74 const __fg_gui_fn_t __fg_gui_fn[] = {
76 {"dumpHiResSnapShot", fgHiResDumpWrapper},
78 {"dumpSnapShot", fgDumpSnapShotWrapper},
82 // Structure termination
87 /* ================ General Purpose Functions ================ */
89 // General Purpose Message Box. Makes sure no more than 5 different
90 // messages are displayed at the same time, and none of them are
91 // duplicates. (5 is a *lot*, but this will hardly ever be reached
92 // and we don't want to miss any, either.)
93 void mkDialog (const char *txt)
95 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
98 SGPropertyNode *master = gui->getDialogProperties("message");
102 const int maxdialogs = 5;
104 SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
106 for (i = 0; i < maxdialogs; i++) {
107 std::ostringstream s;
108 s << "message-" << i;
111 if (!msg->getNode(name.c_str(), false))
114 if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
115 SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
121 msg = msg->getNode(name.c_str(), true);
122 msg->setStringValue("message", txt);
123 msg = msg->getNode("dialog", true);
124 copyProperties(master, msg);
125 msg->setStringValue("name", name.c_str());
127 gui->showDialog(name.c_str());
130 // Message Box to report an error.
131 void guiErrorMessage (const char *txt)
133 SG_LOG(SG_GENERAL, SG_ALERT, txt);
137 // Message Box to report a throwable (usually an exception).
138 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
142 msg += throwable.getFormattedMessage();
143 if (!std::strlen(throwable.getOrigin()) != 0) {
144 msg += "\n (reported by ";
145 msg += throwable.getOrigin();
148 SG_LOG(SG_GENERAL, SG_ALERT, msg);
149 mkDialog(msg.c_str());
154 /* -----------------------------------------------------------------------
155 the Gui callback functions
156 ____________________________________________________________________*/
159 // Hier Neu :-) This is my newly added code
160 // Added by David Findlay <nedz@bigpond.com>
161 // on Sunday 3rd of December
168 SGPath path( globals->get_fg_root() );
169 path.append( "Docs/index.html" );
173 command = globals->get_browser();
174 string::size_type pos;
175 if ((pos = command.find("%u", 0)) != string::npos)
176 command.replace(pos, 2, path.str());
178 command += " " + path.str();
181 system( command.c_str() );
185 // Look for favorite browser
186 char Dummy[1024], ExecName[1024], browserParameter[1024];
187 char win32_name[1024];
189 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
191 strncpy(win32_name,path.c_str(), 1024);
194 FindExecutable(win32_name, Dummy, ExecName);
195 snprintf(browserParameter, 1024, "file:///%s", win32_name);
196 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
201 mkDialog ("Help started in your web browser window.");
204 #if defined( TR_HIRES_SNAP)
209 bool menu_status = fgGetBool("/sim/menubar/visibility");
210 char *filename = new char [24];
211 static int count = 1;
213 static const SGPropertyNode *master_freeze
214 = fgGetNode("/sim/freeze/master");
216 bool freeze = master_freeze->getBoolValue();
218 fgSetBool("/sim/freeze/master", true);
221 fgSetBool("/sim/menubar/visibility", false);
222 int mouse = fgGetMouseCursor();
223 fgSetMouseCursor(MOUSE_CURSOR_NONE);
225 FGRenderer *renderer = globals->get_renderer();
227 renderer->resize( fgGetInt("/sim/startup/xsize"),
228 fgGetInt("/sim/startup/ysize") );
230 // we need two render frames here to clear the menu and cursor
231 // ... not sure why but doing an extra fgRenderFrame() shouldn't
233 //renderer->update( true );
234 //renderer->update( true );
236 // This ImageSize stuff is a temporary hack
237 // should probably use 128x128 tile size and
238 // support any image size
240 // This should be a requester to get multiplier from user
241 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
242 int width = fgGetInt("/sim/startup/xsize");
243 int height = fgGetInt("/sim/startup/ysize");
245 /* allocate buffer large enough to store one tile */
246 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
249 printf("Malloc of tile buffer failed!\n");
253 int imageWidth = multiplier*width;
254 int imageHeight = multiplier*height;
256 /* allocate buffer to hold a row of tiles */
258 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
262 printf("Malloc of tile row buffer failed!\n");
265 TRcontext *tr = trNew();
266 trTileSize(tr, width, height, 0);
267 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
268 trImageSize(tr, imageWidth, imageHeight);
269 trRowOrder(tr, TR_TOP_TO_BOTTOM);
271 // sgFrustum *frustum = ssgGetFrustum();
273 // frustum->getLeft(), frustum->getRight(),
274 // frustum->getBot(), frustum->getTop(),
275 // frustum->getNear(), frustum->getFar());
277 /* Prepare ppm output file */
278 while (count < 1000) {
279 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
280 if ( (f = fopen(filename, "r")) == NULL )
284 f = fopen(filename, "wb");
286 printf("Couldn't open image file: %s\n", filename);
293 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
294 fprintf(f,"%i %i\n", imageWidth, imageHeight);
297 /* just to be safe... */
298 glPixelStorei(GL_PACK_ALIGNMENT, 1);
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);
307 bool do_hud = fgGetBool("/sim/hud/visibility");
308 GLfloat hud_col_step = 640.0 / ncols;
309 GLfloat hud_row_step = 480.0 / nrows;
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;
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);
325 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
326 // int curRow = trGet(tr, TR_CURRENT_ROW);
328 renderer->update( false );
331 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
332 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
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);
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);
347 for (i=0;i<height;i++) {
348 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
349 tile + i*bytesPerTileRow, /* Src */
350 bytesPerCurrentTileRow); /* Byte count*/
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);
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);
367 renderer->resize( width, height );
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 );
385 message = "Snapshot saved to \"";
388 mkDialog (message.c_str());
395 fgSetMouseCursor(mouse);
396 fgSetBool("/sim/menubar/visibility", menu_status);
399 fgSetBool("/sim/freeze/master", false);
402 #endif // #if defined( TR_HIRES_SNAP)
404 void fgDumpSnapShotWrapper () {
409 void fgHiResDumpWrapper () {
415 using namespace flightgear;
417 class GUISnapShotOperation :
418 public GraphicsContextOperation
422 // start new snap shot
425 // allow only one snapshot at a time
426 if (_snapShotOp.valid())
428 _snapShotOp = new GUISnapShotOperation();
429 /* register with graphics context so actual snap shot is done
430 * in the graphics context (thread) */
431 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
432 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
433 osg::GraphicsContext* gc = 0;
435 gc = guiCamera->getGraphicsContext();
437 gc->add(_snapShotOp.get());
439 wsa->windows[0]->gc->add(_snapShotOp.get());
445 // constructor to be executed in main loop's thread
446 GUISnapShotOperation() :
447 flightgear::GraphicsContextOperation(std::string("GUI snap shot")),
448 _master_freeze(fgGetNode("/sim/freeze/master", true)),
449 _freeze(_master_freeze->getBoolValue()),
451 _mouse(fgGetMouseCursor())
454 _master_freeze->setBoolValue(true);
456 fgSetMouseCursor(MOUSE_CURSOR_NONE);
458 string dir = fgGetString("/sim/paths/screenshot-dir");
460 dir = fgGetString("/sim/fg-current");
462 _path.set(dir + '/');
463 if (_path.create_dir( 0755 )) {
464 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
465 << dir << "'. Trying home directory.");
466 dir = fgGetString("/sim/fg-home");
470 static int count = 1;
471 while (count < 1000) {
472 snprintf(filename, 24, "fgfs-screen-%03d.png", count++);
482 _xsize = fgGetInt("/sim/startup/xsize");
483 _ysize = fgGetInt("/sim/startup/ysize");
485 FGRenderer *renderer = globals->get_renderer();
486 renderer->resize(_xsize, _ysize);
487 globals->get_event_mgr()->addTask("SnapShotTimer",
488 this, &GUISnapShotOperation::timerExpired,
492 // to be executed in graphics context (maybe separate thread)
493 void run(osg::GraphicsContext* gc)
495 _result = sg_glDumpWindow(_path.c_str(),
500 // timer method, to be executed in main loop's thread
501 virtual void timerExpired()
505 globals->get_event_mgr()->removeTask("SnapShotTimer");
507 fgSetString("/sim/paths/screenshot-last", _path.c_str());
508 fgSetBool("/sim/signals/screenshot", _result);
510 fgSetMouseCursor(_mouse);
513 _master_freeze->setBoolValue(false);
519 static osg::ref_ptr<GUISnapShotOperation> _snapShotOp;
520 SGPropertyNode_ptr _master_freeze;
530 osg::ref_ptr<GUISnapShotOperation> GUISnapShotOperation::_snapShotOp;
532 // do a screen snap shot
533 bool fgDumpSnapShot ()
536 // start snap shot operation, while needs to be executed in
538 return GUISnapShotOperation::start();
540 // obsolete code => remove when new code is stable
541 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
543 bool freeze = master_freeze->getBoolValue();
545 fgSetBool("/sim/freeze/master", true);
548 int mouse = fgGetMouseCursor();
549 fgSetMouseCursor(MOUSE_CURSOR_NONE);
551 fgSetBool("/sim/signals/screenshot", true);
553 FGRenderer *renderer = globals->get_renderer();
554 renderer->resize( fgGetInt("/sim/startup/xsize"),
555 fgGetInt("/sim/startup/ysize") );
557 // we need two render frames here to clear the menu and cursor
558 // ... not sure why but doing an extra fgRenderFrame() shouldn't
560 renderer->update( true );
561 renderer->update( true );
563 string dir = fgGetString("/sim/paths/screenshot-dir");
565 dir = fgGetString("/sim/fg-current");
567 SGPath path(dir + '/');
568 if (path.create_dir( 0755 )) {
569 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
570 << dir << "'. Trying home directory.");
571 dir = fgGetString("/sim/fg-home");
575 static int count = 1;
576 while (count < 1000) {
577 snprintf(filename, 24, "fgfs-screen-%03d.png", count++);
587 bool result = sg_glDumpWindow(path.c_str(),
588 fgGetInt("/sim/startup/xsize"),
589 fgGetInt("/sim/startup/ysize"));
591 fgSetString("/sim/paths/screenshot-last", path.c_str());
592 fgSetBool("/sim/signals/screenshot", false);
594 fgSetMouseCursor(mouse);
597 fgSetBool("/sim/freeze/master", false);
603 // do an entire scenegraph dump
604 void fgDumpSceneGraph()
606 char *filename = new char [24];
608 static int count = 1;
610 static const SGPropertyNode *master_freeze
611 = fgGetNode("/sim/freeze/master");
613 bool freeze = master_freeze->getBoolValue();
615 fgSetBool("/sim/freeze/master", true);
618 while (count < 1000) {
620 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
621 if ( (fp = fopen(filename, "r")) == NULL )
626 if ( fgDumpSceneGraphToFile(filename)) {
627 message = "Entire scene graph saved to \"";
631 message = "Failed to save to \"";
636 mkDialog (message.c_str());
641 fgSetBool("/sim/freeze/master", false);
646 // do an terrain branch dump
647 void fgDumpTerrainBranch()
649 char *filename = new char [24];
651 static int count = 1;
653 static const SGPropertyNode *master_freeze
654 = fgGetNode("/sim/freeze/master");
656 bool freeze = master_freeze->getBoolValue();
658 fgSetBool("/sim/freeze/master", true);
661 while (count < 1000) {
663 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
664 if ( (fp = fopen(filename, "r")) == NULL )
669 if ( fgDumpTerrainBranchToFile(filename)) {
670 message = "Terrain graph saved to \"";
674 message = "Failed to save to \"";
679 mkDialog (message.c_str());
684 fgSetBool("/sim/freeze/master", false);
688 void fgPrintVisibleSceneInfoCommand()
690 static const SGPropertyNode *master_freeze
691 = fgGetNode("/sim/freeze/master");
693 bool freeze = master_freeze->getBoolValue();
695 fgSetBool("/sim/freeze/master", true);
698 flightgear::printVisibleSceneInfo(globals->get_renderer());
701 fgSetBool("/sim/freeze/master", false);