Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WindowGLFW.cpp 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*!
  2. * \file src/system/WindowGLFW.cpp
  3. * \brief GLFW windowing implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Log.h"
  9. #include "RunTime.h"
  10. #include "UI.h"
  11. #include "system/Window.h"
  12. #include "system/WindowGLFW.h"
  13. glm::i32vec2 WindowGLFW::size(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  14. bool WindowGLFW::fullscreen = false;
  15. bool WindowGLFW::mousegrab = false;
  16. bool WindowGLFW::textinput = false;
  17. GLFWwindow* WindowGLFW::window = nullptr;
  18. int WindowGLFW::lastMouseX = 0;
  19. int WindowGLFW::lastMouseY = 0;
  20. bool WindowGLFW::modShift = false;
  21. bool WindowGLFW::modControl = false;
  22. bool WindowGLFW::modAlt = false;
  23. bool WindowGLFW::modSuper = false;
  24. int WindowGLFW::initialize() {
  25. glfwSetErrorCallback(WindowGLFW::errorCallback);
  26. if (!glfwInit()) {
  27. return -1;
  28. }
  29. glfwWindowHint(GLFW_SAMPLES, 4);
  30. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  31. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  32. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  33. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  34. window = glfwCreateWindow(size.x, size.y, VERSION,
  35. fullscreen ? glfwGetPrimaryMonitor() : nullptr, nullptr);
  36. if (!window) {
  37. glfwTerminate();
  38. return -2;
  39. }
  40. glfwMakeContextCurrent(window);
  41. glfwSetWindowSizeCallback(window, WindowGLFW::sizeCallback);
  42. glfwSetCursorPosCallback(window, WindowGLFW::cursorCallback);
  43. glfwSetKeyCallback(window, WindowGLFW::keyCallback);
  44. glfwSetMouseButtonCallback(window, WindowGLFW::buttonCallback);
  45. glfwSetScrollCallback(window, WindowGLFW::scrollCallback);
  46. return 0;
  47. }
  48. void WindowGLFW::eventHandling() {
  49. glfwPollEvents();
  50. if (glfwWindowShouldClose(window)) {
  51. RunTime::setRunning(false);
  52. }
  53. UI::eventsFinished();
  54. }
  55. void WindowGLFW::swapBuffers() {
  56. glfwSwapBuffers(window);
  57. }
  58. void WindowGLFW::shutdown() {
  59. if (window) {
  60. glfwDestroyWindow(window);
  61. glfwTerminate();
  62. window = nullptr;
  63. }
  64. }
  65. void WindowGLFW::setSize(glm::i32vec2 s) {
  66. assert((s.x > 0) && (s.y > 0));
  67. if (window) {
  68. if ((size.x != s.x) || (size.y != s.y)) {
  69. glfwSetWindowSize(window, s.x, s.y);
  70. }
  71. }
  72. size = s;
  73. }
  74. void WindowGLFW::setFullscreen(bool f) {
  75. fullscreen = f;
  76. //! \todo GLFW does not support toggling fullscreen?!
  77. }
  78. void WindowGLFW::setMousegrab(bool g) {
  79. mousegrab = g;
  80. if (window) {
  81. if (mousegrab)
  82. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  83. else
  84. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  85. }
  86. }
  87. void WindowGLFW::setTextInput(bool t) {
  88. textinput = t;
  89. }
  90. void WindowGLFW::setClipboard(const char* s) {
  91. if (window)
  92. glfwSetClipboardString(window, s);
  93. }
  94. const char* WindowGLFW::getClipboard() {
  95. if (window)
  96. return glfwGetClipboardString(window);
  97. return nullptr;
  98. }
  99. void WindowGLFW::errorCallback(int error, const char* desc) {
  100. Log::get(LOG_ERROR) << "GLFW Error (" << error << "): " << desc << Log::endl;
  101. }
  102. void WindowGLFW::sizeCallback(GLFWwindow* w, int width, int height) {
  103. size = glm::i32vec2(width, height);
  104. Window::setSize(size);
  105. }
  106. void WindowGLFW::cursorCallback(GLFWwindow* w, double xpos, double ypos) {
  107. int xrel = xpos - lastMouseX;
  108. int yrel = ypos - lastMouseY;
  109. UI::handleMouseMotion(xrel, yrel, xpos, ypos);
  110. lastMouseX = xpos;
  111. lastMouseY = ypos;
  112. }
  113. void WindowGLFW::keyCallback(GLFWwindow* w, int key, int scancode, int action, int mods) {
  114. if (((mods & GLFW_MOD_SHIFT) != 0) != modShift) {
  115. modShift = (mods & GLFW_MOD_SHIFT) != 0;
  116. UI::handleKeyboard(leftshiftKey, modShift);
  117. }
  118. if (((mods & GLFW_MOD_CONTROL) != 0) != modControl) {
  119. modControl = (mods & GLFW_MOD_CONTROL) != 0;
  120. UI::handleKeyboard(leftctrlKey, modControl);
  121. }
  122. if (((mods & GLFW_MOD_ALT) != 0) != modAlt) {
  123. modAlt = (mods & GLFW_MOD_ALT) != 0;
  124. UI::handleKeyboard(leftaltKey, modAlt);
  125. }
  126. if (((mods & GLFW_MOD_SUPER) != 0) != modSuper) {
  127. modSuper = (mods & GLFW_MOD_SUPER) != 0;
  128. UI::handleKeyboard(leftguiKey, modSuper);
  129. }
  130. if (textinput && (action != GLFW_RELEASE)) {
  131. //! \todo GLFW does not support UTF8 text input?!
  132. if ((key >= '0') && (key <= '9')) {
  133. char s[2] = { (char)key, '\0' };
  134. UI::handleText(s, false);
  135. } else if ((key >= 'A') && (key <= 'Z')) {
  136. key = key - 'A' + 'a';
  137. char s[2] = { (char)key, '\0' };
  138. UI::handleText(s, false);
  139. key = key - 'a' + 'A';
  140. }
  141. }
  142. KeyboardButton b = convertAsciiButton(key);
  143. UI::handleKeyboard(b, (action != GLFW_RELEASE));
  144. }
  145. void WindowGLFW::buttonCallback(GLFWwindow* w, int button, int action, int mods) {
  146. if (((mods & GLFW_MOD_SHIFT) != 0) != modShift) {
  147. modShift = (mods & GLFW_MOD_SHIFT) != 0;
  148. UI::handleKeyboard(leftshiftKey, modShift);
  149. }
  150. if (((mods & GLFW_MOD_CONTROL) != 0) != modControl) {
  151. modControl = (mods & GLFW_MOD_CONTROL) != 0;
  152. UI::handleKeyboard(leftctrlKey, modControl);
  153. }
  154. if (((mods & GLFW_MOD_ALT) != 0) != modAlt) {
  155. modAlt = (mods & GLFW_MOD_ALT) != 0;
  156. UI::handleKeyboard(leftaltKey, modAlt);
  157. }
  158. if (((mods & GLFW_MOD_SUPER) != 0) != modSuper) {
  159. modSuper = (mods & GLFW_MOD_SUPER) != 0;
  160. UI::handleKeyboard(leftguiKey, modSuper);
  161. }
  162. KeyboardButton b;
  163. switch (button) {
  164. case GLFW_MOUSE_BUTTON_LEFT:
  165. b = leftmouseKey;
  166. break;
  167. case GLFW_MOUSE_BUTTON_RIGHT:
  168. b = rightmouseKey;
  169. break;
  170. case GLFW_MOUSE_BUTTON_MIDDLE:
  171. b = middlemouseKey;
  172. break;
  173. default:
  174. b = unknownKey;
  175. break;
  176. }
  177. UI::handleMouseClick(lastMouseX, lastMouseY, b, (action == GLFW_RELEASE));
  178. }
  179. void WindowGLFW::scrollCallback(GLFWwindow* w, double xoffset, double yoffset) {
  180. UI::handleMouseScroll(xoffset, yoffset);
  181. }
  182. KeyboardButton WindowGLFW::convertAsciiButton(int key) {
  183. // Alphanumerics can be returned as is
  184. if ((key >= '0') && (key <= '9')) {
  185. return static_cast<KeyboardButton>(key);
  186. } else if ((key >= 'A') && (key <= 'Z')) {
  187. key = key - 'A' + 'a';
  188. return static_cast<KeyboardButton>(key);
  189. }
  190. //! \fixme GLFW requires keyboard layout? Currently US is hard coded
  191. switch (key) {
  192. case ' ':
  193. return spaceKey;
  194. case '!':
  195. return oneKey;
  196. case '@':
  197. return twoKey;
  198. case '#':
  199. return threeKey;
  200. case '$':
  201. return fourKey;
  202. case '%':
  203. return fiveKey;
  204. case '^':
  205. return sixKey;
  206. case '&':
  207. return sevenKey;
  208. case '*':
  209. return eightKey;
  210. case '(':
  211. return nineKey;
  212. case ')':
  213. return zeroKey;
  214. case '"':
  215. case '\'':
  216. return quoteKey;
  217. case '+':
  218. case '=':
  219. return equalsKey;
  220. case ',':
  221. case '<':
  222. return commaKey;
  223. case '-':
  224. case '_':
  225. return minusKey;
  226. case '.':
  227. case '>':
  228. return dotKey;
  229. case '/':
  230. case '?':
  231. return slashKey;
  232. case ':':
  233. case ';':
  234. return semicolonKey;
  235. case '[':
  236. case '{':
  237. return leftbracketKey;
  238. case ']':
  239. case '}':
  240. return rightbracketKey;
  241. case '\\':
  242. case '|':
  243. return backslashKey;
  244. case '`':
  245. case '~':
  246. return backquoteKey;
  247. case GLFW_KEY_TAB:
  248. return tabKey;
  249. case GLFW_KEY_BACKSPACE:
  250. return backspaceKey;
  251. case GLFW_KEY_ENTER:
  252. return enterKey;
  253. case GLFW_KEY_ESCAPE:
  254. return escapeKey;
  255. case GLFW_KEY_F1:
  256. return f1Key;
  257. case GLFW_KEY_F2:
  258. return f2Key;
  259. case GLFW_KEY_F3:
  260. return f3Key;
  261. case GLFW_KEY_F4:
  262. return f4Key;
  263. case GLFW_KEY_F5:
  264. return f5Key;
  265. case GLFW_KEY_F6:
  266. return f6Key;
  267. case GLFW_KEY_F7:
  268. return f7Key;
  269. case GLFW_KEY_F8:
  270. return f8Key;
  271. case GLFW_KEY_F9:
  272. return f9Key;
  273. case GLFW_KEY_F10:
  274. return f10Key;
  275. case GLFW_KEY_F11:
  276. return f11Key;
  277. case GLFW_KEY_F12:
  278. return f12Key;
  279. case GLFW_KEY_LEFT:
  280. return leftKey;
  281. case GLFW_KEY_UP:
  282. return upKey;
  283. case GLFW_KEY_RIGHT:
  284. return rightKey;
  285. case GLFW_KEY_DOWN:
  286. return downKey;
  287. case GLFW_KEY_PAGE_UP:
  288. return pageupKey;
  289. case GLFW_KEY_PAGE_DOWN:
  290. return pagedownKey;
  291. case GLFW_KEY_HOME:
  292. return homeKey;
  293. case GLFW_KEY_END:
  294. return endKey;
  295. case GLFW_KEY_INSERT:
  296. return insertKey;
  297. default:
  298. return unknownKey;
  299. }
  300. }