forked from funkey/gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenGl.h
150 lines (107 loc) · 2.96 KB
/
OpenGl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef OPEN_GL_H__
#define OPEN_GL_H__
#include <string>
#include <boost/thread.hpp>
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <util/exceptions.h>
namespace gui {
namespace priv {
/**
* Check for an OpenGl error. Throws an exception if an error was found.
*/
void glCheckError(const char* file, const char* function, unsigned int line);
#ifndef NDEBUG
// executes an OpenGl call and throw an exception if an OpenGl error occured
#define glCheck(call) ((call), gui::priv::glCheckError(__FILE__, BOOST_CURRENT_FUNCTION, __LINE__))
#else
#define glCheck(call) (call)
#endif
} // namespace priv
// forward declaration
class GlContext;
class GlContextCreator;
// exceptions
struct OpenGlError : virtual GuiError {};
class OpenGl {
public:
/**
* Instantiation of this class guarantees the calling thread
* to have a valid OpenGl context.
*
* This is guaranteed for the whole scope of the instantiation,
* that means in particular, it is exception safe.
*/
class Guard {
public:
/**
* Default contructor.
*
* Creates a new GlContext for the calling thread if none has been
* set so far and activates it. As soon as the thread stops, this
* context will be destructed.
*/
Guard();
/**
* Replaces the current GlContext (if existing) for the current
* thread with the one created by contextCreator. If contextCreator
* is 0, this guard ensures that the previous context of this thread
* will be destructed.
*
* @param A pointer to a GlContextCreator that returns a new
* GlContext or 0 to destruct the previous GlContext.
*/
Guard(GlContextCreator* contextCreator);
/**
* Deactivates the current context.
*/
~Guard();
private:
GlContextCreator* getPreviousContextCreator();
bool createNewContex(GlContextCreator* contextCreator);
bool reusePreviousContext();
void invalidateCurrentContext();
// the singleton object
OpenGl* _openGl;
// indicates whether to deactivate the current context on destruction
bool _deactivateContext;
};
/**
* Get the global opengl mutex.
*
* Protect every opengl operation to the global context with this mutex.
*/
static boost::mutex& getMutex();
/**
* Get access to the global OpenGL context.
*/
static GlContext* getGlobalContext();
/**
* Flush the currently active GlContext.
*/
static void flush();
private:
/**
* This class is not supposed to be instantiated.
*/
OpenGl();
/**
* Even singletons should be cleand up.
*/
~OpenGl();
/**
* Get the singleton OpenGl object.
*/
static OpenGl* getInstance();
boost::mutex _mutex;
GlContext* _globalContext;
// a GlContext for the current context
boost::thread_specific_ptr<GlContext> _context;
// the factory that created the current context of the current thread
boost::thread_specific_ptr<GlContextCreator*> _contextCreator;
};
} // namespace gui
#endif // OPEN_GL_H__