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

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