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

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