OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
WindowGLFW.cpp
Go to the documentation of this file.
1 
8 #include "global.h"
9 #include "Log.h"
10 #include "RunTime.h"
11 #include "UI.h"
12 #include "system/Window.h"
13 #include "system/WindowGLFW.h"
14 
15 glm::i32vec2 WindowGLFW::size(DEFAULT_WIDTH, DEFAULT_HEIGHT);
16 bool WindowGLFW::fullscreen = false;
17 bool WindowGLFW::mousegrab = false;
18 bool WindowGLFW::textinput = false;
19 GLFWwindow* WindowGLFW::window = nullptr;
22 bool WindowGLFW::modShift = false;
23 bool WindowGLFW::modControl = false;
24 bool WindowGLFW::modAlt = false;
25 bool WindowGLFW::modSuper = false;
26 
28  glfwSetErrorCallback(WindowGLFW::errorCallback);
29  if (!glfwInit()) {
30  return -1;
31  }
32 
33  glfwWindowHint(GLFW_SAMPLES, 4);
34  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
35  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
36  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
37  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
38 
39  window = glfwCreateWindow(size.x, size.y, VERSION,
40  fullscreen ? glfwGetPrimaryMonitor() : nullptr, nullptr);
41  if (!window) {
42  glfwTerminate();
43  return -2;
44  }
45 
46  glfwMakeContextCurrent(window);
47 
48  glfwSetWindowSizeCallback(window, WindowGLFW::sizeCallback);
49  glfwSetCursorPosCallback(window, WindowGLFW::cursorCallback);
50  glfwSetKeyCallback(window, WindowGLFW::keyCallback);
51  glfwSetMouseButtonCallback(window, WindowGLFW::buttonCallback);
52  glfwSetScrollCallback(window, WindowGLFW::scrollCallback);
53 
54  int w = size.x, h = size.y;
55  glfwGetWindowSize(window, &w, &h);
56  size.x = w;
57  size.y = h;
58 
59  return 0;
60 }
61 
63  glfwPollEvents();
64 
65  if (glfwWindowShouldClose(window)) {
66  RunTime::setRunning(false);
67  }
68 
70 }
71 
73  glfwSwapBuffers(window);
74 }
75 
77  if (window) {
78  glfwDestroyWindow(window);
79  glfwTerminate();
80  window = nullptr;
81  }
82 }
83 
84 void WindowGLFW::setSize(glm::i32vec2 s) {
85  assert((s.x > 0) && (s.y > 0));
86  if (window) {
87  if ((size.x != s.x) || (size.y != s.y)) {
88  glfwSetWindowSize(window, s.x, s.y);
89  }
90  }
91  size = s;
92 }
93 
95  fullscreen = f;
97 }
98 
100  mousegrab = g;
101 
102  if (window) {
103  if (mousegrab)
104  glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
105  else
106  glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
107  }
108 }
109 
111  textinput = t;
112 }
113 
114 void WindowGLFW::setClipboard(const char* s) {
115  if (window)
116  glfwSetClipboardString(window, s);
117 }
118 
120  if (window)
121  return glfwGetClipboardString(window);
122 
123  return nullptr;
124 }
125 
127 
128 }
129 
130 void WindowGLFW::errorCallback(int error, const char* desc) {
131  Log::get(LOG_ERROR) << "GLFW Error (" << error << "): " << desc << Log::endl;
132 }
133 
134 void WindowGLFW::sizeCallback(GLFWwindow* w, int width, int height) {
135  size = glm::i32vec2(width, height);
137 }
138 
139 void WindowGLFW::cursorCallback(GLFWwindow* w, double xpos, double ypos) {
140  int xrel = xpos - lastMouseX;
141  int yrel = ypos - lastMouseY;
142  UI::handleMouseMotion(xrel, yrel, xpos, ypos);
143  lastMouseX = xpos;
144  lastMouseY = ypos;
145 }
146 
147 void WindowGLFW::keyCallback(GLFWwindow* w, int key, int scancode, int action, int mods) {
148  if (((mods & GLFW_MOD_SHIFT) != 0) != modShift) {
149  modShift = (mods & GLFW_MOD_SHIFT) != 0;
151  }
152 
153  if (((mods & GLFW_MOD_CONTROL) != 0) != modControl) {
154  modControl = (mods & GLFW_MOD_CONTROL) != 0;
156  }
157 
158  if (((mods & GLFW_MOD_ALT) != 0) != modAlt) {
159  modAlt = (mods & GLFW_MOD_ALT) != 0;
161  }
162 
163  if (((mods & GLFW_MOD_SUPER) != 0) != modSuper) {
164  modSuper = (mods & GLFW_MOD_SUPER) != 0;
166  }
167 
168  if (textinput && (action != GLFW_RELEASE)) {
170  if ((key >= '0') && (key <= '9')) {
171  char s[2] = { (char)key, '\0' };
172  UI::handleText(s, false);
173  } else if ((key >= 'A') && (key <= 'Z')) {
174  key = key - 'A' + 'a';
175  char s[2] = { (char)key, '\0' };
176  UI::handleText(s, false);
177  key = key - 'a' + 'A';
178  }
179  }
180 
182  UI::handleKeyboard(b, (action != GLFW_RELEASE));
183 }
184 
185 void WindowGLFW::buttonCallback(GLFWwindow* w, int button, int action, int mods) {
186  if (((mods & GLFW_MOD_SHIFT) != 0) != modShift) {
187  modShift = (mods & GLFW_MOD_SHIFT) != 0;
189  }
190 
191  if (((mods & GLFW_MOD_CONTROL) != 0) != modControl) {
192  modControl = (mods & GLFW_MOD_CONTROL) != 0;
194  }
195 
196  if (((mods & GLFW_MOD_ALT) != 0) != modAlt) {
197  modAlt = (mods & GLFW_MOD_ALT) != 0;
199  }
200 
201  if (((mods & GLFW_MOD_SUPER) != 0) != modSuper) {
202  modSuper = (mods & GLFW_MOD_SUPER) != 0;
204  }
205 
206  KeyboardButton b;
207  switch (button) {
208  case GLFW_MOUSE_BUTTON_LEFT:
209  b = leftmouseKey;
210  break;
211 
212  case GLFW_MOUSE_BUTTON_RIGHT:
213  b = rightmouseKey;
214  break;
215 
216  case GLFW_MOUSE_BUTTON_MIDDLE:
217  b = middlemouseKey;
218  break;
219 
220  default:
221  b = unknownKey;
222  break;
223  }
224 
225  UI::handleMouseClick(lastMouseX, lastMouseY, b, (action == GLFW_RELEASE));
226 }
227 
228 void WindowGLFW::scrollCallback(GLFWwindow* w, double xoffset, double yoffset) {
229  UI::handleMouseScroll(xoffset, yoffset);
230 }
231 
233  // Alphanumerics can be returned as is
234  if ((key >= '0') && (key <= '9')) {
235  return static_cast<KeyboardButton>(key);
236  } else if ((key >= 'A') && (key <= 'Z')) {
237  key = key - 'A' + 'a';
238  return static_cast<KeyboardButton>(key);
239  }
240 
242  switch (key) {
243  case ' ':
244  return spaceKey;
245 
246  case '!':
247  return oneKey;
248 
249  case '@':
250  return twoKey;
251 
252  case '#':
253  return threeKey;
254 
255  case '$':
256  return fourKey;
257 
258  case '%':
259  return fiveKey;
260 
261  case '^':
262  return sixKey;
263 
264  case '&':
265  return sevenKey;
266 
267  case '*':
268  return eightKey;
269 
270  case '(':
271  return nineKey;
272 
273  case ')':
274  return zeroKey;
275 
276  case '"':
277  case '\'':
278  return quoteKey;
279 
280  case '+':
281  case '=':
282  return equalsKey;
283 
284  case ',':
285  case '<':
286  return commaKey;
287 
288  case '-':
289  case '_':
290  return minusKey;
291 
292  case '.':
293  case '>':
294  return dotKey;
295 
296  case '/':
297  case '?':
298  return slashKey;
299 
300  case ':':
301  case ';':
302  return semicolonKey;
303 
304  case '[':
305  case '{':
306  return leftbracketKey;
307 
308  case ']':
309  case '}':
310  return rightbracketKey;
311 
312  case '\\':
313  case '|':
314  return backslashKey;
315 
316  case '`':
317  case '~':
318  return backquoteKey;
319 
320  case GLFW_KEY_TAB:
321  return tabKey;
322 
323  case GLFW_KEY_BACKSPACE:
324  return backspaceKey;
325 
326  case GLFW_KEY_ENTER:
327  return enterKey;
328 
329  case GLFW_KEY_ESCAPE:
330  return escapeKey;
331 
332  case GLFW_KEY_F1:
333  return f1Key;
334 
335  case GLFW_KEY_F2:
336  return f2Key;
337 
338  case GLFW_KEY_F3:
339  return f3Key;
340 
341  case GLFW_KEY_F4:
342  return f4Key;
343 
344  case GLFW_KEY_F5:
345  return f5Key;
346 
347  case GLFW_KEY_F6:
348  return f6Key;
349 
350  case GLFW_KEY_F7:
351  return f7Key;
352 
353  case GLFW_KEY_F8:
354  return f8Key;
355 
356  case GLFW_KEY_F9:
357  return f9Key;
358 
359  case GLFW_KEY_F10:
360  return f10Key;
361 
362  case GLFW_KEY_F11:
363  return f11Key;
364 
365  case GLFW_KEY_F12:
366  return f12Key;
367 
368  case GLFW_KEY_LEFT:
369  return leftKey;
370 
371  case GLFW_KEY_UP:
372  return upKey;
373 
374  case GLFW_KEY_RIGHT:
375  return rightKey;
376 
377  case GLFW_KEY_DOWN:
378  return downKey;
379 
380  case GLFW_KEY_PAGE_UP:
381  return pageupKey;
382 
383  case GLFW_KEY_PAGE_DOWN:
384  return pagedownKey;
385 
386  case GLFW_KEY_HOME:
387  return homeKey;
388 
389  case GLFW_KEY_END:
390  return endKey;
391 
392  case GLFW_KEY_INSERT:
393  return insertKey;
394 
395  default:
396  return unknownKey;
397  }
398 }
399 
static void swapBuffers()
Definition: WindowGLFW.cpp:72
Definition: global.h:48
static GLFWwindow * window
Definition: WindowGLFW.h:53
static void setFullscreen(bool f)
Definition: WindowGLFW.cpp:94
static void handleMouseScroll(int xrel, int yrel)
Definition: UI.cpp:403
static void eventHandling()
Definition: WindowGLFW.cpp:62
Definition: global.h:48
GLFW Windowing Implementation.
static void cursorCallback(GLFWwindow *w, double xpos, double ypos)
Definition: WindowGLFW.cpp:139
static glm::i32vec2 size
Definition: WindowGLFW.h:49
static bool modAlt
Definition: WindowGLFW.h:58
Definition: global.h:49
static bool fullscreen
Definition: WindowGLFW.h:50
static void setClipboard(const char *s)
Definition: WindowGLFW.cpp:114
Definition: global.h:36
Definition: global.h:49
static bool modControl
Definition: WindowGLFW.h:57
KeyboardButton
Definition: global.h:34
Definition: global.h:36
Definition: global.h:36
static bool textinput
Definition: WindowGLFW.h:52
Included everywhere.
Definition: global.h:47
static LogLevel & get(int level)
Definition: Log.cpp:14
static int lastMouseX
Definition: WindowGLFW.h:54
Definition: global.h:37
static void handleMouseMotion(int xrel, int yrel, int xabs, int yabs)
Definition: UI.cpp:396
static bool mousegrab
Definition: WindowGLFW.h:51
static void setMousegrab(bool g)
Definition: WindowGLFW.cpp:99
static void scrollCallback(GLFWwindow *w, double xoffset, double yoffset)
Definition: WindowGLFW.cpp:228
Global Logging Utility.
Definition: global.h:49
Windowing Interface.
Definition: global.h:37
#define assert(x)
Definition: global.h:124
static void handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released)
Definition: UI.cpp:378
static void error(char *msg)
Definition: commander.c:19
static KeyboardButton convertAsciiButton(int key)
Definition: WindowGLFW.cpp:232
static int initialize()
Definition: WindowGLFW.cpp:27
static void setRunning(bool run)
Definition: RunTime.h:36
static int lastMouseY
Definition: WindowGLFW.h:55
Definition: global.h:39
static void buttonCallback(GLFWwindow *w, int button, int action, int mods)
Definition: WindowGLFW.cpp:185
static bool modShift
Definition: WindowGLFW.h:56
Definition: global.h:47
static void errorCallback(int error, const char *desc)
Definition: WindowGLFW.cpp:130
Definition: global.h:49
Definition: global.h:48
Definition: global.h:50
Definition: global.h:52
static bool modSuper
Definition: WindowGLFW.h:59
static const char endl
Definition: Log.h:35
Runtime Configuration Storage.
static const char * getClipboard()
Definition: WindowGLFW.cpp:119
#define LOG_ERROR
Definition: Log.h:19
Definition: global.h:49
static void handleKeyboard(KeyboardButton key, bool pressed)
Definition: UI.cpp:355
Definition: global.h:48
static void sizeCallback(GLFWwindow *w, int width, int height)
Definition: WindowGLFW.cpp:134
UI/Event Manager.
Definition: global.h:48
Definition: global.h:48
Definition: global.h:54
Definition: global.h:49
Definition: global.h:49
Definition: global.h:47
Definition: global.h:38
static void setTextInput(bool t)
Definition: WindowGLFW.cpp:110
static void eventsFinished()
Definition: UI.cpp:147
static void handleText(char *text, bool notFinished)
Definition: UI.cpp:367
static void setSize(glm::i32vec2 s)
Definition: Window.cpp:59
static void inputPositionCallback(int x, int y)
Definition: WindowGLFW.cpp:126
static void keyCallback(GLFWwindow *w, int key, int scancode, int action, int mods)
Definition: WindowGLFW.cpp:147
static void shutdown()
Definition: WindowGLFW.cpp:76
static void setSize(glm::i32vec2 s)
Definition: WindowGLFW.cpp:84