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

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