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.

System.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*!
  2. * \file System.cpp
  3. * \brief Mostly defines the interface of System implementations.
  4. *
  5. * Currently only SDL is used, but there was a GLUT implementation.
  6. *
  7. * \author Mongoose
  8. */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <string.h>
  14. #include <stdarg.h>
  15. #include <cmath>
  16. #ifdef USING_OPENGL
  17. #ifdef __APPLE__
  18. #include <OpenGL/gl.h>
  19. #include <OpenGL/glu.h>
  20. #else
  21. #include <GL/gl.h>
  22. #include <GL/glu.h>
  23. #endif
  24. #endif
  25. #ifdef HAVE_LIBFERIT
  26. #include <ferit/Url.h>
  27. #include <ferit/TcpProtocol.h>
  28. #include <ferit/Http.h>
  29. #include <ferit/Ftp.h>
  30. #endif
  31. #if defined(linux) || defined(__APPLE__)
  32. #include <time.h>
  33. #include <sys/time.h>
  34. #endif
  35. #ifdef MEMEORY_TEST
  36. #include "memeory_test.h"
  37. #endif
  38. #ifdef PS2_LINUX
  39. #include "ps2linux.h"
  40. #endif
  41. #include "System.h"
  42. ////////////////////////////////////////////////////////////
  43. // Constructors
  44. ////////////////////////////////////////////////////////////
  45. System::System()
  46. {
  47. m_width = 800;
  48. m_height = 600;
  49. m_fastCard = true;
  50. m_driver = 0x0;
  51. m_clipFar = 4000.0f;
  52. m_clipNear = 4.0f;
  53. m_fovY = 45.0f;
  54. mConsoleMode = false;
  55. printf("[System.Core]\n");
  56. // Hack for bad Map class, as well as reserved commands
  57. addCommandMode("[System.Console]");
  58. mConsoleKey = '`';
  59. bindKeyCommand("+console", mConsoleKey, 0);
  60. #ifdef WIN32
  61. setDriverGL("libGL32.dll");
  62. #else
  63. setDriverGL("/usr/lib/libGL.so.1");
  64. #endif
  65. }
  66. System::~System()
  67. {
  68. }
  69. ////////////////////////////////////////////////////////////
  70. // Public Accessors
  71. ////////////////////////////////////////////////////////////
  72. char *System::bufferString(const char *string, ...)
  73. {
  74. int sz = 60;
  75. int n;
  76. char *text;
  77. va_list args;
  78. // Mongoose 2002.01.01, Only allow valid strings
  79. // we must assume it's NULL terminated also if it passes...
  80. if (!string || !string[0])
  81. {
  82. return NULL;
  83. }
  84. text = new char[sz];
  85. va_start(args, string);
  86. // Mongoose 2002.01.01, Get exact size needed if the first try fails
  87. n = vsnprintf(text, sz, string, args);
  88. // Mongoose 2002.01.01, Realloc correct amount if truncated
  89. while (1)
  90. {
  91. if (n > -1 && n < sz)
  92. {
  93. break;
  94. }
  95. // Mongoose 2002.01.01, For glibc 2.1
  96. if (n > -1)
  97. {
  98. sz = n + 1;
  99. delete [] text;
  100. text = new char[sz];
  101. n = vsnprintf(text, sz, string, args);
  102. break;
  103. }
  104. else // glibc 2.0
  105. {
  106. sz *= 2;
  107. delete [] text;
  108. text = new char[sz];
  109. n = vsnprintf(text, sz, string, args);
  110. }
  111. }
  112. va_end(args);
  113. return text;
  114. }
  115. char *System::fullPath(const char *path, char end)
  116. {
  117. unsigned int i, lenPath, lenEnv, len;
  118. char *env, *dir;
  119. if (!path || !path[0])
  120. return 0;
  121. if (path[0] == '~')
  122. {
  123. #if defined(unix) || defined(__APPLE__)
  124. env = getenv("HOME");
  125. if (!env || !env[0])
  126. {
  127. return 0;
  128. }
  129. lenEnv = strlen(env);
  130. lenPath = strlen(path);
  131. len = lenEnv + lenPath;
  132. dir = new char[len+1];
  133. // Mongoose 2002.08.17, Copy ENV, strip '~', append rest of path
  134. for (i = 0; i < len; ++i)
  135. {
  136. if (i < lenEnv)
  137. {
  138. dir[i] = env[i];
  139. }
  140. else
  141. {
  142. dir[i] = path[1+(i-lenEnv)];
  143. }
  144. }
  145. #else
  146. #error Platform not supported!
  147. #endif
  148. }
  149. else
  150. {
  151. lenPath = strlen(path);
  152. dir = new char[lenPath+1];
  153. strncpy(dir, path, lenPath);
  154. i = lenPath;
  155. }
  156. // Make sure ends in "end" char
  157. if (end && dir[i-1] != end)
  158. {
  159. dir[i++] = end;
  160. }
  161. dir[i] = 0;
  162. return dir;
  163. }
  164. char *System::getFileFromFullPath(char *filename)
  165. {
  166. int i, j, len;
  167. char *str;
  168. len = strlen(filename);
  169. for (i = len, j = 0; i > 0; --i, ++j)
  170. {
  171. if (filename[i] == '/' || filename[i] == '\\')
  172. break;
  173. }
  174. j--;
  175. str = new char[len - j + 1];
  176. for (i = 0; i < len - j; ++i)
  177. {
  178. str[i] = filename[i + len - j];
  179. }
  180. str[i] = 0;
  181. return str;
  182. }
  183. unsigned int System::getTicks()
  184. {
  185. return system_timer(1);
  186. }
  187. int System::createDir(char *path)
  188. {
  189. #ifdef WIN32
  190. return _mkdir(path);
  191. #else
  192. return mkdir(path, S_IRWXU | S_IRWXG);
  193. #endif
  194. }
  195. int System::downloadToBuffer(char *urlString,
  196. unsigned char **buffer, unsigned int *size)
  197. {
  198. #ifdef HAVE_LIBFERIT
  199. printf("ERROR: This build is libferit enabled, but func not implemented\n");
  200. return -1;
  201. #else
  202. printf("ERROR: This build not libferit enabled, unable to download\n");
  203. return -1000;
  204. #endif
  205. }
  206. int System::downloadToFile(char *urlString, char *filename)
  207. {
  208. #ifdef HAVE_LIBFERIT
  209. int err = 0;
  210. unsigned int timeout = 10;
  211. Url *url = 0x0;
  212. TcpProtocol *client = 0x0;
  213. if (!urlString || !urlString[0])
  214. return -1;
  215. url = new Url("");
  216. url->UrlToSession(urlString);
  217. url->Options(OPT_RESUME | OPT_QUIET);
  218. url->Outfile(url->Filename());
  219. switch (url->Protocol())
  220. {
  221. case FTP_SESSION:
  222. client = new Ftp(timeout);
  223. break;
  224. case HTTP_SESSION:
  225. client = new Http(timeout);
  226. break;
  227. default:
  228. printf("Sorry the protocol used in the URL isn't unsupported.\n");
  229. if (client)
  230. {
  231. delete client;
  232. }
  233. return -2;
  234. }
  235. if (client)
  236. {
  237. err = client->Download(url, NULL);
  238. delete client;
  239. }
  240. if (url)
  241. delete url;
  242. return err;
  243. #else
  244. printf("ERROR: This build not libferit enabled, unable to download\n");
  245. return -1000;
  246. #endif
  247. }
  248. ////////////////////////////////////////////////////////////
  249. // Public Mutators
  250. ////////////////////////////////////////////////////////////
  251. unsigned int System::addCommandMode(const char *command)
  252. {
  253. if (command && command[0] == '[')
  254. {
  255. mCmdModes.pushBack(command);
  256. return (mCmdModes.size() - 1);
  257. }
  258. else
  259. {
  260. return 0;
  261. }
  262. }
  263. //! \fixme Modifer support later
  264. void System::bindKeyCommand(const char *cmd, unsigned int key, int event)
  265. {
  266. printf("Bound command '%s' -> event %i (0x%x key)\n", cmd, event, key);
  267. mKeyEvents.Add(key, event);
  268. }
  269. void System::command(const char *cmd)
  270. {
  271. bool modeFound = false;
  272. char *cmdbuf;
  273. if (!cmd || !cmd[0]) // Null command string
  274. return;
  275. if (cmd[0] == '[') // Set a mode, eg "[Engine.OpenGL.Driver]"
  276. {
  277. for (mCmdModes.start(); mCmdModes.forward(); mCmdModes.next())
  278. {
  279. if (strcmp(cmd, mCmdModes.current()) == 0)
  280. {
  281. mCommandMode = mCmdModes.getCurrentIndex();
  282. modeFound = true;
  283. }
  284. }
  285. if (!modeFound)
  286. {
  287. // mCommandMode = 0;
  288. printf("Command> Unknown mode '%s'\n", cmd);
  289. }
  290. }
  291. else // Execute a command in current mode, eg "stat fps"
  292. {
  293. cmdbuf = new char[strlen(cmd) + 1];
  294. strncpy(cmdbuf, cmd, strlen(cmd) + 1);
  295. handleCommand(cmdbuf, mCommandMode);
  296. }
  297. }
  298. int System::loadResourceFile(const char *filename)
  299. {
  300. char buffer[256];
  301. bool line_comment = false;
  302. FILE *f;
  303. char c;
  304. int i, j;
  305. f = fopen(filename, "r");
  306. if (!f)
  307. {
  308. perror(filename);
  309. return -1;
  310. }
  311. printf("Loading %s...\n", filename);
  312. i = 0;
  313. buffer[0] = 0;
  314. // Strip out whitespace and comments
  315. while (fscanf(f, "%c", &c) != EOF)
  316. {
  317. if (line_comment && c != '\n')
  318. continue;
  319. if (i > 254)
  320. {
  321. printf("loadResourceFile> Overflow handled\n");
  322. i = 254;
  323. }
  324. switch (c)
  325. {
  326. case '\v':
  327. case '\t':
  328. break;
  329. case '#':
  330. buffer[i++] = 0;
  331. line_comment = true;
  332. break;
  333. case '\n':
  334. if (line_comment)
  335. {
  336. line_comment = false;
  337. }
  338. if (buffer[0] == 0)
  339. {
  340. i = 0;
  341. continue;
  342. }
  343. buffer[i] = 0;
  344. //printf("'%s'\n", buffer);
  345. // 'Preprocessor' commands
  346. if (buffer[0] == '@')
  347. {
  348. if (strncmp(buffer, "@include ", 9) == 0)
  349. {
  350. for (j = 9; j < i; ++j)
  351. {
  352. buffer[j-9] = buffer[j];
  353. buffer[j-8] = 0;
  354. }
  355. printf("Importing '%s'\n", buffer);
  356. loadResourceFile(fullPath(buffer, '/'));
  357. }
  358. }
  359. else
  360. {
  361. command(buffer);
  362. }
  363. i = 0;
  364. buffer[0] = 0;
  365. break;
  366. default:
  367. buffer[i++] = c;
  368. }
  369. }
  370. fclose(f);
  371. return 0;
  372. }
  373. void System::setDriverGL(const char *driver)
  374. {
  375. unsigned int len;
  376. if (m_driver)
  377. {
  378. delete [] m_driver;
  379. }
  380. if (driver && driver[0])
  381. {
  382. len = strlen(driver);
  383. m_driver = new char[len+1];
  384. strncpy(m_driver, driver, len);
  385. m_driver[len] = 0;
  386. }
  387. }
  388. void System::setFastCardPerformance(bool is_fast)
  389. {
  390. m_fastCard = is_fast;
  391. }
  392. void System::resetTicks()
  393. {
  394. system_timer(0);
  395. }
  396. void System::initGL()
  397. {
  398. char *s;
  399. // Print driver support information
  400. printf("\n\n\t## GL Driver Info 1 ##\n");
  401. printf("\tVendor : %s\n", glGetString(GL_VENDOR));
  402. printf("\tRenderer : %s\n", glGetString(GL_RENDERER));
  403. printf("\tVersion : %s\n", glGetString(GL_VERSION));
  404. printf("\tExtensions : %s\n\n\n", (char*)glGetString(GL_EXTENSIONS));
  405. // Testing for goodies
  406. // Mongoose 2001.12.31, Fixed string use to check for bad strings
  407. s = (char*)glGetString(GL_EXTENSIONS);
  408. if (s && s[0])
  409. {
  410. printf("\tGL_ARB_multitexture \t\t");
  411. if (strstr(s, "GL_ARB_multitexture"))
  412. {
  413. printf("YES\n");
  414. }
  415. else
  416. {
  417. printf("NO\n");
  418. }
  419. //glActiveTextureARB
  420. //glMultiTexCoord2fARB
  421. //glFogCoordfEXT
  422. printf("\tGL_EXT_texture_env_combine\t\t");
  423. if (strstr(s, "GL_EXT_texture_env_combine"))
  424. {
  425. printf("YES\n");
  426. }
  427. else
  428. {
  429. printf("NO\n");
  430. }
  431. }
  432. // Set up Z buffer
  433. glEnable(GL_DEPTH_TEST);
  434. glDepthFunc(GL_LESS);
  435. // Set up culling
  436. glEnable(GL_CULL_FACE);
  437. //glFrontFace(GL_CW);
  438. glFrontFace(GL_CCW);
  439. //glCullFace(GL_FRONT);
  440. // Set background to black
  441. glClearColor(0.0, 0.0, 0.0, 1.0);
  442. // Disable lighting
  443. glDisable(GL_LIGHTING);
  444. // Set up alpha blending
  445. if (m_fastCard)
  446. {
  447. glEnable(GL_BLEND);
  448. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  449. //glEnable(GL_ALPHA_TEST); // Disable per pixel alpha blending
  450. glAlphaFunc(GL_GREATER, 0);
  451. }
  452. else
  453. {
  454. glDisable(GL_BLEND);
  455. glDisable(GL_ALPHA_TEST);
  456. }
  457. glPointSize(5.0);
  458. // Setup shading
  459. glShadeModel(GL_SMOOTH);
  460. if (m_fastCard)
  461. {
  462. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  463. glHint(GL_FOG_HINT, GL_NICEST);
  464. glDisable(GL_COLOR_MATERIAL);
  465. glEnable(GL_DITHER);
  466. // AA polygon edges
  467. //glEnable(GL_POLYGON_SMOOTH);
  468. //glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
  469. glEnable(GL_POINT_SMOOTH);
  470. }
  471. else
  472. {
  473. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  474. glHint(GL_FOG_HINT, GL_FASTEST);
  475. glDisable(GL_COLOR_MATERIAL);
  476. glDisable(GL_DITHER);
  477. glDisable(GL_POLYGON_SMOOTH);
  478. glDisable(GL_POINT_SMOOTH);
  479. glDisable(GL_FOG);
  480. }
  481. glDisable(GL_LINE_SMOOTH);
  482. glDisable(GL_AUTO_NORMAL);
  483. glDisable(GL_LOGIC_OP);
  484. glDisable(GL_TEXTURE_1D);
  485. glDisable(GL_STENCIL_TEST);
  486. glDisable(GL_NORMALIZE);
  487. glEnableClientState(GL_VERTEX_ARRAY);
  488. glDisableClientState(GL_EDGE_FLAG_ARRAY);
  489. glDisableClientState(GL_COLOR_ARRAY);
  490. glDisableClientState(GL_NORMAL_ARRAY);
  491. glPolygonMode(GL_FRONT, GL_FILL);
  492. glMatrixMode(GL_MODELVIEW);
  493. }
  494. void System::resizeGL(unsigned int w, unsigned int h)
  495. {
  496. if (!w || !h)
  497. {
  498. printf("resizeGL> ERROR assertions 'w > 0', 'h > 0' failed\n");
  499. return;
  500. }
  501. glViewport(0, 0, w, h);
  502. glMatrixMode(GL_PROJECTION);
  503. glLoadIdentity();
  504. // Adjust clipping
  505. // gluPerspective is deprecated!
  506. // gluPerspective(m_fovY, ((GLdouble)w)/((GLdouble)h), m_clipNear, m_clipFar);
  507. // Fix: http://stackoverflow.com/a/2417756
  508. GLfloat fH = tan(float(m_fovY / 360.0f * 3.14159f)) * m_clipNear;
  509. GLfloat fW = fH * ((GLdouble)w)/((GLdouble)h);
  510. glFrustum(-fW, fW, -fH, fH, m_clipNear, m_clipFar);
  511. glMatrixMode(GL_MODELVIEW);
  512. }
  513. ////////////////////////////////////////////////////////////
  514. // Private Accessors
  515. ////////////////////////////////////////////////////////////
  516. ////////////////////////////////////////////////////////////
  517. // Private Mutators
  518. ////////////////////////////////////////////////////////////
  519. ////////////////////////////////////////////////////////////
  520. // Gobal helper functions
  521. ////////////////////////////////////////////////////////////
  522. // Mongoose 2002.03.23, Checks command to see if it's same
  523. // as symbol, then returns the arg list in command buffer
  524. bool rc_command(const char *symbol, char *command)
  525. {
  526. int i, j, lens, lenc;
  527. if (!symbol || !symbol[0] || !command || !command[0])
  528. {
  529. return false;
  530. }
  531. lens = strlen(symbol);
  532. if (strncmp(command, symbol, lens) == 0)
  533. {
  534. lenc = strlen(command);
  535. // lens+1 skips '=' or ' '
  536. for (i = 0, j = lens+1; j < lenc; ++i, ++j)
  537. {
  538. command[i] = command[j];
  539. command[i+1] = 0;
  540. }
  541. return true;
  542. }
  543. return false;
  544. }
  545. int rc_get_bool(char *buffer, bool *val)
  546. {
  547. if (!buffer || !buffer[0])
  548. {
  549. return -1;
  550. }
  551. if (strncmp(buffer, "true", 4) == 0)
  552. *val = true;
  553. else if (strncmp(buffer, "false", 5) == 0)
  554. *val = false;
  555. else
  556. return -2;
  557. return 0;
  558. }
  559. unsigned int system_timer(int state)
  560. {
  561. static struct timeval start;
  562. static struct timeval stop;
  563. static struct timeval total;
  564. static struct timezone tz;
  565. switch (state)
  566. {
  567. case 0:
  568. gettimeofday(&start, &tz);
  569. total.tv_sec = 0;
  570. total.tv_usec = 0;
  571. break;
  572. case 1:
  573. gettimeofday(&stop, &tz);
  574. if (start.tv_usec > stop.tv_usec)
  575. {
  576. #ifdef OBSOLETE
  577. stop.tv_usec = (1000000 + stop.tv_usec);
  578. #else
  579. stop.tv_usec = (1000 + stop.tv_usec);
  580. #endif
  581. stop.tv_sec--;
  582. }
  583. stop.tv_usec -= start.tv_usec;
  584. stop.tv_sec -= start.tv_sec;
  585. #ifdef OBSOLETE
  586. total.tv_sec += stop.tv_sec;
  587. total.tv_usec += stop.tv_usec;
  588. while (total.tv_usec > 1000000)
  589. {
  590. total.tv_usec -= 1000000;
  591. total.tv_sec++;
  592. }
  593. return total.tv_sec * 1000000 + total.tv_usec;
  594. #else
  595. return (stop.tv_sec-start.tv_sec)*1000+(stop.tv_usec-start.tv_usec)/1000;
  596. #endif
  597. break;
  598. }
  599. return 0;
  600. }