Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

WindowGLUT.cpp 7.8KB

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