]> git.mxchange.org Git - flightgear.git/blob - src/Main/WindowBuilder.cxx
Merge branch 'merge-requests/1555' 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     GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
56
57     traits->readDISPLAY();
58     if (traits->displayNum < 0)
59         traits->displayNum = 0;
60     if (traits->screenNum < 0)
61         traits->screenNum = 0;
62
63     int bpp = fgGetInt("/sim/rendering/bits-per-pixel");
64     bool alpha = fgGetBool("/sim/rendering/clouds3d-enable");
65     int cbits = (bpp <= 16) ?  5 :  8;
66     int zbits = (bpp <= 16) ? 16 : 24;
67     traits->red = traits->green = traits->blue = cbits;
68     traits->depth = zbits;
69     if (alpha)
70         traits->alpha = 8;
71
72     if (stencil)
73         traits->stencil = 8;
74
75     unsigned screenwidth = 0;
76     unsigned screenheight = 0;
77     wsi->getScreenResolution(*traits, screenwidth, screenheight);
78
79     traits->doubleBuffer = true;
80     traits->mipMapGeneration = true;
81     traits->windowName = "FlightGear";
82     // XXX should check per window too.
83     traits->sampleBuffers = fgGetBool("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
84     traits->samples = fgGetInt("/sim/rendering/multi-samples", traits->samples);
85     traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
86     traits->windowDecoration = !fgGetBool("/sim/startup/fullscreen");
87
88     if (!traits->windowDecoration) {
89         // fullscreen
90         traits->supportsResize = false;
91         traits->width = screenwidth;
92         traits->height = screenheight;
93     } else {
94         // window
95         int w = fgGetInt("/sim/startup/xsize");
96         int h = fgGetInt("/sim/startup/ysize");
97         traits->supportsResize = true;
98         traits->width = w;
99         traits->height = h;
100         if ((w>0)&&(h>0))
101         {
102             traits->x = ((unsigned)w>screenwidth) ? 0 : (screenwidth-w)/3;
103             traits->y = ((unsigned)h>screenheight) ? 0 : (screenheight-h)/3;
104         }
105     }
106     return traits;
107 }
108 }
109
110 namespace
111 {
112 // Helper functions that set a value based on a property if it exists,
113 // returning 1 if the value was set.
114
115 inline int setFromProperty(string& place, const SGPropertyNode* node,
116                             const char* name)
117 {
118     const SGPropertyNode* valNode = node->getNode(name);
119     if (valNode) {
120         place = valNode->getStringValue();
121         return 1;
122     }
123     return 0;
124 }
125
126 inline int setFromProperty(int& place, const SGPropertyNode* node,
127                             const char* name)
128 {
129     const SGPropertyNode* valNode = node->getNode(name);
130     if (valNode) {
131         place = valNode->getIntValue();
132         return 1;
133     }
134     return 0;
135 }
136
137 inline int setFromProperty(bool& place, const SGPropertyNode* node,
138                             const char* name)
139 {
140     const SGPropertyNode* valNode = node->getNode(name);
141     if (valNode) {
142         place = valNode->getBoolValue();
143         return 1;
144     }
145     return 0;
146 }
147 }
148
149 namespace flightgear
150 {
151 GraphicsWindow* WindowBuilder::buildWindow(const SGPropertyNode* winNode)
152 {
153     GraphicsContext::WindowingSystemInterface* wsi
154         = osg::GraphicsContext::getWindowingSystemInterface();
155     WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
156     string windowName;
157     if (winNode->hasChild("window-name"))
158         windowName = winNode->getStringValue("window-name");
159     else if (winNode->hasChild("name"))
160         windowName = winNode->getStringValue("name");
161     GraphicsWindow* result = 0;
162     if (!windowName.empty()) {
163         result = wsa->findWindow(windowName);
164         if (result)
165             return result;
166     }
167     GraphicsContext::Traits* traits
168         = new GraphicsContext::Traits(*defaultTraits);
169     int traitsSet = setFromProperty(traits->hostName, winNode, "host-name");
170     traitsSet |= setFromProperty(traits->displayNum, winNode, "display");
171     traitsSet |= setFromProperty(traits->screenNum, winNode, "screen");
172
173     const SGPropertyNode* fullscreenNode = winNode->getNode("fullscreen");
174
175     if (fullscreenNode && fullscreenNode->getBoolValue()) {
176         // fullscreen mode
177         unsigned width = 0;
178         unsigned height = 0;
179         wsi->getScreenResolution(*traits, width, height);
180         traits->windowDecoration = false;
181         traits->width = width;
182         traits->height = height;
183         traits->supportsResize = false;
184         traits->x = 0;
185         traits->y = 0;
186         traitsSet = 1;
187     } else {
188         int resizable = 0;
189         if (fullscreenNode && !fullscreenNode->getBoolValue())
190         {
191             traits->windowDecoration = true;
192             resizable = 1;
193         }
194         resizable |= setFromProperty(traits->windowDecoration, winNode,
195                                      "decoration");
196         resizable |= setFromProperty(traits->width, winNode, "width");
197         resizable |= setFromProperty(traits->height, winNode, "height");
198         if (resizable) {
199             traits->supportsResize = true;
200             traitsSet = 1;
201         }
202         // Otherwise use default values.
203     }
204     traitsSet |= setFromProperty(traits->x, winNode, "x");
205     traitsSet |= setFromProperty(traits->y, winNode, "y");
206     if (!windowName.empty() && windowName != traits->windowName) {
207         traits->windowName = windowName;
208         traitsSet = 1;
209     } else if (traitsSet) {
210         traits->windowName = makeName("FlightGear", defaultCounter++);
211     }
212     bool drawGUI = false;
213     traitsSet |= setFromProperty(drawGUI, winNode, "gui");
214     if (traitsSet) {
215         GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
216         if (gc) {
217             GraphicsWindow* window = WindowSystemAdapter::getWSA()
218                 ->registerWindow(gc, traits->windowName);
219             if (drawGUI)
220                 window->flags |= GraphicsWindow::GUI;
221             return window;
222         } else {
223             return 0;
224         }
225     } else {
226         // XXX What if the window has no traits, but does have a name?
227         // We should create a "default window" registered with that name.
228         return getDefaultWindow();
229     }
230 }
231
232 GraphicsWindow* WindowBuilder::getDefaultWindow()
233 {
234     GraphicsWindow* defaultWindow
235         = WindowSystemAdapter::getWSA()->findWindow(defaultWindowName);
236     if (defaultWindow)
237         return defaultWindow;
238     GraphicsContext::Traits* traits
239         = new GraphicsContext::Traits(*defaultTraits);
240     traits->windowName = "FlightGear";
241     
242     GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
243     if (gc) {
244         defaultWindow = WindowSystemAdapter::getWSA()
245             ->registerWindow(gc, defaultWindowName);
246         return defaultWindow;
247     } else {
248         SG_LOG(SG_GENERAL, SG_ALERT, "getDefaultWindow: failed to create GraphicsContext");
249         return 0;
250     }
251 }
252 }