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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. #ifdef MEMEORY_TEST
  32. # include "memeory_test.h"
  33. #endif
  34. #ifdef HAVE_OPENGL
  35. #include <SDL/SDL_opengl.h>
  36. #else
  37. # error "SDLSystem requires -DHAVE_OPENGL"
  38. #endif
  39. #ifdef PS2_LINUX
  40. # include "ps2.h"
  41. #endif
  42. #include "SDLSystem.h"
  43. unsigned int *gWidth = 0x0;
  44. unsigned int *gHeight = 0x0;
  45. /* 500, 50 */
  46. void glDrawGrid(float size, float step)
  47. {
  48. float x, y;
  49. // Draw grid
  50. glPushMatrix();
  51. glScalef(2.0f, 2.0f, 2.0f);
  52. glColor3f(0.4f, 0.4f, 0.6f);
  53. for (x = -size; x < size; x += step)
  54. {
  55. glBegin(GL_LINE_LOOP);
  56. for (y = -size; y < size; y += step)
  57. {
  58. glVertex3f(x + step, 0.0f, y);
  59. glVertex3f(x, 0.0f, y);
  60. glVertex3f(x, 0.0f, y + step);
  61. glVertex3f(x + step, 0.0f, y + step);
  62. }
  63. glEnd();
  64. }
  65. glPopMatrix();
  66. }
  67. void glDrawAxis(float length, float arrowLenght)
  68. {
  69. /* Draw axis list to show bone orientation */
  70. glBegin(GL_LINES);
  71. /* X axis */
  72. glColor3f(1.0f, 0.0f, 0.0f);
  73. glVertex3f(-8.0f, 0.0f, 0.0f);
  74. glVertex3f(8.0f, 0.0f, 0.0f);
  75. /* X direction */
  76. glVertex3f(8.0f, 0.0f, 0.0f);
  77. glVertex3f(7.0f, 1.0f, 0.0f);
  78. glVertex3f(8.0f, 0.0f, 0.0f);
  79. glVertex3f(7.0f, -1.0f, 0.0f);
  80. /* Y axis */
  81. glColor3f(0.0f, 1.0f, 0.0f);
  82. glVertex3f(0.0f, -8.0f, 0.0f);
  83. glVertex3f(0.0f, 8.0f, 0.0f);
  84. /* Y direction */
  85. glVertex3f(0.0f, 8.0f, 0.0f);
  86. glVertex3f(0.0f, 7.0f, 1.0f);
  87. glVertex3f(0.0f, 8.0f, 0.0f);
  88. glVertex3f(0.0f, 7.0f, -1.0f);
  89. /* Z axis */
  90. glColor3f(0.0f, 0.0f, 1.0f);
  91. glVertex3f(0.0f, 0.0f, -8.0f);
  92. glVertex3f(0.0f, 0.0f, 8.0f);
  93. /* Z direction */
  94. glVertex3f(0.0f, 0.0f, 8.0f);
  95. glVertex3f(0.0f, 1.0f, 7.0f);
  96. glVertex3f(0.0f, 0.0f, 8.0f);
  97. glVertex3f(0.0f, -1.0f, 7.0f);
  98. glEnd();
  99. }
  100. ////////////////////////////////////////////////////////////
  101. // Constructors
  102. ////////////////////////////////////////////////////////////
  103. SDLSystem::SDLSystem() : System()
  104. {
  105. mWindow = 0x0;
  106. gWidth = &m_width;
  107. gHeight = &m_height;
  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. on ? SDL_WM_GrabInput(SDL_GRAB_ON) : SDL_WM_GrabInput(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. if (fullscreen)
  198. {
  199. flags |= SDL_FULLSCREEN;
  200. SDL_ShowCursor(SDL_DISABLE);
  201. }
  202. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  203. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  204. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  205. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  206. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  207. mWindow = SDL_SetVideoMode(width, height, 16, flags);
  208. SDL_WM_SetCaption(VERSION, VERSION);
  209. //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  210. SDL_EnableKeyRepeat(100, SDL_DEFAULT_REPEAT_INTERVAL);
  211. #ifdef UNICODE_SUPPORT
  212. //@JML get Unicode value of key for international keyboard
  213. SDL_EnableUNICODE(1);
  214. #endif
  215. // Start game renderer
  216. initGL();
  217. // Resize context
  218. resizeGL(width, height);
  219. }
  220. void SDLSystem::resize(unsigned int width, unsigned int height)
  221. {
  222. GLfloat aspect;
  223. m_width = width;
  224. m_height = height;
  225. aspect = (GLfloat)width/(GLfloat)height;
  226. glViewport(0, 0, width, height);
  227. glMatrixMode(GL_PROJECTION);
  228. glLoadIdentity();
  229. gluPerspective(m_fovY, aspect, m_clipNear, m_clipFar);
  230. glMatrixMode(GL_MODELVIEW);
  231. glLoadIdentity();
  232. // Resize window
  233. mWindow = SDL_SetVideoMode(width, height, 16, SDL_OPENGL);
  234. // Resize context
  235. resizeGL(width, height);
  236. }
  237. void SDLSystem::runGame()
  238. {
  239. SDL_Event event;
  240. unsigned int mkeys, mod, key;
  241. int btn;
  242. bool specialKey;
  243. for (;;)
  244. {
  245. // Pause for 10-20 ms
  246. SDL_Delay(10);
  247. while (SDL_PollEvent(&event))
  248. {
  249. switch (event.type)
  250. {
  251. case SDL_QUIT:
  252. shutdown(0);
  253. break;
  254. case SDL_MOUSEMOTION:
  255. // Wrap motion
  256. handleMouseMotionEvent(event.motion.xrel*2, event.motion.yrel*2);
  257. break;
  258. case SDL_MOUSEBUTTONDOWN:
  259. case SDL_MOUSEBUTTONUP:
  260. //handleMouseEvent(event.button.button, event.button.state,
  261. // event.button.x, event.button.y);
  262. switch (event.button.button)
  263. {
  264. case SDL_BUTTON_LEFT:
  265. btn = SYS_MOUSE_LEFT;
  266. break;
  267. case SDL_BUTTON_RIGHT:
  268. btn = SYS_MOUSE_RIGHT;
  269. break;
  270. case SDL_BUTTON_MIDDLE:
  271. btn = SYS_MOUSE_MIDDLE;
  272. break;
  273. }
  274. if (event.button.state == SDL_PRESSED)
  275. {
  276. handleKeyPressEvent(btn, 0); // FIXME: mod not used
  277. }
  278. else
  279. {
  280. handleKeyReleaseEvent(btn, 0); // FIXME: mod not used
  281. }
  282. break;
  283. case SDL_KEYUP:
  284. case SDL_KEYDOWN:
  285. //SDL_GetMouseState(&x, &y); // Get cursor pos
  286. mkeys = (unsigned int)SDL_GetModState();
  287. mod = 0;
  288. if (mkeys & KMOD_LSHIFT)
  289. mod |= SYS_MOD_KEY_LSHIFT;
  290. if (mkeys & KMOD_RSHIFT)
  291. mod |= SYS_MOD_KEY_RSHIFT;
  292. if (mkeys & KMOD_LCTRL)
  293. mod |= SYS_MOD_KEY_LCTRL;
  294. if (mkeys & KMOD_RCTRL)
  295. mod |= SYS_MOD_KEY_RCTRL;
  296. if (mkeys & KMOD_LALT)
  297. mod |= SYS_MOD_KEY_LALT;
  298. if (mkeys & KMOD_RALT)
  299. mod |= SYS_MOD_KEY_RALT;
  300. key = event.key.keysym.sym;
  301. specialKey = false;
  302. switch (key)
  303. {
  304. case SDLK_F1:
  305. key = SYS_KEY_F1;
  306. specialKey = true;
  307. break;
  308. case SDLK_F2:
  309. key = SYS_KEY_F2;
  310. specialKey = true;
  311. break;
  312. case SDLK_F3:
  313. key = SYS_KEY_F3;
  314. specialKey = true;
  315. break;
  316. case SDLK_F4:
  317. key = SYS_KEY_F4;
  318. specialKey = true;
  319. break;
  320. case SDLK_F5:
  321. key = SYS_KEY_F5;
  322. specialKey = true;
  323. break;
  324. case SDLK_F6:
  325. key = SYS_KEY_F6;
  326. specialKey = true;
  327. break;
  328. case SDLK_F7:
  329. key = SYS_KEY_F7;
  330. specialKey = true;
  331. break;
  332. case SDLK_F8:
  333. key = SYS_KEY_F8;
  334. specialKey = true;
  335. break;
  336. case SDLK_F9:
  337. key = SYS_KEY_F9;
  338. specialKey = true;
  339. break;
  340. case SDLK_F10:
  341. key = SYS_KEY_F10;
  342. specialKey = true;
  343. break;
  344. case SDLK_F11:
  345. key = SYS_KEY_F11;
  346. specialKey = true;
  347. break;
  348. case SDLK_F12:
  349. key = SYS_KEY_F12;
  350. specialKey = true;
  351. break;
  352. case SDLK_UP:
  353. key = SYS_KEY_UP;
  354. specialKey = true;
  355. break;
  356. case SDLK_DOWN:
  357. key = SYS_KEY_DOWN;
  358. specialKey = true;
  359. break;
  360. case SDLK_RIGHT:
  361. key = SYS_KEY_RIGHT;
  362. specialKey = true;
  363. break;
  364. case SDLK_LEFT:
  365. key = SYS_KEY_LEFT;
  366. specialKey = true;
  367. break;
  368. case SDLK_PAGEDOWN:
  369. key = SYS_KEY_PAGEDOWN;
  370. specialKey = true;
  371. break;
  372. case SDLK_PAGEUP:
  373. key = SYS_KEY_PAGEUP;
  374. specialKey = true;
  375. break;
  376. }
  377. #ifdef UNICODE_SUPPORT
  378. // JML: if a std key was pressed get it ascii code
  379. if (!specialKey && key != 0)
  380. {
  381. if ((event.key.keysym.unicode & 0xFF80) == 0)
  382. {
  383. key= (unsigned int)(event.key.keysym.unicode & 0x7F);
  384. }
  385. else
  386. {
  387. key = 0;
  388. }
  389. }
  390. #else
  391. // FIXME: Avoid passing modifers as a key, since the
  392. // consoles using this expect text characters, add unicode
  393. // support later when they're able to handle it
  394. if (key > 255 && key < 1000)
  395. {
  396. key = 0;
  397. }
  398. #endif
  399. if (key == mConsoleKey)
  400. {
  401. if (event.type == SDL_KEYDOWN)
  402. {
  403. mConsoleMode = !mConsoleMode;
  404. // Tmp hack
  405. handleConsoleKeyPressEvent(mConsoleKey, 0);
  406. }
  407. }
  408. else if (mConsoleMode) // Console keying ( text input )
  409. {
  410. switch (event.type)
  411. {
  412. case SDL_KEYDOWN:
  413. handleConsoleKeyPressEvent(key, mod);
  414. break;
  415. default:
  416. ;
  417. }
  418. }
  419. else if (mKeyEvents[key] != 0)// Bound key
  420. {
  421. //if (key < 255 && mKeyEvents[key] != 0)
  422. key = mKeyEvents[key];
  423. switch (event.type)
  424. {
  425. case SDL_KEYDOWN:
  426. handleBoundKeyPressEvent(key);
  427. break;
  428. default:
  429. handleBoundKeyReleaseEvent(key);
  430. }
  431. }
  432. else // 'Classic' key event handlers
  433. {
  434. switch (event.type)
  435. {
  436. case SDL_KEYDOWN:
  437. handleKeyPressEvent(key, mod);
  438. break;
  439. default:
  440. handleKeyReleaseEvent(key, mod);
  441. }
  442. }
  443. break;
  444. case SDL_VIDEORESIZE:
  445. resizeGL(event.resize.w, event.resize.h);
  446. gameFrame();
  447. break;
  448. }
  449. }
  450. // Game frame
  451. gameFrame();
  452. }
  453. }
  454. void SDLSystem::shutdown(int i)
  455. {
  456. //SDL_QuitSubSystem(SDL_OPENGL);
  457. //SDL_Quit(); // Moved to atexit() call
  458. //#ifdef DEBUG_MEMEORY
  459. //printf("[Mongoose MEMEORY_DEBUG]\nUnfreed memory table:\n");
  460. //dump_memory_report();
  461. //#endif
  462. exit(0);
  463. }
  464. void SDLSystem::toggleFullscreen()
  465. {
  466. if (mWindow)
  467. {
  468. SDL_ShowCursor(SDL_DISABLE);
  469. SDL_WM_ToggleFullScreen(mWindow);
  470. }
  471. }
  472. void SDLSystem::swapBuffersGL()
  473. {
  474. SDL_GL_SwapBuffers();
  475. }
  476. ////////////////////////////////////////////////////////////
  477. // Unit Test code
  478. ////////////////////////////////////////////////////////////
  479. #ifdef __UNIT_TEST__
  480. int SDL::unitTest(int argc, char *argv[])
  481. {
  482. return 0;
  483. }
  484. int main(int argc, char *argv[])
  485. {
  486. SDL test;
  487. printf("[SDL class test]\n");
  488. return test._UnitTest(argc, argv);
  489. }
  490. #endif