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));
250 printf("Malloc of tile buffer failed!\n");
254 int imageWidth = multiplier*width;
255 int imageHeight = multiplier*height;
257 /* allocate buffer to hold a row of tiles */
259 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
263 printf("Malloc of tile row buffer failed!\n");
266 TRcontext *tr = trNew();
267 trTileSize(tr, width, height, 0);
268 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
269 trImageSize(tr, imageWidth, imageHeight);
270 trRowOrder(tr, TR_TOP_TO_BOTTOM);
272 // sgFrustum *frustum = ssgGetFrustum();
274 // frustum->getLeft(), frustum->getRight(),
275 // frustum->getBot(), frustum->getTop(),
276 // frustum->getNear(), frustum->getFar());
278 /* Prepare ppm output file */
279 while (count < 1000) {
280 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
281 if ( (f = fopen(filename, "r")) == NULL )
285 f = fopen(filename, "wb");
287 printf("Couldn't open image file: %s\n", filename);
294 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
295 fprintf(f,"%i %i\n", imageWidth, imageHeight);
298 /* just to be safe... */
299 glPixelStorei(GL_PACK_ALIGNMENT, 1);
301 /* Because the HUD and Panel change the ViewPort we will
302 * need to handle some lowlevel stuff ourselves */
303 int ncols = trGet(tr, TR_COLUMNS);
304 int nrows = trGet(tr, TR_ROWS);
306 bool do_hud = fgGetBool("/sim/hud/visibility");
307 GLfloat hud_col_step = 640.0 / ncols;
308 GLfloat hud_row_step = 480.0 / nrows;
310 bool do_panel = fgPanelVisible();
311 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
312 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
314 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
315 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
316 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
317 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
318 glHint(GL_FOG_HINT, GL_NICEST);
324 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
325 int curRow = trGet(tr, TR_CURRENT_ROW);
327 renderer->update( false );
330 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
331 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
334 // globals->get_current_panel()->update(
335 // curColumn*panel_col_step, panel_col_step,
336 // curRow*panel_row_step, panel_row_step );
337 more = trEndTile(tr);
339 /* save tile into tile row buffer*/
340 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
341 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
342 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
343 int xOffset = curColumn * bytesPerTileRow;
344 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
346 for (i=0;i<height;i++) {
347 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
348 tile + i*bytesPerTileRow, /* Src */
349 bytesPerCurrentTileRow); /* Byte count*/
352 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
353 /* write this buffered row of tiles to the file */
354 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
355 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
357 for (i=0;i<curTileHeight;i++) {
358 /* Remember, OpenGL images are bottom to top. Have to reverse. */
359 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
360 fwrite(rowPtr, 1, imageWidth*3, f);
366 renderer->resize( width, height );
370 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
371 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
372 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
373 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
374 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
375 (!fgGetBool("/sim/rendering/shading"))) {
376 // if fastest fog requested, or if flat shading force fastest
377 glHint ( GL_FOG_HINT, GL_FASTEST );
378 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
379 glHint ( GL_FOG_HINT, GL_DONT_CARE );
384 message = "Snapshot saved to \"";
387 mkDialog (message.c_str());
394 fgSetMouseCursor(mouse);
395 fgSetBool("/sim/menubar/visibility", menu_status);
398 fgSetBool("/sim/freeze/master", false);
401 #endif // #if defined( TR_HIRES_SNAP)
404 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
406 void rotateView( double roll, double pitch, double yaw )
412 GLubyte *hiResScreenCapture( int multiplier )
414 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
415 float fov = oldfov / multiplier;
416 FGViewer *v = globals->get_current_view();
417 fgSetDouble("/sim/current-view/field-of-view", fov);
418 // globals->get_renderer()->init();
419 int cur_width = fgGetInt("/sim/startup/xsize");
420 int cur_height = fgGetInt("/sim/startup/ysize");
422 // New empty (mostly) bitmap
423 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
425 for ( y = 0; y < multiplier; y++ ) {
426 for ( x = 0; x < multiplier; x++ ) {
427 globals->get_renderer()->resize( cur_width, cur_height );
429 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
430 globals->get_renderer()->update( false );
433 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
436 fgSetDouble("/sim/current-view/field-of-view", oldfov);
437 return b1->getBitmap();
441 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
442 // win32 print screen function
443 void printScreen () {
444 int mouse = fgGetMouseCursor();
445 fgSetMouseCursor(MOUSE_CURSOR_NONE);
447 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
448 int cur_width = fgGetInt("/sim/startup/xsize");
449 int cur_height = fgGetInt("/sim/startup/ysize");
450 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
451 p.End( hiResScreenCapture(3) );
453 fgSetMouseCursor(mouse);
455 #endif // #ifdef WIN32
458 void fgDumpSnapShotWrapper () {
463 void fgHiResDumpWrapper () {
468 // do a screen snap shot
469 bool fgDumpSnapShot () {
470 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
472 bool freeze = master_freeze->getBoolValue();
474 fgSetBool("/sim/freeze/master", true);
477 int mouse = fgGetMouseCursor();
478 fgSetMouseCursor(MOUSE_CURSOR_NONE);
480 fgSetBool("/sim/signals/screenshot", true);
482 FGRenderer *renderer = globals->get_renderer();
483 renderer->resize( fgGetInt("/sim/startup/xsize"),
484 fgGetInt("/sim/startup/ysize") );
486 // we need two render frames here to clear the menu and cursor
487 // ... not sure why but doing an extra fgRenderFrame() shouldn't
489 renderer->update( true );
490 renderer->update( true );
492 string dir = fgGetString("/sim/paths/screenshot-dir");
494 dir = fgGetString("/sim/fg-current");
496 SGPath path(dir + '/');
497 if (path.create_dir( 0755 )) {
498 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
499 << dir << "'. Trying home directory.");
500 dir = fgGetString("/sim/fg-home");
504 static int count = 1;
505 while (count < 1000) {
506 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
516 int result = sg_glDumpWindow(path.c_str(),
517 fgGetInt("/sim/startup/xsize"),
518 fgGetInt("/sim/startup/ysize"));
520 fgSetString("/sim/paths/screenshot-last", path.c_str());
521 fgSetBool("/sim/signals/screenshot", false);
523 fgSetMouseCursor(mouse);
526 fgSetBool("/sim/freeze/master", false);
531 // do an entire scenegraph dump
532 void fgDumpSceneGraph()
534 char *filename = new char [24];
536 static int count = 1;
538 FGRenderer *renderer = globals->get_renderer();
540 static const SGPropertyNode *master_freeze
541 = fgGetNode("/sim/freeze/master");
543 bool freeze = master_freeze->getBoolValue();
545 fgSetBool("/sim/freeze/master", true);
548 while (count < 1000) {
550 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
551 if ( (fp = fopen(filename, "r")) == NULL )
556 if ( fgDumpSceneGraphToFile(filename)) {
557 message = "Entire scene graph saved to \"";
561 message = "Failed to save to \"";
566 mkDialog (message.c_str());
571 fgSetBool("/sim/freeze/master", false);
576 // do an terrain branch dump
577 void fgDumpTerrainBranch()
579 char *filename = new char [24];
581 static int count = 1;
583 FGRenderer *renderer = globals->get_renderer();
585 static const SGPropertyNode *master_freeze
586 = fgGetNode("/sim/freeze/master");
588 bool freeze = master_freeze->getBoolValue();
590 fgSetBool("/sim/freeze/master", true);
593 while (count < 1000) {
595 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
596 if ( (fp = fopen(filename, "r")) == NULL )
601 if ( fgDumpTerrainBranchToFile(filename)) {
602 message = "Terrain graph saved to \"";
606 message = "Failed to save to \"";
611 mkDialog (message.c_str());
616 fgSetBool("/sim/freeze/master", false);