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 8.8KB

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