]> git.mxchange.org Git - flightgear.git/blob - src/Main/WindowSystemAdapter.hxx
header cleanups
[flightgear.git] / src / Main / WindowSystemAdapter.hxx
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/GraphicsContext>
26 #include <osg/GraphicsThread>
27
28 #include <simgear/structure/SGAtomic.hxx>
29
30 // Flexible Camera and window support
31 namespace flightgear
32 {
33 /** A window opened by default or via rendering properties
34  */
35 class GraphicsWindow : public osg::Referenced
36 {
37 public:
38     GraphicsWindow(osg::GraphicsContext* gc_, const std::string& name_,
39                    int id_, unsigned flags_ = 0) :
40         gc(gc_), name(name_), id(id_), flags(flags_)
41     {
42     }
43     /** The OSG graphics context for this window.
44      */
45     osg::ref_ptr<osg::GraphicsContext> gc;
46     /** The window's internal name.
47      */
48     std::string name;
49     enum Flags {
50         /** The GUI (and 2D cockpit) will be drawn on this window.
51          */
52         GUI = 1
53     };
54     int id;
55     unsigned flags;
56 };
57
58 /** Camera associated with a 3d view. The camera might occupy an
59  * entire window or share one with other cameras.
60  */
61 class Camera3D : public osg::Referenced
62 {
63 public:
64     Camera3D(GraphicsWindow* window_, osg::Camera* camera_, const std::string& name_,
65              unsigned flags_ = 0) :
66         window(window_), camera(camera_), name(name_), flags(flags_)
67     {
68     }
69     osg::ref_ptr<GraphicsWindow> window;
70     osg::ref_ptr<osg::Camera> camera;
71     std::string name;
72     enum Flags {
73         SHARES_WINDOW = 1,      /**< Camera shares window with other cameras*/
74         MASTER = 2              /**< Camera has same view as master camera*/
75     };
76     unsigned flags;
77 };
78
79 typedef std::vector<osg::ref_ptr<GraphicsWindow> >  WindowVector;
80 typedef std::vector<osg::ref_ptr<Camera3D> >  Camera3DVector;
81
82 /**
83  * An operation that is run once with a particular GraphicsContext
84  * current.
85  */
86 class GraphicsContextOperation : public osg::GraphicsOperation
87 {
88 public:
89     GraphicsContextOperation(const std::string& name) :
90         osg::GraphicsOperation(name, false)
91     {
92     }
93     virtual void operator()(osg::GraphicsContext* gc);
94     virtual void run(osg::GraphicsContext* gc) = 0;
95     bool isFinished() const { return done != 0; }
96 private:
97     SGAtomic done;
98 };
99
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.
104  */
105 class WindowSystemAdapter : public osg::Referenced
106 {
107 public:
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.
121      */
122     virtual void puInitialize();
123     /** Returns true if pui initialization has finished.
124      */
125     template<typename T>
126     class FlagTester : public std::unary_function<osg::ref_ptr<T>, bool>
127     {
128     public:
129         FlagTester(unsigned flags_) : flags(flags_) {}
130         bool operator() (const osg::ref_ptr<T>& obj)
131         {
132             return (obj->flags & flags) != 0;
133         }
134         unsigned flags;
135     };
136     static WindowSystemAdapter* getWSA() { return _wsa.get(); }
137     static void setWSA(WindowSystemAdapter* wsa) { _wsa = wsa; }
138 protected:
139     int _nextWindowID;
140     int _nextCameraID;
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);
147
148 };
149 }
150 #endif