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

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