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.
17 #include "WindowBuilder.hxx"
19 #include "WindowSystemAdapter.hxx"
20 #include "fg_props.hxx"
29 string makeName(const string& prefix, int num)
32 stream << prefix << num;
36 ref_ptr<WindowBuilder> WindowBuilder::windowBuilder;
38 const string WindowBuilder::defaultWindowName("FlightGear");
40 void WindowBuilder::initWindowBuilder(bool stencil)
42 windowBuilder = new WindowBuilder(stencil);
45 WindowBuilder::WindowBuilder(bool stencil) : defaultCounter(0)
47 defaultTraits = makeDefaultTraits(stencil);
50 GraphicsContext::Traits*
51 WindowBuilder::makeDefaultTraits(bool stencil)
53 GraphicsContext::WindowingSystemInterface* wsi
54 = osg::GraphicsContext::getWindowingSystemInterface();
55 GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
57 traits->readDISPLAY();
58 if (traits->displayNum < 0)
59 traits->displayNum = 0;
60 if (traits->screenNum < 0)
61 traits->screenNum = 0;
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;
75 unsigned screenwidth = 0;
76 unsigned screenheight = 0;
77 wsi->getScreenResolution(*traits, screenwidth, screenheight);
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");
88 if (!traits->windowDecoration) {
90 traits->supportsResize = false;
91 traits->width = screenwidth;
92 traits->height = screenheight;
95 int w = fgGetInt("/sim/startup/xsize");
96 int h = fgGetInt("/sim/startup/ysize");
97 traits->supportsResize = true;
102 traits->x = ((unsigned)w>screenwidth) ? 0 : (screenwidth-w)/3;
103 traits->y = ((unsigned)h>screenheight) ? 0 : (screenheight-h)/3;
112 // Helper functions that set a value based on a property if it exists,
113 // returning 1 if the value was set.
115 inline int setFromProperty(string& place, const SGPropertyNode* node,
118 const SGPropertyNode* valNode = node->getNode(name);
120 place = valNode->getStringValue();
126 inline int setFromProperty(int& place, const SGPropertyNode* node,
129 const SGPropertyNode* valNode = node->getNode(name);
131 place = valNode->getIntValue();
137 inline int setFromProperty(bool& place, const SGPropertyNode* node,
140 const SGPropertyNode* valNode = node->getNode(name);
142 place = valNode->getBoolValue();
151 GraphicsWindow* WindowBuilder::buildWindow(const SGPropertyNode* winNode)
153 GraphicsContext::WindowingSystemInterface* wsi
154 = osg::GraphicsContext::getWindowingSystemInterface();
155 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
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);
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");
173 const SGPropertyNode* fullscreenNode = winNode->getNode("fullscreen");
175 if (fullscreenNode && fullscreenNode->getBoolValue()) {
179 wsi->getScreenResolution(*traits, width, height);
180 traits->windowDecoration = false;
181 traits->width = width;
182 traits->height = height;
183 traits->supportsResize = false;
189 if (fullscreenNode && !fullscreenNode->getBoolValue())
191 traits->windowDecoration = true;
194 resizable |= setFromProperty(traits->windowDecoration, winNode,
196 resizable |= setFromProperty(traits->width, winNode, "width");
197 resizable |= setFromProperty(traits->height, winNode, "height");
199 traits->supportsResize = true;
202 // Otherwise use default values.
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;
209 } else if (traitsSet) {
210 traits->windowName = makeName("FlightGear", defaultCounter++);
212 bool drawGUI = false;
213 traitsSet |= setFromProperty(drawGUI, winNode, "gui");
215 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
217 GraphicsWindow* window = WindowSystemAdapter::getWSA()
218 ->registerWindow(gc, traits->windowName);
220 window->flags |= GraphicsWindow::GUI;
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();
232 GraphicsWindow* WindowBuilder::getDefaultWindow()
234 GraphicsWindow* defaultWindow
235 = WindowSystemAdapter::getWSA()->findWindow(defaultWindowName);
237 return defaultWindow;
238 GraphicsContext::Traits* traits
239 = new GraphicsContext::Traits(*defaultTraits);
240 traits->windowName = "FlightGear";
242 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
244 defaultWindow = WindowSystemAdapter::getWSA()
245 ->registerWindow(gc, defaultWindowName);
246 return defaultWindow;
248 SG_LOG(SG_VIEW, SG_ALERT, "getDefaultWindow: failed to create GraphicsContext");