Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

WindowSDL.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. tempText.text = new char[256];
  33. tempText.color[0] = 0xFF;
  34. tempText.color[1] = 0xFF;
  35. tempText.color[2] = 0xFF;
  36. tempText.color[3] = 0xFF;
  37. tempText.scale = 1.2f;
  38. tempText.w = 0;
  39. tempText.h = 0;
  40. }
  41. WindowSDL::~WindowSDL() {
  42. if (mInit) {
  43. SDL_QuitSubSystem(SUBSYSTEMS_USED);
  44. SDL_Quit();
  45. }
  46. if (mFont)
  47. TTF_CloseFont(mFont);
  48. if (mFontInit)
  49. TTF_Quit();
  50. if (mDriver)
  51. delete [] mDriver;
  52. if (mFontName)
  53. delete [] mFontName;
  54. if (tempText.text)
  55. delete [] tempText.text;
  56. }
  57. void WindowSDL::setDriver(const char *driver) {
  58. assert(driver != NULL);
  59. assert(driver[0] != '\0');
  60. assert(mInit == false);
  61. mDriver = bufferString("%s", driver);
  62. }
  63. void WindowSDL::setSize(unsigned int width, unsigned int height) {
  64. assert(width > 0);
  65. assert(height > 0);
  66. mWidth = width;
  67. mHeight = height;
  68. if (mInit == true) {
  69. SDL_SetWindowSize(mWindow, mWidth, mHeight);
  70. resizeGL();
  71. }
  72. }
  73. void WindowSDL::setFullscreen(bool fullscreen) {
  74. mFullscreen = fullscreen;
  75. if (mInit == true) {
  76. if (mFullscreen)
  77. SDL_SetWindowFullscreen(mWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  78. else
  79. SDL_SetWindowFullscreen(mWindow, 0);
  80. }
  81. }
  82. void WindowSDL::setMousegrab(bool grab) {
  83. mMousegrab = grab;
  84. if (mInit == true) {
  85. if (mMousegrab) {
  86. SDL_SetRelativeMouseMode(SDL_TRUE);
  87. } else {
  88. SDL_SetRelativeMouseMode(SDL_FALSE);
  89. SDL_ShowCursor(1);
  90. }
  91. }
  92. }
  93. int WindowSDL::initialize() {
  94. assert(mInit == false);
  95. if (SDL_Init(SUBSYSTEMS_USED) != 0) {
  96. printf("SDL_Init Error: %s\n", SDL_GetError());
  97. return -1;
  98. }
  99. #ifndef __APPLE__
  100. assert(mDriver != NULL);
  101. assert(mDriver[0] != '\0');
  102. if (SDL_GL_LoadLibrary(mDriver) < 0) {
  103. SDL_ClearError();
  104. if (SDL_GL_LoadLibrary("libGL.so") < 0) {
  105. SDL_ClearError();
  106. if (SDL_GL_LoadLibrary("libGL.so.1") < 0) {
  107. printf("Could not load OpenGL driver!\n");
  108. printf("SDL_GL_LoadLibrary Error: %s\n", SDL_GetError());
  109. return -2;
  110. }
  111. }
  112. }
  113. #endif
  114. int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
  115. if (mFullscreen)
  116. flags |= SDL_WINDOW_FULLSCREEN;
  117. mInit = true;
  118. setMousegrab(mMousegrab);
  119. if ((SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5) != 0)
  120. || (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5) != 0)
  121. || (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5) != 0)
  122. || (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16) != 0)
  123. || (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0)) {
  124. printf("SDL_GL_SetAttribute Error: %s\n", SDL_GetError());
  125. mInit = false;
  126. return -3;
  127. }
  128. mWindow = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  129. mWidth, mHeight, flags);
  130. if (mWindow == NULL) {
  131. printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
  132. mInit = false;
  133. return -4;
  134. }
  135. mGLContext = SDL_GL_CreateContext(mWindow);
  136. if (mGLContext == NULL) {
  137. printf("SDL_GL_CreateContext Error: %s\n", SDL_GetError());
  138. mInit = false;
  139. return -5;
  140. }
  141. setSize(mWidth, mHeight);
  142. return 0;
  143. }
  144. void WindowSDL::eventHandling() {
  145. SDL_Event event;
  146. assert(mInit == true);
  147. while(SDL_PollEvent(&event)) {
  148. switch (event.type) {
  149. case SDL_MOUSEMOTION:
  150. getOpenRaider().handleMouseMotion(event.motion.xrel, event.motion.yrel);
  151. break;
  152. case SDL_MOUSEBUTTONDOWN:
  153. case SDL_MOUSEBUTTONUP:
  154. KeyboardButton button;
  155. if (event.button.button == SDL_BUTTON_LEFT)
  156. button = leftmouseKey;
  157. else if (event.button.button == SDL_BUTTON_RIGHT)
  158. button = rightmouseKey;
  159. else
  160. button = middlemouseKey;
  161. getOpenRaider().handleMouseClick(event.button.x, event.button.y, button, (event.type == SDL_MOUSEBUTTONUP));
  162. break;
  163. case SDL_MOUSEWHEEL:
  164. getOpenRaider().handleMouseScroll(event.wheel.x, event.wheel.y);
  165. break;
  166. case SDL_TEXTINPUT:
  167. case SDL_TEXTEDITING:
  168. getOpenRaider().handleText(event.text.text, (event.type == SDL_TEXTEDITING));
  169. break;
  170. case SDL_KEYDOWN:
  171. case SDL_KEYUP:
  172. KeyboardButton key;
  173. switch (event.key.keysym.sym) {
  174. case SDLK_0:
  175. key = zeroKey;
  176. break;
  177. case SDLK_1:
  178. key = oneKey;
  179. break;
  180. case SDLK_2:
  181. key = twoKey;
  182. break;
  183. case SDLK_3:
  184. key = threeKey;
  185. break;
  186. case SDLK_4:
  187. key = fourKey;
  188. break;
  189. case SDLK_5:
  190. key = fiveKey;
  191. break;
  192. case SDLK_6:
  193. key = sixKey;
  194. break;
  195. case SDLK_7:
  196. key = sevenKey;
  197. break;
  198. case SDLK_8:
  199. key = eightKey;
  200. break;
  201. case SDLK_9:
  202. key = nineKey;
  203. break;
  204. case SDLK_a:
  205. key = aKey;
  206. break;
  207. case SDLK_b:
  208. key = bKey;
  209. break;
  210. case SDLK_c:
  211. key = cKey;
  212. break;
  213. case SDLK_d:
  214. key = dKey;
  215. break;
  216. case SDLK_e:
  217. key = eKey;
  218. break;
  219. case SDLK_f:
  220. key = fKey;
  221. break;
  222. case SDLK_g:
  223. key = gKey;
  224. break;
  225. case SDLK_h:
  226. key = hKey;
  227. break;
  228. case SDLK_i:
  229. key = iKey;
  230. break;
  231. case SDLK_j:
  232. key = jKey;
  233. break;
  234. case SDLK_k:
  235. key = kKey;
  236. break;
  237. case SDLK_l:
  238. key = lKey;
  239. break;
  240. case SDLK_m:
  241. key = mKey;
  242. break;
  243. case SDLK_n:
  244. key = nKey;
  245. break;
  246. case SDLK_o:
  247. key = oKey;
  248. break;
  249. case SDLK_p:
  250. key = pKey;
  251. break;
  252. case SDLK_q:
  253. key = qKey;
  254. break;
  255. case SDLK_r:
  256. key = rKey;
  257. break;
  258. case SDLK_s:
  259. key = sKey;
  260. break;
  261. case SDLK_t:
  262. key = tKey;
  263. break;
  264. case SDLK_u:
  265. key = uKey;
  266. break;
  267. case SDLK_v:
  268. key = vKey;
  269. break;
  270. case SDLK_w:
  271. key = wKey;
  272. break;
  273. case SDLK_x:
  274. key = xKey;
  275. break;
  276. case SDLK_y:
  277. key = yKey;
  278. break;
  279. case SDLK_z:
  280. key = zKey;
  281. break;
  282. case SDLK_QUOTE:
  283. key = quoteKey;
  284. break;
  285. case SDLK_BACKSLASH:
  286. key = backslashKey;
  287. break;
  288. case SDLK_BACKSPACE:
  289. key = backspaceKey;
  290. break;
  291. case SDLK_CAPSLOCK:
  292. key = capslockKey;
  293. break;
  294. case SDLK_COMMA:
  295. key = commaKey;
  296. break;
  297. case SDLK_DELETE:
  298. key = delKey;
  299. break;
  300. case SDLK_UP:
  301. key = upKey;
  302. break;
  303. case SDLK_DOWN:
  304. key = downKey;
  305. break;
  306. case SDLK_LEFT:
  307. key = leftKey;
  308. break;
  309. case SDLK_RIGHT:
  310. key = rightKey;
  311. break;
  312. case SDLK_END:
  313. key = endKey;
  314. break;
  315. case SDLK_EQUALS:
  316. key = equalsKey;
  317. break;
  318. case SDLK_ESCAPE:
  319. key = escapeKey;
  320. break;
  321. case SDLK_F1:
  322. key = f1Key;
  323. break;
  324. case SDLK_F2:
  325. key = f2Key;
  326. break;
  327. case SDLK_F3:
  328. key = f3Key;
  329. break;
  330. case SDLK_F4:
  331. key = f4Key;
  332. break;
  333. case SDLK_F5:
  334. key = f5Key;
  335. break;
  336. case SDLK_F6:
  337. key = f6Key;
  338. break;
  339. case SDLK_F7:
  340. key = f7Key;
  341. break;
  342. case SDLK_F8:
  343. key = f8Key;
  344. break;
  345. case SDLK_F9:
  346. key = f9Key;
  347. break;
  348. case SDLK_F10:
  349. key = f10Key;
  350. break;
  351. case SDLK_F11:
  352. key = f11Key;
  353. break;
  354. case SDLK_F12:
  355. key = f12Key;
  356. break;
  357. case SDLK_BACKQUOTE:
  358. key = backquoteKey;
  359. break;
  360. case SDLK_HOME:
  361. key = homeKey;
  362. break;
  363. case SDLK_INSERT:
  364. key = insertKey;
  365. break;
  366. case SDLK_LALT:
  367. key = leftaltKey;
  368. break;
  369. case SDLK_LCTRL:
  370. key = leftctrlKey;
  371. break;
  372. case SDLK_LEFTBRACKET:
  373. key = leftbracketKey;
  374. break;
  375. case SDLK_LGUI:
  376. key = leftguiKey;
  377. break;
  378. case SDLK_LSHIFT:
  379. key = leftshiftKey;
  380. break;
  381. case SDLK_MINUS:
  382. key = minusKey;
  383. break;
  384. case SDLK_NUMLOCKCLEAR:
  385. key = numlockKey;
  386. break;
  387. case SDLK_PAGEDOWN:
  388. key = pagedownKey;
  389. break;
  390. case SDLK_PAGEUP:
  391. key = pageupKey;
  392. break;
  393. case SDLK_PAUSE:
  394. key = pauseKey;
  395. break;
  396. case SDLK_PERIOD:
  397. key = dotKey;
  398. break;
  399. case SDLK_RALT:
  400. key = rightaltKey;
  401. break;
  402. case SDLK_RCTRL:
  403. key = rightctrlKey;
  404. break;
  405. case SDLK_RETURN:
  406. case SDLK_RETURN2:
  407. key = enterKey;
  408. break;
  409. case SDLK_RGUI:
  410. key = rightguiKey;
  411. break;
  412. case SDLK_RIGHTBRACKET:
  413. key = rightbracketKey;
  414. break;
  415. case SDLK_RSHIFT:
  416. key = rightshiftKey;
  417. break;
  418. case SDLK_SCROLLLOCK:
  419. key = scrolllockKey;
  420. break;
  421. case SDLK_SEMICOLON:
  422. key = semicolonKey;
  423. break;
  424. case SDLK_SLASH:
  425. key = slashKey;
  426. break;
  427. case SDLK_SPACE:
  428. key = spaceKey;
  429. break;
  430. case SDLK_TAB:
  431. key = tabKey;
  432. break;
  433. default:
  434. key = unknownKey;
  435. break;
  436. }
  437. getOpenRaider().handleKeyboard(key, (event.type == SDL_KEYDOWN));
  438. break;
  439. case SDL_WINDOWEVENT:
  440. if (event.window.event == SDL_WINDOWEVENT_RESIZED)
  441. setSize(event.window.data1, event.window.data2);
  442. break;
  443. case SDL_QUIT:
  444. exit(0);
  445. }
  446. }
  447. }
  448. void WindowSDL::setTextInput(bool on) {
  449. assert(mInit == true);
  450. mTextInput = on;
  451. if (mTextInput)
  452. SDL_StartTextInput();
  453. else
  454. SDL_StopTextInput();
  455. }
  456. void WindowSDL::delay(clock_t ms) {
  457. assert(mInit == true);
  458. SDL_Delay(ms);
  459. }
  460. void WindowSDL::swapBuffersGL() {
  461. assert(mInit == true);
  462. SDL_GL_SwapWindow(mWindow);
  463. }
  464. void WindowSDL::setFont(const char *font) {
  465. assert(font != NULL);
  466. assert(font[0] != '\0');
  467. assert(mFontInit == false);
  468. mFontName = fullPath(font, 0);
  469. }
  470. int WindowSDL::initializeFont() {
  471. assert(mFontInit == false);
  472. assert(mFontName != NULL);
  473. assert(mFontName[0] != '\0');
  474. if (TTF_Init() != 0) {
  475. printf("Could not initialize SDL-TTF!\n");
  476. return -1;
  477. }
  478. mFont = TTF_OpenFont(mFontName, 24);
  479. if (mFont == NULL) {
  480. printf("TTF_OpenFont Error: %s\n", TTF_GetError());
  481. return -2;
  482. }
  483. glGenTextures(1, &mFontTexture);
  484. mFontInit = true;
  485. return 0;
  486. }
  487. void WindowSDL::writeString(WindowString *s) {
  488. assert(s != NULL);
  489. assert(s->text != NULL);
  490. assert(mInit == true);
  491. SDL_Color color;
  492. color.r = (unsigned char)(s->color[0] * 255.0f);
  493. color.g = (unsigned char)(s->color[1] * 255.0f);
  494. color.b = (unsigned char)(s->color[2] * 255.0f);
  495. color.a = (unsigned char)(s->color[3] * 255.0f);
  496. SDL_Surface *surface = TTF_RenderUTF8_Blended(mFont, s->text, color);
  497. if (surface == NULL) {
  498. printf("TTF_RenderUTF8_Blended Error: %s\n", TTF_GetError());
  499. return;
  500. }
  501. s->w = (int)((float)surface->w * s->scale);
  502. s->h = (int)((float)surface->h * s->scale);
  503. GLenum textureFormat;
  504. if (surface->format->BytesPerPixel == 4) {
  505. if (surface->format->Rmask == 0x000000FF)
  506. textureFormat = GL_RGBA;
  507. else
  508. textureFormat = GL_BGRA_EXT;
  509. } else {
  510. if (surface->format->Rmask == 0x000000FF)
  511. textureFormat = GL_RGB;
  512. else
  513. textureFormat = GL_BGR;
  514. }
  515. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  516. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  517. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  518. glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
  519. GLuint xMin = s->x;
  520. GLuint yMin = s->y;
  521. GLuint xMax = xMin + (int)((float)surface->w * s->scale);
  522. GLuint yMax = yMin + (int)((float)surface->h * s->scale);
  523. glColor4f(color.r / 256.0f, color.g / 256.0f, color.b / 256.0f, color.a / 256.0f);
  524. glBegin(GL_QUADS);
  525. glTexCoord2f(0.0f, 0.0f);
  526. glVertex2i(xMin, yMin);
  527. glTexCoord2f(0.0f, 1.0f);
  528. glVertex2i(xMin, yMax);
  529. glTexCoord2f(1.0f, 1.0f);
  530. glVertex2i(xMax, yMax);
  531. glTexCoord2f(1.0f, 0.0f);
  532. glVertex2i(xMax, yMin);
  533. glEnd();
  534. SDL_FreeSurface(surface);
  535. }
  536. void WindowSDL::drawText(unsigned int x, unsigned int y, float scale, const float color[4], const char *s, ...) {
  537. va_list args;
  538. va_start(args, s);
  539. vsnprintf(tempText.text, 256, s, args);
  540. tempText.text[255] = '\0';
  541. va_end(args);
  542. tempText.scale = scale;
  543. tempText.x = x;
  544. tempText.y = y;
  545. if (color) {
  546. tempText.color[0] = color[0];
  547. tempText.color[1] = color[1];
  548. tempText.color[2] = color[2];
  549. tempText.color[3] = color[3];
  550. }
  551. writeString(&tempText);
  552. }