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/fg_os.hxx>
50 #include <Main/renderer.hxx>
51 #include <Main/viewmgr.hxx>
52 #include <GUI/new_gui.hxx>
54 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
55 # include <simgear/screen/win32-printer.h>
56 # include <simgear/screen/GlBitmaps.h>
66 #if defined( TR_HIRES_SNAP)
67 #include <simgear/screen/tr.h>
68 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
69 GLfloat x_end, GLfloat y_end );
73 const __fg_gui_fn_t __fg_gui_fn[] = {
75 {"dumpHiResSnapShot", fgHiResDumpWrapper},
77 {"dumpSnapShot", fgDumpSnapShotWrapper},
78 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
79 {"printScreen", printScreen},
84 // Structure termination
89 /* ================ General Purpose Functions ================ */
91 // General Purpose Message Box. Makes sure no more than 5 different
92 // messages are displayed at the same time, and none of them are
93 // duplicates. (5 is a *lot*, but this will hardly ever be reached
94 // and we don't want to miss any, either.)
95 void mkDialog (const char *txt)
97 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
100 SGPropertyNode *master = gui->getDialogProperties("message");
104 const int maxdialogs = 5;
106 SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
108 for (i = 0; i < maxdialogs; i++) {
109 std::ostringstream s;
110 s << "message-" << i;
113 if (!msg->getNode(name.c_str(), false))
116 if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
117 SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
123 msg = msg->getNode(name.c_str(), true);
124 msg->setStringValue("message", txt);
125 msg = msg->getNode("dialog", true);
126 copyProperties(master, msg);
127 msg->setStringValue("name", name.c_str());
129 gui->showDialog(name.c_str());
132 // Message Box to report an error.
133 void guiErrorMessage (const char *txt)
135 SG_LOG(SG_GENERAL, SG_ALERT, txt);
139 // Message Box to report a throwable (usually an exception).
140 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
144 msg += throwable.getFormattedMessage();
145 if (!std::strlen(throwable.getOrigin()) != 0) {
146 msg += "\n (reported by ";
147 msg += throwable.getOrigin();
150 SG_LOG(SG_GENERAL, SG_ALERT, msg);
151 mkDialog(msg.c_str());
156 /* -----------------------------------------------------------------------
157 the Gui callback functions
158 ____________________________________________________________________*/
161 // Hier Neu :-) This is my newly added code
162 // Added by David Findlay <nedz@bigpond.com>
163 // on Sunday 3rd of December
170 SGPath path( globals->get_fg_root() );
171 path.append( "Docs/index.html" );
175 command = globals->get_browser();
176 string::size_type pos;
177 if ((pos = command.find("%u", 0)) != string::npos)
178 command.replace(pos, 2, path.str());
180 command += " " + path.str();
183 system( command.c_str() );
187 // Look for favorite browser
188 char Dummy[1024], ExecName[1024], browserParameter[1024];
189 char win32_name[1024];
191 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
193 strncpy(win32_name,path.c_str(), 1024);
196 FindExecutable(win32_name, Dummy, ExecName);
197 snprintf(browserParameter, 1024, "file:///%s", win32_name);
198 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
203 mkDialog ("Help started in your web browser window.");
206 #if defined( TR_HIRES_SNAP)
211 bool menu_status = fgGetBool("/sim/menubar/visibility");
212 char *filename = new char [24];
213 static int count = 1;
215 static const SGPropertyNode *master_freeze
216 = fgGetNode("/sim/freeze/master");
218 bool freeze = master_freeze->getBoolValue();
220 fgSetBool("/sim/freeze/master", true);
223 fgSetBool("/sim/menubar/visibility", false);
224 int mouse = fgGetMouseCursor();
225 fgSetMouseCursor(MOUSE_CURSOR_NONE);
227 FGRenderer *renderer = globals->get_renderer();
229 renderer->resize( fgGetInt("/sim/startup/xsize"),
230 fgGetInt("/sim/startup/ysize") );
232 // we need two render frames here to clear the menu and cursor
233 // ... not sure why but doing an extra fgRenderFrame() shouldn't
235 //renderer->update( true );
236 //renderer->update( true );
238 // This ImageSize stuff is a temporary hack
239 // should probably use 128x128 tile size and
240 // support any image size
242 // This should be a requester to get multiplier from user
243 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
244 int width = fgGetInt("/sim/startup/xsize");
245 int height = fgGetInt("/sim/startup/ysize");
247 /* allocate buffer large enough to store one tile */
248 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
251 printf("Malloc of tile buffer failed!\n");
255 int imageWidth = multiplier*width;
256 int imageHeight = multiplier*height;
258 /* allocate buffer to hold a row of tiles */
260 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
264 printf("Malloc of tile row buffer failed!\n");
267 TRcontext *tr = trNew();
268 trTileSize(tr, width, height, 0);
269 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
270 trImageSize(tr, imageWidth, imageHeight);
271 trRowOrder(tr, TR_TOP_TO_BOTTOM);
273 // sgFrustum *frustum = ssgGetFrustum();
275 // frustum->getLeft(), frustum->getRight(),
276 // frustum->getBot(), frustum->getTop(),
277 // frustum->getNear(), frustum->getFar());
279 /* Prepare ppm output file */
280 while (count < 1000) {
281 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
282 if ( (f = fopen(filename, "r")) == NULL )
286 f = fopen(filename, "wb");
288 printf("Couldn't open image file: %s\n", filename);
295 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
296 fprintf(f,"%i %i\n", imageWidth, imageHeight);
299 /* just to be safe... */
300 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)
405 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
407 void rotateView( double roll, double pitch, double yaw )
413 GLubyte *hiResScreenCapture( int multiplier )
415 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
416 float fov = oldfov / multiplier;
417 FGViewer *v = globals->get_current_view();
418 fgSetDouble("/sim/current-view/field-of-view", fov);
419 // globals->get_renderer()->init();
420 int cur_width = fgGetInt("/sim/startup/xsize");
421 int cur_height = fgGetInt("/sim/startup/ysize");
423 // New empty (mostly) bitmap
424 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
426 for ( y = 0; y < multiplier; y++ ) {
427 for ( x = 0; x < multiplier; x++ ) {
428 globals->get_renderer()->resize( cur_width, cur_height );
430 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
431 globals->get_renderer()->update( false );
434 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
437 fgSetDouble("/sim/current-view/field-of-view", oldfov);
438 return b1->getBitmap();
442 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
443 // win32 print screen function
444 void printScreen () {
445 int mouse = fgGetMouseCursor();
446 fgSetMouseCursor(MOUSE_CURSOR_NONE);
448 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
449 int cur_width = fgGetInt("/sim/startup/xsize");
450 int cur_height = fgGetInt("/sim/startup/ysize");
451 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
452 p.End( hiResScreenCapture(3) );
454 fgSetMouseCursor(mouse);
456 #endif // #ifdef WIN32
459 void fgDumpSnapShotWrapper () {
464 void fgHiResDumpWrapper () {
469 // do a screen snap shot
470 bool fgDumpSnapShot () {
471 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
473 bool freeze = master_freeze->getBoolValue();
475 fgSetBool("/sim/freeze/master", true);
478 int mouse = fgGetMouseCursor();
479 fgSetMouseCursor(MOUSE_CURSOR_NONE);
481 fgSetBool("/sim/signals/screenshot", true);
483 FGRenderer *renderer = globals->get_renderer();
484 renderer->resize( fgGetInt("/sim/startup/xsize"),
485 fgGetInt("/sim/startup/ysize") );
487 // we need two render frames here to clear the menu and cursor
488 // ... not sure why but doing an extra fgRenderFrame() shouldn't
490 renderer->update( true );
491 renderer->update( true );
493 string dir = fgGetString("/sim/paths/screenshot-dir");
495 dir = fgGetString("/sim/fg-current");
497 SGPath path(dir + '/');
498 if (path.create_dir( 0755 )) {
499 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
500 << dir << "'. Trying home directory.");
501 dir = fgGetString("/sim/fg-home");
505 static int count = 1;
506 while (count < 1000) {
507 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
517 int result = sg_glDumpWindow(path.c_str(),
518 fgGetInt("/sim/startup/xsize"),
519 fgGetInt("/sim/startup/ysize"));
521 fgSetString("/sim/paths/screenshot-last", path.c_str());
522 fgSetBool("/sim/signals/screenshot", false);
524 fgSetMouseCursor(mouse);
527 fgSetBool("/sim/freeze/master", false);
532 // do an entire scenegraph dump
533 void fgDumpSceneGraph()
535 char *filename = new char [24];
537 static int count = 1;
539 FGRenderer *renderer = globals->get_renderer();
541 static const SGPropertyNode *master_freeze
542 = fgGetNode("/sim/freeze/master");
544 bool freeze = master_freeze->getBoolValue();
546 fgSetBool("/sim/freeze/master", true);
549 while (count < 1000) {
551 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
552 if ( (fp = fopen(filename, "r")) == NULL )
557 if ( fgDumpSceneGraphToFile(filename)) {
558 message = "Entire scene graph saved to \"";
562 message = "Failed to save to \"";
567 mkDialog (message.c_str());
572 fgSetBool("/sim/freeze/master", false);
577 // do an terrain branch dump
578 void fgDumpTerrainBranch()
580 char *filename = new char [24];
582 static int count = 1;
584 FGRenderer *renderer = globals->get_renderer();
586 static const SGPropertyNode *master_freeze
587 = fgGetNode("/sim/freeze/master");
589 bool freeze = master_freeze->getBoolValue();
591 fgSetBool("/sim/freeze/master", true);
594 while (count < 1000) {
596 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
597 if ( (fp = fopen(filename, "r")) == NULL )
602 if ( fgDumpTerrainBranchToFile(filename)) {
603 message = "Terrain graph saved to \"";
607 message = "Failed to save to \"";
612 mkDialog (message.c_str());
617 fgSetBool("/sim/freeze/master", false);