Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SDLSystem.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 MEMEORY_TEST
  33. # include "memeory_test.h"
  34. #endif
  35. #ifdef HAVE_OPENGL
  36. #include <SDL/SDL_opengl.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(const 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. #ifndef __APPLE__
  179. if (!m_driver || !m_driver[0] || SDL_GL_LoadLibrary(m_driver) < 0)
  180. {
  181. SDL_ClearError();
  182. // Fallback 1
  183. if (SDL_GL_LoadLibrary("libGL.so") < 0)
  184. {
  185. SDL_ClearError();
  186. // Fallback 2
  187. if (SDL_GL_LoadLibrary("libGL.so.1") < 0)
  188. {
  189. fprintf(stderr, "initVideo> SDL_GL_LoadLibrary failed!\n");
  190. fprintf(stderr, "initVideo> Error is [%s].\n", SDL_GetError());
  191. shutdown(1);
  192. }
  193. }
  194. }
  195. #endif
  196. flags = 0;
  197. flags |= SDL_OPENGL;
  198. if (fullscreen)
  199. {
  200. flags |= SDL_FULLSCREEN;
  201. SDL_ShowCursor(SDL_DISABLE);
  202. }
  203. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  204. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  205. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  206. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  207. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  208. mWindow = SDL_SetVideoMode(width, height, 16, flags);
  209. SDL_WM_SetCaption(VERSION, VERSION);
  210. //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  211. SDL_EnableKeyRepeat(100, SDL_DEFAULT_REPEAT_INTERVAL);
  212. #ifdef UNICODE_SUPPORT
  213. //@JML get Unicode value of key for international keyboard
  214. SDL_EnableUNICODE(1);
  215. #endif
  216. // Start game renderer
  217. initGL();
  218. // Resize context
  219. resizeGL(width, height);
  220. }
  221. void SDLSystem::resize(unsigned int width, unsigned int height)
  222. {
  223. GLfloat aspect;
  224. m_width = width;
  225. m_height = height;
  226. aspect = (GLfloat)width/(GLfloat)height;
  227. glViewport(0, 0, width, height);
  228. glMatrixMode(GL_PROJECTION);
  229. glLoadIdentity();
  230. // gluPerspective is deprecated!
  231. // gluPerspective(m_fovY, aspect, m_clipNear, m_clipFar);
  232. // fix: http://stackoverflow.com/a/2417756
  233. GLfloat fH = tan(float(m_fovY / 360.0f * 3.14159f) ) * m_clipNear;
  234. GLfloat fW = fH * aspect;
  235. glFrustum(-fW, fW, -fH, fH, m_clipNear, m_clipFar);
  236. glMatrixMode(GL_MODELVIEW);
  237. glLoadIdentity();
  238. // Resize window
  239. mWindow = SDL_SetVideoMode(width, height, 16, SDL_OPENGL);
  240. // Resize context
  241. resizeGL(width, height);
  242. }
  243. void SDLSystem::runGame()
  244. {
  245. SDL_Event event;
  246. unsigned int mkeys, mod, key;
  247. int btn;
  248. bool specialKey;
  249. for (;;)
  250. {
  251. // Pause for 10-20 ms
  252. SDL_Delay(10);
  253. while (SDL_PollEvent(&event))
  254. {
  255. switch (event.type)
  256. {
  257. case SDL_QUIT:
  258. shutdown(0);
  259. break;
  260. case SDL_MOUSEMOTION:
  261. // Wrap motion
  262. handleMouseMotionEvent(event.motion.xrel*2, event.motion.yrel*2);
  263. break;
  264. case SDL_MOUSEBUTTONDOWN:
  265. case SDL_MOUSEBUTTONUP:
  266. //handleMouseEvent(event.button.button, event.button.state,
  267. // event.button.x, event.button.y);
  268. switch (event.button.button)
  269. {
  270. case SDL_BUTTON_LEFT:
  271. btn = SYS_MOUSE_LEFT;
  272. break;
  273. case SDL_BUTTON_RIGHT:
  274. btn = SYS_MOUSE_RIGHT;
  275. break;
  276. case SDL_BUTTON_MIDDLE:
  277. btn = SYS_MOUSE_MIDDLE;
  278. break;
  279. }
  280. if (event.button.state == SDL_PRESSED)
  281. {
  282. handleKeyPressEvent(btn, 0); // FIXME: mod not used
  283. }
  284. else
  285. {
  286. handleKeyReleaseEvent(btn, 0); // FIXME: mod not used
  287. }
  288. break;
  289. case SDL_KEYUP:
  290. case SDL_KEYDOWN:
  291. //SDL_GetMouseState(&x, &y); // Get cursor pos
  292. mkeys = (unsigned int)SDL_GetModState();
  293. mod = 0;
  294. if (mkeys & KMOD_LSHIFT)
  295. mod |= SYS_MOD_KEY_LSHIFT;
  296. if (mkeys & KMOD_RSHIFT)
  297. mod |= SYS_MOD_KEY_RSHIFT;
  298. if (mkeys & KMOD_LCTRL)
  299. mod |= SYS_MOD_KEY_LCTRL;
  300. if (mkeys & KMOD_RCTRL)
  301. mod |= SYS_MOD_KEY_RCTRL;
  302. if (mkeys & KMOD_LALT)
  303. mod |= SYS_MOD_KEY_LALT;
  304. if (mkeys & KMOD_RALT)
  305. mod |= SYS_MOD_KEY_RALT;
  306. key = event.key.keysym.sym;
  307. specialKey = false;
  308. switch (key)
  309. {
  310. case SDLK_F1:
  311. key = SYS_KEY_F1;
  312. specialKey = true;
  313. break;
  314. case SDLK_F2:
  315. key = SYS_KEY_F2;
  316. specialKey = true;
  317. break;
  318. case SDLK_F3:
  319. key = SYS_KEY_F3;
  320. specialKey = true;
  321. break;
  322. case SDLK_F4:
  323. key = SYS_KEY_F4;
  324. specialKey = true;
  325. break;
  326. case SDLK_F5:
  327. key = SYS_KEY_F5;
  328. specialKey = true;
  329. break;
  330. case SDLK_F6:
  331. key = SYS_KEY_F6;
  332. specialKey = true;
  333. break;
  334. case SDLK_F7:
  335. key = SYS_KEY_F7;
  336. specialKey = true;
  337. break;
  338. case SDLK_F8:
  339. key = SYS_KEY_F8;
  340. specialKey = true;
  341. break;
  342. case SDLK_F9:
  343. key = SYS_KEY_F9;
  344. specialKey = true;
  345. break;
  346. case SDLK_F10:
  347. key = SYS_KEY_F10;
  348. specialKey = true;
  349. break;
  350. case SDLK_F11:
  351. key = SYS_KEY_F11;
  352. specialKey = true;
  353. break;
  354. case SDLK_F12:
  355. key = SYS_KEY_F12;
  356. specialKey = true;
  357. break;
  358. case SDLK_UP:
  359. key = SYS_KEY_UP;
  360. specialKey = true;
  361. break;
  362. case SDLK_DOWN:
  363. key = SYS_KEY_DOWN;
  364. specialKey = true;
  365. break;
  366. case SDLK_RIGHT:
  367. key = SYS_KEY_RIGHT;
  368. specialKey = true;
  369. break;
  370. case SDLK_LEFT:
  371. key = SYS_KEY_LEFT;
  372. specialKey = true;
  373. break;
  374. case SDLK_PAGEDOWN:
  375. key = SYS_KEY_PAGEDOWN;
  376. specialKey = true;
  377. break;
  378. case SDLK_PAGEUP:
  379. key = SYS_KEY_PAGEUP;
  380. specialKey = true;
  381. break;
  382. }
  383. #ifdef UNICODE_SUPPORT
  384. // JML: if a std key was pressed get it ascii code
  385. if (!specialKey && key != 0)
  386. {
  387. if ((event.key.keysym.unicode & 0xFF80) == 0)
  388. {
  389. key= (unsigned int)(event.key.keysym.unicode & 0x7F);
  390. }
  391. else
  392. {
  393. key = 0;
  394. }
  395. }
  396. #else
  397. // FIXME: Avoid passing modifers as a key, since the
  398. // consoles using this expect text characters, add unicode
  399. // support later when they're able to handle it
  400. if (key > 255 && key < 1000)
  401. {
  402. key = 0;
  403. }
  404. #endif
  405. if (key == mConsoleKey)
  406. {
  407. if (event.type == SDL_KEYDOWN)
  408. {
  409. mConsoleMode = !mConsoleMode;
  410. // Tmp hack
  411. handleConsoleKeyPressEvent(mConsoleKey, 0);
  412. }
  413. }
  414. else if (mConsoleMode) // Console keying ( text input )
  415. {
  416. switch (event.type)
  417. {
  418. case SDL_KEYDOWN:
  419. handleConsoleKeyPressEvent(key, mod);
  420. break;
  421. default:
  422. ;
  423. }
  424. }
  425. else if (mKeyEvents[key] != 0)// Bound key
  426. {
  427. //if (key < 255 && mKeyEvents[key] != 0)
  428. key = mKeyEvents[key];
  429. switch (event.type)
  430. {
  431. case SDL_KEYDOWN:
  432. handleBoundKeyPressEvent(key);
  433. break;
  434. default:
  435. handleBoundKeyReleaseEvent(key);
  436. }
  437. }
  438. else // 'Classic' key event handlers
  439. {
  440. switch (event.type)
  441. {
  442. case SDL_KEYDOWN:
  443. handleKeyPressEvent(key, mod);
  444. break;
  445. default:
  446. handleKeyReleaseEvent(key, mod);
  447. }
  448. }
  449. break;
  450. case SDL_VIDEORESIZE:
  451. resizeGL(event.resize.w, event.resize.h);
  452. gameFrame();
  453. break;
  454. }
  455. }
  456. // Game frame
  457. gameFrame();
  458. }
  459. }
  460. void SDLSystem::shutdown(int i)
  461. {
  462. //SDL_QuitSubSystem(SDL_OPENGL);
  463. //SDL_Quit(); // Moved to atexit() call
  464. //#ifdef DEBUG_MEMEORY
  465. //printf("[Mongoose MEMEORY_DEBUG]\nUnfreed memory table:\n");
  466. //dump_memory_report();
  467. //#endif
  468. exit(0);
  469. }
  470. void SDLSystem::toggleFullscreen()
  471. {
  472. if (mWindow)
  473. {
  474. SDL_ShowCursor(SDL_DISABLE);
  475. SDL_WM_ToggleFullScreen(mWindow);
  476. }
  477. }
  478. void SDLSystem::swapBuffersGL()
  479. {
  480. SDL_GL_SwapBuffers();
  481. }
  482. ////////////////////////////////////////////////////////////
  483. // Unit Test code
  484. ////////////////////////////////////////////////////////////
  485. #ifdef __UNIT_TEST__
  486. int SDL::unitTest(int argc, char *argv[])
  487. {
  488. return 0;
  489. }
  490. int main(int argc, char *argv[])
  491. {
  492. SDL test;
  493. printf("[SDL class test]\n");
  494. return test._UnitTest(argc, argv);
  495. }
  496. #endif