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 if (traits->displayNum < 0)
64 traits->displayNum = 0;
65 if (traits->screenNum < 0)
66 traits->screenNum = 0;
67 int cbits = (bpp <= 16) ? 5 : 8;
68 int zbits = (bpp <= 16) ? 16 : 24;
69 traits->red = traits->green = traits->blue = cbits;
70 traits->depth = zbits;
75 traits->doubleBuffer = true;
76 traits->mipMapGeneration = true;
77 traits->windowName = "FlightGear";
78 // XXX should check per window too.
79 traits->sampleBuffers = fgGetBool("/sim/rendering/multi-sample-buffers", traits->sampleBuffers);
80 traits->samples = fgGetInt("/sim/rendering/multi-samples", traits->samples);
81 traits->vsync = fgGetBool("/sim/rendering/vsync-enable", traits->vsync);
85 wsi->getScreenResolution(*traits, width, height);
86 traits->windowDecoration = false;
87 traits->width = width;
88 traits->height = height;
89 traits->supportsResize = false;
91 traits->windowDecoration = true;
94 #if defined(WIN32) || defined(__APPLE__)
95 // Ugly Hack, why does CW_USEDEFAULT works like phase of the moon?
96 // Mac also needs this to show window frame, menubar and Docks
100 traits->supportsResize = true;
108 // Helper functions that set a value based on a property if it exists,
109 // returning 1 if the value was set.
111 inline int setFromProperty(string& place, const SGPropertyNode* node,
114 const SGPropertyNode* valNode = node->getNode(name);
116 place = valNode->getStringValue();
122 inline int setFromProperty(int& place, const SGPropertyNode* node,
125 const SGPropertyNode* valNode = node->getNode(name);
127 place = valNode->getIntValue();
133 inline int setFromProperty(bool& place, const SGPropertyNode* node,
136 const SGPropertyNode* valNode = node->getNode(name);
138 place = valNode->getBoolValue();
147 GraphicsWindow* WindowBuilder::buildWindow(const SGPropertyNode* winNode)
149 GraphicsContext::WindowingSystemInterface* wsi
150 = osg::GraphicsContext::getWindowingSystemInterface();
151 WindowSystemAdapter* wsa = WindowSystemAdapter::getWSA();
153 if (winNode->hasChild("window-name"))
154 windowName = winNode->getStringValue("window-name");
155 else if (winNode->hasChild("name"))
156 windowName = winNode->getStringValue("name");
157 GraphicsWindow* result = 0;
158 if (!windowName.empty()) {
159 result = wsa->findWindow(windowName);
163 GraphicsContext::Traits* traits
164 = new GraphicsContext::Traits(*defaultTraits);
165 int traitsSet = setFromProperty(traits->hostName, winNode, "host-name");
166 traitsSet |= setFromProperty(traits->displayNum, winNode, "display");
167 traitsSet |= setFromProperty(traits->screenNum, winNode, "screen");
168 const SGPropertyNode* fullscreenNode = winNode->getNode("fullscreen");
169 if (fullscreenNode && fullscreenNode->getBoolValue()) {
172 wsi->getScreenResolution(*traits, width, height);
173 traits->windowDecoration = false;
174 traits->width = width;
175 traits->height = height;
176 traits->supportsResize = false;
180 resizable |= setFromProperty(traits->windowDecoration, winNode,
182 resizable |= setFromProperty(traits->width, winNode, "width");
183 resizable |= setFromProperty(traits->height, winNode, "height");
185 traits->supportsResize = true;
188 // Otherwise use default values.
190 traitsSet |= setFromProperty(traits->x, winNode, "x");
191 traitsSet |= setFromProperty(traits->y, winNode, "y");
192 if (!windowName.empty() && windowName != traits->windowName) {
193 traits->windowName = windowName;
195 } else if (traitsSet) {
196 traits->windowName = makeName("FlightGear", defaultCounter++);
198 bool drawGUI = false;
199 traitsSet |= setFromProperty(drawGUI, winNode, "gui");
201 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
203 GraphicsWindow* window = WindowSystemAdapter::getWSA()
204 ->registerWindow(gc, traits->windowName);
206 window->flags |= GraphicsWindow::GUI;
212 // XXX What if the window has no traits, but does have a name?
213 // We should create a "default window" registered with that name.
214 return getDefaultWindow();
218 GraphicsWindow* WindowBuilder::getDefaultWindow()
220 GraphicsWindow* defaultWindow
221 = WindowSystemAdapter::getWSA()->findWindow(defaultWindowName);
223 return defaultWindow;
224 GraphicsContext::Traits* traits
225 = new GraphicsContext::Traits(*defaultTraits);
226 traits->windowName = "FlightGear";
228 GraphicsContext* gc = GraphicsContext::createGraphicsContext(traits);
230 defaultWindow = WindowSystemAdapter::getWSA()
231 ->registerWindow(gc, defaultWindowName);
232 return defaultWindow;
234 SG_LOG(SG_GENERAL, SG_ALERT, "getDefaultWindow: failed to create GraphicsContext");