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