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>
50 # include <shellapi.h>
52 # include <sys/cygwin.h>
56 #include <simgear/debug/logstream.hxx>
57 #include <simgear/misc/sg_path.hxx>
58 #include <simgear/screen/screen-dump.hxx>
60 #include <Cockpit/panel.hxx>
61 #include <Main/globals.hxx>
62 #include <Main/fg_props.hxx>
63 #include <Main/renderer.hxx>
64 #include <Main/viewmgr.hxx>
65 #include <GUI/new_gui.hxx>
67 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
68 # include <simgear/screen/win32-printer.h>
69 # include <simgear/screen/GlBitmaps.h>
77 #if defined( TR_HIRES_SNAP)
78 #include <simgear/screen/tr.h>
79 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
80 GLfloat x_end, GLfloat y_end );
84 const __fg_gui_fn_t __fg_gui_fn[] = {
86 {"dumpHiResSnapShot", fgHiResDumpWrapper},
88 {"dumpSnapShot", fgDumpSnapShotWrapper},
89 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
90 {"printScreen", printScreen},
95 // Structure termination
100 /* ================ General Purpose Functions ================ */
102 // General Purpose Message Box. Makes sure no more than 5 different
103 // messages are displayed at the same time, and none of them are
104 // duplicates. (5 is a *lot*, but this will hardly ever be reached
105 // and we don't want to miss any, either.)
106 void mkDialog (const char *txt)
108 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
111 SGPropertyNode *master = gui->getDialogProperties("message");
115 const int maxdialogs = 5;
117 SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
119 for (i = 0; i < maxdialogs; i++) {
120 std::ostringstream s;
121 s << "message-" << i;
124 if (!msg->getNode(name.c_str(), false))
127 if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
128 SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
134 msg = msg->getNode(name.c_str(), true);
135 msg->setStringValue("message", txt);
136 msg = msg->getNode("dialog", true);
137 copyProperties(master, msg);
138 msg->setStringValue("name", name.c_str());
140 gui->showDialog(name.c_str());
143 // Message Box to report an error.
144 void guiErrorMessage (const char *txt)
146 SG_LOG(SG_GENERAL, SG_ALERT, txt);
150 // Message Box to report a throwable (usually an exception).
151 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
155 msg += throwable.getFormattedMessage();
156 if (!throwable.getOrigin().empty()) {
157 msg += "\n (reported by ";
158 msg += throwable.getOrigin();
161 SG_LOG(SG_GENERAL, SG_ALERT, msg);
162 mkDialog(msg.c_str());
167 /* -----------------------------------------------------------------------
168 the Gui callback functions
169 ____________________________________________________________________*/
172 // Hier Neu :-) This is my newly added code
173 // Added by David Findlay <nedz@bigpond.com>
174 // on Sunday 3rd of December
181 SGPath path( globals->get_fg_root() );
182 path.append( "Docs/index.html" );
186 command = globals->get_browser();
187 string::size_type pos;
188 if ((pos = command.find("%u", 0)) != string::npos)
189 command.replace(pos, 2, path.str());
191 command += " " + path.str();
194 system( command.c_str() );
198 // Look for favorite browser
199 char Dummy[1024], ExecName[1024], browserParameter[1024];
200 char win32_name[1024];
202 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
204 strncpy(win32_name,path.c_str(), 1024);
207 FindExecutable(win32_name, Dummy, ExecName);
208 snprintf(browserParameter, 1024, "file:///%s", win32_name);
209 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
214 mkDialog ("Help started in your web browser window.");
217 #if defined( TR_HIRES_SNAP)
222 bool menu_status = fgGetBool("/sim/menubar/visibility");
223 char *filename = new char [24];
224 static int count = 1;
226 static const SGPropertyNode *master_freeze
227 = fgGetNode("/sim/freeze/master");
229 bool freeze = master_freeze->getBoolValue();
231 fgSetBool("/sim/freeze/master", true);
234 fgSetBool("/sim/menubar/visibility", false);
235 int mouse = fgGetMouseCursor();
236 fgSetMouseCursor(MOUSE_CURSOR_NONE);
238 FGRenderer *renderer = globals->get_renderer();
240 renderer->resize( fgGetInt("/sim/startup/xsize"),
241 fgGetInt("/sim/startup/ysize") );
243 // we need two render frames here to clear the menu and cursor
244 // ... not sure why but doing an extra fgRenderFrame() shouldn't
246 //renderer->update( true );
247 //renderer->update( true );
249 // This ImageSize stuff is a temporary hack
250 // should probably use 128x128 tile size and
251 // support any image size
253 // This should be a requester to get multiplier from user
254 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
255 int width = fgGetInt("/sim/startup/xsize");
256 int height = fgGetInt("/sim/startup/ysize");
258 /* allocate buffer large enough to store one tile */
259 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
261 printf("Malloc of tile buffer failed!\n");
265 int imageWidth = multiplier*width;
266 int imageHeight = multiplier*height;
268 /* allocate buffer to hold a row of tiles */
270 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
273 printf("Malloc of tile row buffer failed!\n");
276 TRcontext *tr = trNew();
277 trTileSize(tr, width, height, 0);
278 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
279 trImageSize(tr, imageWidth, imageHeight);
280 trRowOrder(tr, TR_TOP_TO_BOTTOM);
282 // sgFrustum *frustum = ssgGetFrustum();
284 // frustum->getLeft(), frustum->getRight(),
285 // frustum->getBot(), frustum->getTop(),
286 // frustum->getNear(), frustum->getFar());
288 /* Prepare ppm output file */
289 while (count < 1000) {
290 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
291 if ( (f = fopen(filename, "r")) == NULL )
295 f = fopen(filename, "wb");
297 printf("Couldn't open image file: %s\n", filename);
303 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
304 fprintf(f,"%i %i\n", imageWidth, imageHeight);
307 /* just to be safe... */
308 glPixelStorei(GL_PACK_ALIGNMENT, 1);
310 /* Because the HUD and Panel change the ViewPort we will
311 * need to handle some lowlevel stuff ourselves */
312 int ncols = trGet(tr, TR_COLUMNS);
313 int nrows = trGet(tr, TR_ROWS);
315 bool do_hud = fgGetBool("/sim/hud/visibility");
316 GLfloat hud_col_step = 640.0 / ncols;
317 GLfloat hud_row_step = 480.0 / nrows;
319 bool do_panel = fgPanelVisible();
320 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
321 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
323 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
324 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
325 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
326 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
327 glHint(GL_FOG_HINT, GL_NICEST);
333 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
334 int curRow = trGet(tr, TR_CURRENT_ROW);
336 renderer->update( false );
339 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
340 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
343 // globals->get_current_panel()->update(
344 // curColumn*panel_col_step, panel_col_step,
345 // curRow*panel_row_step, panel_row_step );
346 more = trEndTile(tr);
348 /* save tile into tile row buffer*/
349 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
350 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
351 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
352 int xOffset = curColumn * bytesPerTileRow;
353 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
355 for (i=0;i<height;i++) {
356 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
357 tile + i*bytesPerTileRow, /* Src */
358 bytesPerCurrentTileRow); /* Byte count*/
361 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
362 /* write this buffered row of tiles to the file */
363 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
364 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
366 for (i=0;i<curTileHeight;i++) {
367 /* Remember, OpenGL images are bottom to top. Have to reverse. */
368 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
369 fwrite(rowPtr, 1, imageWidth*3, f);
375 renderer->resize( width, height );
379 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
380 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
381 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
382 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
383 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
384 (!fgGetBool("/sim/rendering/shading"))) {
385 // if fastest fog requested, or if flat shading force fastest
386 glHint ( GL_FOG_HINT, GL_FASTEST );
387 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
388 glHint ( GL_FOG_HINT, GL_DONT_CARE );
393 message = "Snapshot saved to \"";
396 mkDialog (message.c_str());
403 fgSetMouseCursor(mouse);
404 fgSetBool("/sim/menubar/visibility", menu_status);
407 fgSetBool("/sim/freeze/master", false);
410 #endif // #if defined( TR_HIRES_SNAP)
413 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
415 void rotateView( double roll, double pitch, double yaw )
421 GLubyte *hiResScreenCapture( int multiplier )
423 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
424 float fov = oldfov / multiplier;
425 FGViewer *v = globals->get_current_view();
426 fgSetDouble("/sim/current-view/field-of-view", fov);
427 // globals->get_renderer()->init();
428 int cur_width = fgGetInt("/sim/startup/xsize");
429 int cur_height = fgGetInt("/sim/startup/ysize");
431 // New empty (mostly) bitmap
432 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
434 for ( y = 0; y < multiplier; y++ ) {
435 for ( x = 0; x < multiplier; x++ ) {
436 globals->get_renderer()->resize( cur_width, cur_height );
438 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
439 globals->get_renderer()->update( false );
442 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
445 fgSetDouble("/sim/current-view/field-of-view", oldfov);
446 return b1->getBitmap();
450 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
451 // win32 print screen function
452 void printScreen () {
453 int mouse = fgGetMouseCursor();
454 fgSetMouseCursor(MOUSE_CURSOR_NONE);
456 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
457 int cur_width = fgGetInt("/sim/startup/xsize");
458 int cur_height = fgGetInt("/sim/startup/ysize");
459 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
460 p.End( hiResScreenCapture(3) );
462 fgSetMouseCursor(mouse);
464 #endif // #ifdef WIN32
467 void fgDumpSnapShotWrapper () {
472 void fgHiResDumpWrapper () {
477 // do a screen snap shot
478 bool fgDumpSnapShot () {
479 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
481 bool freeze = master_freeze->getBoolValue();
483 fgSetBool("/sim/freeze/master", true);
486 int mouse = fgGetMouseCursor();
487 fgSetMouseCursor(MOUSE_CURSOR_NONE);
489 fgSetBool("/sim/signals/screenshot", true);
491 FGRenderer *renderer = globals->get_renderer();
492 renderer->resize( fgGetInt("/sim/startup/xsize"),
493 fgGetInt("/sim/startup/ysize") );
495 // we need two render frames here to clear the menu and cursor
496 // ... not sure why but doing an extra fgRenderFrame() shouldn't
498 renderer->update( true );
499 renderer->update( true );
501 string dir = fgGetString("/sim/paths/screenshot-dir", fgGetString("/sim/fg-current"));
502 SGPath path(dir + '/');
503 if (path.create_dir( 0755 )) {
504 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
505 << dir << "'. Trying home directory.");
506 dir = fgGetString("/sim/fg-home");
510 static int count = 1;
511 while (count < 1000) {
512 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
522 int result = sg_glDumpWindow(path.c_str(),
523 fgGetInt("/sim/startup/xsize"),
524 fgGetInt("/sim/startup/ysize"));
526 fgSetString("/sim/paths/screenshot-last", path.c_str());
527 fgSetBool("/sim/signals/screenshot", false);
529 fgSetMouseCursor(mouse);
532 fgSetBool("/sim/freeze/master", false);
537 // do an entire scenegraph dump
538 void fgDumpSceneGraph()
540 char *filename = new char [24];
542 static int count = 1;
544 FGRenderer *renderer = globals->get_renderer();
546 static const SGPropertyNode *master_freeze
547 = fgGetNode("/sim/freeze/master");
549 bool freeze = master_freeze->getBoolValue();
551 fgSetBool("/sim/freeze/master", true);
554 while (count < 1000) {
556 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
557 if ( (fp = fopen(filename, "r")) == NULL )
562 if ( fgDumpSceneGraphToFile(filename)) {
563 message = "Entire scene graph saved to \"";
567 message = "Failed to save to \"";
572 mkDialog (message.c_str());
577 fgSetBool("/sim/freeze/master", false);
582 // do an terrain branch dump
583 void fgDumpTerrainBranch()
585 char *filename = new char [24];
587 static int count = 1;
589 FGRenderer *renderer = globals->get_renderer();
591 static const SGPropertyNode *master_freeze
592 = fgGetNode("/sim/freeze/master");
594 bool freeze = master_freeze->getBoolValue();
596 fgSetBool("/sim/freeze/master", true);
599 while (count < 1000) {
601 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
602 if ( (fp = fopen(filename, "r")) == NULL )
607 if ( fgDumpTerrainBranchToFile(filename)) {
608 message = "Terrain graph saved to \"";
612 message = "Failed to save to \"";
617 mkDialog (message.c_str());
622 fgSetBool("/sim/freeze/master", false);