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

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