]> git.mxchange.org Git - flightgear.git/blob - src/GUI/gui.cxx
Trying to bullet-proof the traffic code.
[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         FGFontCache *fc = globals->get_fontcache();
83         fc->initializeFonts();
84         puFont *GuiFont
85             = fc->get(globals->get_locale()->getDefaultFont("typewriter.txf"),
86                       15);
87         puSetDefaultFonts(*GuiFont, *GuiFont);
88         guiFnt = puGetDefaultLabelFont();
89
90         LayoutWidget::setDefaultFont(GuiFont, 15);
91   
92         if (!fgHasNode("/sim/startup/mouse-pointer")) {
93             // no preference specified for mouse pointer
94         } else if ( !fgGetBool("/sim/startup/mouse-pointer") ) {
95             // don't show pointer
96         } else {
97             // force showing pointer
98             puShowCursor();
99         }
100     }
101 };
102
103 // Operation for querying OpenGL parameters. This must be done in a
104 // valid OpenGL context, potentially in another thread.
105
106 struct GeneralInitOperation : public GraphicsContextOperation
107 {
108     GeneralInitOperation()
109         : GraphicsContextOperation(std::string("General init"))
110     {
111     }
112     void run(osg::GraphicsContext* gc)
113     {
114         SGPropertyNode* simRendering = fgGetNode("/sim/rendering");
115
116         simRendering->setStringValue("gl-vendor", (char*) glGetString(GL_VENDOR));
117         SG_LOG( SG_GENERAL, SG_INFO, glGetString(GL_VENDOR));
118
119         simRendering->setStringValue("gl-renderer", (char*) glGetString(GL_RENDERER));
120         SG_LOG( SG_GENERAL, SG_INFO, glGetString(GL_RENDERER));
121
122         simRendering->setStringValue("gl-version", (char*) glGetString(GL_VERSION));
123         SG_LOG( SG_GENERAL, SG_INFO, glGetString(GL_VERSION));
124
125         // Old hardware without support for OpenGL 2.0 does not support GLSL and
126         // glGetString returns NULL for GL_SHADING_LANGUAGE_VERSION.
127         //
128         // See http://flightgear.org/forums/viewtopic.php?f=17&t=19670&start=15#p181945
129         const char* glsl_version = (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION);
130         if( !glsl_version )
131           glsl_version = "UNSUPPORTED";
132         simRendering->setStringValue("gl-shading-language-version", glsl_version);
133         SG_LOG( SG_GENERAL, SG_INFO, glsl_version);
134
135         GLint tmp;
136         glGetIntegerv( GL_MAX_TEXTURE_SIZE, &tmp );
137         simRendering->setIntValue("max-texture-size", tmp);
138
139         glGetIntegerv( GL_DEPTH_BITS, &tmp );
140         simRendering->setIntValue("depth-buffer-bits", tmp);
141     }
142 };
143
144 osg::ref_ptr<GUIInitOperation> initOp;
145
146 }
147
148 /** Initializes GUI.
149  * Returns true when done, false when still busy (call again). */
150 bool guiInit()
151 {
152     static osg::ref_ptr<GeneralInitOperation> genOp;
153
154     if (!genOp.valid())
155     {
156         // Pick some window on which to do queries.
157         // XXX Perhaps all this graphics initialization code should be
158         // moved to renderer.cxx?
159         genOp = new GeneralInitOperation;
160         osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
161         WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
162         osg::GraphicsContext* gc = 0;
163         if (guiCamera)
164             gc = guiCamera->getGraphicsContext();
165         if (gc) {
166             gc->add(genOp.get());
167             initOp = new GUIInitOperation;
168             gc->add(initOp.get());
169         } else {
170             wsa->windows[0]->gc->add(genOp.get());
171         }
172         return false; // not ready yet
173     }
174     else
175     {
176         if (!genOp->isFinished())
177             return false;
178         if (!initOp.valid())
179             return true;
180         if (!initOp->isFinished())
181             return false;
182         genOp = 0;
183         initOp = 0;
184         // we're done
185         return true;
186     }
187 }
188
189 void syncPausePopupState()
190 {
191     bool paused = fgGetBool("/sim/freeze/master",true) | fgGetBool("/sim/freeze/clock",true);
192     SGPropertyNode_ptr args(new SGPropertyNode);
193     args->setStringValue("id", "sim-pause");
194     if (paused && fgGetBool("/sim/view-name-popup")) {
195       args->setStringValue("label", "Simulation is paused");
196       globals->get_commands()->execute("show-message", args);
197     } else {
198       globals->get_commands()->execute("clear-message", args);
199     }
200
201 }