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