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

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