]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui_funcs.cxx
add "signals" for start and end of screenshot capturing. Like the other
[flightgear.git] / src / GUI / gui_funcs.cxx
1 /**************************************************************************
2  * gui_funcs.cxx
3  *
4  * Based on gui.cxx and renamed on 2002/08/13 by Erik Hofman.
5  *
6  * Written 1998 by Durk Talsma, started Juni, 1998.  For the flight gear
7  * project.
8  *
9  * Additional mouse supported added by David Megginson, 1999.
10  *
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.
15  *
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.
20  *
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.
24  *
25  * $Id$
26  **************************************************************************/
27
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include <simgear/compiler.h>
34
35 #ifdef SG_MATH_EXCEPTION_CLASH
36 #  include <math.h>
37 #endif
38
39 #ifdef HAVE_WINDOWS_H
40 #  include <windows.h>
41 #endif
42
43 #include SG_GL_H
44
45 #if defined(FX) && defined(XMESA)
46 #  include <GL/xmesa.h>
47 #endif
48
49 #include STL_FSTREAM
50 #include STL_STRING
51
52 #include <stdlib.h>
53 #include <string.h>
54
55 // for help call back
56 #ifdef WIN32
57 # include <shellapi.h>
58 # ifdef __CYGWIN__
59 #  include <sys/cygwin.h>
60 # endif
61 #endif
62
63 #include <plib/ssg.h>
64
65 #include <simgear/constants.h>
66 #include <simgear/debug/logstream.hxx>
67 #include <simgear/misc/sg_path.hxx>
68 #include <simgear/screen/screen-dump.hxx>
69
70 #include <Include/general.hxx>
71 #include <Aircraft/aircraft.hxx>
72 #include <Aircraft/controls.hxx>
73 #include <Airports/simple.hxx>
74 #include <Cockpit/panel.hxx>
75 #include <FDM/flight.hxx>
76 #include <Main/main.hxx>
77 #include <Main/fg_init.hxx>
78 #include <Main/fg_io.hxx>
79 #include <Main/globals.hxx>
80 #include <Main/fg_props.hxx>
81 #include <Main/renderer.hxx>
82 #include <Main/viewmgr.hxx>
83 #include <GUI/new_gui.hxx>
84
85 #if defined( WIN32 ) && !defined( __CYGWIN__ ) && !defined(__MINGW32__)
86 #  include <simgear/screen/win32-printer.h>
87 #  include <simgear/screen/GlBitmaps.h>
88 #endif
89
90 #include "gui.h"
91 #include "gui_local.hxx"
92 #include "preset_dlg.hxx"
93 #include "prop_picker.hxx"
94 #include "sgVec3Slider.hxx"
95
96 SG_USING_STD(string);
97 SG_USING_STD(cout);
98
99 // from cockpit.cxx
100 extern void fgLatLonFormatToggle( puObject *);
101
102 #if defined( TR_HIRES_SNAP)
103 #include <simgear/screen/tr.h>
104 extern void fgUpdateHUD( GLfloat x_start, GLfloat y_start,
105                          GLfloat x_end, GLfloat y_end );
106 #endif
107
108
109 // TODO: remove after the last hardcoded dialog has died
110 char *gui_msg_OK     = "OK";
111 char *gui_msg_NO     = "NO";
112 char *gui_msg_YES    = "YES";
113 char *gui_msg_CANCEL = "CANCEL";
114 char *gui_msg_RESET  = "RESET";
115
116
117 const __fg_gui_fn_t __fg_gui_fn[] = {
118
119         // File
120         {"reInit", reInit},
121 #ifdef TR_HIRES_SNAP
122         {"dumpHiResSnapShot", fgHiResDumpWrapper},
123 #endif
124         {"dumpSnapShot", fgDumpSnapShotWrapper},
125 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
126         {"printScreen", printScreen},
127 #endif
128
129         //View
130         {"guiTogglePanel", guiTogglePanel},
131         {"prop_pickerView", prop_pickerView},
132
133         // Environment
134         {"fgPresetAirport", fgPresetAirport},
135         {"fgPresetRunway", fgPresetRunway},
136         {"fgPresetOffsetDistance", fgPresetOffsetDistance},
137         {"fgPresetAltitude", fgPresetAltitude},
138         {"fgPresetGlideslope", fgPresetGlideslope},
139         {"fgPresetAirspeed", fgPresetAirspeed},
140         {"fgPresetCommit", fgPresetCommit},
141         {"fgLatLonFormatToggle", fgLatLonFormatToggle},
142
143         // Help
144         {"helpCb", helpCb},
145
146         // Structure termination
147         {"", NULL}
148 };
149
150
151 /* ================ General Purpose Functions ================ */
152
153 // General Purpose Message Box
154 void mkDialog (const char *txt)
155 {
156     NewGUI *gui = (NewGUI *)globals->get_subsystem("gui");
157     if (!gui)
158         return;
159     SGPropertyNode_ptr dlg = gui->getDialogProperties("message");
160     if (!dlg)
161         return;
162
163     dlg->setStringValue("text/label", txt);
164     dlg->setStringValue("button/legend", "OK");
165     gui->showDialog("message");
166 }
167
168 // Message Box to report an error.
169 void guiErrorMessage (const char *txt)
170 {
171     SG_LOG(SG_GENERAL, SG_ALERT, txt);
172     mkDialog(txt);
173 }
174
175 // Message Box to report a throwable (usually an exception).
176 void guiErrorMessage (const char *txt, const sg_throwable &throwable)
177 {
178     string msg = txt;
179     msg += '\n';
180     msg += throwable.getFormattedMessage();
181     if (!throwable.getOrigin().empty()) {
182       msg += "\n (reported by ";
183       msg += throwable.getOrigin();
184       msg += ')';
185     }
186     SG_LOG(SG_GENERAL, SG_ALERT, msg);
187     mkDialog(msg.c_str());
188 }
189
190
191
192 /* -----------------------------------------------------------------------
193 the Gui callback functions 
194 ____________________________________________________________________*/
195
196
197 // Hier Neu :-) This is my newly added code
198 // Added by David Findlay <nedz@bigpond.com>
199 // on Sunday 3rd of December
200
201
202 // This is the accessor function
203 void guiTogglePanel(puObject *cb)
204 {
205   if (fgGetBool("/sim/panel/visibility"))
206     fgSetBool("/sim/panel/visibility", false);
207   else
208     fgSetBool("/sim/panel/visibility", true);
209
210   globals->get_renderer()->resize(fgGetInt("/sim/startup/xsize"),
211                                   fgGetInt("/sim/startup/ysize"));
212 }
213
214 void helpCb (puObject *)
215 {
216     string command;
217         
218 #if defined(FX) && !defined(WIN32)
219 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
220     if ( globals->get_fullscreen() ) {
221         globals->set_fullscreen(false);
222         XMesaSetFXmode( XMESA_FX_WINDOW );
223     }
224 #  endif
225 #endif
226         
227     SGPath path( globals->get_fg_root() );
228     path.append( "Docs/index.html" );
229         
230 #if !defined(WIN32)
231
232     string help_app = fgGetString("/sim/startup/browser-app");
233
234     if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
235         command = help_app + " -remote \"openURL(" + path.str() + ")\"";
236     } else {
237         command = help_app + " " + path.str();
238     }
239     command += " &";
240     system( command.c_str() );
241
242 #else // WIN32
243
244     // Look for favorite browser
245     char Dummy[1024], ExecName[1024], browserParameter[1024];
246     char win32_name[1024];
247 # ifdef __CYGWIN__
248     cygwin32_conv_to_full_win32_path(path.c_str(),win32_name);
249 # else
250     strncpy(win32_name,path.c_str(), 1024);
251 # endif
252     Dummy[0] = 0;
253     FindExecutable(win32_name, Dummy, ExecName);
254     snprintf(browserParameter, 1024, "file:///%s", win32_name);
255     ShellExecute ( NULL, "open", ExecName, browserParameter, Dummy,
256                    SW_SHOWNORMAL ) ;
257
258 #endif
259         
260     mkDialog ("Help started in your web browser window.");
261 }
262
263 #if defined( TR_HIRES_SNAP)
264 void fgHiResDump()
265 {
266     FILE *f;
267     string message;
268     bool show_pu_cursor = false;
269     bool menu_status = fgGetBool("/sim/menubar/visibility");
270     char *filename = new char [24];
271     static int count = 1;
272
273     static const SGPropertyNode *master_freeze
274         = fgGetNode("/sim/freeze/master");
275
276     bool freeze = master_freeze->getBoolValue();
277     if ( !freeze ) {
278         fgSetBool("/sim/freeze/master", true);
279     }
280
281     fgSetBool("/sim/menubar/visibility", false);
282     TurnCursorOff();
283     if ( !puCursorIsHidden() ) {
284         show_pu_cursor = true;
285         puHideCursor();
286     }
287
288     FGRenderer *renderer = globals->get_renderer();
289     renderer->init();
290     renderer->resize( fgGetInt("/sim/startup/xsize"),
291                       fgGetInt("/sim/startup/ysize") );
292
293     // we need two render frames here to clear the menu and cursor
294     // ... not sure why but doing an extra fgRenderFrame() shouldn't
295     // hurt anything
296     //renderer->update( true );
297     //renderer->update( true );
298
299     // This ImageSize stuff is a temporary hack
300     // should probably use 128x128 tile size and
301     // support any image size
302
303     // This should be a requester to get multiplier from user
304     int multiplier = fgGetInt("/sim/startup/hires-multiplier", 3);
305     int width = fgGetInt("/sim/startup/xsize");
306     int height = fgGetInt("/sim/startup/ysize");
307         
308     /* allocate buffer large enough to store one tile */
309     GLubyte *tile = (GLubyte *)malloc(width * height * 3 * sizeof(GLubyte));
310     if (!tile) {
311         printf("Malloc of tile buffer failed!\n");
312         return;
313     }
314
315     int imageWidth  = multiplier*width;
316     int imageHeight = multiplier*height;
317
318     /* allocate buffer to hold a row of tiles */
319     GLubyte *buffer
320         = (GLubyte *)malloc(imageWidth * height * 3 * sizeof(GLubyte));
321     if (!buffer) {
322         free(tile);
323         printf("Malloc of tile row buffer failed!\n");
324         return;
325     }
326     TRcontext *tr = trNew();
327     trTileSize(tr, width, height, 0);
328     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
329     trImageSize(tr, imageWidth, imageHeight);
330     trRowOrder(tr, TR_TOP_TO_BOTTOM);
331     sgFrustum *frustum = ssgGetFrustum();
332     trFrustum(tr,
333               frustum->getLeft(), frustum->getRight(),
334               frustum->getBot(),  frustum->getTop(), 
335               frustum->getNear(), frustum->getFar());
336         
337     /* Prepare ppm output file */
338     while (count < 1000) {
339         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
340         if ( (f = fopen(filename, "r")) == NULL )
341             break;
342         fclose(f);
343     }
344     f = fopen(filename, "wb");
345     if (!f) {
346         printf("Couldn't open image file: %s\n", filename);
347         free(buffer);
348         free(tile);
349         return;
350     }
351     fprintf(f,"P6\n");
352     fprintf(f,"# ppm-file created by %s\n", "trdemo2");
353     fprintf(f,"%i %i\n", imageWidth, imageHeight);
354     fprintf(f,"255\n");
355
356     /* just to be safe... */
357     glPixelStorei(GL_PACK_ALIGNMENT, 1);
358
359     /* Because the HUD and Panel change the ViewPort we will
360      * need to handle some lowlevel stuff ourselves */
361     int ncols = trGet(tr, TR_COLUMNS);
362     int nrows = trGet(tr, TR_ROWS);
363
364     bool do_hud = fgGetBool("/sim/hud/visibility");
365     GLfloat hud_col_step = 640.0 / ncols;
366     GLfloat hud_row_step = 480.0 / nrows;
367         
368     bool do_panel = fgPanelVisible();
369     GLfloat panel_col_step = globals->get_current_panel()->getWidth() / ncols;
370     GLfloat panel_row_step = globals->get_current_panel()->getHeight() / nrows;
371
372     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
373     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
374     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
375     glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
376     glHint(GL_FOG_HINT, GL_NICEST);
377         
378     /* Draw tiles */
379     int more = 1;
380     while (more) {
381         trBeginTile(tr);
382         int curColumn = trGet(tr, TR_CURRENT_COLUMN);
383         int curRow =  trGet(tr, TR_CURRENT_ROW);
384
385         renderer->update( false );
386         if ( do_hud )
387             fgUpdateHUD( curColumn*hud_col_step,      curRow*hud_row_step,
388                          (curColumn+1)*hud_col_step, (curRow+1)*hud_row_step );
389         if (do_panel)
390             globals->get_current_panel()->update(
391                                    curColumn*panel_col_step, panel_col_step,
392                                    curRow*panel_row_step,    panel_row_step );
393         more = trEndTile(tr);
394
395         /* save tile into tile row buffer*/
396         int curTileWidth = trGet(tr, TR_CURRENT_TILE_WIDTH);
397         int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
398         int bytesPerTileRow = (width) * 3*sizeof(GLubyte);
399         int xOffset = curColumn * bytesPerTileRow;
400         int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
401         int i;
402         for (i=0;i<height;i++) {
403             memcpy(buffer + i*bytesPerImageRow + xOffset, /* Dest */
404                    tile + i*bytesPerTileRow,              /* Src */
405                    bytesPerCurrentTileRow);               /* Byte count*/
406         }
407
408         if (curColumn == trGet(tr, TR_COLUMNS)-1) {
409             /* write this buffered row of tiles to the file */
410             int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
411             int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
412             int i;
413             for (i=0;i<curTileHeight;i++) {
414                 /* Remember, OpenGL images are bottom to top.  Have to reverse. */
415                 GLubyte *rowPtr = buffer + (curTileHeight-1-i) * bytesPerImageRow;
416                 fwrite(rowPtr, 1, imageWidth*3, f);
417             }
418         }
419
420     }
421
422     renderer->resize( width, height );
423
424     trDelete(tr);
425
426     glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
427     glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
428     glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
429     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);
430     if ( (!strcmp(fgGetString("/sim/rendering/fog"), "disabled")) ||
431          (!fgGetBool("/sim/rendering/shading"))) {
432         // if fastest fog requested, or if flat shading force fastest
433         glHint ( GL_FOG_HINT, GL_FASTEST );
434     } else if ( !strcmp(fgGetString("/sim/rendering/fog"), "nicest") ) {
435         glHint ( GL_FOG_HINT, GL_DONT_CARE );
436     }
437
438     fclose(f);
439
440     message = "Snapshot saved to \"";
441     message += filename;
442     message += "\".";
443     mkDialog (message.c_str());
444
445     free(tile);
446     free(buffer);
447
448     delete [] filename;
449
450     if ( show_pu_cursor ) {
451         puShowCursor();
452     }
453
454     TurnCursorOn();
455     fgSetBool("/sim/menubar/visibility", menu_status);
456
457     if ( !freeze ) {
458         fgSetBool("/sim/freeze/master", false);
459     }
460 }
461 #endif // #if defined( TR_HIRES_SNAP)
462
463
464 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
465
466 void rotateView( double roll, double pitch, double yaw )
467 {
468         // rotate view
469 }
470
471 GlBitmap *b1 = NULL;
472 GLubyte *hiResScreenCapture( int multiplier )
473 {
474     float oldfov = fgGetDouble("/sim/current-view/field-of-view");
475     float fov = oldfov / multiplier;
476     FGViewer *v = globals->get_current_view();
477     fgSetDouble("/sim/current-view/field-of-view", fov);
478     globals->get_renderer()->init();
479     int cur_width = fgGetInt("/sim/startup/xsize");
480     int cur_height = fgGetInt("/sim/startup/ysize");
481     delete( b1 );
482     // New empty (mostly) bitmap
483     b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
484     int x,y;
485     for ( y = 0; y < multiplier; y++ ) {
486         for ( x = 0; x < multiplier; x++ ) {
487             globals->get_renderer()->resize( cur_width, cur_height );
488             // pan to tile
489             rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
490             globals->get_renderer()->update( false );
491             // restore view
492             GlBitmap b2;
493             b1->copyBitmap( &b2, cur_width*x, cur_height*y );
494         }
495     }
496     fgSetDouble("/sim/current-view/field-of-view", oldfov);
497     return b1->getBitmap();
498 }
499 #endif
500
501 #if defined( WIN32 ) && !defined( __CYGWIN__) && !defined(__MINGW32__)
502 // win32 print screen function
503 void printScreen ( puObject *obj ) {
504     bool show_pu_cursor = false;
505     TurnCursorOff();
506     if ( !puCursorIsHidden() ) {
507         show_pu_cursor = true;
508         puHideCursor();
509     }
510     // BusyCursor( 0 );
511
512     CGlPrinter p( CGlPrinter::PRINT_BITMAP );
513     int cur_width = fgGetInt("/sim/startup/xsize");
514     int cur_height = fgGetInt("/sim/startup/ysize");
515     p.Begin( "FlightGear", cur_width*3, cur_height*3 );
516     p.End( hiResScreenCapture(3) );
517
518     // BusyCursor(1);
519     if ( show_pu_cursor ) {
520         puShowCursor();
521     }
522     TurnCursorOn();
523 }
524 #endif // #ifdef WIN32
525
526
527 void fgDumpSnapShotWrapper ( puObject *obj ) {
528     fgDumpSnapShot();
529 }
530
531
532 void fgHiResDumpWrapper ( puObject *obj ) {
533     fgHiResDump();
534 }
535
536
537 // do a screen snap shot
538 void fgDumpSnapShot () {
539     bool show_pu_cursor = false;
540     char *filename = new char [24];
541     string message;
542     static int count = 1;
543
544     static const SGPropertyNode *master_freeze
545         = fgGetNode("/sim/freeze/master");
546
547     bool freeze = master_freeze->getBoolValue();
548     if ( !freeze ) {
549         fgSetBool("/sim/freeze/master", true);
550     }
551
552     TurnCursorOff();
553     if ( !puCursorIsHidden() ) {
554         show_pu_cursor = true;
555         puHideCursor();
556     }
557     fgSetBool("/sim/signals/screenshot", true);
558
559     FGRenderer *renderer = globals->get_renderer();
560     renderer->init();
561     renderer->resize( fgGetInt("/sim/startup/xsize"),
562                         fgGetInt("/sim/startup/ysize") );
563
564     // we need two render frames here to clear the menu and cursor
565     // ... not sure why but doing an extra fgRenderFrame() shouldn't
566     // hurt anything
567     renderer->update( true );
568     renderer->update( true );
569
570     while (count < 1000) {
571         FILE *fp;
572         snprintf(filename, 24, "fgfs-screen-%03d.ppm", count++);
573         if ( (fp = fopen(filename, "r")) == NULL )
574             break;
575         fclose(fp);
576     }
577
578     if ( sg_glDumpWindow( filename,
579                           fgGetInt("/sim/startup/xsize"), 
580                           fgGetInt("/sim/startup/ysize")) ) {
581         message = "Snapshot saved to \"";
582         message += filename;
583         message += "\".";
584     } else {
585         message = "Failed to save to \"";
586         message += filename;
587         message += "\".";
588     }
589
590     fgSetBool("/sim/signals/screenshot", false);
591     mkDialog (message.c_str());
592
593     delete [] filename;
594
595     if ( show_pu_cursor ) {
596         puShowCursor();
597     }
598
599     TurnCursorOn();
600
601     if ( !freeze ) {
602         fgSetBool("/sim/freeze/master", false);
603     }
604 }
605