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.

OpenRaider.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*!
  2. * \file src/OpenRaider.cpp
  3. * \brief Main Game Object
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <assert.h>
  10. #include "WindowSDL.h"
  11. #include "config.h"
  12. #include "Console.h"
  13. #include "Game.h"
  14. #include "main.h"
  15. #include "math/math.h"
  16. #include "Menu.h"
  17. #include "Sound.h"
  18. #include "TombRaider.h"
  19. #include "utils/strings.h"
  20. #include "utils/time.h"
  21. #include "OpenRaider.h"
  22. OpenRaider::OpenRaider() {
  23. mRunning = false;
  24. mFPS = false;
  25. mBaseDir = NULL;
  26. mPakDir = NULL;
  27. mAudioDir = NULL;
  28. mDataDir = NULL;
  29. for (int i = 0; i < ActionEventCount; i++)
  30. keyBindings[i] = unknownKey;
  31. }
  32. OpenRaider::~OpenRaider() {
  33. if (mBaseDir)
  34. delete mBaseDir;
  35. if (mPakDir)
  36. delete mPakDir;
  37. if (mAudioDir)
  38. delete mAudioDir;
  39. if (mDataDir)
  40. delete mDataDir;
  41. }
  42. int OpenRaider::initialize() {
  43. // Initialize Windowing
  44. int error = getWindow().initialize();
  45. if (error != 0) {
  46. printf("Could not initialize Window (%d)!\n", error);
  47. return -1;
  48. }
  49. // Initialize OpenGL
  50. error = getWindow().initializeGL();
  51. if (error != 0) {
  52. printf("Could not initialize OpenGL (%d)!\n", error);
  53. return -2;
  54. }
  55. error = getWindow().initializeFont();
  56. if (error != 0) {
  57. printf("Could not initialize SDL-TTF (%d)!\n", error);
  58. return -3;
  59. }
  60. error = getSound().initialize();
  61. if (error != 0) {
  62. printf("Could not initialize Sound (%d)!\n", error);
  63. return -4;
  64. }
  65. // Initialize game engine
  66. error = getGame().initialize();
  67. if (error != 0) {
  68. printf("Could not initialize Game Engine (%d)!\n", error);
  69. return -5;
  70. }
  71. #ifdef DEBUG
  72. mFPS = true;
  73. #endif
  74. getMenu().setVisible(true);
  75. systemTimerReset();
  76. return 0;
  77. }
  78. int OpenRaider::loadConfig(const char *config) {
  79. assert(config != NULL);
  80. assert(config[0] != '\0');
  81. char *configFile = fullPath(config, 0);
  82. getConsole().print("Loading config from \"%s\"...", configFile);
  83. FILE *f = fopen(configFile, "r");
  84. if (f == NULL) {
  85. getConsole().print("Could not open file!");
  86. return -1;
  87. }
  88. char buffer[256];
  89. while (fgets(buffer, 256, f) != NULL) {
  90. int error = command(buffer);
  91. if (error != 0) {
  92. getConsole().print("Error Code: %d", error);
  93. }
  94. }
  95. fclose(f);
  96. return 0;
  97. }
  98. int OpenRaider::command(const char *command) {
  99. assert(command != NULL);
  100. assert(command[0] != '\0');
  101. int returnValue = 0;
  102. char *cmd = bufferString("%s", command);
  103. size_t length = strlen(cmd);
  104. // Command ends at '\n' or # when a comment begins
  105. for (size_t i = 0; i < length; i++) {
  106. if (cmd[i] == '\n' || cmd[i] == '#') {
  107. cmd[i] = '\0';
  108. break;
  109. }
  110. }
  111. char *strtokState;
  112. char *token = strtok_r(cmd, " \t", &strtokState);
  113. if (token != NULL) {
  114. // token is the command to execute
  115. // get arguments
  116. std::vector<char *> args;
  117. char *next;
  118. while ((next = strtok_r(NULL, " \t", &strtokState)) != NULL) {
  119. args.push_back(next);
  120. }
  121. // Execute
  122. returnValue = this->command(token, &args);
  123. }
  124. free(cmd);
  125. return returnValue;
  126. }
  127. char *OpenRaider::expandDirectoryNames(const char *s) {
  128. assert(s != NULL);
  129. assert(s[0] != '\0');
  130. if (mBaseDir != NULL) {
  131. const char *base = "$(basedir)";
  132. if (strstr(s, base) != NULL) {
  133. return stringReplace(s, base, mBaseDir);
  134. }
  135. }
  136. if (mPakDir != NULL) {
  137. const char *pak = "$(pakdir)";
  138. if (strstr(s, pak) != NULL) {
  139. return stringReplace(s, pak, mPakDir);
  140. }
  141. }
  142. if (mAudioDir != NULL) {
  143. const char *audio = "$(audiodir)";
  144. if (strstr(s, audio) != NULL) {
  145. return stringReplace(s, audio, mAudioDir);
  146. }
  147. }
  148. if (mDataDir != NULL) {
  149. const char *data = "$(datadir)";
  150. if (strstr(s, data) != NULL) {
  151. return stringReplace(s, data, mDataDir);
  152. }
  153. }
  154. return NULL;
  155. }
  156. #define CHANGE_DIR_WITH_EXPANSION(a) do { \
  157. char *quotes = stringRemoveQuotes(value); \
  158. char *tmp = expandDirectoryNames(quotes); \
  159. if (tmp == NULL) { \
  160. a = fullPath(quotes, 0); \
  161. } else { \
  162. a = fullPath(tmp, 0); \
  163. delete [] tmp; \
  164. } \
  165. delete [] quotes; \
  166. } while(false)
  167. int OpenRaider::set(const char *var, const char *value) {
  168. assert(var != NULL);
  169. assert(var[0] != '\0');
  170. assert(value != NULL);
  171. assert(value[0] != '\0');
  172. if (strcmp(var, "size") == 0) {
  173. // value has format like "\"1024x768\""
  174. unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
  175. if (sscanf(value, "\"%5dx%5d\"", &w, &h) != 2) {
  176. getConsole().print("set-size-Error: Invalid value (%s)", value);
  177. return -2;
  178. }
  179. getWindow().setSize(w, h);
  180. } else if (strcmp(var, "fullscreen") == 0) {
  181. bool fullscreen = false;
  182. if (readBool(value, &fullscreen) != 0) {
  183. getConsole().print("set-fullscreen-Error: Invalid value (%s)", value);
  184. return -3;
  185. }
  186. getWindow().setFullscreen(fullscreen);
  187. } else if (strcmp(var, "gldriver") == 0) {
  188. getWindow().setDriver(value);
  189. } else if (strcmp(var, "audio") == 0) {
  190. bool audio = false;
  191. if (readBool(value, &audio) != 0) {
  192. getConsole().print("set-audio-Error: Invalid value (%s)", value);
  193. return -4;
  194. }
  195. getSound().setEnabled(audio);
  196. } else if (strcmp(var, "volume") == 0) {
  197. float vol = 1.0f;
  198. if (sscanf(value, "%5f", &vol) != 1) {
  199. getConsole().print("set-volume-Error: Invalid value (%s)", value);
  200. return -5;
  201. }
  202. getSound().setVolume(vol);
  203. } else if (strcmp(var, "mouse_x") == 0) {
  204. float sense = 1.0f;
  205. if (sscanf(value, "%5f", &sense) != 1) {
  206. getConsole().print("set-mouse_x-Error: Invalid value (%s)", value);
  207. return -6;
  208. }
  209. getCamera().setSensitivityX(OR_DEG_TO_RAD(sense));
  210. } else if (strcmp(var, "mouse_y") == 0) {
  211. float sense = 1.0f;
  212. if (sscanf(value, "%5f", &sense) != 1) {
  213. getConsole().print("set-mouse_y-Error: Invalid value (%s)", value);
  214. return -7;
  215. }
  216. getCamera().setSensitivityY(OR_DEG_TO_RAD(sense));
  217. } else if (strcmp(var, "fps") == 0) {
  218. bool fps = false;
  219. if (readBool(value, &fps) != 0) {
  220. getConsole().print("set-fps-Error: Invalid value (%s)", value);
  221. return -8;
  222. }
  223. mFPS = fps;
  224. } else if (strcmp(var, "basedir") == 0) {
  225. CHANGE_DIR_WITH_EXPANSION(mBaseDir);
  226. } else if (strcmp(var, "pakdir") == 0) {
  227. CHANGE_DIR_WITH_EXPANSION(mPakDir);
  228. } else if (strcmp(var, "audiodir") == 0) {
  229. CHANGE_DIR_WITH_EXPANSION(mAudioDir);
  230. } else if (strcmp(var, "datadir") == 0) {
  231. CHANGE_DIR_WITH_EXPANSION(mDataDir);
  232. } else if (strcmp(var, "font") == 0) {
  233. char *quotes = stringReplace(value, "\"", "");
  234. char *tmp = expandDirectoryNames(quotes);
  235. if (tmp == NULL) {
  236. getWindow().setFont(quotes);
  237. } else {
  238. getWindow().setFont(tmp);
  239. delete [] tmp;
  240. }
  241. delete [] quotes;
  242. } else {
  243. getConsole().print("set-Error: Unknown variable (%s = %s)", var, value);
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. int OpenRaider::bind(const char *action, const char *key) {
  249. assert(action != NULL);
  250. assert(action[0] != '\0');
  251. assert(key != NULL);
  252. assert(key[0] != '\0');
  253. const char *tmp = action;
  254. if (action[0] == '+')
  255. tmp++;
  256. if (strcmp(tmp, "menu") == 0) {
  257. return bind(menuAction, key);
  258. } else if (strcmp(tmp, "console") == 0) {
  259. return bind(consoleAction, key);
  260. } else if (strcmp(tmp, "forward") == 0) {
  261. return bind(forwardAction, key);
  262. } else if (strcmp(tmp, "backward") == 0) {
  263. return bind(backwardAction, key);
  264. } else if (strcmp(tmp, "left") == 0) {
  265. return bind(leftAction, key);
  266. } else if (strcmp(tmp, "right") == 0) {
  267. return bind(rightAction, key);
  268. } else if (strcmp(tmp, "jump") == 0) {
  269. return bind(jumpAction, key);
  270. } else if (strcmp(tmp, "crouch") == 0) {
  271. return bind(crouchAction, key);
  272. } else if (strcmp(tmp, "use") == 0) {
  273. return bind(useAction, key);
  274. } else if (strcmp(tmp, "holster") == 0) {
  275. return bind(holsterAction, key);
  276. } else {
  277. getConsole().print("bind-Error: Unknown action (%s --> %s)", key, action);
  278. return -1;
  279. }
  280. }
  281. int OpenRaider::bind(ActionEvents action, const char *key) {
  282. assert(action < ActionEventCount);
  283. assert(key != NULL);
  284. assert(key[0] != '\0');
  285. size_t length = strlen(key);
  286. if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
  287. // Literal character like w, a, s, d, 0, 1...
  288. char c = key[1];
  289. if (((c >= '0') && (c <= '9'))
  290. || ((c >= 'a') && (c <= 'z'))) {
  291. keyBindings[action] = (KeyboardButton)c;
  292. } else {
  293. getConsole().print("bind-\'\'-Error: Unknown key (%s)", key);
  294. return -1;
  295. }
  296. } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
  297. // Special characters like tilde, esc, quote...
  298. char *tmp = stringRemoveQuotes(key);
  299. if (strcmp(tmp, "quote") == 0) {
  300. keyBindings[action] = quoteKey;
  301. } else if (strcmp(tmp, "backslash") == 0) {
  302. keyBindings[action] = backslashKey;
  303. } else if (strcmp(tmp, "backspace") == 0) {
  304. keyBindings[action] = backspaceKey;
  305. } else if (strcmp(tmp, "capslock") == 0) {
  306. keyBindings[action] = capslockKey;
  307. } else if (strcmp(tmp, "comma") == 0) {
  308. keyBindings[action] = commaKey;
  309. } else if (strcmp(tmp, "del") == 0) {
  310. keyBindings[action] = delKey;
  311. } else if (strcmp(tmp, "up") == 0) {
  312. keyBindings[action] = upKey;
  313. } else if (strcmp(tmp, "down") == 0) {
  314. keyBindings[action] = downKey;
  315. } else if (strcmp(tmp, "left") == 0) {
  316. keyBindings[action] = leftKey;
  317. } else if (strcmp(tmp, "right") == 0) {
  318. keyBindings[action] = rightKey;
  319. } else if (strcmp(tmp, "end") == 0) {
  320. keyBindings[action] = endKey;
  321. } else if (strcmp(tmp, "equals") == 0) {
  322. keyBindings[action] = equalsKey;
  323. } else if (strcmp(tmp, "escape") == 0) {
  324. keyBindings[action] = escapeKey;
  325. } else if (strcmp(tmp, "f1") == 0) {
  326. keyBindings[action] = f1Key;
  327. } else if (strcmp(tmp, "f2") == 0) {
  328. keyBindings[action] = f2Key;
  329. } else if (strcmp(tmp, "f3") == 0) {
  330. keyBindings[action] = f3Key;
  331. } else if (strcmp(tmp, "f4") == 0) {
  332. keyBindings[action] = f4Key;
  333. } else if (strcmp(tmp, "f5") == 0) {
  334. keyBindings[action] = f5Key;
  335. } else if (strcmp(tmp, "f6") == 0) {
  336. keyBindings[action] = f6Key;
  337. } else if (strcmp(tmp, "f7") == 0) {
  338. keyBindings[action] = f7Key;
  339. } else if (strcmp(tmp, "f8") == 0) {
  340. keyBindings[action] = f8Key;
  341. } else if (strcmp(tmp, "f9") == 0) {
  342. keyBindings[action] = f9Key;
  343. } else if (strcmp(tmp, "f10") == 0) {
  344. keyBindings[action] = f10Key;
  345. } else if (strcmp(tmp, "f11") == 0) {
  346. keyBindings[action] = f11Key;
  347. } else if (strcmp(tmp, "f12") == 0) {
  348. keyBindings[action] = f12Key;
  349. } else if (strcmp(tmp, "backquote") == 0) {
  350. keyBindings[action] = backquoteKey;
  351. } else if (strcmp(tmp, "home") == 0) {
  352. keyBindings[action] = homeKey;
  353. } else if (strcmp(tmp, "insert") == 0) {
  354. keyBindings[action] = insertKey;
  355. } else if (strcmp(tmp, "leftalt") == 0) {
  356. keyBindings[action] = leftaltKey;
  357. } else if (strcmp(tmp, "leftctrl") == 0) {
  358. keyBindings[action] = leftctrlKey;
  359. } else if (strcmp(tmp, "leftbracket") == 0) {
  360. keyBindings[action] = leftbracketKey;
  361. } else if (strcmp(tmp, "leftgui") == 0) {
  362. keyBindings[action] = leftguiKey;
  363. } else if (strcmp(tmp, "leftshift") == 0) {
  364. keyBindings[action] = leftshiftKey;
  365. } else if (strcmp(tmp, "minus") == 0) {
  366. keyBindings[action] = minusKey;
  367. } else if (strcmp(tmp, "numlock") == 0) {
  368. keyBindings[action] = numlockKey;
  369. } else if (strcmp(tmp, "pagedown") == 0) {
  370. keyBindings[action] = pagedownKey;
  371. } else if (strcmp(tmp, "pageup") == 0) {
  372. keyBindings[action] = pageupKey;
  373. } else if (strcmp(tmp, "pause") == 0) {
  374. keyBindings[action] = pauseKey;
  375. } else if (strcmp(tmp, "dot") == 0) {
  376. keyBindings[action] = dotKey;
  377. } else if (strcmp(tmp, "rightalt") == 0) {
  378. keyBindings[action] = rightaltKey;
  379. } else if (strcmp(tmp, "rightctrl") == 0) {
  380. keyBindings[action] = rightctrlKey;
  381. } else if (strcmp(tmp, "enter") == 0) {
  382. keyBindings[action] = enterKey;
  383. } else if (strcmp(tmp, "rightgui") == 0) {
  384. keyBindings[action] = rightguiKey;
  385. } else if (strcmp(tmp, "rightbracket") == 0) {
  386. keyBindings[action] = rightbracketKey;
  387. } else if (strcmp(tmp, "rightshift") == 0) {
  388. keyBindings[action] = rightshiftKey;
  389. } else if (strcmp(tmp, "scrolllock") == 0) {
  390. keyBindings[action] = scrolllockKey;
  391. } else if (strcmp(tmp, "semicolon") == 0) {
  392. keyBindings[action] = semicolonKey;
  393. } else if (strcmp(tmp, "slash") == 0) {
  394. keyBindings[action] = slashKey;
  395. } else if (strcmp(tmp, "space") == 0) {
  396. keyBindings[action] = spaceKey;
  397. } else if (strcmp(tmp, "tab") == 0) {
  398. keyBindings[action] = tabKey;
  399. } else if (strcmp(tmp, "leftmouse") == 0) {
  400. keyBindings[action] = leftmouseKey;
  401. } else if (strcmp(tmp, "middlemouse") == 0) {
  402. keyBindings[action] = middlemouseKey;
  403. } else if (strcmp(tmp, "rightmouse") == 0) {
  404. keyBindings[action] = rightmouseKey;
  405. } else {
  406. getConsole().print("bind-\"\"-Error: Unknown key (%s)", key);
  407. delete [] tmp;
  408. return -2;
  409. }
  410. delete [] tmp;
  411. } else {
  412. getConsole().print("bind-Error: Unknown key (%s)", key);
  413. return -3;
  414. }
  415. return 0;
  416. }
  417. void OpenRaider::run() {
  418. assert(mRunning == false);
  419. mRunning = true;
  420. while (mRunning) {
  421. frame();
  422. }
  423. }
  424. void OpenRaider::frame() {
  425. assert(mRunning == true);
  426. static clock_t fpsSum = 0, fpsCount = 0;
  427. static int fps = 0;
  428. clock_t startTime = systemTimerGet();
  429. // Get keyboard and mouse input
  430. getWindow().eventHandling();
  431. // Clear screen
  432. glClearColor(0.00f, 0.00f, 0.00f, 1.0f);
  433. glClear(GL_COLOR_BUFFER_BIT);
  434. // Draw game scene
  435. getRender().display();
  436. // Draw 2D overlays (console and menu)
  437. getWindow().glEnter2D();
  438. getConsole().display();
  439. getMenu().display();
  440. // Draw FPS counter
  441. if (mFPS)
  442. getWindow().drawText(10, getWindow().mHeight - 20, 0.5f, OR_BLUE, "%dFPS", fps);
  443. #ifdef DEBUG
  444. // Draw debug infos
  445. if (getGame().isLoaded()) {
  446. for (int i = 0; i < 3; i++) {
  447. getWindow().drawText(10, getWindow().mHeight - ((4 - i) * 20), 0.5f, OR_BLUE, "%.2f (%.2f)",
  448. getGame().mLara->pos[i] / 256.0f, getGame().mLara->angles[i]);
  449. }
  450. }
  451. #endif
  452. getWindow().glExit2D();
  453. // Put new frame on screen
  454. getWindow().swapBuffersGL();
  455. // Calculate FPS display value
  456. fpsCount++;
  457. fpsSum += (systemTimerGet() - startTime);
  458. if (fpsSum >= 500) {
  459. // Update every 500ms
  460. fps = (int)((float)fpsCount * (1000.0f / (float)fpsSum));
  461. fpsCount = fpsSum = 0;
  462. }
  463. }
  464. void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
  465. assert(key < unknownKey);
  466. assert(mRunning == true);
  467. if ((keyBindings[menuAction] == key) && pressed) {
  468. getMenu().setVisible(!getMenu().isVisible());
  469. } else if (!getMenu().isVisible()) {
  470. if ((keyBindings[consoleAction] == key) && pressed) {
  471. getConsole().setVisible(!getConsole().isVisible());
  472. } else if (!getConsole().isVisible()) {
  473. for (int i = forwardAction; i < ActionEventCount; i++) {
  474. if (keyBindings[i] == key) {
  475. getGame().handleAction((ActionEvents)i, pressed);
  476. }
  477. }
  478. } else {
  479. getConsole().handleKeyboard(key, pressed);
  480. }
  481. } else {
  482. getMenu().handleKeyboard(key, pressed);
  483. }
  484. getWindow().setMousegrab(!(getMenu().isVisible() || getConsole().isVisible()));
  485. }
  486. void OpenRaider::handleText(char *text, bool notFinished) {
  487. assert(text != NULL);
  488. assert(text[0] != '\0');
  489. assert(mRunning == true);
  490. if ((getConsole().isVisible()) && (!getMenu().isVisible())) {
  491. getConsole().handleText(text, notFinished);
  492. }
  493. }
  494. void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  495. assert(button < unknownKey);
  496. assert(mRunning == true);
  497. if (getMenu().isVisible()) {
  498. getMenu().handleMouseClick(x, y, button, released);
  499. } else if (!getConsole().isVisible()) {
  500. for (int i = forwardAction; i < ActionEventCount; i++) {
  501. if (keyBindings[i] == button) {
  502. getGame().handleAction((ActionEvents)i, !released);
  503. }
  504. }
  505. }
  506. }
  507. void OpenRaider::handleMouseMotion(int xrel, int yrel) {
  508. assert((xrel != 0) || (yrel != 0));
  509. assert(mRunning == true);
  510. if ((!getConsole().isVisible()) && (!getMenu().isVisible())) {
  511. getGame().handleMouseMotion(xrel, yrel);
  512. }
  513. }
  514. void OpenRaider::handleMouseScroll(int xrel, int yrel) {
  515. assert((xrel != 0) || (yrel != 0));
  516. assert(mRunning == true);
  517. if ((getConsole().isVisible()) && (!getMenu().isVisible())) {
  518. getConsole().handleMouseScroll(xrel, yrel);
  519. }
  520. }