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

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