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.

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