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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*!
  2. * \file src/WindowSDL.cpp
  3. * \brief SDL windowing implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdio>
  8. #include <assert.h>
  9. #include "config.h"
  10. #include "main.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. mDriver = NULL;
  17. mWidth = DEFAULT_WIDTH;
  18. mHeight = DEFAULT_HEIGHT;
  19. mFullscreen = false;
  20. mMousegrab = false;
  21. mTextInput = false;
  22. mWindow = NULL;
  23. mGLContext = NULL;
  24. mFontInit = false;
  25. mFontName = NULL;
  26. mFont = NULL;
  27. #ifdef WIN32
  28. setDriver("libGL32.dll");
  29. #elif !defined(__APPLE__)
  30. setDriver("/usr/lib/libGL.so.1");
  31. #endif
  32. }
  33. WindowSDL::~WindowSDL() {
  34. if (mInit) {
  35. SDL_QuitSubSystem(SUBSYSTEMS_USED);
  36. SDL_Quit();
  37. }
  38. if (mFont)
  39. TTF_CloseFont(mFont);
  40. if (mFontInit)
  41. TTF_Quit();
  42. if (mDriver)
  43. delete [] mDriver;
  44. if (mFontName)
  45. delete [] mFontName;
  46. }
  47. void WindowSDL::setDriver(const char *driver) {
  48. assert(driver != NULL);
  49. assert(driver[0] != '\0');
  50. assert(mInit == false);
  51. mDriver = bufferString("%s", driver);
  52. }
  53. void WindowSDL::setSize(unsigned int width, unsigned int height) {
  54. assert(width > 0);
  55. assert(height > 0);
  56. mWidth = width;
  57. mHeight = height;
  58. if (mInit == true) {
  59. SDL_SetWindowSize(mWindow, mWidth, mHeight);
  60. resizeGL();
  61. }
  62. }
  63. void WindowSDL::setFullscreen(bool fullscreen) {
  64. mFullscreen = fullscreen;
  65. if (mInit == true) {
  66. if (mFullscreen)
  67. SDL_SetWindowFullscreen(mWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  68. else
  69. SDL_SetWindowFullscreen(mWindow, 0);
  70. }
  71. }
  72. void WindowSDL::setMousegrab(bool grab) {
  73. mMousegrab = grab;
  74. if (mInit == true) {
  75. if (mMousegrab) {
  76. SDL_SetRelativeMouseMode(SDL_TRUE);
  77. } else {
  78. SDL_SetRelativeMouseMode(SDL_FALSE);
  79. SDL_ShowCursor(1);
  80. }
  81. }
  82. }
  83. int WindowSDL::initialize() {
  84. assert(mInit == false);
  85. if (SDL_Init(SUBSYSTEMS_USED) != 0) {
  86. printf("SDL_Init Error: %s\n", SDL_GetError());
  87. return -1;
  88. }
  89. #ifndef __APPLE__
  90. assert(mDriver != NULL);
  91. assert(mDriver[0] != '\0');
  92. if (SDL_GL_LoadLibrary(mDriver) < 0) {
  93. SDL_ClearError();
  94. if (SDL_GL_LoadLibrary("libGL.so") < 0) {
  95. SDL_ClearError();
  96. if (SDL_GL_LoadLibrary("libGL.so.1") < 0) {
  97. printf("Could not load OpenGL driver!\n");
  98. printf("SDL_GL_LoadLibrary Error: %s\n", SDL_GetError());
  99. return -2;
  100. }
  101. }
  102. }
  103. #endif
  104. int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
  105. if (mFullscreen)
  106. flags |= SDL_WINDOW_FULLSCREEN;
  107. mInit = true;
  108. setMousegrab(mMousegrab);
  109. if ((SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5) != 0)
  110. || (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5) != 0)
  111. || (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5) != 0)
  112. || (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16) != 0)
  113. || (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0)) {
  114. printf("SDL_GL_SetAttribute Error: %s\n", SDL_GetError());
  115. mInit = false;
  116. return -3;
  117. }
  118. mWindow = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  119. mWidth, mHeight, flags);
  120. if (mWindow == NULL) {
  121. printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
  122. mInit = false;
  123. return -4;
  124. }
  125. mGLContext = SDL_GL_CreateContext(mWindow);
  126. if (mGLContext == NULL) {
  127. printf("SDL_GL_CreateContext Error: %s\n", SDL_GetError());
  128. mInit = false;
  129. return -5;
  130. }
  131. setSize(mWidth, mHeight);
  132. return 0;
  133. }
  134. void WindowSDL::eventHandling() {
  135. SDL_Event event;
  136. assert(mInit == true);
  137. while(SDL_PollEvent(&event)) {
  138. switch (event.type) {
  139. case SDL_MOUSEMOTION:
  140. gOpenRaider->handleMouseMotion(event.motion.xrel, event.motion.yrel);
  141. break;
  142. case SDL_MOUSEBUTTONDOWN:
  143. case SDL_MOUSEBUTTONUP:
  144. MouseButton button;
  145. if (event.button.button == SDL_BUTTON_LEFT)
  146. button = leftButton;
  147. else if (event.button.button == SDL_BUTTON_RIGHT)
  148. button = rightButton;
  149. else
  150. button = middleButton;
  151. gOpenRaider->handleMouseClick(event.button.x, event.button.y, button, (event.type == SDL_MOUSEBUTTONUP));
  152. break;
  153. case SDL_TEXTINPUT:
  154. case SDL_TEXTEDITING:
  155. gOpenRaider->handleText(event.text.text, (event.type == SDL_TEXTEDITING));
  156. break;
  157. case SDL_KEYDOWN:
  158. case SDL_KEYUP:
  159. KeyboardButton key;
  160. switch (event.key.keysym.sym) {
  161. case SDLK_0:
  162. key = zero;
  163. break;
  164. case SDLK_1:
  165. key = one;
  166. break;
  167. case SDLK_2:
  168. key = two;
  169. break;
  170. case SDLK_3:
  171. key = three;
  172. break;
  173. case SDLK_4:
  174. key = four;
  175. break;
  176. case SDLK_5:
  177. key = five;
  178. break;
  179. case SDLK_6:
  180. key = six;
  181. break;
  182. case SDLK_7:
  183. key = seven;
  184. break;
  185. case SDLK_8:
  186. key = eight;
  187. break;
  188. case SDLK_9:
  189. key = nine;
  190. break;
  191. case SDLK_a:
  192. key = a;
  193. break;
  194. case SDLK_b:
  195. key = b;
  196. break;
  197. case SDLK_c:
  198. key = c;
  199. break;
  200. case SDLK_d:
  201. key = d;
  202. break;
  203. case SDLK_e:
  204. key = e;
  205. break;
  206. case SDLK_f:
  207. key = f;
  208. break;
  209. case SDLK_g:
  210. key = g;
  211. break;
  212. case SDLK_h:
  213. key = h;
  214. break;
  215. case SDLK_i:
  216. key = i;
  217. break;
  218. case SDLK_j:
  219. key = j;
  220. break;
  221. case SDLK_k:
  222. key = k;
  223. break;
  224. case SDLK_l:
  225. key = l;
  226. break;
  227. case SDLK_m:
  228. key = m;
  229. break;
  230. case SDLK_n:
  231. key = n;
  232. break;
  233. case SDLK_o:
  234. key = o;
  235. break;
  236. case SDLK_p:
  237. key = p;
  238. break;
  239. case SDLK_q:
  240. key = q;
  241. break;
  242. case SDLK_r:
  243. key = r;
  244. break;
  245. case SDLK_s:
  246. key = s;
  247. break;
  248. case SDLK_t:
  249. key = t;
  250. break;
  251. case SDLK_u:
  252. key = u;
  253. break;
  254. case SDLK_v:
  255. key = v;
  256. break;
  257. case SDLK_w:
  258. key = w;
  259. break;
  260. case SDLK_x:
  261. key = x;
  262. break;
  263. case SDLK_y:
  264. key = y;
  265. break;
  266. case SDLK_z:
  267. key = z;
  268. break;
  269. case SDLK_QUOTE:
  270. key = quote;
  271. break;
  272. case SDLK_BACKSLASH:
  273. key = backslash;
  274. break;
  275. case SDLK_BACKSPACE:
  276. key = backspace;
  277. break;
  278. case SDLK_CAPSLOCK:
  279. key = capslock;
  280. break;
  281. case SDLK_COMMA:
  282. key = comma;
  283. break;
  284. case SDLK_DELETE:
  285. key = del;
  286. break;
  287. case SDLK_UP:
  288. key = up;
  289. break;
  290. case SDLK_DOWN:
  291. key = down;
  292. break;
  293. case SDLK_LEFT:
  294. key = left;
  295. break;
  296. case SDLK_RIGHT:
  297. key = right;
  298. break;
  299. case SDLK_END:
  300. key = end;
  301. break;
  302. case SDLK_EQUALS:
  303. key = equals;
  304. break;
  305. case SDLK_ESCAPE:
  306. key = escape;
  307. break;
  308. case SDLK_F1:
  309. key = f1;
  310. break;
  311. case SDLK_F2:
  312. key = f2;
  313. break;
  314. case SDLK_F3:
  315. key = f3;
  316. break;
  317. case SDLK_F4:
  318. key = f4;
  319. break;
  320. case SDLK_F5:
  321. key = f5;
  322. break;
  323. case SDLK_F6:
  324. key = f6;
  325. break;
  326. case SDLK_F7:
  327. key = f7;
  328. break;
  329. case SDLK_F8:
  330. key = f8;
  331. break;
  332. case SDLK_F9:
  333. key = f9;
  334. break;
  335. case SDLK_F10:
  336. key = f10;
  337. break;
  338. case SDLK_F11:
  339. key = f11;
  340. break;
  341. case SDLK_F12:
  342. key = f12;
  343. break;
  344. case SDLK_BACKQUOTE:
  345. key = backquote;
  346. break;
  347. case SDLK_HOME:
  348. key = home;
  349. break;
  350. case SDLK_INSERT:
  351. key = insert;
  352. break;
  353. case SDLK_LALT:
  354. key = leftalt;
  355. break;
  356. case SDLK_LCTRL:
  357. key = leftctrl;
  358. break;
  359. case SDLK_LEFTBRACKET:
  360. key = leftbracket;
  361. break;
  362. case SDLK_LGUI:
  363. key = leftgui;
  364. break;
  365. case SDLK_LSHIFT:
  366. key = leftshift;
  367. break;
  368. case SDLK_MINUS:
  369. key = minus;
  370. break;
  371. case SDLK_NUMLOCKCLEAR:
  372. key = numlock;
  373. break;
  374. case SDLK_PAGEDOWN:
  375. key = pagedown;
  376. break;
  377. case SDLK_PAGEUP:
  378. key = pageup;
  379. break;
  380. case SDLK_PAUSE:
  381. key = pause;
  382. break;
  383. case SDLK_PERIOD:
  384. key = dot;
  385. break;
  386. case SDLK_RALT:
  387. key = rightalt;
  388. break;
  389. case SDLK_RCTRL:
  390. key = rightctrl;
  391. break;
  392. case SDLK_RETURN:
  393. case SDLK_RETURN2:
  394. key = enter;
  395. break;
  396. case SDLK_RGUI:
  397. key = rightgui;
  398. break;
  399. case SDLK_RIGHTBRACKET:
  400. key = rightbracket;
  401. break;
  402. case SDLK_RSHIFT:
  403. key = rightshift;
  404. break;
  405. case SDLK_SCROLLLOCK:
  406. key = scrolllock;
  407. break;
  408. case SDLK_SEMICOLON:
  409. key = semicolon;
  410. break;
  411. case SDLK_SLASH:
  412. key = slash;
  413. break;
  414. case SDLK_SPACE:
  415. key = space;
  416. break;
  417. case SDLK_TAB:
  418. key = tab;
  419. break;
  420. default:
  421. key = unknown;
  422. break;
  423. }
  424. gOpenRaider->handleKeyboard(key, (event.type == SDL_KEYDOWN));
  425. break;
  426. case SDL_WINDOWEVENT:
  427. if (event.window.event == SDL_WINDOWEVENT_RESIZED)
  428. setSize(event.window.data1, event.window.data2);
  429. break;
  430. case SDL_QUIT:
  431. exit(0);
  432. }
  433. }
  434. }
  435. void WindowSDL::setTextInput(bool on) {
  436. assert(mInit == true);
  437. mTextInput = on;
  438. if (mTextInput)
  439. SDL_StartTextInput();
  440. else
  441. SDL_StopTextInput();
  442. }
  443. void WindowSDL::delay(clock_t ms) {
  444. assert(mInit == true);
  445. SDL_Delay(ms);
  446. }
  447. void WindowSDL::swapBuffersGL() {
  448. assert(mInit == true);
  449. SDL_GL_SwapWindow(mWindow);
  450. }
  451. void WindowSDL::setFont(const char *font) {
  452. assert(font != NULL);
  453. assert(font[0] != '\0');
  454. assert(mFontInit == false);
  455. mFontName = fullPath(font, 0);
  456. }
  457. int WindowSDL::initializeFont() {
  458. assert(mFontInit == false);
  459. assert(mFontName != NULL);
  460. assert(mFontName[0] != '\0');
  461. if (TTF_Init() != 0) {
  462. printf("Could not initialize SDL-TTF!\n");
  463. return -1;
  464. }
  465. mFont = TTF_OpenFont(mFontName, 24);
  466. if (mFont == NULL) {
  467. printf("TTF_OpenFont Error: %s\n", TTF_GetError());
  468. return -2;
  469. }
  470. glGenTextures(1, &mFontTexture);
  471. mFontInit = true;
  472. return 0;
  473. }
  474. void WindowSDL::writeString(WindowString *s) {
  475. assert(s != NULL);
  476. assert(s->text != NULL);
  477. assert(mInit == true);
  478. SDL_Color color;
  479. color.r = s->color[0];
  480. color.g = s->color[1];
  481. color.b = s->color[2];
  482. color.a = s->color[3];
  483. SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s->text, color);
  484. if (surface == NULL) {
  485. printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
  486. return;
  487. }
  488. s->w = (int)((float)surface->w * s->scale);
  489. s->h = (int)((float)surface->h * s->scale);
  490. GLenum textureFormat;
  491. if (surface->format->BytesPerPixel == 4) {
  492. if (surface->format->Rmask == 0x000000FF)
  493. textureFormat = GL_RGBA;
  494. else
  495. textureFormat = GL_BGRA_EXT;
  496. } else {
  497. textureFormat = GL_RGB;
  498. }
  499. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  500. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  501. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  502. glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  503. GLuint xMin = s->x;
  504. GLuint yMin = s->y;
  505. GLuint xMax = xMin + (int)((float)surface->w * s->scale);
  506. GLuint yMax = yMin + (int)((float)surface->h * s->scale);
  507. glColor4f(color.r / 256.0f, color.g / 256.0f, color.b / 256.0f, color.a / 256.0f);
  508. glBegin(GL_QUADS);
  509. glTexCoord2f(0.0f, 0.0f);
  510. glVertex2i(xMin, yMin);
  511. glTexCoord2f(0.0f, 1.0f);
  512. glVertex2i(xMin, yMax);
  513. glTexCoord2f(1.0f, 1.0f);
  514. glVertex2i(xMax, yMax);
  515. glTexCoord2f(1.0f, 0.0f);
  516. glVertex2i(xMax, yMin);
  517. glEnd();
  518. SDL_FreeSurface(surface);
  519. }