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.

WindowGLUT.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*!
  2. * \file src/WindowGLUT.cpp
  3. * \brief GLUT windowing implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstring>
  8. #include <GL/freeglut.h>
  9. #include "global.h"
  10. #include "RunTime.h"
  11. #include "UI.h"
  12. #include "utils/strings.h"
  13. #include "system/WindowGLUT.h"
  14. //! \todo Modifier keys currently don't create keyboard events...
  15. static int lastMouseX = 0;
  16. static int lastMouseY = 0;
  17. WindowGLUT::WindowGLUT() {
  18. mInit = false;
  19. mWidth = DEFAULT_WIDTH;
  20. mHeight = DEFAULT_HEIGHT;
  21. mFullscreen = false;
  22. mMousegrab = false;
  23. mTextInput = false;
  24. }
  25. void WindowGLUT::setSize(unsigned int width, unsigned int height) {
  26. assert(width > 0);
  27. assert(height > 0);
  28. mWidth = width;
  29. mHeight = height;
  30. if (mInit == true) {
  31. glutReshapeWindow(width, height);
  32. }
  33. }
  34. void WindowGLUT::setFullscreen(bool fullscreen) {
  35. mFullscreen = fullscreen;
  36. if (mInit == true) {
  37. if (mFullscreen)
  38. glutFullScreen();
  39. else
  40. glutLeaveFullScreen();
  41. }
  42. }
  43. void WindowGLUT::setMousegrab(bool grab) {
  44. mMousegrab = grab;
  45. if (mInit == true) {
  46. if (mMousegrab)
  47. glutSetCursor(GLUT_CURSOR_NONE);
  48. else
  49. glutSetCursor(GLUT_CURSOR_INHERIT);
  50. }
  51. }
  52. int WindowGLUT::initialize() {
  53. assert(mInit == false);
  54. int argc = 1;
  55. char* argv[] = { new char[11], nullptr };
  56. strcpy(argv[0], "OpenRaider");
  57. glutInitWindowSize(mWidth, mHeight);
  58. glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH | GLUT_MULTISAMPLE);
  59. glutInitContextVersion(3, 3);
  60. glutInitContextProfile(GLUT_CORE_PROFILE);
  61. glutInit(&argc, argv);
  62. glutCreateWindow(VERSION);
  63. glutReshapeFunc(WindowGLUT::reshapeCallback);
  64. glutKeyboardFunc(WindowGLUT::keyboardCallback);
  65. glutKeyboardUpFunc(WindowGLUT::keyboardUpCallback);
  66. glutSpecialFunc(WindowGLUT::specialCallback);
  67. glutSpecialUpFunc(WindowGLUT::specialUpCallback);
  68. glutMouseFunc(WindowGLUT::mouseCallback);
  69. glutMotionFunc(WindowGLUT::motionCallback);
  70. glutPassiveMotionFunc(WindowGLUT::motionCallback);
  71. glutMouseWheelFunc(WindowGLUT::mouseWheelCallback);
  72. delete [] argv[0];
  73. mInit = true;
  74. return 0;
  75. }
  76. void WindowGLUT::eventHandling() {
  77. assert(mInit == true);
  78. glutMainLoopEvent();
  79. UI::eventsFinished();
  80. }
  81. void WindowGLUT::setTextInput(bool on) {
  82. assert(mInit == true);
  83. mTextInput = on;
  84. }
  85. void WindowGLUT::swapBuffersGL() {
  86. assert(mInit == true);
  87. glutSwapBuffers();
  88. }
  89. void WindowGLUT::reshapeCallback(int width, int height) {
  90. getWindow().resizeGL();
  91. }
  92. // Note that the escape, backspace, and delete keys are generated as an ASCII character.
  93. void WindowGLUT::keyboardCallback(unsigned char key, int x, int y) {
  94. if (getWindow().getTextInput()) {
  95. if ((key >= ' ') && (key <= '~')) {
  96. char s[2] = { static_cast<char>(key), '\0' };
  97. UI::handleText(s, false);
  98. }
  99. }
  100. KeyboardButton b = convertAsciiButton(key);
  101. UI::handleKeyboard(b, true);
  102. }
  103. void WindowGLUT::keyboardUpCallback(unsigned char key, int x, int y) {
  104. KeyboardButton b = convertAsciiButton(key);
  105. UI::handleKeyboard(b, false);
  106. }
  107. void WindowGLUT::specialCallback(int key, int x, int y) {
  108. KeyboardButton b = convertKeyCode(key);
  109. UI::handleKeyboard(b, true);
  110. }
  111. void WindowGLUT::specialUpCallback(int key, int x, int y) {
  112. KeyboardButton b = convertKeyCode(key);
  113. UI::handleKeyboard(b, false);
  114. }
  115. void WindowGLUT::mouseCallback(int button, int state, int x, int y) {
  116. KeyboardButton b;
  117. switch (button) {
  118. case GLUT_LEFT_BUTTON:
  119. b = leftmouseKey;
  120. break;
  121. case GLUT_RIGHT_BUTTON:
  122. b = rightmouseKey;
  123. break;
  124. case GLUT_MIDDLE_BUTTON:
  125. b = middlemouseKey;
  126. break;
  127. default:
  128. b = unknownKey;
  129. break;
  130. }
  131. UI::handleMouseClick(x, y, b, (state == GLUT_UP));
  132. }
  133. // The x and y callback parameters indicate the mouse location in window relative coordinates.
  134. void WindowGLUT::motionCallback(int x, int y) {
  135. int xrel = x - lastMouseX;
  136. int yrel = y - lastMouseY;
  137. UI::handleMouseMotion(xrel, yrel, x, y);
  138. lastMouseX = x;
  139. lastMouseY = y;
  140. if (getWindow().getMousegrab()) {
  141. lastMouseX = getWindow().getWidth() / 2;
  142. lastMouseY = getWindow().getHeight() / 2;
  143. glutWarpPointer(lastMouseX, lastMouseY);
  144. }
  145. }
  146. void WindowGLUT::mouseWheelCallback(int wheel, int direction, int x, int y) {
  147. int xrel = 0, yrel = 0;
  148. if (wheel == 0)
  149. yrel = direction;
  150. else
  151. xrel = direction;
  152. UI::handleMouseScroll(xrel, yrel);
  153. }
  154. KeyboardButton WindowGLUT::convertAsciiButton(unsigned char key) {
  155. // Convert Uppercase to Lowercase
  156. if ((key >= 'A') && (key <= 'Z'))
  157. key = key - 'A' + 'a';
  158. // Alphanumerics can be returned as is
  159. if (((key >= '0') && (key <= '9'))
  160. || ((key >= 'a') && (key <= 'z'))) {
  161. return static_cast<KeyboardButton>(key);
  162. }
  163. //! \fixme GLUT requires keyboard layout? Currently US is hard coded
  164. switch (key) {
  165. case ' ':
  166. return spaceKey;
  167. case '!':
  168. return oneKey;
  169. case '@':
  170. return twoKey;
  171. case '#':
  172. return threeKey;
  173. case '$':
  174. return fourKey;
  175. case '%':
  176. return fiveKey;
  177. case '^':
  178. return sixKey;
  179. case '&':
  180. return sevenKey;
  181. case '*':
  182. return eightKey;
  183. case '(':
  184. return nineKey;
  185. case ')':
  186. return zeroKey;
  187. case '"':
  188. case '\'':
  189. return quoteKey;
  190. case '+':
  191. case '=':
  192. return equalsKey;
  193. case ',':
  194. case '<':
  195. return commaKey;
  196. case '-':
  197. case '_':
  198. return minusKey;
  199. case '.':
  200. case '>':
  201. return dotKey;
  202. case '/':
  203. case '?':
  204. return slashKey;
  205. case ':':
  206. case ';':
  207. return semicolonKey;
  208. case '[':
  209. case '{':
  210. return leftbracketKey;
  211. case ']':
  212. case '}':
  213. return rightbracketKey;
  214. case '\\':
  215. case '|':
  216. return backslashKey;
  217. case '`':
  218. case '~':
  219. return backquoteKey;
  220. case '\t':
  221. return tabKey;
  222. case 8: // Backspace
  223. return backspaceKey;
  224. case '\r':
  225. case '\n':
  226. return enterKey;
  227. case 27: // Escape
  228. return escapeKey;
  229. default:
  230. return unknownKey;
  231. }
  232. }
  233. KeyboardButton WindowGLUT::convertKeyCode(int key) {
  234. switch (key) {
  235. case GLUT_KEY_F1:
  236. return f1Key;
  237. case GLUT_KEY_F2:
  238. return f2Key;
  239. case GLUT_KEY_F3:
  240. return f3Key;
  241. case GLUT_KEY_F4:
  242. return f4Key;
  243. case GLUT_KEY_F5:
  244. return f5Key;
  245. case GLUT_KEY_F6:
  246. return f6Key;
  247. case GLUT_KEY_F7:
  248. return f7Key;
  249. case GLUT_KEY_F8:
  250. return f8Key;
  251. case GLUT_KEY_F9:
  252. return f9Key;
  253. case GLUT_KEY_F10:
  254. return f10Key;
  255. case GLUT_KEY_F11:
  256. return f11Key;
  257. case GLUT_KEY_F12:
  258. return f12Key;
  259. case GLUT_KEY_LEFT:
  260. return leftKey;
  261. case GLUT_KEY_UP:
  262. return upKey;
  263. case GLUT_KEY_RIGHT:
  264. return rightKey;
  265. case GLUT_KEY_DOWN:
  266. return downKey;
  267. case GLUT_KEY_PAGE_UP:
  268. return pageupKey;
  269. case GLUT_KEY_PAGE_DOWN:
  270. return pagedownKey;
  271. case GLUT_KEY_HOME:
  272. return homeKey;
  273. case GLUT_KEY_END:
  274. return endKey;
  275. case GLUT_KEY_INSERT:
  276. return insertKey;
  277. default:
  278. return unknownKey;
  279. }
  280. }