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/GraphicsContext>
26 #include <osg/GraphicsThread>
28 #include <simgear/structure/SGAtomic.hxx>
30 // Flexible Camera and window support
33 /** A window opened by default or via rendering properties
35 class GraphicsWindow : public osg::Referenced
38 GraphicsWindow(osg::GraphicsContext* gc_, const std::string& name_,
39 int id_, unsigned flags_ = 0) :
40 gc(gc_), name(name_), id(id_), flags(flags_)
43 /** The OSG graphics context for this window.
45 osg::ref_ptr<osg::GraphicsContext> gc;
46 /** The window's internal name.
50 /** The GUI (and 2D cockpit) will be drawn on this window.
58 /** Camera associated with a 3d view. The camera might occupy an
59 * entire window or share one with other cameras.
61 class Camera3D : public osg::Referenced
64 Camera3D(GraphicsWindow* window_, osg::Camera* camera_, const std::string& name_,
65 unsigned flags_ = 0) :
66 window(window_), camera(camera_), name(name_), flags(flags_)
69 osg::ref_ptr<GraphicsWindow> window;
70 osg::ref_ptr<osg::Camera> camera;
73 SHARES_WINDOW = 1, /**< Camera shares window with other cameras*/
74 MASTER = 2 /**< Camera has same view as master camera*/
79 typedef std::vector<osg::ref_ptr<GraphicsWindow> > WindowVector;
80 typedef std::vector<osg::ref_ptr<Camera3D> > Camera3DVector;
83 * An operation that is run once with a particular GraphicsContext
86 class GraphicsContextOperation : public osg::GraphicsOperation
89 GraphicsContextOperation(const std::string& name) :
90 osg::GraphicsOperation(name, false)
93 virtual void operator()(osg::GraphicsContext* gc);
94 virtual void run(osg::GraphicsContext* gc) = 0;
95 bool isFinished() const { return done != 0; }
100 /** Adapter from windows system / graphics context management API to
101 * functions used by flightgear. This papers over the difference
102 * between osgViewer Viewer, which handles multiple windows, graphics
103 * threads, etc., and the embedded viewer used with GLUT and SDL.
105 class WindowSystemAdapter : public osg::Referenced
108 WindowSystemAdapter();
109 virtual ~WindowSystemAdapter() {}
110 WindowVector windows;
111 Camera3DVector cameras;
112 GraphicsWindow* registerWindow(osg::GraphicsContext* gc,
113 const std::string& windowName);
114 Camera3D* registerCamera3D(GraphicsWindow* gw, osg::Camera* camera,
115 const std::string& cameraName);
116 GraphicsWindow* getGUIWindow();
117 int getGUIWindowID();
118 osg::GraphicsContext* getGUIGraphicsContext();
119 /** Initialize the plib pui interface library. This might happen
120 *in another thread and may be deferred.
122 virtual void puInitialize();
123 /** Returns true if pui initialization has finished.
126 class FlagTester : public std::unary_function<osg::ref_ptr<T>, bool>
129 FlagTester(unsigned flags_) : flags(flags_) {}
130 bool operator() (const osg::ref_ptr<T>& obj)
132 return (obj->flags & flags) != 0;
136 static WindowSystemAdapter* getWSA() { return _wsa.get(); }
137 static void setWSA(WindowSystemAdapter* wsa) { _wsa = wsa; }
141 osg::ref_ptr<GraphicsContextOperation> _puInitOp;
142 bool _isPuInitialized;
143 static osg::ref_ptr<WindowSystemAdapter> _wsa;
144 // Default callbacks for plib
145 static int puGetWindow();
146 static void puGetWindowSize(int* width, int* height);