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>
41 #if defined(FX) && defined(XMESA)
42 # include <GL/xmesa.h>
53 # include <shellapi.h>
55 # include <sys/cygwin.h>
61 #include <simgear/constants.h>
62 #include <simgear/debug/logstream.hxx>
63 #include <simgear/misc/sg_path.hxx>
64 #include <simgear/screen/screen-dump.hxx>
66 #include <Include/general.hxx>
67 #include <Aircraft/aircraft.hxx>
68 #include <Aircraft/controls.hxx>
69 #include <Airports/simple.hxx>
70 #include <Cockpit/panel.hxx>
71 #include <FDM/flight.hxx>
72 #include <Main/main.hxx>
73 #include <Main/fg_init.hxx>
74 #include <Main/fg_io.hxx>
75 #include <Main/globals.hxx>
76 #include <Main/fg_props.hxx>
77 #include <Main/renderer.hxx>
78 #include <Main/viewmgr.hxx>
79 #include <GUI/new_gui.hxx>
81 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
82 # include <simgear/screen/win32-printer.h>
83 # include <simgear/screen/GlBitmaps.h>
92 #if defined( TR_HIRES_SNAP)
93 #include <simgear/screen/tr.h>
94 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
95 GLfloat x_end, GLfloat y_end );
99 const __fg_gui_fn_t __fg_gui_fn[] = {
101 {"dumpHiResSnapShot", fgHiResDumpWrapper},
103 {"dumpSnapShot", fgDumpSnapShotWrapper},
104 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
105 {"printScreen", printScreen},
110 // Structure termination
115 /* ================ General Purpose Functions ================ */
117 // General Purpose Message Box. Makes sure no more than 5 different
118 // messages are displayed at the same time, and none of them are
119 // duplicates. (5 is a *lot*, but this will hardly ever be reached
120 // and we don't want to miss any, either.)
121 void mkDialog (const char *txt)
123 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
126 SGPropertyNode *master = gui->getDialogProperties("message");
130 const int maxdialogs = 5;
132 SGPropertyNode *msg = fgGetNode("/sim/gui/dialogs", true);
134 for (i = 0; i < maxdialogs; i++) {
135 std::ostringstream s;
136 s << "message-" << i;
139 if (!msg->getNode(name.c_str(), false))
142 if (!strcmp(txt, msg->getNode(name.c_str())->getStringValue("message"))) {
143 SG_LOG(SG_GENERAL, SG_WARN, "mkDialog(): duplicate of message " << txt);
149 msg = msg->getNode(name.c_str(), true);
150 msg->setStringValue("message", txt);
151 msg = msg->getNode("dialog", true);
152 copyProperties(master, msg);
153 msg->setStringValue("name", name.c_str());
155 gui->showDialog(name.c_str());
158 // Message Box to report an error.
159 void guiErrorMessage (const char *txt)
161 SG_LOG(SG_GENERAL, SG_ALERT, txt);
165 // Message Box to report a throwable (usually an exception).
166 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
170 msg += throwable.getFormattedMessage();
171 if (!throwable.getOrigin().empty()) {
172 msg += "\n (reported by ";
173 msg += throwable.getOrigin();
176 SG_LOG(SG_GENERAL, SG_ALERT, msg);
177 mkDialog(msg.c_str());
182 /* -----------------------------------------------------------------------
183 the Gui callback functions
184 ____________________________________________________________________*/
187 // Hier Neu :-) This is my newly added code
188 // Added by David Findlay <nedz@bigpond.com>
189 // on Sunday 3rd of December
196 #if defined(FX) && !defined(WIN32)
197 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
198 if ( globals->get_fullscreen() ) {
199 globals->set_fullscreen(false);
200 XMesaSetFXmode( XMESA_FX_WINDOW );
205 SGPath path( globals->get_fg_root() );
206 path.append( "Docs/index.html" );
210 command = globals->get_browser();
211 string::size_type pos;
212 if ((pos = command.find("%u", 0)) != string::npos)
213 command.replace(pos, 2, path.str());
215 command += " " + path.str();
218 system( command.c_str() );
222 // Look for favorite browser
223 char Dummy[1024], ExecName[1024], browserParameter[1024];
224 char win32_name[1024];
226 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
228 strncpy(win32_name,path.c_str(), 1024);
231 FindExecutable(win32_name, Dummy, ExecName);
232 snprintf(browserParameter, 1024, "file:///%s", win32_name);
233 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
238 mkDialog ("Help started in your web browser window.");
241 #if defined( TR_HIRES_SNAP)
246 bool show_pu_cursor = false;
247 bool menu_status = fgGetBool("/sim/menubar/visibility");
248 char *filename = new char [24];
249 static int count = 1;
251 static const SGPropertyNode *master_freeze
252 = fgGetNode("/sim/freeze/master");
254 bool freeze = master_freeze->getBoolValue();
256 fgSetBool("/sim/freeze/master", true);
259 fgSetBool("/sim/menubar/visibility", false);
261 if ( !puCursorIsHidden() ) {
262 show_pu_cursor = true;
266 FGRenderer *renderer = globals->get_renderer();
268 renderer->resize( fgGetInt("/sim/startup/xsize"),
269 fgGetInt("/sim/startup/ysize") );
271 // we need two render frames here to clear the menu and cursor
272 // ... not sure why but doing an extra fgRenderFrame() shouldn't
274 //renderer->update( true );
275 //renderer->update( true );
277 // This ImageSize stuff is a temporary hack
278 // should probably use 128x128 tile size and
279 // support any image size
281 // This should be a requester to get multiplier from user
282 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
283 int width = fgGetInt("/sim/startup/xsize");
284 int height = fgGetInt("/sim/startup/ysize");
286 /* allocate buffer large enough to store one tile */
287 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
289 printf("Malloc of tile buffer failed!\n");
293 int imageWidth = multiplier*width;
294 int imageHeight = multiplier*height;
296 /* allocate buffer to hold a row of tiles */
298 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
301 printf("Malloc of tile row buffer failed!\n");
304 TRcontext *tr = trNew();
305 trTileSize(tr, width, height, 0);
306 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
307 trImageSize(tr, imageWidth, imageHeight);
308 trRowOrder(tr, TR_TOP_TO_BOTTOM);
310 // sgFrustum *frustum = ssgGetFrustum();
312 // frustum->getLeft(), frustum->getRight(),
313 // frustum->getBot(), frustum->getTop(),
314 // frustum->getNear(), frustum->getFar());
316 /* Prepare ppm output file */
317 while (count < 1000) {
318 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
319 if ( (f = fopen(filename, "r")) == NULL )
323 f = fopen(filename, "wb");
325 printf("Couldn't open image file: %s\n", filename);
331 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
332 fprintf(f,"%i %i\n", imageWidth, imageHeight);
335 /* just to be safe... */
336 glPixelStorei(GL_PACK_ALIGNMENT, 1);
338 /* Because the HUD and Panel change the ViewPort we will
339 * need to handle some lowlevel stuff ourselves */
340 int ncols = trGet(tr, TR_COLUMNS);
341 int nrows = trGet(tr, TR_ROWS);
343 bool do_hud = fgGetBool("/sim/hud/visibility");
344 GLfloat hud_col_step = 640.0 / ncols;
345 GLfloat hud_row_step = 480.0 / nrows;
347 bool do_panel = fgPanelVisible();
348 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
349 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
351 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
352 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
353 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
354 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
355 glHint(GL_FOG_HINT, GL_NICEST);
361 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
362 int curRow = trGet(tr, TR_CURRENT_ROW);
364 renderer->update( false );
367 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
368 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
371 // globals->get_current_panel()->update(
372 // curColumn*panel_col_step, panel_col_step,
373 // curRow*panel_row_step, panel_row_step );
374 more = trEndTile(tr);
376 /* save tile into tile row buffer*/
377 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
378 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
379 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
380 int xOffset = curColumn * bytesPerTileRow;
381 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
383 for (i=0;i<height;i++) {
384 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
385 tile + i*bytesPerTileRow, /* Src */
386 bytesPerCurrentTileRow); /* Byte count*/
389 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
390 /* write this buffered row of tiles to the file */
391 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
392 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
394 for (i=0;i<curTileHeight;i++) {
395 /* Remember, OpenGL images are bottom to top. Have to reverse. */
396 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
397 fwrite(rowPtr, 1, imageWidth*3, f);
403 renderer->resize( width, height );
407 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
408 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
409 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
410 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
411 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
412 (!fgGetBool("/sim/rendering/shading"))) {
413 // if fastest fog requested, or if flat shading force fastest
414 glHint ( GL_FOG_HINT, GL_FASTEST );
415 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
416 glHint ( GL_FOG_HINT, GL_DONT_CARE );
421 message = "Snapshot saved to \"";
424 mkDialog (message.c_str());
431 if ( show_pu_cursor ) {
436 fgSetBool("/sim/menubar/visibility", menu_status);
439 fgSetBool("/sim/freeze/master", false);
442 #endif // #if defined( TR_HIRES_SNAP)
445 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
447 void rotateView( double roll, double pitch, double yaw )
453 GLubyte *hiResScreenCapture( int multiplier )
455 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
456 float fov = oldfov / multiplier;
457 FGViewer *v = globals->get_current_view();
458 fgSetDouble("/sim/current-view/field-of-view", fov);
459 // globals->get_renderer()->init();
460 int cur_width = fgGetInt("/sim/startup/xsize");
461 int cur_height = fgGetInt("/sim/startup/ysize");
463 // New empty (mostly) bitmap
464 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
466 for ( y = 0; y < multiplier; y++ ) {
467 for ( x = 0; x < multiplier; x++ ) {
468 globals->get_renderer()->resize( cur_width, cur_height );
470 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
471 globals->get_renderer()->update( false );
474 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
477 fgSetDouble("/sim/current-view/field-of-view", oldfov);
478 return b1->getBitmap();
482 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
483 // win32 print screen function
484 void printScreen () {
485 bool show_pu_cursor = false;
487 if ( !puCursorIsHidden() ) {
488 show_pu_cursor = true;
492 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
493 int cur_width = fgGetInt("/sim/startup/xsize");
494 int cur_height = fgGetInt("/sim/startup/ysize");
495 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
496 p.End( hiResScreenCapture(3) );
498 if ( show_pu_cursor ) {
503 #endif // #ifdef WIN32
506 void fgDumpSnapShotWrapper () {
511 void fgHiResDumpWrapper () {
516 // do a screen snap shot
517 bool fgDumpSnapShot () {
518 bool show_pu_cursor = false;
519 static SGConstPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master");
521 bool freeze = master_freeze->getBoolValue();
523 fgSetBool("/sim/freeze/master", true);
527 if ( !puCursorIsHidden() ) {
528 show_pu_cursor = true;
531 fgSetBool("/sim/signals/screenshot", true);
533 FGRenderer *renderer = globals->get_renderer();
534 renderer->resize( fgGetInt("/sim/startup/xsize"),
535 fgGetInt("/sim/startup/ysize") );
537 // we need two render frames here to clear the menu and cursor
538 // ... not sure why but doing an extra fgRenderFrame() shouldn't
540 renderer->update( true );
541 renderer->update( true );
543 string dir = fgGetString("/sim/paths/screenshot-dir", fgGetString("/sim/fg-current"));
544 SGPath path(dir + '/');
545 if (path.create_dir( 0755 )) {
546 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot create screenshot directory '"
547 << dir << "'. Trying home directory.");
548 dir = fgGetString("/sim/fg-home");
552 static int count = 1;
553 while (count < 1000) {
554 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
564 int result = sg_glDumpWindow(path.c_str(),
565 fgGetInt("/sim/startup/xsize"),
566 fgGetInt("/sim/startup/ysize"));
568 fgSetString("/sim/paths/screenshot-last", path.c_str());
569 fgSetBool("/sim/signals/screenshot", false);
571 if ( show_pu_cursor ) {
578 fgSetBool("/sim/freeze/master", false);
583 // do an entire scenegraph dump
584 void fgDumpSceneGraph()
586 char *filename = new char [24];
588 static int count = 1;
590 FGRenderer *renderer = globals->get_renderer();
592 static const SGPropertyNode *master_freeze
593 = fgGetNode("/sim/freeze/master");
595 bool freeze = master_freeze->getBoolValue();
597 fgSetBool("/sim/freeze/master", true);
600 while (count < 1000) {
602 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
603 if ( (fp = fopen(filename, "r")) == NULL )
608 if ( fgDumpSceneGraphToFile(filename)) {
609 message = "Entire scene graph saved to \"";
613 message = "Failed to save to \"";
618 mkDialog (message.c_str());
623 fgSetBool("/sim/freeze/master", false);
628 // do an terrain branch dump
629 void fgDumpTerrainBranch()
631 char *filename = new char [24];
633 static int count = 1;
635 FGRenderer *renderer = globals->get_renderer();
637 static const SGPropertyNode *master_freeze
638 = fgGetNode("/sim/freeze/master");
640 bool freeze = master_freeze->getBoolValue();
642 fgSetBool("/sim/freeze/master", true);
645 while (count < 1000) {
647 snprintf(filename, 24, "fgfs-graph-%03d.osg", count++);
648 if ( (fp = fopen(filename, "r")) == NULL )
653 if ( fgDumpTerrainBranchToFile(filename)) {
654 message = "Terrain graph saved to \"";
658 message = "Failed to save to \"";
663 mkDialog (message.c_str());
668 fgSetBool("/sim/freeze/master", false);