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 #ifndef FLIGHTGEAR_WINDOWSYSTEMADAPTER_HXX
18 #define FLIGHTGEAR_WINDOWSYSTEMADAPTER_HXX 1
23 #include <osg/Referenced>
25 #include <osg/GraphicsThread>
26 #include <osg/ref_ptr>
28 #include <simgear/structure/SGAtomic.hxx>
32 class GraphicsContext;
35 // Flexible window support
38 /** A window with a graphics context and an integer ID
40 class GraphicsWindow : public osg::Referenced
43 GraphicsWindow(osg::GraphicsContext* gc_, const std::string& name_,
44 int id_, unsigned flags_ = 0) :
45 gc(gc_), name(name_), id(id_), flags(flags_)
48 /** The OSG graphics context for this window.
50 osg::ref_ptr<osg::GraphicsContext> gc;
51 /** The window's internal name.
54 /** A unique ID for the window.
58 GUI = 1 /**< The GUI (and 2D cockpit) will be drawn on this window. */
60 /** Flags for the window.
65 typedef std::vector<osg::ref_ptr<GraphicsWindow> > WindowVector;
68 * An operation that is run once with a particular GraphicsContext
69 * current. It will probably be deferred and may run in a different
72 class GraphicsContextOperation : public osg::GraphicsOperation
75 GraphicsContextOperation(const std::string& name) :
76 osg::GraphicsOperation(name, false)
79 /** Don't override this!
81 virtual void operator()(osg::GraphicsContext* gc);
82 /** The body of the operation.
84 virtual void run(osg::GraphicsContext* gc) = 0;
85 /** Test if the operation has completed.
86 * @return true if the run() method has finished.
88 bool isFinished() const { return done != 0; }
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.
98 class WindowSystemAdapter : public osg::Referenced
101 WindowSystemAdapter();
102 virtual ~WindowSystemAdapter() {}
103 /** Vector of all the registered windows.
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
111 GraphicsWindow* registerWindow(osg::GraphicsContext* gc,
112 const std::string& windowName);
113 /** Initialize the plib pui interface library. This might happen
114 *in another thread and may be deferred.
116 virtual void puInitialize();
117 /** Find a window by name.
118 * @param name the window name
119 * @return the window or 0
121 GraphicsWindow* findWindow(const std::string& name);
122 /** Get the global WindowSystemAdapter
123 * @return the adapter
125 static WindowSystemAdapter* getWSA() { return _wsa.get(); }
126 /** Set the global adapter
127 * @param wsa the adapter
129 static void setWSA(WindowSystemAdapter* wsa) { _wsa = wsa; }
132 osg::ref_ptr<GraphicsContextOperation> _puInitOp;
133 bool _isPuInitialized;
134 static osg::ref_ptr<WindowSystemAdapter> _wsa;
135 // Default callbacks for plib
136 static int puGetWindow();
137 static void puGetWindowSize(int* width, int* height);
142 * Class for testing if flags are set in an object with a flags member.
145 class FlagTester : public std::unary_function<osg::ref_ptr<T>, bool>
148 /** Initialize with flags to test for.
149 * @param flags logical or of flags to test.
151 FlagTester(unsigned flags_) : flags(flags_) {}
153 * @param obj An object with a flags member
154 * @return true if flags member of obj contains any of the flags
155 * (bitwise and with flags is nonzero).
157 bool operator() (const osg::ref_ptr<T>& obj)
159 return (obj->flags & flags) != 0;