| 1 | // Copyright (C) 2008 Tim Moore |
| 2 | // |
| 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. |
| 7 | // |
| 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. |
| 12 | // |
| 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. |
| 16 | |
| 17 | #ifndef FLIGHTGEAR_WINDOWSYSTEMADAPTER_HXX |
| 18 | #define FLIGHTGEAR_WINDOWSYSTEMADAPTER_HXX 1 |
| 19 | |
| 20 | #include <functional> |
| 21 | #include <string> |
| 22 | |
| 23 | #include <osg/Referenced> |
| 24 | #include <osg/Camera> |
| 25 | #include <osg/GraphicsThread> |
| 26 | #include <osg/ref_ptr> |
| 27 | |
| 28 | #include <simgear/structure/SGAtomic.hxx> |
| 29 | |
| 30 | namespace osg |
| 31 | { |
| 32 | class GraphicsContext; |
| 33 | } |
| 34 | |
| 35 | // Flexible window support |
| 36 | namespace flightgear |
| 37 | { |
| 38 | /** A window with a graphics context and an integer ID |
| 39 | */ |
| 40 | class GraphicsWindow : public osg::Referenced |
| 41 | { |
| 42 | public: |
| 43 | GraphicsWindow(osg::GraphicsContext* gc_, const std::string& name_, |
| 44 | int id_, unsigned flags_ = 0) : |
| 45 | gc(gc_), name(name_), id(id_), flags(flags_) |
| 46 | { |
| 47 | } |
| 48 | /** The OSG graphics context for this window. |
| 49 | */ |
| 50 | osg::ref_ptr<osg::GraphicsContext> gc; |
| 51 | /** The window's internal name. |
| 52 | */ |
| 53 | std::string name; |
| 54 | /** A unique ID for the window. |
| 55 | */ |
| 56 | int id; |
| 57 | enum Flags { |
| 58 | GUI = 1 /**< The GUI (and 2D cockpit) will be drawn on this window. */ |
| 59 | }; |
| 60 | /** Flags for the window. |
| 61 | */ |
| 62 | unsigned flags; |
| 63 | }; |
| 64 | |
| 65 | typedef std::vector<osg::ref_ptr<GraphicsWindow> > WindowVector; |
| 66 | |
| 67 | /** |
| 68 | * An operation that is run once with a particular GraphicsContext |
| 69 | * current. It will probably be deferred and may run in a different |
| 70 | * thread. |
| 71 | */ |
| 72 | class GraphicsContextOperation : public osg::GraphicsOperation |
| 73 | { |
| 74 | public: |
| 75 | GraphicsContextOperation(const std::string& name) : |
| 76 | osg::GraphicsOperation(name, false) |
| 77 | { |
| 78 | } |
| 79 | /** Don't override this! |
| 80 | */ |
| 81 | virtual void operator()(osg::GraphicsContext* gc); |
| 82 | /** The body of the operation. |
| 83 | */ |
| 84 | virtual void run(osg::GraphicsContext* gc) = 0; |
| 85 | /** Test if the operation has completed. |
| 86 | * @return true if the run() method has finished. |
| 87 | */ |
| 88 | bool isFinished() const { return done != 0; } |
| 89 | private: |
| 90 | SGAtomic done; |
| 91 | }; |
| 92 | |
| 93 | /** Adapter from windows system / graphics context management API to |
| 94 | * functions used by flightgear. This papers over the difference |
| 95 | * between osgViewer::Viewer, which handles multiple windows, graphics |
| 96 | * threads, etc., and the embedded viewer used with GLUT and SDL. |
| 97 | */ |
| 98 | class WindowSystemAdapter : public osg::Referenced |
| 99 | { |
| 100 | public: |
| 101 | WindowSystemAdapter(); |
| 102 | virtual ~WindowSystemAdapter() {} |
| 103 | /** Vector of all the registered windows. |
| 104 | */ |
| 105 | WindowVector windows; |
| 106 | /** Register a window, assigning it an ID. |
| 107 | * @param gc graphics context |
| 108 | * @param windowName internal name (not displayed) |
| 109 | * @return a graphics window |
| 110 | */ |
| 111 | GraphicsWindow* registerWindow(osg::GraphicsContext* gc, |
| 112 | const std::string& windowName); |
| 113 | |
| 114 | /** Find a window by name. |
| 115 | * @param name the window name |
| 116 | * @return the window or 0 |
| 117 | */ |
| 118 | GraphicsWindow* findWindow(const std::string& name); |
| 119 | /** Get the global WindowSystemAdapter |
| 120 | * @return the adapter |
| 121 | */ |
| 122 | static WindowSystemAdapter* getWSA() { return _wsa.get(); } |
| 123 | /** Set the global adapter |
| 124 | * @param wsa the adapter |
| 125 | */ |
| 126 | static void setWSA(WindowSystemAdapter* wsa) { _wsa = wsa; } |
| 127 | protected: |
| 128 | int _nextWindowID; |
| 129 | static osg::ref_ptr<WindowSystemAdapter> _wsa; |
| 130 | |
| 131 | }; |
| 132 | |
| 133 | } |
| 134 | #endif |