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 void WindowBuilder::initWindowBuilder(bool stencil)
40 windowBuilder = new WindowBuilder(stencil);
43 WindowBuilder::WindowBuilder(bool stencil) : defaultCounter(0)
45 defaultTraits = makeDefaultTraits(stencil);
48 GraphicsContext::Traits*
49 WindowBuilder::makeDefaultTraits(bool stencil)
51 GraphicsContext::WindowingSystemInterface* wsi
52 = osg::GraphicsContext::getWindowingSystemInterface();
53 int w = fgGetInt("/sim/startup/xsize");
54 int h = fgGetInt("/sim/startup/ysize");
55 int bpp = fgGetInt("/sim/rendering/bits-per-pixel");
56 bool alpha = fgGetBool("/sim/rendering/clouds3d-enable");
57 bool fullscreen = fgGetBool("/sim/startup/fullscreen");
59 GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
60 traits->readDISPLAY();
61 int cbits = (bpp <= 16) ? 5 : 8;
62 int zbits = (bpp <= 16) ? 16 : 24;
63 traits->red = traits->green = traits->blue = cbits;
64 traits->depth = zbits;
69 traits->doubleBuffer = true;
70 traits->mipMapGeneration = true;
71 traits->windowName = "FlightGear";
72 // XXX should check per window too.
73 traits->sampleBuffers = fgGetBool("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
74 traits->samples = fgGetBool("/sim/rendering/multi-samples", traits->samples);
75 traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
79 wsi->getScreenResolution(*traits, width, height);
80 traits->windowDecoration = false;
81 traits->width = width;
82 traits->height = height;
83 traits->supportsResize = false;
85 traits->windowDecoration = true;
88 #if defined(WIN32) || defined(__APPLE__)
89 // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
90 // Mac also needs this to show window frame, menubar and Docks
94 traits->supportsResize = true;
102 // Helper functions that set a value based on a property if it exists,
103 // returning 1 if the value was set.
105 inline int setFromProperty(string& place, const SGPropertyNode* node,
108 const SGPropertyNode* valNode = node->getNode(name);
110 place = valNode->getStringValue();
116 inline int setFromProperty(int& place, const SGPropertyNode* node,
119 const SGPropertyNode* valNode = node->getNode(name);
121 place = valNode->getIntValue();
127 inline int setFromProperty(bool& place, const SGPropertyNode* node,
130 const SGPropertyNode* valNode = node->getNode(name);
132 place = valNode->getBoolValue();
141 GraphicsWindow* WindowBuilder::buildWindow(const SGPropertyNode* winNode)
143 GraphicsContext::WindowingSystemInterface* wsi
144 = osg::GraphicsContext::getWindowingSystemInterface();
145 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
147 if (winNode->hasChild("window-name"))
148 windowName = winNode->getStringValue("window-name");
149 else if (winNode->hasChild("name"))
150 windowName = winNode->getStringValue("name");
151 GraphicsWindow* result = 0;
152 if (!windowName.empty()) {
153 result = wsa->findWindow(windowName);
157 GraphicsContext::Traits* traits
158 = new GraphicsContext::Traits(*defaultTraits);
159 int traitsSet = setFromProperty(traits->hostName, winNode, "host-name");
160 traitsSet |= setFromProperty(traits->displayNum, winNode, "display");
161 traitsSet |= setFromProperty(traits->screenNum, winNode, "screen");
162 const SGPropertyNode* fullscreenNode = winNode->getNode("fullscreen");
163 if (fullscreenNode && fullscreenNode->getBoolValue()) {
166 wsi->getScreenResolution(*traits, width, height);
167 traits->windowDecoration = false;
168 traits->width = width;
169 traits->height = height;
170 traits->supportsResize = false;
174 resizable |= setFromProperty(traits->windowDecoration, winNode,
176 resizable |= setFromProperty(traits->width, winNode, "width");
177 resizable |= setFromProperty(traits->height, winNode, "height");
179 traits->supportsResize = true;
182 // Otherwise use default values.
184 traitsSet |= setFromProperty(traits->x, winNode, "x");
185 traitsSet |= setFromProperty(traits->y, winNode, "y");
186 if (!windowName.empty() && windowName != traits->windowName) {
187 traits->windowName = windowName;
189 } else if (traitsSet) {
190 traits->windowName = makeName("FlightGear", defaultCounter++);
192 bool drawGUI = false;
193 traitsSet |= setFromProperty(drawGUI, winNode, "gui");
195 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
198 GraphicsWindow* window = WindowSystemAdapter::getWSA()
199 ->registerWindow(gc, traits->windowName);
201 window->flags |= GraphicsWindow::GUI;
207 return getDefaultWindow();
211 GraphicsWindow* WindowBuilder::getDefaultWindow()
213 if (defaultWindow.valid())
214 return defaultWindow.get();
215 GraphicsContext::Traits* traits
216 = new GraphicsContext::Traits(*defaultTraits);
217 traits->windowName = "FlightGear";
218 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
221 defaultWindow = WindowSystemAdapter::getWSA()
222 ->registerWindow(gc, traits->windowName);
223 return defaultWindow.get();