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

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