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.

WindowSDL.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*!
  2. * \file src/WindowSDL.cpp
  3. * \brief SDL windowing implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdio>
  8. #include <ctime>
  9. #include "global.h"
  10. #include "OpenRaider.h"
  11. #include "utils/strings.h"
  12. #include "WindowSDL.h"
  13. #define SUBSYSTEMS_USED (SDL_INIT_VIDEO | SDL_INIT_EVENTS)
  14. WindowSDL::WindowSDL() {
  15. mInit = false;
  16. mWidth = DEFAULT_WIDTH;
  17. mHeight = DEFAULT_HEIGHT;
  18. mFullscreen = false;
  19. mMousegrab = false;
  20. mTextInput = false;
  21. mWindow = NULL;
  22. mGLContext = NULL;
  23. }
  24. WindowSDL::~WindowSDL() {
  25. if (mInit) {
  26. SDL_QuitSubSystem(SUBSYSTEMS_USED);
  27. SDL_Quit();
  28. }
  29. }
  30. void WindowSDL::setSize(unsigned int width, unsigned int height) {
  31. assert(width > 0);
  32. assert(height > 0);
  33. mWidth = width;
  34. mHeight = height;
  35. if (mInit == true) {
  36. SDL_SetWindowSize(mWindow, mWidth, mHeight);
  37. resizeGL();
  38. }
  39. }
  40. void WindowSDL::setFullscreen(bool fullscreen) {
  41. mFullscreen = fullscreen;
  42. if (mInit == true) {
  43. if (mFullscreen)
  44. SDL_SetWindowFullscreen(mWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  45. else
  46. SDL_SetWindowFullscreen(mWindow, 0);
  47. }
  48. }
  49. void WindowSDL::setMousegrab(bool grab) {
  50. mMousegrab = grab;
  51. if (mInit == true) {
  52. if (mMousegrab) {
  53. SDL_SetRelativeMouseMode(SDL_TRUE);
  54. } else {
  55. SDL_SetRelativeMouseMode(SDL_FALSE);
  56. SDL_ShowCursor(1);
  57. }
  58. }
  59. }
  60. bool WindowSDL::getMousegrab() {
  61. return mMousegrab;
  62. }
  63. int WindowSDL::initialize() {
  64. assert(mInit == false);
  65. if (SDL_Init(SUBSYSTEMS_USED) != 0) {
  66. printf("SDL_Init Error: %s\n", SDL_GetError());
  67. return -1;
  68. }
  69. int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
  70. if (mFullscreen)
  71. flags |= SDL_WINDOW_FULLSCREEN;
  72. mInit = true;
  73. setMousegrab(mMousegrab);
  74. if ((SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5) != 0)
  75. || (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5) != 0)
  76. || (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5) != 0)
  77. || (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16) != 0)
  78. || (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0)) {
  79. printf("SDL_GL_SetAttribute Error: %s\n", SDL_GetError());
  80. mInit = false;
  81. return -3;
  82. }
  83. mWindow = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  84. mWidth, mHeight, flags);
  85. if (mWindow == NULL) {
  86. printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
  87. mInit = false;
  88. return -4;
  89. }
  90. mGLContext = SDL_GL_CreateContext(mWindow);
  91. if (mGLContext == NULL) {
  92. printf("SDL_GL_CreateContext Error: %s\n", SDL_GetError());
  93. mInit = false;
  94. return -5;
  95. }
  96. setSize(mWidth, mHeight);
  97. return 0;
  98. }
  99. void WindowSDL::eventHandling() {
  100. SDL_Event event;
  101. assert(mInit == true);
  102. while(SDL_PollEvent(&event)) {
  103. switch (event.type) {
  104. case SDL_MOUSEMOTION:
  105. getOpenRaider().handleMouseMotion(event.motion.xrel, event.motion.yrel);
  106. break;
  107. case SDL_MOUSEBUTTONDOWN:
  108. case SDL_MOUSEBUTTONUP:
  109. KeyboardButton button;
  110. if (event.button.button == SDL_BUTTON_LEFT)
  111. button = leftmouseKey;
  112. else if (event.button.button == SDL_BUTTON_RIGHT)
  113. button = rightmouseKey;
  114. else if (event.button.button == SDL_BUTTON_MIDDLE)
  115. button = middlemouseKey;
  116. else if (event.button.button == SDL_BUTTON_X1)
  117. button = fourthmouseKey;
  118. else if (event.button.button == SDL_BUTTON_X2)
  119. button = fifthmouseKey;
  120. else
  121. button = unknownKey;
  122. getOpenRaider().handleMouseClick(event.button.x, event.button.y, button, (event.type == SDL_MOUSEBUTTONUP));
  123. break;
  124. case SDL_MOUSEWHEEL:
  125. if ((event.wheel.x != 0) || (event.wheel.y != 0))
  126. getOpenRaider().handleMouseScroll(event.wheel.x, event.wheel.y);
  127. break;
  128. case SDL_TEXTINPUT:
  129. case SDL_TEXTEDITING:
  130. if (event.text.text != NULL)
  131. getOpenRaider().handleText(event.text.text, (event.type == SDL_TEXTEDITING));
  132. break;
  133. case SDL_KEYDOWN:
  134. case SDL_KEYUP:
  135. KeyboardButton key;
  136. switch (event.key.keysym.sym) {
  137. case SDLK_0:
  138. key = zeroKey;
  139. break;
  140. case SDLK_1:
  141. key = oneKey;
  142. break;
  143. case SDLK_2:
  144. key = twoKey;
  145. break;
  146. case SDLK_3:
  147. key = threeKey;
  148. break;
  149. case SDLK_4:
  150. key = fourKey;
  151. break;
  152. case SDLK_5:
  153. key = fiveKey;
  154. break;
  155. case SDLK_6:
  156. key = sixKey;
  157. break;
  158. case SDLK_7:
  159. key = sevenKey;
  160. break;
  161. case SDLK_8:
  162. key = eightKey;
  163. break;
  164. case SDLK_9:
  165. key = nineKey;
  166. break;
  167. case SDLK_a:
  168. key = aKey;
  169. break;
  170. case SDLK_b:
  171. key = bKey;
  172. break;
  173. case SDLK_c:
  174. key = cKey;
  175. break;
  176. case SDLK_d:
  177. key = dKey;
  178. break;
  179. case SDLK_e:
  180. key = eKey;
  181. break;
  182. case SDLK_f:
  183. key = fKey;
  184. break;
  185. case SDLK_g:
  186. key = gKey;
  187. break;
  188. case SDLK_h:
  189. key = hKey;
  190. break;
  191. case SDLK_i:
  192. key = iKey;
  193. break;
  194. case SDLK_j:
  195. key = jKey;
  196. break;
  197. case SDLK_k:
  198. key = kKey;
  199. break;
  200. case SDLK_l:
  201. key = lKey;
  202. break;
  203. case SDLK_m:
  204. key = mKey;
  205. break;
  206. case SDLK_n:
  207. key = nKey;
  208. break;
  209. case SDLK_o:
  210. key = oKey;
  211. break;
  212. case SDLK_p:
  213. key = pKey;
  214. break;
  215. case SDLK_q:
  216. key = qKey;
  217. break;
  218. case SDLK_r:
  219. key = rKey;
  220. break;
  221. case SDLK_s:
  222. key = sKey;
  223. break;
  224. case SDLK_t:
  225. key = tKey;
  226. break;
  227. case SDLK_u:
  228. key = uKey;
  229. break;
  230. case SDLK_v:
  231. key = vKey;
  232. break;
  233. case SDLK_w:
  234. key = wKey;
  235. break;
  236. case SDLK_x:
  237. key = xKey;
  238. break;
  239. case SDLK_y:
  240. key = yKey;
  241. break;
  242. case SDLK_z:
  243. key = zKey;
  244. break;
  245. case SDLK_QUOTE:
  246. key = quoteKey;
  247. break;
  248. case SDLK_BACKSLASH:
  249. key = backslashKey;
  250. break;
  251. case SDLK_BACKSPACE:
  252. key = backspaceKey;
  253. break;
  254. case SDLK_CAPSLOCK:
  255. key = capslockKey;
  256. break;
  257. case SDLK_COMMA:
  258. key = commaKey;
  259. break;
  260. case SDLK_DELETE:
  261. key = delKey;
  262. break;
  263. case SDLK_UP:
  264. key = upKey;
  265. break;
  266. case SDLK_DOWN:
  267. key = downKey;
  268. break;
  269. case SDLK_LEFT:
  270. key = leftKey;
  271. break;
  272. case SDLK_RIGHT:
  273. key = rightKey;
  274. break;
  275. case SDLK_END:
  276. key = endKey;
  277. break;
  278. case SDLK_EQUALS:
  279. key = equalsKey;
  280. break;
  281. case SDLK_ESCAPE:
  282. key = escapeKey;
  283. break;
  284. case SDLK_F1:
  285. key = f1Key;
  286. break;
  287. case SDLK_F2:
  288. key = f2Key;
  289. break;
  290. case SDLK_F3:
  291. key = f3Key;
  292. break;
  293. case SDLK_F4:
  294. key = f4Key;
  295. break;
  296. case SDLK_F5:
  297. key = f5Key;
  298. break;
  299. case SDLK_F6:
  300. key = f6Key;
  301. break;
  302. case SDLK_F7:
  303. key = f7Key;
  304. break;
  305. case SDLK_F8:
  306. key = f8Key;
  307. break;
  308. case SDLK_F9:
  309. key = f9Key;
  310. break;
  311. case SDLK_F10:
  312. key = f10Key;
  313. break;
  314. case SDLK_F11:
  315. key = f11Key;
  316. break;
  317. case SDLK_F12:
  318. key = f12Key;
  319. break;
  320. case SDLK_BACKQUOTE:
  321. key = backquoteKey;
  322. break;
  323. case SDLK_HOME:
  324. key = homeKey;
  325. break;
  326. case SDLK_INSERT:
  327. key = insertKey;
  328. break;
  329. case SDLK_LALT:
  330. key = leftaltKey;
  331. break;
  332. case SDLK_LCTRL:
  333. key = leftctrlKey;
  334. break;
  335. case SDLK_LEFTBRACKET:
  336. key = leftbracketKey;
  337. break;
  338. case SDLK_LGUI:
  339. key = leftguiKey;
  340. break;
  341. case SDLK_LSHIFT:
  342. key = leftshiftKey;
  343. break;
  344. case SDLK_MINUS:
  345. key = minusKey;
  346. break;
  347. case SDLK_NUMLOCKCLEAR:
  348. key = numlockKey;
  349. break;
  350. case SDLK_PAGEDOWN:
  351. key = pagedownKey;
  352. break;
  353. case SDLK_PAGEUP:
  354. key = pageupKey;
  355. break;
  356. case SDLK_PAUSE:
  357. key = pauseKey;
  358. break;
  359. case SDLK_PERIOD:
  360. key = dotKey;
  361. break;
  362. case SDLK_RALT:
  363. key = rightaltKey;
  364. break;
  365. case SDLK_RCTRL:
  366. key = rightctrlKey;
  367. break;
  368. case SDLK_RETURN:
  369. case SDLK_RETURN2:
  370. key = enterKey;
  371. break;
  372. case SDLK_RGUI:
  373. key = rightguiKey;
  374. break;
  375. case SDLK_RIGHTBRACKET:
  376. key = rightbracketKey;
  377. break;
  378. case SDLK_RSHIFT:
  379. key = rightshiftKey;
  380. break;
  381. case SDLK_SCROLLLOCK:
  382. key = scrolllockKey;
  383. break;
  384. case SDLK_SEMICOLON:
  385. key = semicolonKey;
  386. break;
  387. case SDLK_SLASH:
  388. key = slashKey;
  389. break;
  390. case SDLK_SPACE:
  391. key = spaceKey;
  392. break;
  393. case SDLK_TAB:
  394. key = tabKey;
  395. break;
  396. default:
  397. key = unknownKey;
  398. break;
  399. }
  400. getOpenRaider().handleKeyboard(key, (event.type == SDL_KEYDOWN));
  401. break;
  402. case SDL_WINDOWEVENT:
  403. if (event.window.event == SDL_WINDOWEVENT_RESIZED)
  404. setSize(event.window.data1, event.window.data2);
  405. break;
  406. case SDL_QUIT:
  407. exit(0);
  408. }
  409. }
  410. }
  411. void WindowSDL::setTextInput(bool on) {
  412. assert(mInit == true);
  413. mTextInput = on;
  414. if (mTextInput)
  415. SDL_StartTextInput();
  416. else
  417. SDL_StopTextInput();
  418. }
  419. void WindowSDL::delay(unsigned int ms) {
  420. assert(mInit == true);
  421. SDL_Delay(ms);
  422. }
  423. void WindowSDL::swapBuffersGL() {
  424. assert(mInit == true);
  425. SDL_GL_SwapWindow(mWindow);
  426. }