]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui.cxx
Code cleanups, code updates and fix at least on (possible) devide-by-zero
[flightgear.git] / src / GUI / gui.cxx
1 /**************************************************************************
2  * gui.cxx
3  *
4  * Written 1998 by Durk Talsma, started Juni, 1998.  For the flight gear
5  * project.
6  *
7  * Additional mouse supported added by David Megginson, 1999.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22  *
23  * $Id$
24  **************************************************************************/
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #include <string>
34
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/structure/commands.hxx>
37
38 #include <simgear/misc/sg_path.hxx>
39 #include <simgear/props/props.hxx>
40 #include <simgear/props/props_io.hxx>
41
42 #include <plib/pu.h>
43
44 #include <Main/main.hxx>
45 #include <Main/globals.hxx>
46 #include <Main/locale.hxx>
47 #include <Main/fg_props.hxx>
48 #include <Viewer/WindowSystemAdapter.hxx>
49 #include <Viewer/CameraGroup.hxx>
50 #include <GUI/new_gui.hxx>
51 #include <GUI/FGFontCache.hxx>
52
53 #include "gui.h"
54 #include "layout.hxx"
55
56 #include <osg/GraphicsContext>
57
58 using namespace flightgear;
59
60 puFont guiFnt = 0;
61
62
63 /* -------------------------------------------------------------------------
64 init the gui
65 _____________________________________________________________________*/
66
67 namespace
68 {
69 class GUIInitOperation : public GraphicsContextOperation
70 {
71 public:
72     GUIInitOperation() : GraphicsContextOperation(std::string("GUI init"))
73     {
74     }
75     void run(osg::GraphicsContext* gc)
76     {
77         WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
78         wsa->puInitialize();
79         puSetDefaultStyle         ( PUSTYLE_SMALL_SHADED ); //PUSTYLE_DEFAULT
80         puSetDefaultColourScheme  (0.8, 0.8, 0.9, 1);
81
82         // OSG Texture2D sets this, which breaks PLIB fntLoadTXF
83         // force it back to zero so width passed to glTexImage2D is used
84         glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
85
86         FGFontCache *fc = FGFontCache::instance();
87         fc->initializeFonts();
88         puFont *GuiFont
89             = fc->get(globals->get_locale()->getDefaultFont("typewriter.txf"),
90                       15);
91         puSetDefaultFonts(*GuiFont, *GuiFont);
92         guiFnt = puGetDefaultLabelFont();
93
94         LayoutWidget::setDefaultFont(GuiFont, 15);
95   
96         if (!fgHasNode("/sim/startup/mouse-pointer")) {
97             // no preference specified for mouse pointer
98         } else if ( !fgGetBool("/sim/startup/mouse-pointer") ) {
99             // don't show pointer
100         } else {
101             // force showing pointer
102             puShowCursor();
103         }
104     }
105 };
106
107 // Operation for querying OpenGL parameters. This must be done in a
108 // valid OpenGL context, potentially in another thread.
109
110 struct GeneralInitOperation : public GraphicsContextOperation
111 {
112     GeneralInitOperation()
113         : GraphicsContextOperation(std::string("General init"))
114     {
115     }
116     void run(osg::GraphicsContext* gc)
117     {
118         SGPropertyNode* simRendering = fgGetNode("/sim/rendering");
119
120         simRendering->setStringValue("gl-vendor", (char*) glGetString(GL_VENDOR));
121         SG_LOG( SG_GENERAL, SG_INFO, glGetString(GL_VENDOR));
122
123         simRendering->setStringValue("gl-renderer", (char*) glGetString(GL_RENDERER));
124         SG_LOG( SG_GENERAL, SG_INFO, glGetString(GL_RENDERER));
125
126         simRendering->setStringValue("gl-version", (char*) glGetString(GL_VERSION));
127         SG_LOG( SG_GENERAL, SG_INFO, glGetString(GL_VERSION));
128
129         // Old hardware without support for OpenGL 2.0 does not support GLSL and
130         // glGetString returns NULL for GL_SHADING_LANGUAGE_VERSION.
131         //
132         // See http://flightgear.org/forums/viewtopic.php?f=17&t=19670&start=15#p181945
133         const char* glsl_version = (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION);
134         if( !glsl_version )
135           glsl_version = "UNSUPPORTED";
136         simRendering->setStringValue("gl-shading-language-version", glsl_version);
137         SG_LOG( SG_GENERAL, SG_INFO, glsl_version);
138
139         GLint tmp;
140         glGetIntegerv( GL_MAX_TEXTURE_SIZE, &tmp );
141         simRendering->setIntValue("max-texture-size", tmp);
142
143         glGetIntegerv( GL_DEPTH_BITS, &tmp );
144         simRendering->setIntValue("depth-buffer-bits", tmp);
145     }
146 };
147
148 osg::ref_ptr<GUIInitOperation> initOp;
149
150 }
151
152 /** Initializes GUI.
153  * Returns true when done, false when still busy (call again). */
154 bool guiInit()
155 {
156     static osg::ref_ptr<GeneralInitOperation> genOp;
157
158     if (!genOp.valid())
159     {
160         // Pick some window on which to do queries.
161         // XXX Perhaps all this graphics initialization code should be
162         // moved to renderer.cxx?
163         genOp = new GeneralInitOperation;
164         osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
165         WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
166         osg::GraphicsContext* gc = 0;
167         if (guiCamera)
168             gc = guiCamera->getGraphicsContext();
169         if (gc) {
170             gc->add(genOp.get());
171             initOp = new GUIInitOperation;
172             gc->add(initOp.get());
173         } else {
174             wsa->windows[0]->gc->add(genOp.get());
175         }
176         return false; // not ready yet
177     }
178     else
179     {
180         if (!genOp->isFinished())
181             return false;
182         if (!initOp.valid())
183             return true;
184         if (!initOp->isFinished())
185             return false;
186         genOp = 0;
187         initOp = 0;
188         // we're done
189         return true;
190     }
191 }
192
193 void syncPausePopupState()
194 {
195     bool paused = fgGetBool("/sim/freeze/master",true) | fgGetBool("/sim/freeze/clock",true);
196     SGPropertyNode_ptr args(new SGPropertyNode);
197     args->setStringValue("id", "sim-pause");
198     if (paused && fgGetBool("/sim/view-name-popup")) {
199       args->setStringValue("label", "Simulation is paused");
200       globals->get_commands()->execute("show-message", args);
201     } else {
202       globals->get_commands()->execute("clear-message", args);
203     }
204
205 }