-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglfwlibrary.h
238 lines (195 loc) · 5.72 KB
/
glfwlibrary.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifndef GLFWW_LIBRARY_H
#define GLFWW_LIBRARY_H
#include <cstring>
#include <functional>
#include <string>
#include "monitor.h"
#include "window.h"
namespace glfwW
{
enum class ErrorCode
{
NO_ERROR = 0,
NOT_INITIALIZED = 0x00010001,
NO_CURRENT_CONTEXT = 0x00010002,
INVALID_ENUM = 0x00010003,
INVALID_VALUE = 0x00010004,
OUT_OF_MEMORY = 0x00010005,
API_UNAVAILABLE = 0x00010006,
VERSION_UNAVAILABLE = 0x00010007,
PLATFORM_ERROR = 0x00010008,
FORMAT_UNAVAILABLE = 0x00010009,
NO_WINDOW_CONTEXT = 0x0001000A,
};
struct Error
{
ErrorCode code = ErrorCode::NO_ERROR;
std::string description;
};
enum class MonitorEventType
{
CONNECTED,
DISCONNECTED
};
MonitorEventType fromGlfwMonitorEventType(int type);
struct MonitorEvent
{
Monitor monitor;
MonitorEventType type = MonitorEventType::CONNECTED;
};
void errorCallback(int errorCode, const char *description);
void monitorCallback(GLFWmonitor* monitor, int event);
/*!
* \brief The GLFW library wrapper class
*/
class GLFWlibrary
{
public:
typedef void(* ErrorHandler) (const Error&);
typedef void(* MonitorHandler) (const MonitorEvent&);
struct InitHints
{
InitHints():
joystickHatButtons(true),
cocoaChdirResources(true),
cocoaMenubar(true)
{
}
bool joystickHatButtons = true;
bool cocoaChdirResources = true;
bool cocoaMenubar = true;
};
struct Version
{
int major = 0;
int minor = 0;
int revision = 0;
std::string versionString;
};
public:
static GLFWlibrary& instance()
{
static GLFWlibrary inst;
return inst;
}
//INITIALIZATION
/*!
* \brief Initialize GLFW library. Has to be called before library using.
*/
Error init(InitHints hints = InitHints());
/*!
* \brief Returns if GLFW has been initialized already.
*/
bool initialized() const {return m_initialized;}
void deinit()
{
m_errorHandler = nullptr;
glfwTerminate();
}
//ERRORS
void setErrorCallback(ErrorHandler* handler);
Error readLastError() const
{
Error result;
const char* err;
result.code = static_cast<ErrorCode>(glfwGetError(&err));
result.description.assign(err);
return result;
}
//INFORMATION
Version version() const
{
Version result;
glfwGetVersion(&result.major, &result.minor, &result.revision);
const char* versionString = glfwGetVersionString();
result.versionString.assign(versionString);
return result;
}
//MONITORS
/*!
* \brief Returns information about primary monitor
*/
Monitor getPrimaryMonitor() const;
/*!
* \brief Returns information about all currently connected monitors
* \return
*/
std::vector<Monitor> getMonitors() const;
/*!
* \brief Returns current monitor for the window.
*/
Monitor getContainingMonitor(const Window& window);
/*!
* \brief Sets monitor handler callback. Callback is invoked if monitor was connected or disconnected.
*/
void setMonitorHandler(MonitorHandler* h);
//WINDOWS
/*!
* \brief Creates a fullscreen window on a particular monitor
*/
Window createWindow(const Monitor& monitor, const std::string& title);
/*!
* \brief Creates a fullscreen window on a particular monitor
*/
Window createWindow(const Monitor& monitor, Vec2<int> resolution, const std::string& title);
/*!
* \brief Creates a new window in windowed mode
*/
Window createWindow(Vec2<int> size, const std::string& title);
/*!
* \brief Creates a new window with particular creation hints
*/
Window createWindow(const WindowCreationHints& hints, Vec2<int> size, const std::string& title);
/*!
* \brief Returns a set of window creation hints which were changed by this wrapper.
*/
WindowCreationHints getWindowCreationHints() const;
/*!
* \brief Changes given window creation hints.
*/
void apply(WindowCreationHints hints);
/*!
* \brief Resets window creation hints to default values.
*/
void resetWindowCreationHintsToDefault();
//EVENTS
/*!
* \brief Processes events from the event queue immediately. Processing events will cause the window and input callbacks associated with those events to be called.
*/
void pollEvents() const;
/*!
* \brief Puts the calling thread to sleep until at least one event is available in the event queue.
* Once one or more events are available, the events in the queue are processed and the function then returns immediately.
*/
void waitEvents() const;
/*!
* \brief Puts the calling thread to sleep until at least one event is available in the event queue, or until the specified timeout is reached.
*/
void waitEventsTimeout(double time) const;
// KEYBOARD
/*!
* \brief Returns scancode for the key
*/
int getKeyScancode(Key key) const;
const char* getKeyName(Key key) const;
const char* getKeyName(int scancode);
// MOUSE
bool isRawMouseMotionSupported() const;
private:
friend void errorCallback(int errorCode, const char *description);
friend void monitorCallback(GLFWmonitor* monitor, int event);
GLFWlibrary() = default;
~GLFWlibrary()
{
deinit();
}
void onError(int errorCode, const char *description) const;
void onMonitorEvent(GLFWmonitor* monitor, int event) const;
private:
bool m_initialized = false;
ErrorHandler* m_errorHandler = nullptr;
MonitorHandler* m_monitorHandler = nullptr;
WindowCreationHints m_currentHints;
};
}
#endif