1 // Copyright (C) 2008 Tim Moore
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.
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.
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.
21 #include "WindowBuilder.hxx"
23 #include "WindowSystemAdapter.hxx"
24 #include <Main/fg_props.hxx>
33 string makeName(const string& prefix, int num)
36 stream << prefix << num;
40 ref_ptr<WindowBuilder> WindowBuilder::windowBuilder;
42 const string WindowBuilder::defaultWindowName("FlightGear");
44 void WindowBuilder::initWindowBuilder(bool stencil)
46 windowBuilder = new WindowBuilder(stencil);
49 WindowBuilder::WindowBuilder(bool stencil) : defaultCounter(0)
51 defaultTraits = makeDefaultTraits(stencil);
54 GraphicsContext::Traits*
55 WindowBuilder::makeDefaultTraits(bool stencil)
57 GraphicsContext::WindowingSystemInterface* wsi
58 = osg::GraphicsContext::getWindowingSystemInterface();
59 GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
61 traits->readDISPLAY();
62 if (traits->displayNum < 0)
63 traits->displayNum = 0;
64 if (traits->screenNum < 0)
65 traits->screenNum = 0;
67 int bpp = fgGetInt("/sim/rendering/bits-per-pixel");
68 bool alpha = fgGetBool("/sim/rendering/clouds3d-enable");
69 int cbits = (bpp <= 16) ? 5 : 8;
70 int zbits = (bpp <= 16) ? 16 : 24;
71 traits->red = traits->green = traits->blue = cbits;
72 traits->depth = zbits;
79 unsigned screenwidth = 0;
80 unsigned screenheight = 0;
81 wsi->getScreenResolution(*traits, screenwidth, screenheight);
83 traits->doubleBuffer = true;
84 traits->mipMapGeneration = true;
85 traits->windowName = "FlightGear";
86 // XXX should check per window too.
87 traits->sampleBuffers = fgGetInt("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
88 traits->samples = fgGetInt("/sim/rendering/multi-samples", traits->samples);
89 traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
90 traits->windowDecoration = !fgGetBool("/sim/startup/fullscreen");
92 if (!traits->windowDecoration) {
94 traits->supportsResize = false;
95 traits->width = screenwidth;
96 traits->height = screenheight;
97 SG_LOG(SG_VIEW,SG_DEBUG,"Using full screen size for window: " << screenwidth << " x " << screenheight);
100 int w = fgGetInt("/sim/startup/xsize");
101 int h = fgGetInt("/sim/startup/ysize");
102 traits->supportsResize = true;
107 traits->x = ((unsigned)w>screenwidth) ? 0 : (screenwidth-w)/3;
108 traits->y = ((unsigned)h>screenheight) ? 0 : (screenheight-h)/3;
110 SG_LOG(SG_VIEW,SG_DEBUG,"Using initial window size: " << w << " x " << h);
118 // Helper functions that set a value based on a property if it exists,
119 // returning 1 if the value was set.
121 inline int setFromProperty(string& place, const SGPropertyNode* node,
124 const SGPropertyNode* valNode = node->getNode(name);
126 place = valNode->getStringValue();
132 inline int setFromProperty(int& place, const SGPropertyNode* node,
135 const SGPropertyNode* valNode = node->getNode(name);
137 place = valNode->getIntValue();
143 inline int setFromProperty(bool& place, const SGPropertyNode* node,
146 const SGPropertyNode* valNode = node->getNode(name);
148 place = valNode->getBoolValue();
157 GraphicsWindow* WindowBuilder::buildWindow(const SGPropertyNode* winNode)
159 GraphicsContext::WindowingSystemInterface* wsi
160 = osg::GraphicsContext::getWindowingSystemInterface();
161 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
163 if (winNode->hasChild("window-name"))
164 windowName = winNode->getStringValue("window-name");
165 else if (winNode->hasChild("name"))
166 windowName = winNode->getStringValue("name");
167 GraphicsWindow* result = 0;
168 if (!windowName.empty()) {
169 result = wsa->findWindow(windowName);
173 GraphicsContext::Traits* traits
174 = new GraphicsContext::Traits(*defaultTraits);
175 int traitsSet = setFromProperty(traits->hostName, winNode, "host-name");
176 traitsSet |= setFromProperty(traits->displayNum, winNode, "display");
177 traitsSet |= setFromProperty(traits->screenNum, winNode, "screen");
179 const SGPropertyNode* fullscreenNode = winNode->getNode("fullscreen");
180 const SGPropertyNode* orrNode = winNode->getNode("overrideRedirect");
182 if (fullscreenNode && fullscreenNode->getBoolValue()) {
186 wsi->getScreenResolution(*traits, width, height);
187 traits->windowDecoration = false;
188 traits->width = width;
189 traits->height = height;
190 traits->supportsResize = false;
191 bool overrideRedirect = orrNode && orrNode->getBoolValue();
192 traits->overrideRedirect = overrideRedirect;
198 if (fullscreenNode && !fullscreenNode->getBoolValue())
200 traits->windowDecoration = true;
203 resizable |= setFromProperty(traits->windowDecoration, winNode,
205 resizable |= setFromProperty(traits->width, winNode, "width");
206 resizable |= setFromProperty(traits->height, winNode, "height");
208 traits->supportsResize = true;
211 // Otherwise use default values.
213 traitsSet |= setFromProperty(traits->x, winNode, "x");
214 traitsSet |= setFromProperty(traits->y, winNode, "y");
215 if (!windowName.empty() && windowName != traits->windowName) {
216 traits->windowName = windowName;
218 } else if (traitsSet) {
219 traits->windowName = makeName("FlightGear", defaultCounter++);
221 bool drawGUI = false;
222 traitsSet |= setFromProperty(drawGUI, winNode, "gui");
224 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
226 GraphicsWindow* window = WindowSystemAdapter::getWSA()
227 ->registerWindow(gc, traits->windowName);
229 window->flags |= GraphicsWindow::GUI;
235 // XXX What if the window has no traits, but does have a name?
236 // We should create a "default window" registered with that name.
237 return getDefaultWindow();
241 GraphicsWindow* WindowBuilder::getDefaultWindow()
243 GraphicsWindow* defaultWindow
244 = WindowSystemAdapter::getWSA()->findWindow(defaultWindowName);
246 return defaultWindow;
247 GraphicsContext::Traits* traits
248 = new GraphicsContext::Traits(*defaultTraits);
249 traits->windowName = "FlightGear";
251 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
253 defaultWindow = WindowSystemAdapter::getWSA()
254 ->registerWindow(gc, defaultWindowName);
255 return defaultWindow;
257 SG_LOG(SG_VIEW, SG_ALERT, "getDefaultWindow: failed to create GraphicsContext");