| 1 | // Viewer.hxx -- alternative flightgear viewer application |
| 2 | // |
| 3 | // Copyright (C) 2009 - 2012 Mathias Froehlich |
| 4 | // |
| 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. |
| 9 | // |
| 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. |
| 14 | // |
| 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. |
| 18 | |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | |
| 23 | #include "Drawable.hxx" |
| 24 | |
| 25 | #include "Viewer.hxx" |
| 26 | |
| 27 | #include <simgear/structure/SGWeakPtr.hxx> |
| 28 | |
| 29 | namespace fgviewer { |
| 30 | |
| 31 | class Drawable::_ResizedCallback : public osg::GraphicsContext::ResizedCallback { |
| 32 | public: |
| 33 | _ResizedCallback(Drawable* drawable) : |
| 34 | _drawable(drawable) |
| 35 | { } |
| 36 | virtual ~_ResizedCallback() |
| 37 | { } |
| 38 | virtual void resizedImplementation(osg::GraphicsContext*, int x, int y, int width, int height) |
| 39 | { |
| 40 | SGSharedPtr<Drawable> drawable = _drawable.lock(); |
| 41 | if (!drawable.valid()) |
| 42 | return; |
| 43 | drawable->resize(x, y, width, height); |
| 44 | } |
| 45 | SGWeakPtr<Drawable> _drawable; |
| 46 | }; |
| 47 | |
| 48 | Drawable::Drawable(const std::string& name) : |
| 49 | _name(name), |
| 50 | _position(0, 0), |
| 51 | _size(600, 800), |
| 52 | _offscreen(false), |
| 53 | _fullscreen(false) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | Drawable::~Drawable() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | Drawable::attachSlaveCamera(SlaveCamera* slaveCamera) |
| 63 | { |
| 64 | if (!slaveCamera) |
| 65 | return; |
| 66 | _slaveCameraList.push_back(slaveCamera); |
| 67 | } |
| 68 | |
| 69 | bool |
| 70 | Drawable::resize(int x, int y, int width, int height) |
| 71 | { |
| 72 | unsigned numCameras = _slaveCameraList.size(); |
| 73 | if (1 < numCameras) |
| 74 | return false; |
| 75 | _graphicsContext->resizedImplementation(x, y, width, height); |
| 76 | if (numCameras < 1) |
| 77 | return true; |
| 78 | _slaveCameraList.front()->setViewport(SGVec4i(0, 0, width, height)); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool |
| 83 | Drawable::realize(Viewer& viewer) |
| 84 | { |
| 85 | if (_graphicsContext.valid()) |
| 86 | return false; |
| 87 | _graphicsContext = _realizeImplementation(viewer); |
| 88 | if (!_graphicsContext.valid()) |
| 89 | return false; |
| 90 | _graphicsContext->setResizedCallback(new _ResizedCallback(this)); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | osg::GraphicsContext* |
| 95 | Drawable::_realizeImplementation(Viewer& viewer) |
| 96 | { |
| 97 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = _getTraits(viewer); |
| 98 | return viewer.createGraphicsContext(traits.get()); |
| 99 | } |
| 100 | |
| 101 | osg::ref_ptr<osg::GraphicsContext::Traits> |
| 102 | Drawable::_getTraits(Viewer& viewer) |
| 103 | { |
| 104 | osg::GraphicsContext::ScreenIdentifier screenIdentifier; |
| 105 | screenIdentifier.setScreenIdentifier(_screenIdentifier); |
| 106 | |
| 107 | osg::ref_ptr<osg::GraphicsContext::Traits> traits; |
| 108 | traits = viewer.getTraits(screenIdentifier); |
| 109 | |
| 110 | // The window name as displayed by the window manager |
| 111 | traits->windowName = getName(); |
| 112 | |
| 113 | traits->x = _position[0]; |
| 114 | traits->y = _position[1]; |
| 115 | if (!_fullscreen) { |
| 116 | traits->width = _size[0]; |
| 117 | traits->height = _size[1]; |
| 118 | } |
| 119 | |
| 120 | traits->windowDecoration = !_fullscreen; |
| 121 | if (_slaveCameraList.size() <= 1) |
| 122 | traits->supportsResize = true; |
| 123 | else |
| 124 | traits->supportsResize = false; |
| 125 | traits->pbuffer = _offscreen; |
| 126 | |
| 127 | return traits; |
| 128 | } |
| 129 | |
| 130 | } // namespace fgviewer |