1 // Viewer.hxx -- alternative flightgear viewer application
3 // Copyright (C) 2009 - 2012 Mathias Froehlich
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "Drawable.hxx"
27 #include <simgear/structure/SGWeakPtr.hxx>
31 class Drawable::_ResizedCallback : public osg::GraphicsContext::ResizedCallback {
33 _ResizedCallback(Drawable* drawable) :
36 virtual ~_ResizedCallback()
38 virtual void resizedImplementation(osg::GraphicsContext*, int x, int y, int width, int height)
40 SGSharedPtr<Drawable> drawable = _drawable.lock();
41 if (!drawable.valid())
43 drawable->resize(x, y, width, height);
45 SGWeakPtr<Drawable> _drawable;
48 Drawable::Drawable(const std::string& name) :
62 Drawable::attachSlaveCamera(SlaveCamera* slaveCamera)
66 _slaveCameraList.push_back(slaveCamera);
70 Drawable::resize(int x, int y, int width, int height)
72 unsigned numCameras = _slaveCameraList.size();
75 _graphicsContext->resizedImplementation(x, y, width, height);
78 _slaveCameraList.front()->setViewport(SGVec4i(0, 0, width, height));
83 Drawable::realize(Viewer& viewer)
85 if (_graphicsContext.valid())
87 _graphicsContext = _realizeImplementation(viewer);
88 if (!_graphicsContext.valid())
90 _graphicsContext->setResizedCallback(new _ResizedCallback(this));
95 Drawable::_realizeImplementation(Viewer& viewer)
97 osg::ref_ptr<osg::GraphicsContext::Traits> traits = _getTraits(viewer);
98 return viewer.createGraphicsContext(traits.get());
101 osg::ref_ptr<osg::GraphicsContext::Traits>
102 Drawable::_getTraits(Viewer& viewer)
104 osg::GraphicsContext::ScreenIdentifier screenIdentifier;
105 screenIdentifier.setScreenIdentifier(_screenIdentifier);
107 osg::ref_ptr<osg::GraphicsContext::Traits> traits;
108 traits = viewer.getTraits(screenIdentifier);
110 // The window name as displayed by the window manager
111 traits->windowName = getName();
113 traits->x = _position[0];
114 traits->y = _position[1];
116 traits->width = _size[0];
117 traits->height = _size[1];
120 traits->windowDecoration = !_fullscreen;
121 if (_slaveCameraList.size() <= 1)
122 traits->supportsResize = true;
124 traits->supportsResize = false;
125 traits->pbuffer = _offscreen;
130 } // namespace fgviewer