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.

SDLSystem.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : OpenRaider
  5. * Author : Terry 'Mongoose' Hendrix II
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : SDL
  9. * License : No use w/o permission (C) 2002 Mongoose
  10. * Comments:
  11. *
  12. *
  13. * This file was generated using Mongoose's C++
  14. * template generator script. <stu7440@westga.edu>
  15. *
  16. *-- History -------------------------------------------------
  17. *
  18. * 2003.06.30,
  19. * Mongoose - SDL_TTF support moved to Texture class
  20. *
  21. * 2003.06.03:
  22. * Mongoose - SDL_TTF support based on Sam Lantinga's public domain
  23. * SDL_TTF demo functions and algorithms
  24. *
  25. * 2002.06.06:
  26. * Mongoose - Created
  27. =================================================================*/
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <cmath>
  32. #ifdef MEMORY_TEST
  33. #include <memory_test.h>
  34. #endif
  35. #ifdef USING_OPENGL
  36. #include <SDL/SDL_opengl.h>
  37. #else
  38. #error "SDLSystem requires -DUSING_OPENGL"
  39. #endif
  40. #include <SDLSystem.h>
  41. unsigned int *gWidth = 0x0;
  42. unsigned int *gHeight = 0x0;
  43. /* 500, 50 */
  44. void glDrawGrid(float size, float step)
  45. {
  46. float x, y;
  47. // Draw grid
  48. glPushMatrix();
  49. glScalef(2.0f, 2.0f, 2.0f);
  50. glColor3f(0.4f, 0.4f, 0.6f);
  51. for (x = -size; x < size; x += step)
  52. {
  53. glBegin(GL_LINE_LOOP);
  54. for (y = -size; y < size; y += step)
  55. {
  56. glVertex3f(x + step, 0.0f, y);
  57. glVertex3f(x, 0.0f, y);
  58. glVertex3f(x, 0.0f, y + step);
  59. glVertex3f(x + step, 0.0f, y + step);
  60. }
  61. glEnd();
  62. }
  63. glPopMatrix();
  64. }
  65. void glDrawAxis(float length, float arrowLenght)
  66. {
  67. /* Draw axis list to show bone orientation */
  68. glBegin(GL_LINES);
  69. /* X axis */
  70. glColor3f(1.0f, 0.0f, 0.0f);
  71. glVertex3f(-8.0f, 0.0f, 0.0f);
  72. glVertex3f(8.0f, 0.0f, 0.0f);
  73. /* X direction */
  74. glVertex3f(8.0f, 0.0f, 0.0f);
  75. glVertex3f(7.0f, 1.0f, 0.0f);
  76. glVertex3f(8.0f, 0.0f, 0.0f);
  77. glVertex3f(7.0f, -1.0f, 0.0f);
  78. /* Y axis */
  79. glColor3f(0.0f, 1.0f, 0.0f);
  80. glVertex3f(0.0f, -8.0f, 0.0f);
  81. glVertex3f(0.0f, 8.0f, 0.0f);
  82. /* Y direction */
  83. glVertex3f(0.0f, 8.0f, 0.0f);
  84. glVertex3f(0.0f, 7.0f, 1.0f);
  85. glVertex3f(0.0f, 8.0f, 0.0f);
  86. glVertex3f(0.0f, 7.0f, -1.0f);
  87. /* Z axis */
  88. glColor3f(0.0f, 0.0f, 1.0f);
  89. glVertex3f(0.0f, 0.0f, -8.0f);
  90. glVertex3f(0.0f, 0.0f, 8.0f);
  91. /* Z direction */
  92. glVertex3f(0.0f, 0.0f, 8.0f);
  93. glVertex3f(0.0f, 1.0f, 7.0f);
  94. glVertex3f(0.0f, 0.0f, 8.0f);
  95. glVertex3f(0.0f, -1.0f, 7.0f);
  96. glEnd();
  97. }
  98. ////////////////////////////////////////////////////////////
  99. // Constructors
  100. ////////////////////////////////////////////////////////////
  101. SDLSystem::SDLSystem() : System()
  102. {
  103. mWindow = 0x0;
  104. gWidth = &m_width;
  105. gHeight = &m_height;
  106. mFirstMouseEvent = false;
  107. mFullscreen = false;
  108. }
  109. SDLSystem::~SDLSystem()
  110. {
  111. }
  112. ////////////////////////////////////////////////////////////
  113. // Public Accessors
  114. ////////////////////////////////////////////////////////////
  115. unsigned int SDLSystem::getTicks()
  116. {
  117. return SDL_GetTicks();
  118. }
  119. ////////////////////////////////////////////////////////////
  120. // Public Mutators
  121. ////////////////////////////////////////////////////////////
  122. #ifdef FIXME
  123. void SDLSystem::bindKeyCommand(const char *cmd, int key, int event)
  124. {
  125. if (key > 255)
  126. {
  127. printf("Currently key event mapping only handles ASCII characters\n");
  128. return;
  129. }
  130. printf("Bound command '%s' -> event %i (0x%x key)\n", cmd, event, key);
  131. keyEvents[key] = event;
  132. }
  133. #endif
  134. void SDLSystem::glPrintf2d(float x, float y, char *string)
  135. {
  136. #ifdef HAVE_SDL_TTF
  137. # ifdef FIXME
  138. // FIXME: Filler
  139. glBindTexture(GL_TEXTURE_2D, texture);
  140. glBegin(GL_TRIANGLE_STRIP);
  141. glTexCoord2f(texMinX, texMinY); glVertex2i(x, y );
  142. glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y );
  143. glTexCoord2f(texMinX, texMaxY); glVertex2i(x, y+h);
  144. glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
  145. glEnd();
  146. # endif
  147. #endif
  148. }
  149. void SDLSystem::glPrintf3d(float x, float y, float z, char *string)
  150. {
  151. #ifdef HAVE_SDL_TTF_FIXME
  152. // FIXME: Filler
  153. // FIXME: Billboarding here requires a yaw jackass =)
  154. glBindTexture(GL_TEXTURE_2D, texture);
  155. glBegin(GL_TRIANGLE_STRIP);
  156. glTexCoord2f(texMinX, texMinY); glVertex2i(x, y );
  157. glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y );
  158. glTexCoord2f(texMinX, texMaxY); glVertex2i(x, y+h);
  159. glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
  160. glEnd();
  161. #endif
  162. }
  163. void SDLSystem::setGrabMouse(bool on)
  164. {
  165. SDL_WM_GrabInput(on ? SDL_GRAB_ON : SDL_GRAB_OFF);
  166. }
  167. void SDLSystem::initVideo(unsigned int width, unsigned int height,
  168. bool fullscreen)
  169. {
  170. int flags; //, x, y;
  171. // Create GL context
  172. SDL_Init(SDL_INIT_VIDEO);
  173. printf("Created OpenGL Context\n");
  174. atexit(SDL_Quit);
  175. m_width = width;
  176. m_height = height;
  177. #ifndef __APPLE__
  178. if (!m_driver || !m_driver[0] || SDL_GL_LoadLibrary(m_driver) < 0)
  179. {
  180. SDL_ClearError();
  181. // Fallback 1
  182. if (SDL_GL_LoadLibrary("libGL.so") < 0)
  183. {
  184. SDL_ClearError();
  185. // Fallback 2
  186. if (SDL_GL_LoadLibrary("libGL.so.1") < 0)
  187. {
  188. fprintf(stderr, "initVideo> SDL_GL_LoadLibrary failed!\n");
  189. fprintf(stderr, "initVideo> Error is [%s].\n", SDL_GetError());
  190. shutdown(1);
  191. }
  192. }
  193. }
  194. #endif
  195. flags = 0;
  196. flags |= SDL_OPENGL;
  197. mFullscreen = fullscreen;
  198. if (mFullscreen)
  199. {
  200. flags |= SDL_FULLSCREEN;
  201. }
  202. SDL_ShowCursor(SDL_DISABLE);
  203. setGrabMouse(true); // Always grab mouse!
  204. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  205. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  206. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  207. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  208. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  209. mWindow = SDL_SetVideoMode(width, height, 16, flags);
  210. SDL_WM_SetCaption(VERSION, VERSION);
  211. //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  212. SDL_EnableKeyRepeat(100, SDL_DEFAULT_REPEAT_INTERVAL);
  213. #ifdef UNICODE_SUPPORT
  214. //@JML get Unicode value of key for international keyboard
  215. SDL_EnableUNICODE(1);
  216. #endif
  217. // Start game renderer
  218. initGL();
  219. // Resize context
  220. resizeGL(width, height);
  221. }
  222. void SDLSystem::resize(unsigned int width, unsigned int height)
  223. {
  224. int flags;
  225. GLfloat aspect;
  226. m_width = width;
  227. m_height = height;
  228. aspect = (GLfloat)width/(GLfloat)height;
  229. glViewport(0, 0, width, height);
  230. glMatrixMode(GL_PROJECTION);
  231. glLoadIdentity();
  232. // gluPerspective is deprecated!
  233. // gluPerspective(m_fovY, aspect, m_clipNear, m_clipFar);
  234. // fix: http://stackoverflow.com/a/2417756
  235. GLfloat fH = tan(float(m_fovY / 360.0f * 3.14159f) ) * m_clipNear;
  236. GLfloat fW = fH * aspect;
  237. glFrustum(-fW, fW, -fH, fH, m_clipNear, m_clipFar);
  238. glMatrixMode(GL_MODELVIEW);
  239. glLoadIdentity();
  240. // Resize window
  241. flags = SDL_OPENGL;
  242. if (mFullscreen)
  243. flags |= SDL_FULLSCREEN;
  244. mWindow = SDL_SetVideoMode(width, height, 16, flags);
  245. // Resize context
  246. resizeGL(width, height);
  247. }
  248. void SDLSystem::runGame()
  249. {
  250. SDL_Event event;
  251. unsigned int mkeys, mod, key;
  252. int btn;
  253. bool specialKey;
  254. for (;;)
  255. {
  256. // Pause for 10-20 ms
  257. SDL_Delay(10);
  258. while (SDL_PollEvent(&event))
  259. {
  260. switch (event.type)
  261. {
  262. case SDL_QUIT:
  263. shutdown(0);
  264. break;
  265. case SDL_MOUSEMOTION:
  266. // Wrap motion
  267. if (!mFirstMouseEvent) {
  268. mFirstMouseEvent = true;
  269. } else {
  270. handleMouseMotionEvent(event.motion.xrel/2, event.motion.yrel/2);
  271. }
  272. break;
  273. case SDL_MOUSEBUTTONDOWN:
  274. case SDL_MOUSEBUTTONUP:
  275. //handleMouseEvent(event.button.button, event.button.state,
  276. // event.button.x, event.button.y);
  277. switch (event.button.button)
  278. {
  279. case SDL_BUTTON_LEFT:
  280. btn = SYS_MOUSE_LEFT;
  281. break;
  282. case SDL_BUTTON_RIGHT:
  283. btn = SYS_MOUSE_RIGHT;
  284. break;
  285. case SDL_BUTTON_MIDDLE:
  286. btn = SYS_MOUSE_MIDDLE;
  287. break;
  288. }
  289. if (event.button.state == SDL_PRESSED)
  290. {
  291. handleKeyPressEvent(btn, 0); // FIXME: mod not used
  292. }
  293. else
  294. {
  295. handleKeyReleaseEvent(btn, 0); // FIXME: mod not used
  296. }
  297. break;
  298. case SDL_KEYUP:
  299. case SDL_KEYDOWN:
  300. //SDL_GetMouseState(&x, &y); // Get cursor pos
  301. mkeys = (unsigned int)SDL_GetModState();
  302. mod = 0;
  303. if (mkeys & KMOD_LSHIFT)
  304. mod |= SYS_MOD_KEY_LSHIFT;
  305. if (mkeys & KMOD_RSHIFT)
  306. mod |= SYS_MOD_KEY_RSHIFT;
  307. if (mkeys & KMOD_LCTRL)
  308. mod |= SYS_MOD_KEY_LCTRL;
  309. if (mkeys & KMOD_RCTRL)
  310. mod |= SYS_MOD_KEY_RCTRL;
  311. if (mkeys & KMOD_LALT)
  312. mod |= SYS_MOD_KEY_LALT;
  313. if (mkeys & KMOD_RALT)
  314. mod |= SYS_MOD_KEY_RALT;
  315. if (mkeys & KMOD_LMETA)
  316. mod |= SYS_MOD_KEY_LMETA;
  317. if (mkeys & KMOD_RMETA)
  318. mod |= SYS_MOD_KEY_RMETA;
  319. key = event.key.keysym.sym;
  320. specialKey = false;
  321. switch (key)
  322. {
  323. case SDLK_F1:
  324. key = SYS_KEY_F1;
  325. specialKey = true;
  326. break;
  327. case SDLK_F2:
  328. key = SYS_KEY_F2;
  329. specialKey = true;
  330. break;
  331. case SDLK_F3:
  332. key = SYS_KEY_F3;
  333. specialKey = true;
  334. break;
  335. case SDLK_F4:
  336. key = SYS_KEY_F4;
  337. specialKey = true;
  338. break;
  339. case SDLK_F5:
  340. key = SYS_KEY_F5;
  341. specialKey = true;
  342. break;
  343. case SDLK_F6:
  344. key = SYS_KEY_F6;
  345. specialKey = true;
  346. break;
  347. case SDLK_F7:
  348. key = SYS_KEY_F7;
  349. specialKey = true;
  350. break;
  351. case SDLK_F8:
  352. key = SYS_KEY_F8;
  353. specialKey = true;
  354. break;
  355. case SDLK_F9:
  356. key = SYS_KEY_F9;
  357. specialKey = true;
  358. break;
  359. case SDLK_F10:
  360. key = SYS_KEY_F10;
  361. specialKey = true;
  362. break;
  363. case SDLK_F11:
  364. key = SYS_KEY_F11;
  365. specialKey = true;
  366. break;
  367. case SDLK_F12:
  368. key = SYS_KEY_F12;
  369. specialKey = true;
  370. break;
  371. case SDLK_UP:
  372. key = SYS_KEY_UP;
  373. specialKey = true;
  374. break;
  375. case SDLK_DOWN:
  376. key = SYS_KEY_DOWN;
  377. specialKey = true;
  378. break;
  379. case SDLK_RIGHT:
  380. key = SYS_KEY_RIGHT;
  381. specialKey = true;
  382. break;
  383. case SDLK_LEFT:
  384. key = SYS_KEY_LEFT;
  385. specialKey = true;
  386. break;
  387. case SDLK_PAGEDOWN:
  388. key = SYS_KEY_PAGEDOWN;
  389. specialKey = true;
  390. break;
  391. case SDLK_PAGEUP:
  392. key = SYS_KEY_PAGEUP;
  393. specialKey = true;
  394. break;
  395. }
  396. #ifdef __APPLE__
  397. // Handle CMD+Q to quit in all circumstances
  398. if (key == 'q') {
  399. if (mod & SYS_MOD_KEY_LMETA) {
  400. shutdown(0);
  401. }
  402. }
  403. #endif
  404. #ifdef UNICODE_SUPPORT
  405. // JML: if a std key was pressed get it ascii code
  406. if (!specialKey && key != 0)
  407. {
  408. if ((event.key.keysym.unicode & 0xFF80) == 0)
  409. {
  410. key= (unsigned int)(event.key.keysym.unicode & 0x7F);
  411. }
  412. else
  413. {
  414. key = 0;
  415. }
  416. }
  417. #else
  418. // FIXME: Avoid passing modifers as a key, since the
  419. // consoles using this expect text characters, add unicode
  420. // support later when they're able to handle it
  421. if (key > 255 && key < 1000)
  422. {
  423. key = 0;
  424. }
  425. #endif
  426. if (key == mConsoleKey)
  427. {
  428. if (event.type == SDL_KEYDOWN)
  429. {
  430. mConsoleMode = !mConsoleMode;
  431. // Tmp hack
  432. handleConsoleKeyPressEvent(mConsoleKey, 0);
  433. }
  434. }
  435. else if (mConsoleMode) // Console keying ( text input )
  436. {
  437. switch (event.type)
  438. {
  439. case SDL_KEYDOWN:
  440. handleConsoleKeyPressEvent(key, mod);
  441. break;
  442. default:
  443. ;
  444. }
  445. }
  446. else if (mKeyEvents[key] != 0)// Bound key
  447. {
  448. //if (key < 255 && mKeyEvents[key] != 0)
  449. key = mKeyEvents[key];
  450. switch (event.type)
  451. {
  452. case SDL_KEYDOWN:
  453. handleBoundKeyPressEvent(key);
  454. break;
  455. default:
  456. handleBoundKeyReleaseEvent(key);
  457. }
  458. }
  459. else // 'Classic' key event handlers
  460. {
  461. switch (event.type)
  462. {
  463. case SDL_KEYDOWN:
  464. handleKeyPressEvent(key, mod);
  465. break;
  466. default:
  467. handleKeyReleaseEvent(key, mod);
  468. }
  469. }
  470. break;
  471. case SDL_VIDEORESIZE:
  472. resizeGL(event.resize.w, event.resize.h);
  473. gameFrame();
  474. break;
  475. }
  476. }
  477. // Game frame
  478. gameFrame();
  479. }
  480. }
  481. void SDLSystem::shutdown(int i)
  482. {
  483. //SDL_QuitSubSystem(SDL_OPENGL);
  484. //SDL_Quit(); // Moved to atexit() call
  485. //#ifdef DEBUG_MEMORY
  486. //printf("[Mongoose MEMORY_DEBUG]\nUnfreed memory table:\n");
  487. //dump_memory_report();
  488. //#endif
  489. exit(0);
  490. }
  491. void SDLSystem::toggleFullscreen()
  492. {
  493. if (mWindow)
  494. {
  495. mFullscreen = !mFullscreen;
  496. SDL_ShowCursor(SDL_DISABLE);
  497. // SDL_WM_ToggleFullScreen does not work on all platforms
  498. // eg. Mac OS X
  499. // SDL_WM_ToggleFullScreen(mWindow);
  500. // I added a mFullscreen flag to this class. Then I modified it's
  501. // resize() method to use the SDL_FULLSCREEN flag in the
  502. // SetVideoMode() call based on the mFullscreen flag.
  503. // Then, I modified this method to find out an available
  504. // resolution for the fullscreen mode.
  505. // Now you can see something when switching to Fullscreen,
  506. // but it's full of graphical glitches...? I don't know...
  507. // -- xythobuz 2013-12-31
  508. int width, height;
  509. if (mFullscreen) {
  510. m_old_width = m_width;
  511. m_old_height = m_height;
  512. SDL_Rect **dimensions = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
  513. if (dimensions == NULL) {
  514. printf("Can't enter fullscreen!\n");
  515. mFullscreen = !mFullscreen;
  516. }
  517. if (dimensions != (SDL_Rect **)-1) {
  518. //! \fixme Don't just use first available resolution...
  519. width = dimensions[0]->w;
  520. height = dimensions[0]->h;
  521. } else {
  522. // No restrictions, use current resolution
  523. width = m_width;
  524. height = m_height;
  525. }
  526. }
  527. if (!mFullscreen) {
  528. width = m_old_width;
  529. height = m_old_height;
  530. }
  531. resize(width, height);
  532. }
  533. }
  534. void SDLSystem::swapBuffersGL()
  535. {
  536. SDL_GL_SwapBuffers();
  537. }