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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*!
  2. * \file src/WindowGLFW.cpp
  3. * \brief GLFW windowing implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstring>
  8. #include "global.h"
  9. #include "Log.h"
  10. #include "RunTime.h"
  11. #include "UI.h"
  12. #include "utils/strings.h"
  13. #include "system/WindowGLFW.h"
  14. static int lastMouseX = 0;
  15. static int lastMouseY = 0;
  16. WindowGLFW::WindowGLFW() {
  17. mInit = false;
  18. mWidth = DEFAULT_WIDTH;
  19. mHeight = DEFAULT_HEIGHT;
  20. mFullscreen = false;
  21. mMousegrab = false;
  22. mTextInput = false;
  23. mWindow = nullptr;
  24. }
  25. WindowGLFW::~WindowGLFW() {
  26. if (mInit) {
  27. if (mWindow) {
  28. glfwDestroyWindow(mWindow);
  29. }
  30. glfwTerminate();
  31. }
  32. }
  33. int WindowGLFW::initialize() {
  34. assert(mInit == false);
  35. glfwSetErrorCallback(WindowGLFW::errorCallback);
  36. if (!glfwInit()) {
  37. return -1;
  38. }
  39. glfwWindowHint(GLFW_SAMPLES, 4);
  40. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  41. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  42. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  43. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  44. mWindow = glfwCreateWindow(mWidth, mHeight, VERSION,
  45. mFullscreen ? glfwGetPrimaryMonitor() : nullptr, nullptr);
  46. if (!mWindow) {
  47. glfwTerminate();
  48. return -2;
  49. }
  50. glfwMakeContextCurrent(mWindow);
  51. glfwSetWindowSizeCallback(mWindow, WindowGLFW::sizeCallback);
  52. glfwSetCursorPosCallback(mWindow, WindowGLFW::cursorCallback);
  53. glfwSetKeyCallback(mWindow, WindowGLFW::keyCallback);
  54. glfwSetMouseButtonCallback(mWindow, WindowGLFW::buttonCallback);
  55. glfwSetScrollCallback(mWindow, WindowGLFW::scrollCallback);
  56. mInit = true;
  57. return 0;
  58. }
  59. void WindowGLFW::eventHandling() {
  60. assert(mInit == true);
  61. glfwPollEvents();
  62. if (glfwWindowShouldClose(mWindow)) {
  63. getRunTime().setRunning(false);
  64. }
  65. UI::eventsFinished();
  66. }
  67. void WindowGLFW::setSize(unsigned int width, unsigned int height) {
  68. assert(width > 0);
  69. assert(height > 0);
  70. if (mInit) {
  71. if ((mWidth != width) || (mHeight != height)) {
  72. glfwSetWindowSize(mWindow, width, height);
  73. getWindow().resizeGL();
  74. }
  75. }
  76. mWidth = width;
  77. mHeight = height;
  78. }
  79. void WindowGLFW::setFullscreen(bool fullscreen) {
  80. mFullscreen = fullscreen;
  81. //! \todo GLFW does not support toggling fullscreen?!
  82. }
  83. void WindowGLFW::setMousegrab(bool grab) {
  84. mMousegrab = grab;
  85. if (mInit == true) {
  86. if (mMousegrab)
  87. glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  88. else
  89. glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  90. }
  91. }
  92. void WindowGLFW::setTextInput(bool on) {
  93. assert(mInit == true);
  94. mTextInput = on;
  95. }
  96. void WindowGLFW::swapBuffersGL() {
  97. assert(mInit == true);
  98. glfwSwapBuffers(mWindow);
  99. }
  100. void WindowGLFW::errorCallback(int error, const char* desc) {
  101. getLog() << "GLFW Error (" << error << "): " << desc << Log::endl;
  102. }
  103. void WindowGLFW::sizeCallback(GLFWwindow* w, int width, int height) {
  104. getWindow().setSize(width, height);
  105. }
  106. void WindowGLFW::cursorCallback(GLFWwindow* w, double xpos, double ypos) {
  107. int xrel = xpos - lastMouseX;
  108. int yrel = ypos - lastMouseY;
  109. UI::handleMouseMotion(xrel, yrel, xpos, ypos);
  110. lastMouseX = xpos;
  111. lastMouseY = ypos;
  112. }
  113. static bool modShift = false;
  114. static bool modControl = false;
  115. static bool modAlt = false;
  116. static bool modSuper = false;
  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 (getWindow().getTextInput() && (action != GLFW_RELEASE)) {
  135. //! \todo Handle text input properly!
  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. }