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

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