]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/WindowSystemAdapter.hxx
View can created itself from config properties
[flightgear.git] / src / Viewer / 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/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     /** Initialize the plib pui interface library. This might happen
114      *in another thread and may be deferred.
115      */
116     virtual void puInitialize();
117     /** Find a window by name.
118      * @param name the window name
119      * @return the window or 0
120      */
121     GraphicsWindow* findWindow(const std::string& name);
122     /** Get the global WindowSystemAdapter
123      * @return the adapter
124      */
125     static WindowSystemAdapter* getWSA() { return _wsa.get(); }
126     /** Set the global adapter
127      * @param wsa the adapter
128      */
129     static void setWSA(WindowSystemAdapter* wsa) { _wsa = wsa; }
130 protected:
131     int _nextWindowID;
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);
138
139 };
140
141 /**
142  * Class for testing if flags are set in an object with a flags member.
143  */
144 template<typename T>
145 class FlagTester : public std::unary_function<osg::ref_ptr<T>, bool>
146 {
147 public:
148     /** Initialize with flags to test for.
149      * @param flags logical or of flags to test.
150      */
151     FlagTester(unsigned flags_) : flags(flags_) {}
152     /** test operator
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).
156      */
157     bool operator() (const osg::ref_ptr<T>& obj)
158     {
159         return (obj->flags & flags) != 0;
160     }
161     unsigned flags;
162 };
163
164 }
165 #endif