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 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");
61 GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
62 traits->readDISPLAY();
63 int cbits = (bpp <= 16) ? 5 : 8;
64 int zbits = (bpp <= 16) ? 16 : 24;
65 traits->red = traits->green = traits->blue = cbits;
66 traits->depth = zbits;
71 traits->doubleBuffer = true;
72 traits->mipMapGeneration = true;
73 traits->windowName = "FlightGear";
74 // XXX should check per window too.
75 traits->sampleBuffers = fgGetBool("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
76 traits->samples = fgGetInt("/sim/rendering/multi-samples", traits->samples);
77 traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
81 wsi->getScreenResolution(*traits, width, height);
82 traits->windowDecoration = false;
83 traits->width = width;
84 traits->height = height;
85 traits->supportsResize = false;
87 traits->windowDecoration = true;
90 #if defined(WIN32) || defined(__APPLE__)
91 // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
92 // Mac also needs this to show window frame, menubar and Docks
96 traits->supportsResize = true;
104 // Helper functions that set a value based on a property if it exists,
105 // returning 1 if the value was set.
107 inline int setFromProperty(string& place, const SGPropertyNode* node,
110 const SGPropertyNode* valNode = node->getNode(name);
112 place = valNode->getStringValue();
118 inline int setFromProperty(int& place, const SGPropertyNode* node,
121 const SGPropertyNode* valNode = node->getNode(name);
123 place = valNode->getIntValue();
129 inline int setFromProperty(bool& place, const SGPropertyNode* node,
132 const SGPropertyNode* valNode = node->getNode(name);
134 place = valNode->getBoolValue();
143 GraphicsWindow* WindowBuilder::buildWindow(const SGPropertyNode* winNode)
145 GraphicsContext::WindowingSystemInterface* wsi
146 = osg::GraphicsContext::getWindowingSystemInterface();
147 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
149 if (winNode->hasChild("window-name"))
150 windowName = winNode->getStringValue("window-name");
151 else if (winNode->hasChild("name"))
152 windowName = winNode->getStringValue("name");
153 GraphicsWindow* result = 0;
154 if (!windowName.empty()) {
155 result = wsa->findWindow(windowName);
159 GraphicsContext::Traits* traits
160 = new GraphicsContext::Traits(*defaultTraits);
161 int traitsSet = setFromProperty(traits->hostName, winNode, "host-name");
162 traitsSet |= setFromProperty(traits->displayNum, winNode, "display");
163 traitsSet |= setFromProperty(traits->screenNum, winNode, "screen");
164 const SGPropertyNode* fullscreenNode = winNode->getNode("fullscreen");
165 if (fullscreenNode && fullscreenNode->getBoolValue()) {
168 wsi->getScreenResolution(*traits, width, height);
169 traits->windowDecoration = false;
170 traits->width = width;
171 traits->height = height;
172 traits->supportsResize = false;
176 resizable |= setFromProperty(traits->windowDecoration, winNode,
178 resizable |= setFromProperty(traits->width, winNode, "width");
179 resizable |= setFromProperty(traits->height, winNode, "height");
181 traits->supportsResize = true;
184 // Otherwise use default values.
186 traitsSet |= setFromProperty(traits->x, winNode, "x");
187 traitsSet |= setFromProperty(traits->y, winNode, "y");
188 if (!windowName.empty() && windowName != traits->windowName) {
189 traits->windowName = windowName;
191 } else if (traitsSet) {
192 traits->windowName = makeName("FlightGear", defaultCounter++);
194 bool drawGUI = false;
195 traitsSet |= setFromProperty(drawGUI, winNode, "gui");
197 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
200 GraphicsWindow* window = WindowSystemAdapter::getWSA()
201 ->registerWindow(gc, traits->windowName);
203 window->flags |= GraphicsWindow::GUI;
209 // XXX What if the window has no traits, but does have a name?
210 // We should create a "default window" registered with that name.
211 return getDefaultWindow();
215 GraphicsWindow* WindowBuilder::getDefaultWindow()
217 GraphicsWindow* defaultWindow
218 = WindowSystemAdapter::getWSA()->findWindow(defaultWindowName);
220 return defaultWindow;
221 GraphicsContext::Traits* traits
222 = new GraphicsContext::Traits(*defaultTraits);
223 traits->windowName = "FlightGear";
224 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
227 defaultWindow = WindowSystemAdapter::getWSA()
228 ->registerWindow(gc, defaultWindowName);
229 return defaultWindow;