]> git.mxchange.org Git - flightgear.git/blob - src/Main/WindowBuilder.cxx
Merge commit 'refs/merge-requests/1552' of git@gitorious.org:fg/flightgear into next
[flightgear.git] / src / Main / WindowBuilder.cxx
1 // Copyright (C) 2008  Tim Moore
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17 #include "WindowBuilder.hxx"
18
19 #include "WindowSystemAdapter.hxx"
20 #include "fg_props.hxx"
21
22 #include <sstream>
23
24 using namespace std;
25 using namespace osg;
26
27 namespace flightgear
28 {
29 string makeName(const string& prefix, int num)
30 {
31     stringstream stream;
32     stream << prefix << num;
33     return stream.str();
34 }
35
36 ref_ptr<WindowBuilder> WindowBuilder::windowBuilder;
37
38 const string WindowBuilder::defaultWindowName("FlightGear");
39
40 void WindowBuilder::initWindowBuilder(bool stencil)
41 {
42     windowBuilder = new WindowBuilder(stencil);
43 }
44
45 WindowBuilder::WindowBuilder(bool stencil) : defaultCounter(0)
46 {
47     defaultTraits = makeDefaultTraits(stencil);
48 }
49
50 GraphicsContext::Traits*
51 WindowBuilder::makeDefaultTraits(bool stencil)
52 {
53     GraphicsContext::WindowingSystemInterface* wsi
54         = osg::GraphicsContext::getWindowingSystemInterface();
55     int w = fgGetInt("/sim/startup/xsize");
56     int h = fgGetInt("/sim/startup/ysize");
57     int bpp = fgGetInt("/sim/rendering/bits-per-pixel");
58     bool alpha = fgGetBool("/sim/rendering/clouds3d-enable");
59     bool fullscreen = fgGetBool("/sim/startup/fullscreen");
60
61     GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
62     traits->readDISPLAY();
63     if (traits->displayNum < 0)
64         traits->displayNum = 0;
65     if (traits->screenNum < 0)
66         traits->screenNum = 0;
67     int cbits = (bpp <= 16) ?  5 :  8;
68     int zbits = (bpp <= 16) ? 16 : 24;
69     traits->red = traits->green = traits->blue = cbits;
70     traits->depth = zbits;
71     if (alpha)
72         traits->alpha = 8;
73     if (stencil)
74         traits->stencil = 8;
75     traits->doubleBuffer = true;
76     traits->mipMapGeneration = true;
77     traits->windowName = "FlightGear";
78     // XXX should check per window too.
79     traits->sampleBuffers = fgGetBool("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
80     traits->samples = fgGetInt("/sim/rendering/multi-samples", traits->samples);
81     traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
82     if (fullscreen) {
83         unsigned width = 0;
84         unsigned height = 0;
85         wsi->getScreenResolution(*traits, width, height);
86         traits->windowDecoration = false;
87         traits->width = width;
88         traits->height = height;
89         traits->supportsResize = false;
90     } else {
91         traits->windowDecoration = true;
92         traits->width = w;
93         traits->height = h;
94 #if defined(WIN32) || defined(__APPLE__)
95         // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
96         // Mac also needs this to show window frame, menubar and Docks
97         traits->x = 100;
98         traits->y = 100;
99 #endif
100         traits->supportsResize = true;
101     }
102     return traits;
103 }
104 }
105
106 namespace
107 {
108 // Helper functions that set a value based on a property if it exists,
109 // returning 1 if the value was set.
110
111 inline int setFromProperty(string& place, const SGPropertyNode* node,
112                             const char* name)
113 {
114     const SGPropertyNode* valNode = node->getNode(name);
115     if (valNode) {
116         place = valNode->getStringValue();
117         return 1;
118     }
119     return 0;
120 }
121
122 inline int setFromProperty(int& place, const SGPropertyNode* node,
123                             const char* name)
124 {
125     const SGPropertyNode* valNode = node->getNode(name);
126     if (valNode) {
127         place = valNode->getIntValue();
128         return 1;
129     }
130     return 0;
131 }
132
133 inline int setFromProperty(bool& place, const SGPropertyNode* node,
134                             const char* name)
135 {
136     const SGPropertyNode* valNode = node->getNode(name);
137     if (valNode) {
138         place = valNode->getBoolValue();
139         return 1;
140     }
141     return 0;
142 }
143 }
144
145 namespace flightgear
146 {
147 GraphicsWindow* WindowBuilder::buildWindow(const SGPropertyNode* winNode)
148 {
149     GraphicsContext::WindowingSystemInterface* wsi
150         = osg::GraphicsContext::getWindowingSystemInterface();
151     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
152     string windowName;
153     if (winNode->hasChild("window-name"))
154         windowName = winNode->getStringValue("window-name");
155     else if (winNode->hasChild("name"))
156         windowName = winNode->getStringValue("name");
157     GraphicsWindow* result = 0;
158     if (!windowName.empty()) {
159         result = wsa->findWindow(windowName);
160         if (result)
161             return result;
162     }
163     GraphicsContext::Traits* traits
164         = new GraphicsContext::Traits(*defaultTraits);
165     int traitsSet = setFromProperty(traits->hostName, winNode, "host-name");
166     traitsSet |= setFromProperty(traits->displayNum, winNode, "display");
167     traitsSet |= setFromProperty(traits->screenNum, winNode, "screen");
168     const SGPropertyNode* fullscreenNode = winNode->getNode("fullscreen");
169     if (fullscreenNode && fullscreenNode->getBoolValue()) {
170         unsigned width = 0;
171         unsigned height = 0;
172         wsi->getScreenResolution(*traits, width, height);
173         traits->windowDecoration = false;
174         traits->width = width;
175         traits->height = height;
176         traits->supportsResize = false;
177         traitsSet = 1;
178     } else {
179         int resizable = 0;
180         resizable |= setFromProperty(traits->windowDecoration, winNode,
181                                      "decoration");
182         resizable |= setFromProperty(traits->width, winNode, "width");
183         resizable |= setFromProperty(traits->height, winNode, "height");
184         if (resizable) {
185             traits->supportsResize = true;
186             traitsSet = 1;
187         }
188         // Otherwise use default values.
189     }
190     traitsSet |= setFromProperty(traits->x, winNode, "x");
191     traitsSet |= setFromProperty(traits->y, winNode, "y");
192     if (!windowName.empty() && windowName != traits->windowName) {
193         traits->windowName = windowName;
194         traitsSet = 1;
195     } else if (traitsSet) {
196         traits->windowName = makeName("FlightGear", defaultCounter++);
197     }
198     bool drawGUI = false;
199     traitsSet |= setFromProperty(drawGUI, winNode, "gui");
200     if (traitsSet) {
201         GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
202         if (gc) {
203             GraphicsWindow* window = WindowSystemAdapter::getWSA()
204                 ->registerWindow(gc, traits->windowName);
205             if (drawGUI)
206                 window->flags |= GraphicsWindow::GUI;
207             return window;
208         } else {
209             return 0;
210         }
211     } else {
212         // XXX What if the window has no traits, but does have a name?
213         // We should create a "default window" registered with that name.
214         return getDefaultWindow();
215     }
216 }
217
218 GraphicsWindow* WindowBuilder::getDefaultWindow()
219 {
220     GraphicsWindow* defaultWindow
221         = WindowSystemAdapter::getWSA()->findWindow(defaultWindowName);
222     if (defaultWindow)
223         return defaultWindow;
224     GraphicsContext::Traits* traits
225         = new GraphicsContext::Traits(*defaultTraits);
226     traits->windowName = "FlightGear";
227     GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
228     if (gc) {
229         defaultWindow = WindowSystemAdapter::getWSA()
230             ->registerWindow(gc, defaultWindowName);
231         return defaultWindow;
232     } else {
233         return 0;
234     }
235 }
236 }