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>
65 #if defined( TR_HIRES_SNAP)
66 #include <simgear/screen/tr.h>
67 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
68 GLfloat x_end, GLfloat y_end );
72 const __fg_gui_fn_t __fg_gui_fn[] = {
74 {"dumpHiResSnapShot", fgHiResDumpWrapper},
76 {"dumpSnapShot", fgDumpSnapShotWrapper},
77 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
78 {"printScreen", printScreen},
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 (!throwable.getOrigin().empty()) {
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));
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));
261 printf("Malloc of tile row buffer failed!\n");
264 TRcontext *tr = trNew();
265 trTileSize(tr, width, height, 0);
266 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
267 trImageSize(tr, imageWidth, imageHeight);
268 trRowOrder(tr, TR_TOP_TO_BOTTOM);
270 // sgFrustum *frustum = ssgGetFrustum();
272 // frustum->getLeft(), frustum->getRight(),
273 // frustum->getBot(), frustum->getTop(),
274 // frustum->getNear(), frustum->getFar());
276 /* Prepare ppm output file */
277 while (count < 1000) {
278 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
279 if ( (f = fopen(filename, "r")) == NULL )
283 f = fopen(filename, "wb");
285 printf("Couldn't open image file: %s\n", filename);
291 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
292 fprintf(f,"%i %i\n", imageWidth, imageHeight);
295 /* just to be safe... */
296 glPixelStorei(GL_PACK_ALIGNMENT, 1);
298 /* Because the HUD and Panel change the ViewPort we will
299 * need to handle some lowlevel stuff ourselves */
300 int ncols = trGet(tr, TR_COLUMNS);
301 int nrows = trGet(tr, TR_ROWS);
303 bool do_hud = fgGetBool("/sim/hud/visibility");
304 GLfloat hud_col_step = 640.0 / ncols;
305 GLfloat hud_row_step = 480.0 / nrows;
307 bool do_panel = fgPanelVisible();
308 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
309 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
311 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
312 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
313 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
314 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
315 glHint(GL_FOG_HINT, GL_NICEST);
321 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
322 int curRow = trGet(tr, TR_CURRENT_ROW);
324 renderer->update( false );
327 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
328 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
331 // globals->get_current_panel()->update(
332 // curColumn*panel_col_step, panel_col_step,
333 // curRow*panel_row_step, panel_row_step );
334 more = trEndTile(tr);
336 /* save tile into tile row buffer*/
337 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
338 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
339 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
340 int xOffset = curColumn * bytesPerTileRow;
341 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
343 for (i=0;i<height;i++) {
344 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
345 tile + i*bytesPerTileRow, /* Src */
346 bytesPerCurrentTileRow); /* Byte count*/
349 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
350 /* write this buffered row of tiles to the file */
351 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
352 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
354 for (i=0;i<curTileHeight;i++) {
355 /* Remember, OpenGL images are bottom to top. Have to reverse. */
356 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
357 fwrite(rowPtr, 1, imageWidth*3, f);
363 renderer->resize( width, height );
367 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
368 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
369 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
370 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
371 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
372 (!fgGetBool("/sim/rendering/shading"))) {
373 // if fastest fog requested, or if flat shading force fastest
374 glHint ( GL_FOG_HINT, GL_FASTEST );
375 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
376 glHint ( GL_FOG_HINT, GL_DONT_CARE );
381 message = "Snapshot saved to \"";
384 mkDialog (message.c_str());
391 fgSetMouseCursor(mouse);
392 fgSetBool("/sim/menubar/visibility", menu_status);
395 fgSetBool("/sim/freeze/master", false);
398 #endif // #if defined( TR_HIRES_SNAP)
401 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
403 void rotateView( double roll, double pitch, double yaw )
409 GLubyte *hiResScreenCapture( int multiplier )
411 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
412 float fov = oldfov / multiplier;
413 FGViewer *v = globals->get_current_view();
414 fgSetDouble("/sim/current-view/field-of-view", fov);
415 // globals->get_renderer()->init();
416 int cur_width = fgGetInt("/sim/startup/xsize");
417 int cur_height = fgGetInt("/sim/startup/ysize");
419 // New empty (mostly) bitmap
420 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
422 for ( y = 0; y < multiplier; y++ ) {
423 for ( x = 0; x < multiplier; x++ ) {
424 globals->get_renderer()->resize( cur_width, cur_height );
426 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
427 globals->get_renderer()->update( false );
430 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
433 fgSetDouble("/sim/current-view/field-of-view", oldfov);
434 return b1->getBitmap();
438 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
439 // win32 print screen function
440 void printScreen () {
441 int mouse = fgGetMouseCursor();
442 fgSetMouseCursor(MOUSE_CURSOR_NONE);
444 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
445 int cur_width = fgGetInt("/sim/startup/xsize");
446 int cur_height = fgGetInt("/sim/startup/ysize");
447 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
448 p.End( hiResScreenCapture(3) );
450 fgSetMouseCursor(mouse);
452 #endif // #ifdef WIN32
455 void fgDumpSnapShotWrapper () {
460 void fgHiResDumpWrapper () {
465 // do a screen snap shot
466 bool fgDumpSnapShot () {
467 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
469 bool freeze = master_freeze->getBoolValue();
471 fgSetBool("/sim/freeze/master", true);
474 int mouse = fgGetMouseCursor();
475 fgSetMouseCursor(MOUSE_CURSOR_NONE);
477 fgSetBool("/sim/signals/screenshot", true);
479 FGRenderer *renderer = globals->get_renderer();
480 renderer->resize( fgGetInt("/sim/startup/xsize"),
481 fgGetInt("/sim/startup/ysize") );
483 // we need two render frames here to clear the menu and cursor
484 // ... not sure why but doing an extra fgRenderFrame() shouldn't
486 renderer->update( true );
487 renderer->update( true );
489 string dir = fgGetString("/sim/paths/screenshot-dir", fgGetString("/sim/fg-current"));
490 SGPath path(dir + '/');
491 if (path.create_dir( 0755 )) {
492 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
493 << dir << "'. Trying home directory.");
494 dir = fgGetString("/sim/fg-home");
498 static int count = 1;
499 while (count < 1000) {
500 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
510 int result = sg_glDumpWindow(path.c_str(),
511 fgGetInt("/sim/startup/xsize"),
512 fgGetInt("/sim/startup/ysize"));
514 fgSetString("/sim/paths/screenshot-last", path.c_str());
515 fgSetBool("/sim/signals/screenshot", false);
517 fgSetMouseCursor(mouse);
520 fgSetBool("/sim/freeze/master", false);
525 // do an entire scenegraph dump
526 void fgDumpSceneGraph()
528 char *filename = new char [24];
530 static int count = 1;
532 FGRenderer *renderer = globals->get_renderer();
534 static const SGPropertyNode *master_freeze
535 = fgGetNode("/sim/freeze/master");
537 bool freeze = master_freeze->getBoolValue();
539 fgSetBool("/sim/freeze/master", true);
542 while (count < 1000) {
544 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
545 if ( (fp = fopen(filename, "r")) == NULL )
550 if ( fgDumpSceneGraphToFile(filename)) {
551 message = "Entire scene graph saved to \"";
555 message = "Failed to save to \"";
560 mkDialog (message.c_str());
565 fgSetBool("/sim/freeze/master", false);
570 // do an terrain branch dump
571 void fgDumpTerrainBranch()
573 char *filename = new char [24];
575 static int count = 1;
577 FGRenderer *renderer = globals->get_renderer();
579 static const SGPropertyNode *master_freeze
580 = fgGetNode("/sim/freeze/master");
582 bool freeze = master_freeze->getBoolValue();
584 fgSetBool("/sim/freeze/master", true);
587 while (count < 1000) {
589 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
590 if ( (fp = fopen(filename, "r")) == NULL )
595 if ( fgDumpTerrainBranchToFile(filename)) {
596 message = "Terrain graph saved to \"";
600 message = "Failed to save to \"";
605 mkDialog (message.c_str());
610 fgSetBool("/sim/freeze/master", false);