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.1KB

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