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

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