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>
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>
53 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
54 # include <simgear/screen/win32-printer.h>
55 # include <simgear/screen/GlBitmaps.h>
63 #if defined( TR_HIRES_SNAP)
64 #include <simgear/screen/tr.h>
65 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
66 GLfloat x_end, GLfloat y_end );
70 const __fg_gui_fn_t __fg_gui_fn[] = {
72 {"dumpHiResSnapShot", fgHiResDumpWrapper},
74 {"dumpSnapShot", fgDumpSnapShotWrapper},
75 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
76 {"printScreen", printScreen},
81 // Structure termination
86 /* ================ General Purpose Functions ================ */
88 // General Purpose Message Box. Makes sure no more than 5 different
89 // messages are displayed at the same time, and none of them are
90 // duplicates. (5 is a *lot*, but this will hardly ever be reached
91 // and we don't want to miss any, either.)
92 void mkDialog (const char *txt)
94 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
97 SGPropertyNode *master = gui->getDialogProperties("message");
101 const int maxdialogs = 5;
103 SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
105 for (i = 0; i < maxdialogs; i++) {
106 std::ostringstream s;
107 s << "message-" << i;
110 if (!msg->getNode(name.c_str(), false))
113 if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
114 SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
120 msg = msg->getNode(name.c_str(), true);
121 msg->setStringValue("message", txt);
122 msg = msg->getNode("dialog", true);
123 copyProperties(master, msg);
124 msg->setStringValue("name", name.c_str());
126 gui->showDialog(name.c_str());
129 // Message Box to report an error.
130 void guiErrorMessage (const char *txt)
132 SG_LOG(SG_GENERAL, SG_ALERT, txt);
136 // Message Box to report a throwable (usually an exception).
137 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
141 msg += throwable.getFormattedMessage();
142 if (!throwable.getOrigin().empty()) {
143 msg += "\n (reported by ";
144 msg += throwable.getOrigin();
147 SG_LOG(SG_GENERAL, SG_ALERT, msg);
148 mkDialog(msg.c_str());
153 /* -----------------------------------------------------------------------
154 the Gui callback functions
155 ____________________________________________________________________*/
158 // Hier Neu :-) This is my newly added code
159 // Added by David Findlay <nedz@bigpond.com>
160 // on Sunday 3rd of December
167 SGPath path( globals->get_fg_root() );
168 path.append( "Docs/index.html" );
172 command = globals->get_browser();
173 string::size_type pos;
174 if ((pos = command.find("%u", 0)) != string::npos)
175 command.replace(pos, 2, path.str());
177 command += " " + path.str();
180 system( command.c_str() );
184 // Look for favorite browser
185 char Dummy[1024], ExecName[1024], browserParameter[1024];
186 char win32_name[1024];
188 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
190 strncpy(win32_name,path.c_str(), 1024);
193 FindExecutable(win32_name, Dummy, ExecName);
194 snprintf(browserParameter, 1024, "file:///%s", win32_name);
195 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
200 mkDialog ("Help started in your web browser window.");
203 #if defined( TR_HIRES_SNAP)
208 bool menu_status = fgGetBool("/sim/menubar/visibility");
209 char *filename = new char [24];
210 static int count = 1;
212 static const SGPropertyNode *master_freeze
213 = fgGetNode("/sim/freeze/master");
215 bool freeze = master_freeze->getBoolValue();
217 fgSetBool("/sim/freeze/master", true);
220 fgSetBool("/sim/menubar/visibility", false);
221 int mouse = fgGetMouseCursor();
222 fgSetMouseCursor(MOUSE_CURSOR_NONE);
224 FGRenderer *renderer = globals->get_renderer();
226 renderer->resize( fgGetInt("/sim/startup/xsize"),
227 fgGetInt("/sim/startup/ysize") );
229 // we need two render frames here to clear the menu and cursor
230 // ... not sure why but doing an extra fgRenderFrame() shouldn't
232 //renderer->update( true );
233 //renderer->update( true );
235 // This ImageSize stuff is a temporary hack
236 // should probably use 128x128 tile size and
237 // support any image size
239 // This should be a requester to get multiplier from user
240 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
241 int width = fgGetInt("/sim/startup/xsize");
242 int height = fgGetInt("/sim/startup/ysize");
244 /* allocate buffer large enough to store one tile */
245 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
247 printf("Malloc of tile buffer failed!\n");
251 int imageWidth = multiplier*width;
252 int imageHeight = multiplier*height;
254 /* allocate buffer to hold a row of tiles */
256 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
259 printf("Malloc of tile row buffer failed!\n");
262 TRcontext *tr = trNew();
263 trTileSize(tr, width, height, 0);
264 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
265 trImageSize(tr, imageWidth, imageHeight);
266 trRowOrder(tr, TR_TOP_TO_BOTTOM);
268 // sgFrustum *frustum = ssgGetFrustum();
270 // frustum->getLeft(), frustum->getRight(),
271 // frustum->getBot(), frustum->getTop(),
272 // frustum->getNear(), frustum->getFar());
274 /* Prepare ppm output file */
275 while (count < 1000) {
276 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
277 if ( (f = fopen(filename, "r")) == NULL )
281 f = fopen(filename, "wb");
283 printf("Couldn't open image file: %s\n", filename);
289 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
290 fprintf(f,"%i %i\n", imageWidth, imageHeight);
293 /* just to be safe... */
294 glPixelStorei(GL_PACK_ALIGNMENT, 1);
296 /* Because the HUD and Panel change the ViewPort we will
297 * need to handle some lowlevel stuff ourselves */
298 int ncols = trGet(tr, TR_COLUMNS);
299 int nrows = trGet(tr, TR_ROWS);
301 bool do_hud = fgGetBool("/sim/hud/visibility");
302 GLfloat hud_col_step = 640.0 / ncols;
303 GLfloat hud_row_step = 480.0 / nrows;
305 bool do_panel = fgPanelVisible();
306 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
307 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
309 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
310 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
311 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
312 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
313 glHint(GL_FOG_HINT, GL_NICEST);
319 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
320 int curRow = trGet(tr, TR_CURRENT_ROW);
322 renderer->update( false );
325 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
326 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
329 // globals->get_current_panel()->update(
330 // curColumn*panel_col_step, panel_col_step,
331 // curRow*panel_row_step, panel_row_step );
332 more = trEndTile(tr);
334 /* save tile into tile row buffer*/
335 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
336 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
337 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
338 int xOffset = curColumn * bytesPerTileRow;
339 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
341 for (i=0;i<height;i++) {
342 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
343 tile + i*bytesPerTileRow, /* Src */
344 bytesPerCurrentTileRow); /* Byte count*/
347 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
348 /* write this buffered row of tiles to the file */
349 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
350 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
352 for (i=0;i<curTileHeight;i++) {
353 /* Remember, OpenGL images are bottom to top. Have to reverse. */
354 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
355 fwrite(rowPtr, 1, imageWidth*3, f);
361 renderer->resize( width, height );
365 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
366 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
367 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
368 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
369 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
370 (!fgGetBool("/sim/rendering/shading"))) {
371 // if fastest fog requested, or if flat shading force fastest
372 glHint ( GL_FOG_HINT, GL_FASTEST );
373 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
374 glHint ( GL_FOG_HINT, GL_DONT_CARE );
379 message = "Snapshot saved to \"";
382 mkDialog (message.c_str());
389 fgSetMouseCursor(mouse);
390 fgSetBool("/sim/menubar/visibility", menu_status);
393 fgSetBool("/sim/freeze/master", false);
396 #endif // #if defined( TR_HIRES_SNAP)
399 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
401 void rotateView( double roll, double pitch, double yaw )
407 GLubyte *hiResScreenCapture( int multiplier )
409 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
410 float fov = oldfov / multiplier;
411 FGViewer *v = globals->get_current_view();
412 fgSetDouble("/sim/current-view/field-of-view", fov);
413 // globals->get_renderer()->init();
414 int cur_width = fgGetInt("/sim/startup/xsize");
415 int cur_height = fgGetInt("/sim/startup/ysize");
417 // New empty (mostly) bitmap
418 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
420 for ( y = 0; y < multiplier; y++ ) {
421 for ( x = 0; x < multiplier; x++ ) {
422 globals->get_renderer()->resize( cur_width, cur_height );
424 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
425 globals->get_renderer()->update( false );
428 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
431 fgSetDouble("/sim/current-view/field-of-view", oldfov);
432 return b1->getBitmap();
436 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
437 // win32 print screen function
438 void printScreen () {
439 int mouse = fgGetMouseCursor();
440 fgSetMouseCursor(MOUSE_CURSOR_NONE);
442 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
443 int cur_width = fgGetInt("/sim/startup/xsize");
444 int cur_height = fgGetInt("/sim/startup/ysize");
445 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
446 p.End( hiResScreenCapture(3) );
448 fgSetMouseCursor(mouse);
450 #endif // #ifdef WIN32
453 void fgDumpSnapShotWrapper () {
458 void fgHiResDumpWrapper () {
463 // do a screen snap shot
464 bool fgDumpSnapShot () {
465 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
467 bool freeze = master_freeze->getBoolValue();
469 fgSetBool("/sim/freeze/master", true);
472 int mouse = fgGetMouseCursor();
473 fgSetMouseCursor(MOUSE_CURSOR_NONE);
475 fgSetBool("/sim/signals/screenshot", true);
477 FGRenderer *renderer = globals->get_renderer();
478 renderer->resize( fgGetInt("/sim/startup/xsize"),
479 fgGetInt("/sim/startup/ysize") );
481 // we need two render frames here to clear the menu and cursor
482 // ... not sure why but doing an extra fgRenderFrame() shouldn't
484 renderer->update( true );
485 renderer->update( true );
487 string dir = fgGetString("/sim/paths/screenshot-dir", fgGetString("/sim/fg-current"));
488 SGPath path(dir + '/');
489 if (path.create_dir( 0755 )) {
490 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
491 << dir << "'. Trying home directory.");
492 dir = fgGetString("/sim/fg-home");
496 static int count = 1;
497 while (count < 1000) {
498 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
508 int result = sg_glDumpWindow(path.c_str(),
509 fgGetInt("/sim/startup/xsize"),
510 fgGetInt("/sim/startup/ysize"));
512 fgSetString("/sim/paths/screenshot-last", path.c_str());
513 fgSetBool("/sim/signals/screenshot", false);
515 fgSetMouseCursor(mouse);
518 fgSetBool("/sim/freeze/master", false);
523 // do an entire scenegraph dump
524 void fgDumpSceneGraph()
526 char *filename = new char [24];
528 static int count = 1;
530 FGRenderer *renderer = globals->get_renderer();
532 static const SGPropertyNode *master_freeze
533 = fgGetNode("/sim/freeze/master");
535 bool freeze = master_freeze->getBoolValue();
537 fgSetBool("/sim/freeze/master", true);
540 while (count < 1000) {
542 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
543 if ( (fp = fopen(filename, "r")) == NULL )
548 if ( fgDumpSceneGraphToFile(filename)) {
549 message = "Entire scene graph saved to \"";
553 message = "Failed to save to \"";
558 mkDialog (message.c_str());
563 fgSetBool("/sim/freeze/master", false);
568 // do an terrain branch dump
569 void fgDumpTerrainBranch()
571 char *filename = new char [24];
573 static int count = 1;
575 FGRenderer *renderer = globals->get_renderer();
577 static const SGPropertyNode *master_freeze
578 = fgGetNode("/sim/freeze/master");
580 bool freeze = master_freeze->getBoolValue();
582 fgSetBool("/sim/freeze/master", true);
585 while (count < 1000) {
587 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
588 if ( (fp = fopen(filename, "r")) == NULL )
593 if ( fgDumpTerrainBranchToFile(filename)) {
594 message = "Terrain graph saved to \"";
598 message = "Failed to save to \"";
603 mkDialog (message.c_str());
608 fgSetBool("/sim/freeze/master", false);