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>
35 #ifdef SG_MATH_EXCEPTION_CLASH
45 #if defined(FX) && defined(XMESA)
46 # include <GL/xmesa.h>
57 # include <shellapi.h>
59 # include <sys/cygwin.h>
63 #include <simgear/constants.h>
64 #include <simgear/debug/logstream.hxx>
65 #include <simgear/misc/sg_path.hxx>
66 #include <simgear/screen/screen-dump.hxx>
68 #include <Include/general.hxx>
69 #include <Aircraft/aircraft.hxx>
70 #include <Aircraft/controls.hxx>
71 #include <Airports/simple.hxx>
72 #include <Cockpit/panel.hxx>
73 #include <FDM/flight.hxx>
74 #include <Main/main.hxx>
75 #include <Main/fg_init.hxx>
76 #include <Main/fg_io.hxx>
77 #include <Main/globals.hxx>
78 #include <Main/fg_props.hxx>
79 #include <Main/renderer.hxx>
80 #include <Main/viewmgr.hxx>
81 #include <GUI/new_gui.hxx>
83 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
84 # include <simgear/screen/win32-printer.h>
85 # include <simgear/screen/GlBitmaps.h>
89 #include "gui_local.hxx"
90 #include "preset_dlg.hxx"
91 #include "sgVec3Slider.hxx"
97 #if defined( TR_HIRES_SNAP)
98 #include <simgear/screen/tr.h>
99 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
100 GLfloat x_end, GLfloat y_end );
104 // TODO: remove after the last hardcoded dialog has died
105 char *gui_msg_OK = "OK";
106 char *gui_msg_NO = "NO";
107 char *gui_msg_YES = "YES";
108 char *gui_msg_CANCEL = "CANCEL";
109 char *gui_msg_RESET = "RESET";
112 const __fg_gui_fn_t __fg_gui_fn[] = {
117 {"dumpHiResSnapShot", fgHiResDumpWrapper},
119 {"dumpSnapShot", fgDumpSnapShotWrapper},
120 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
121 {"printScreen", printScreen},
125 {"guiTogglePanel", guiTogglePanel},
128 {"fgPresetAirport", fgPresetAirport},
129 {"fgPresetRunway", fgPresetRunway},
130 {"fgPresetOffsetDistance", fgPresetOffsetDistance},
131 {"fgPresetAltitude", fgPresetAltitude},
132 {"fgPresetGlideslope", fgPresetGlideslope},
133 {"fgPresetAirspeed", fgPresetAirspeed},
134 {"fgPresetCommit", fgPresetCommit},
139 // Structure termination
144 /* ================ General Purpose Functions ================ */
146 // General Purpose Message Box
147 void mkDialog (const char *txt)
149 NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
152 SGPropertyNode_ptr dlg = gui->getDialogProperties("message");
156 dlg->setStringValue("text/label", txt);
157 dlg->setStringValue("button/legend", "OK");
158 gui->showDialog("message");
161 // Message Box to report an error.
162 void guiErrorMessage (const char *txt)
164 SG_LOG(SG_GENERAL, SG_ALERT, txt);
168 // Message Box to report a throwable (usually an exception).
169 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
173 msg += throwable.getFormattedMessage();
174 if (!throwable.getOrigin().empty()) {
175 msg += "\n (reported by ";
176 msg += throwable.getOrigin();
179 SG_LOG(SG_GENERAL, SG_ALERT, msg);
180 mkDialog(msg.c_str());
185 /* -----------------------------------------------------------------------
186 the Gui callback functions
187 ____________________________________________________________________*/
190 // Hier Neu :-) This is my newly added code
191 // Added by David Findlay <nedz@bigpond.com>
192 // on Sunday 3rd of December
195 // This is the accessor function
196 void guiTogglePanel(puObject *cb)
198 if (fgGetBool("/sim/panel/visibility"))
199 fgSetBool("/sim/panel/visibility", false);
201 fgSetBool("/sim/panel/visibility", true);
203 globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
204 fgGetInt("/sim/startup/ysize"));
207 void helpCb (puObject *)
211 #if defined(FX) && !defined(WIN32)
212 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
213 if ( globals->get_fullscreen() ) {
214 globals->set_fullscreen(false);
215 XMesaSetFXmode( XMESA_FX_WINDOW );
220 SGPath path( globals->get_fg_root() );
221 path.append( "Docs/index.html" );
225 string help_app = fgGetString("/sim/startup/browser-app");
227 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
228 command = help_app + " -remote \"openURL(" + path.str() + ")\"";
230 command = help_app + " " + path.str();
233 system( command.c_str() );
237 // Look for favorite browser
238 char Dummy[1024], ExecName[1024], browserParameter[1024];
239 char win32_name[1024];
241 cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
243 strncpy(win32_name,path.c_str(), 1024);
246 FindExecutable(win32_name, Dummy, ExecName);
247 snprintf(browserParameter, 1024, "file:///%s", win32_name);
248 ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
253 mkDialog ("Help started in your web browser window.");
256 #if defined( TR_HIRES_SNAP)
261 bool show_pu_cursor = false;
262 bool menu_status = fgGetBool("/sim/menubar/visibility");
263 char *filename = new char [24];
264 static int count = 1;
266 static const SGPropertyNode *master_freeze
267 = fgGetNode("/sim/freeze/master");
269 bool freeze = master_freeze->getBoolValue();
271 fgSetBool("/sim/freeze/master", true);
274 fgSetBool("/sim/menubar/visibility", false);
276 if ( !puCursorIsHidden() ) {
277 show_pu_cursor = true;
281 FGRenderer *renderer = globals->get_renderer();
283 renderer->resize( fgGetInt("/sim/startup/xsize"),
284 fgGetInt("/sim/startup/ysize") );
286 // we need two render frames here to clear the menu and cursor
287 // ... not sure why but doing an extra fgRenderFrame() shouldn't
289 //renderer->update( true );
290 //renderer->update( true );
292 // This ImageSize stuff is a temporary hack
293 // should probably use 128x128 tile size and
294 // support any image size
296 // This should be a requester to get multiplier from user
297 int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
298 int width = fgGetInt("/sim/startup/xsize");
299 int height = fgGetInt("/sim/startup/ysize");
301 /* allocate buffer large enough to store one tile */
302 GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
304 printf("Malloc of tile buffer failed!\n");
308 int imageWidth = multiplier*width;
309 int imageHeight = multiplier*height;
311 /* allocate buffer to hold a row of tiles */
313 = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
316 printf("Malloc of tile row buffer failed!\n");
319 TRcontext *tr = trNew();
320 trTileSize(tr, width, height, 0);
321 trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
322 trImageSize(tr, imageWidth, imageHeight);
323 trRowOrder(tr, TR_TOP_TO_BOTTOM);
325 // sgFrustum *frustum = ssgGetFrustum();
327 // frustum->getLeft(), frustum->getRight(),
328 // frustum->getBot(), frustum->getTop(),
329 // frustum->getNear(), frustum->getFar());
331 /* Prepare ppm output file */
332 while (count < 1000) {
333 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
334 if ( (f = fopen(filename, "r")) == NULL )
338 f = fopen(filename, "wb");
340 printf("Couldn't open image file: %s\n", filename);
346 fprintf(f,"# ppm-file created by %s\n", "trdemo2");
347 fprintf(f,"%i %i\n", imageWidth, imageHeight);
350 /* just to be safe... */
351 glPixelStorei(GL_PACK_ALIGNMENT, 1);
353 /* Because the HUD and Panel change the ViewPort we will
354 * need to handle some lowlevel stuff ourselves */
355 int ncols = trGet(tr, TR_COLUMNS);
356 int nrows = trGet(tr, TR_ROWS);
358 bool do_hud = fgGetBool("/sim/hud/visibility");
359 GLfloat hud_col_step = 640.0 / ncols;
360 GLfloat hud_row_step = 480.0 / nrows;
362 bool do_panel = fgPanelVisible();
363 GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
364 GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
366 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
367 glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
368 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
369 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
370 glHint(GL_FOG_HINT, GL_NICEST);
376 int curColumn = trGet(tr, TR_CURRENT_COLUMN);
377 int curRow = trGet(tr, TR_CURRENT_ROW);
379 renderer->update( false );
382 // fgUpdateHUD( curColumn*hud_col_step, curRow*hud_row_step,
383 // (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
386 // globals->get_current_panel()->update(
387 // curColumn*panel_col_step, panel_col_step,
388 // curRow*panel_row_step, panel_row_step );
389 more = trEndTile(tr);
391 /* save tile into tile row buffer*/
392 int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
393 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
394 int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
395 int xOffset = curColumn * bytesPerTileRow;
396 int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
398 for (i=0;i<height;i++) {
399 memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
400 tile + i*bytesPerTileRow, /* Src */
401 bytesPerCurrentTileRow); /* Byte count*/
404 if (curColumn == trGet(tr, TR_COLUMNS)-1) {
405 /* write this buffered row of tiles to the file */
406 int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
407 int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
409 for (i=0;i<curTileHeight;i++) {
410 /* Remember, OpenGL images are bottom to top. Have to reverse. */
411 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
412 fwrite(rowPtr, 1, imageWidth*3, f);
418 renderer->resize( width, height );
422 glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
423 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
424 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
425 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
426 if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
427 (!fgGetBool("/sim/rendering/shading"))) {
428 // if fastest fog requested, or if flat shading force fastest
429 glHint ( GL_FOG_HINT, GL_FASTEST );
430 } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
431 glHint ( GL_FOG_HINT, GL_DONT_CARE );
436 message = "Snapshot saved to \"";
439 mkDialog (message.c_str());
446 if ( show_pu_cursor ) {
451 fgSetBool("/sim/menubar/visibility", menu_status);
454 fgSetBool("/sim/freeze/master", false);
457 #endif // #if defined( TR_HIRES_SNAP)
460 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
462 void rotateView( double roll, double pitch, double yaw )
468 GLubyte *hiResScreenCapture( int multiplier )
470 float oldfov = fgGetDouble("/sim/current-view/field-of-view");
471 float fov = oldfov / multiplier;
472 FGViewer *v = globals->get_current_view();
473 fgSetDouble("/sim/current-view/field-of-view", fov);
474 // globals->get_renderer()->init();
475 int cur_width = fgGetInt("/sim/startup/xsize");
476 int cur_height = fgGetInt("/sim/startup/ysize");
478 // New empty (mostly) bitmap
479 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
481 for ( y = 0; y < multiplier; y++ ) {
482 for ( x = 0; x < multiplier; x++ ) {
483 globals->get_renderer()->resize( cur_width, cur_height );
485 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
486 globals->get_renderer()->update( false );
489 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
492 fgSetDouble("/sim/current-view/field-of-view", oldfov);
493 return b1->getBitmap();
497 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
498 // win32 print screen function
499 void printScreen ( puObject *obj ) {
500 bool show_pu_cursor = false;
502 if ( !puCursorIsHidden() ) {
503 show_pu_cursor = true;
508 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
509 int cur_width = fgGetInt("/sim/startup/xsize");
510 int cur_height = fgGetInt("/sim/startup/ysize");
511 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
512 p.End( hiResScreenCapture(3) );
515 if ( show_pu_cursor ) {
520 #endif // #ifdef WIN32
523 void fgDumpSnapShotWrapper ( puObject *obj ) {
528 void fgHiResDumpWrapper ( puObject *obj ) {
533 // do a screen snap shot
534 void fgDumpSnapShot () {
535 bool show_pu_cursor = false;
536 char *filename = new char [24];
538 static int count = 1;
540 static const SGPropertyNode *master_freeze
541 = fgGetNode("/sim/freeze/master");
543 bool freeze = master_freeze->getBoolValue();
545 fgSetBool("/sim/freeze/master", true);
549 if ( !puCursorIsHidden() ) {
550 show_pu_cursor = true;
553 fgSetBool("/sim/signals/screenshot", true);
555 FGRenderer *renderer = globals->get_renderer();
557 renderer->resize( fgGetInt("/sim/startup/xsize"),
558 fgGetInt("/sim/startup/ysize") );
560 // we need two render frames here to clear the menu and cursor
561 // ... not sure why but doing an extra fgRenderFrame() shouldn't
563 renderer->update( true );
564 renderer->update( true );
566 while (count < 1000) {
568 snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
569 if ( (fp = fopen(filename, "r")) == NULL )
574 if ( sg_glDumpWindow( filename,
575 fgGetInt("/sim/startup/xsize"),
576 fgGetInt("/sim/startup/ysize")) ) {
577 message = "Snapshot saved to \"";
581 message = "Failed to save to \"";
586 fgSetBool("/sim/signals/screenshot", false);
587 mkDialog (message.c_str());
591 if ( show_pu_cursor ) {
598 fgSetBool("/sim/freeze/master", false);