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>
55 # include <shellapi.h>
56 # if !defined(__MINGW32__)
57 # include <simgear/screen/win32-printer.h>
58 # 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(__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);
304 /* Because the HUD and Panel change the ViewPort we will
305 * need to handle some lowlevel stuff ourselves */
306 int ncols = trGet(tr, TR_COLUMNS);
307 int nrows = trGet(tr, TR_ROWS);
309 bool do_hud = fgGetBool("/sim/hud/visibility");
310 GLfloat hud_col_step = 640.0 / ncols;
311 GLfloat hud_row_step = 480.0 / nrows;
313 bool do_panel = fgPanelVisible();
314 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
315 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
317 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
318 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
319 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
320 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
321 glHint(GL_FOG_HINT, GL_NICEST);
327 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
328 // int curRow = trGet(tr, TR_CURRENT_ROW);
330 renderer->update( false );
333 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
334 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
337 // globals->get_current_panel()->update(
338 // curColumn*panel_col_step, panel_col_step,
339 // curRow*panel_row_step, panel_row_step );
340 more = trEndTile(tr);
342 /* save tile into tile row buffer*/
343 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
344 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
345 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
346 int xOffset = curColumn * bytesPerTileRow;
347 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
349 for (i=0;i<height;i++) {
350 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
351 tile + i*bytesPerTileRow, /* Src */
352 bytesPerCurrentTileRow); /* Byte count*/
355 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
356 /* write this buffered row of tiles to the file */
357 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
358 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
360 for (i=0;i<curTileHeight;i++) {
361 /* Remember, OpenGL images are bottom to top. Have to reverse. */
362 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
363 fwrite(rowPtr, 1, imageWidth*3, f);
369 renderer->resize( width, height );
373 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
374 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
375 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
376 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
377 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
378 (!fgGetBool("/sim/rendering/shading"))) {
379 // if fastest fog requested, or if flat shading force fastest
380 glHint ( GL_FOG_HINT, GL_FASTEST );
381 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
382 glHint ( GL_FOG_HINT, GL_DONT_CARE );
387 message = "Snapshot saved to \"";
390 mkDialog (message.c_str());
397 fgSetMouseCursor(mouse);
398 fgSetBool("/sim/menubar/visibility", menu_status);
401 fgSetBool("/sim/freeze/master", false);
404 #endif // #if defined( TR_HIRES_SNAP)
407 #if defined( _WIN32 ) && !defined(__MINGW32__)
409 void rotateView( double roll, double pitch, double yaw )
415 GLubyte *hiResScreenCapture( int multiplier )
417 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
418 float fov = oldfov / multiplier;
419 FGViewer *v = globals->get_current_view();
420 fgSetDouble("/sim/current-view/field-of-view", fov);
421 // globals->get_renderer()->init();
422 int cur_width = fgGetInt("/sim/startup/xsize");
423 int cur_height = fgGetInt("/sim/startup/ysize");
425 // New empty (mostly) bitmap
426 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
428 for ( y = 0; y < multiplier; y++ ) {
429 for ( x = 0; x < multiplier; x++ ) {
430 globals->get_renderer()->resize( cur_width, cur_height );
432 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
433 globals->get_renderer()->update( false );
436 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
439 fgSetDouble("/sim/current-view/field-of-view", oldfov);
440 return b1->getBitmap();
444 #if defined( _WIN32 ) && !defined(__MINGW32__)
445 // win32 print screen function
446 void printScreen () {
447 int mouse = fgGetMouseCursor();
448 fgSetMouseCursor(MOUSE_CURSOR_NONE);
450 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
451 int cur_width = fgGetInt("/sim/startup/xsize");
452 int cur_height = fgGetInt("/sim/startup/ysize");
453 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
454 p.End( hiResScreenCapture(3) );
456 fgSetMouseCursor(mouse);
458 #endif // #ifdef _WIN32
461 void fgDumpSnapShotWrapper () {
466 void fgHiResDumpWrapper () {
471 // do a screen snap shot
472 bool fgDumpSnapShot () {
473 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
475 bool freeze = master_freeze->getBoolValue();
477 fgSetBool("/sim/freeze/master", true);
480 int mouse = fgGetMouseCursor();
481 fgSetMouseCursor(MOUSE_CURSOR_NONE);
483 fgSetBool("/sim/signals/screenshot", true);
485 FGRenderer *renderer = globals->get_renderer();
486 renderer->resize( fgGetInt("/sim/startup/xsize"),
487 fgGetInt("/sim/startup/ysize") );
489 // we need two render frames here to clear the menu and cursor
490 // ... not sure why but doing an extra fgRenderFrame() shouldn't
492 renderer->update( true );
493 renderer->update( true );
495 string dir = fgGetString("/sim/paths/screenshot-dir");
497 dir = fgGetString("/sim/fg-current");
499 SGPath path(dir + '/');
500 if (path.create_dir( 0755 )) {
501 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
502 << dir << "'. Trying home directory.");
503 dir = fgGetString("/sim/fg-home");
507 static int count = 1;
508 while (count < 1000) {
509 snprintf(filename, 24, "fgfs-screen-%03d.png", count++);
519 int result = sg_glDumpWindow(path.c_str(),
520 fgGetInt("/sim/startup/xsize"),
521 fgGetInt("/sim/startup/ysize"));
523 fgSetString("/sim/paths/screenshot-last", path.c_str());
524 fgSetBool("/sim/signals/screenshot", false);
526 fgSetMouseCursor(mouse);
529 fgSetBool("/sim/freeze/master", false);
534 // do an entire scenegraph dump
535 void fgDumpSceneGraph()
537 char *filename = new char [24];
539 static int count = 1;
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 static const SGPropertyNode *master_freeze
585 = fgGetNode("/sim/freeze/master");
587 bool freeze = master_freeze->getBoolValue();
589 fgSetBool("/sim/freeze/master", true);
592 while (count < 1000) {
594 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
595 if ( (fp = fopen(filename, "r")) == NULL )
600 if ( fgDumpTerrainBranchToFile(filename)) {
601 message = "Terrain graph saved to \"";
605 message = "Failed to save to \"";
610 mkDialog (message.c_str());
615 fgSetBool("/sim/freeze/master", false);