Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

WindowGLFW.cpp 8.8KB

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