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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 <dirent.h>
  11. #include "WindowSDL.h"
  12. #include "config.h"
  13. #include "utils/strings.h"
  14. #include "utils/time.h"
  15. #include "OpenRaider.h"
  16. #define MAX_MS_PER_FRAME (1000 / MAXIMUM_FPS)
  17. OpenRaider::OpenRaider() {
  18. mInit = false;
  19. mRunning = false;
  20. mBaseDir = NULL;
  21. mPakDir = NULL;
  22. mAudioDir = NULL;
  23. mDataDir = NULL;
  24. mMapListFilled = false;
  25. mMenu = new Menu();
  26. mConsole = new Console();
  27. mSound = new Sound();
  28. mWindow = new WindowSDL();
  29. for (int i = 0; i < ActionEventCount; i++)
  30. keyBindings[i] = unknown;
  31. }
  32. OpenRaider::~OpenRaider() {
  33. if (mMenu)
  34. delete mMenu;
  35. if (mConsole)
  36. delete mConsole;
  37. if (mSound)
  38. delete mSound;
  39. if (mWindow)
  40. delete mWindow;
  41. if (mBaseDir)
  42. delete mBaseDir;
  43. if (mPakDir)
  44. delete mPakDir;
  45. if (mAudioDir)
  46. delete mAudioDir;
  47. if (mDataDir)
  48. delete mDataDir;
  49. while (mMapList.size() > 0) {
  50. delete [] mMapList.back();
  51. mMapList.pop_back();
  52. }
  53. }
  54. int OpenRaider::loadConfig(const char *config) {
  55. assert(config != NULL);
  56. assert(config[0] != '\0');
  57. char *configFile = fullPath(config, 0);
  58. printf("Loading config from \"%s\"...\n", configFile);
  59. FILE *f = fopen(configFile, "r");
  60. if (f == NULL) {
  61. printf("Could not open file!\n");
  62. return -1;
  63. }
  64. char buffer[256];
  65. while (fgets(buffer, 256, f) != NULL) {
  66. command(buffer);
  67. }
  68. fclose(f);
  69. return 0;
  70. }
  71. int OpenRaider::command(const char *command) {
  72. assert(command != NULL);
  73. assert(command[0] != '\0');
  74. int returnValue = 0;
  75. char *cmd = bufferString("%s", command);
  76. size_t length = strlen(cmd);
  77. // Command ends at '\n' or # when a comment begins
  78. for (size_t i = 0; i < length; i++) {
  79. if (cmd[i] == '\n' || cmd[i] == '#') {
  80. cmd[i] = '\0';
  81. break;
  82. }
  83. }
  84. char *token = strtok(cmd, " \t");
  85. if (token != NULL) {
  86. // token is the command to execute
  87. // get arguments
  88. std::vector<char *> args;
  89. char *next;
  90. while ((next = strtok(NULL, " \t")) != NULL) {
  91. args.push_back(next);
  92. }
  93. // Execute
  94. returnValue = this->command(token, &args);
  95. }
  96. free(cmd);
  97. return returnValue;
  98. }
  99. int OpenRaider::command(const char *command, std::vector<char *> *args) {
  100. assert(command != NULL);
  101. assert(command[0] != '\0');
  102. assert(args != NULL);
  103. if (strcmp(command, "set") == 0) {
  104. if (args->size() != 2) {
  105. printf("Invalid use of set-command ");
  106. printStringVector(args);
  107. printf("\n");
  108. return -2;
  109. } else {
  110. return set(args->at(0), args->at(1));
  111. }
  112. } else if (strcmp(command, "bind") == 0) {
  113. if (args->size() != 2) {
  114. printf("Invalid use of bind-command ");
  115. printStringVector(args);
  116. printf("\n");
  117. return -3;
  118. } else {
  119. return bind(args->at(0), args->at(1));
  120. }
  121. } else {
  122. printf("Unknown command: %s ", command);
  123. printStringVector(args);
  124. printf("\n");
  125. return -1;
  126. }
  127. }
  128. char *OpenRaider::expandDirectoryNames(const char *s) {
  129. const char *base = "$(basedir)";
  130. const char *pak = "$(pakdir)";
  131. const char *audio = "$(audiodir)";
  132. const char *data = "$(datadir)";
  133. if (mBaseDir != NULL) {
  134. if (strstr(s, base) != NULL) {
  135. return stringReplace(s, base, mBaseDir);
  136. }
  137. }
  138. if (mPakDir != NULL) {
  139. if (strstr(s, pak) != NULL) {
  140. return stringReplace(s, pak, mPakDir);
  141. }
  142. }
  143. if (mAudioDir != NULL) {
  144. if (strstr(s, audio) != NULL) {
  145. return stringReplace(s, audio, mAudioDir);
  146. }
  147. }
  148. if (mDataDir != NULL) {
  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. if (strcmp(var, "size") == 0) {
  168. // value has format like "\"1024x768\""
  169. unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
  170. if (sscanf(value, "\"%dx%d\"", &w, &h) != 2) {
  171. printf("set-size-Error: Invalid value (%s)\n", value);
  172. return -2;
  173. }
  174. mWindow->setSize(w, h);
  175. } else if (strcmp(var, "fullscreen") == 0) {
  176. bool fullscreen = false;
  177. if (readBool(value, &fullscreen) != 0) {
  178. printf("set-fullscreen-Error: Invalid value (%s)\n", value);
  179. return -3;
  180. }
  181. mWindow->setFullscreen(fullscreen);
  182. } else if (strcmp(var, "gldriver") == 0) {
  183. mWindow->setDriver(value);
  184. } else if (strcmp(var, "audio") == 0) {
  185. bool audio = false;
  186. if (readBool(value, &audio) != 0) {
  187. printf("set-audio-Error: Invalid value (%s)\n", value);
  188. return -4;
  189. }
  190. mSound->setEnabled(audio);
  191. } else if (strcmp(var, "volume") == 0) {
  192. float vol = 1.0f;
  193. if (sscanf(value, "%f", &vol) != 1) {
  194. printf("set-volume-Error: Invalid value (%s)\n", value);
  195. return -5;
  196. }
  197. mSound->setVolume(vol);
  198. } else if (strcmp(var, "mouse_x") == 0) {
  199. float sense = 1.0f;
  200. if (sscanf(value, "%f", &sense) != 1) {
  201. printf("set-mouse_x-Error: Invalid value (%s)\n", value);
  202. return -6;
  203. }
  204. //! \todo mouse support
  205. } else if (strcmp(var, "mouse_y") == 0) {
  206. float sense = 1.0f;
  207. if (sscanf(value, "%f", &sense) != 1) {
  208. printf("set-mouse_y-Error: Invalid value (%s)\n", value);
  209. return -7;
  210. }
  211. //! \todo mouse support
  212. } else if (strcmp(var, "basedir") == 0) {
  213. CHANGE_DIR_WITH_EXPANSION(mBaseDir);
  214. } else if (strcmp(var, "pakdir") == 0) {
  215. CHANGE_DIR_WITH_EXPANSION(mPakDir);
  216. } else if (strcmp(var, "audiodir") == 0) {
  217. CHANGE_DIR_WITH_EXPANSION(mAudioDir);
  218. } else if (strcmp(var, "datadir") == 0) {
  219. CHANGE_DIR_WITH_EXPANSION(mDataDir);
  220. } else if (strcmp(var, "font") == 0) {
  221. char *quotes = stringReplace(value, "\"", "");
  222. char *tmp = expandDirectoryNames(quotes);
  223. if (tmp == NULL) {
  224. mWindow->setFont(quotes);
  225. } else {
  226. mWindow->setFont(tmp);
  227. delete [] tmp;
  228. }
  229. delete [] quotes;
  230. } else {
  231. printf("set-Error: Unknown variable (%s = %s)\n", var, value);
  232. return -1;
  233. }
  234. return 0;
  235. }
  236. int OpenRaider::bind(const char *action, const char *key) {
  237. const char *tmp = action;
  238. if (action[0] == '+')
  239. tmp++;
  240. if (strcmp(tmp, "menu") == 0) {
  241. return bind(menu, key);
  242. } else if (strcmp(tmp, "console") == 0) {
  243. return bind(console, key);
  244. } else if (strcmp(tmp, "forward") == 0) {
  245. return bind(forward, key);
  246. } else if (strcmp(tmp, "backward") == 0) {
  247. return bind(backward, key);
  248. } else if (strcmp(tmp, "left") == 0) {
  249. return bind(left, key);
  250. } else if (strcmp(tmp, "right") == 0) {
  251. return bind(right, key);
  252. } else if (strcmp(tmp, "jump") == 0) {
  253. return bind(jump, key);
  254. } else if (strcmp(tmp, "crouch") == 0) {
  255. return bind(crouch, key);
  256. } else if (strcmp(tmp, "use") == 0) {
  257. return bind(use, key);
  258. } else if (strcmp(tmp, "holster") == 0) {
  259. return bind(holster, key);
  260. } else {
  261. printf("bind-Error: Unknown action (%s --> %s)\n", key, action);
  262. return -1;
  263. }
  264. }
  265. int OpenRaider::bind(ActionEvents action, const char *key) {
  266. assert(action != ActionEventCount);
  267. assert(key != NULL);
  268. assert(key[0] != '\0');
  269. size_t length = strlen(key);
  270. if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
  271. // Literal character like w, a, s, d, 0, 1...
  272. char c = key[1];
  273. if (((c >= '0') && (c <= '9'))
  274. || ((c >= 'a') && (c <= 'z'))) {
  275. keyBindings[action] = (KeyboardButton)c;
  276. } else {
  277. printf("bind-\'\'-Error: Unknown key (%s)\n", key);
  278. return -1;
  279. }
  280. } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
  281. // Special characters like tilde, esc, quote...
  282. char *tmp = stringRemoveQuotes(key);
  283. if (strcmp(tmp, "quote") == 0) {
  284. keyBindings[action] = quote;
  285. } else if (strcmp(tmp, "backslash") == 0) {
  286. keyBindings[action] = quote;
  287. } else if (strcmp(tmp, "backspace") == 0) {
  288. keyBindings[action] = backspace;
  289. } else if (strcmp(tmp, "capslock") == 0) {
  290. keyBindings[action] = capslock;
  291. } else if (strcmp(tmp, "comma") == 0) {
  292. keyBindings[action] = comma;
  293. } else if (strcmp(tmp, "del") == 0) {
  294. keyBindings[action] = del;
  295. } else if (strcmp(tmp, "up") == 0) {
  296. keyBindings[action] = up;
  297. } else if (strcmp(tmp, "down") == 0) {
  298. keyBindings[action] = down;
  299. } else if (strcmp(tmp, "left") == 0) {
  300. keyBindings[action] = KeyboardButton::left;
  301. } else if (strcmp(tmp, "right") == 0) {
  302. keyBindings[action] = KeyboardButton::right;
  303. } else if (strcmp(tmp, "end") == 0) {
  304. keyBindings[action] = end;
  305. } else if (strcmp(tmp, "equals") == 0) {
  306. keyBindings[action] = equals;
  307. } else if (strcmp(tmp, "escape") == 0) {
  308. keyBindings[action] = escape;
  309. } else if (strcmp(tmp, "f1") == 0) {
  310. keyBindings[action] = f1;
  311. } else if (strcmp(tmp, "f2") == 0) {
  312. keyBindings[action] = f2;
  313. } else if (strcmp(tmp, "f3") == 0) {
  314. keyBindings[action] = f3;
  315. } else if (strcmp(tmp, "f4") == 0) {
  316. keyBindings[action] = f4;
  317. } else if (strcmp(tmp, "f5") == 0) {
  318. keyBindings[action] = f5;
  319. } else if (strcmp(tmp, "f6") == 0) {
  320. keyBindings[action] = f6;
  321. } else if (strcmp(tmp, "f7") == 0) {
  322. keyBindings[action] = f7;
  323. } else if (strcmp(tmp, "f8") == 0) {
  324. keyBindings[action] = f8;
  325. } else if (strcmp(tmp, "f9") == 0) {
  326. keyBindings[action] = f9;
  327. } else if (strcmp(tmp, "f10") == 0) {
  328. keyBindings[action] = f10;
  329. } else if (strcmp(tmp, "f11") == 0) {
  330. keyBindings[action] = f11;
  331. } else if (strcmp(tmp, "f12") == 0) {
  332. keyBindings[action] = f12;
  333. } else if (strcmp(tmp, "backquote") == 0) {
  334. keyBindings[action] = backquote;
  335. } else if (strcmp(tmp, "home") == 0) {
  336. keyBindings[action] = home;
  337. } else if (strcmp(tmp, "insert") == 0) {
  338. keyBindings[action] = insert;
  339. } else if (strcmp(tmp, "leftalt") == 0) {
  340. keyBindings[action] = leftalt;
  341. } else if (strcmp(tmp, "leftctrl") == 0) {
  342. keyBindings[action] = leftctrl;
  343. } else if (strcmp(tmp, "leftbracket") == 0) {
  344. keyBindings[action] = leftbracket;
  345. } else if (strcmp(tmp, "leftgui") == 0) {
  346. keyBindings[action] = leftgui;
  347. } else if (strcmp(tmp, "leftshift") == 0) {
  348. keyBindings[action] = leftshift;
  349. } else if (strcmp(tmp, "minus") == 0) {
  350. keyBindings[action] = minus;
  351. } else if (strcmp(tmp, "numlock") == 0) {
  352. keyBindings[action] = numlock;
  353. } else if (strcmp(tmp, "pagedown") == 0) {
  354. keyBindings[action] = pagedown;
  355. } else if (strcmp(tmp, "pageup") == 0) {
  356. keyBindings[action] = pageup;
  357. } else if (strcmp(tmp, "pause") == 0) {
  358. keyBindings[action] = pause;
  359. } else if (strcmp(tmp, "dot") == 0) {
  360. keyBindings[action] = dot;
  361. } else if (strcmp(tmp, "rightalt") == 0) {
  362. keyBindings[action] = rightalt;
  363. } else if (strcmp(tmp, "rightctrl") == 0) {
  364. keyBindings[action] = rightctrl;
  365. } else if (strcmp(tmp, "enter") == 0) {
  366. keyBindings[action] = enter;
  367. } else if (strcmp(tmp, "rightgui") == 0) {
  368. keyBindings[action] = rightgui;
  369. } else if (strcmp(tmp, "rightbracket") == 0) {
  370. keyBindings[action] = rightbracket;
  371. } else if (strcmp(tmp, "rightshift") == 0) {
  372. keyBindings[action] = rightshift;
  373. } else if (strcmp(tmp, "scrolllock") == 0) {
  374. keyBindings[action] = scrolllock;
  375. } else if (strcmp(tmp, "semicolon") == 0) {
  376. keyBindings[action] = semicolon;
  377. } else if (strcmp(tmp, "slash") == 0) {
  378. keyBindings[action] = slash;
  379. } else if (strcmp(tmp, "space") == 0) {
  380. keyBindings[action] = space;
  381. } else if (strcmp(tmp, "tab") == 0) {
  382. keyBindings[action] = tab;
  383. } else {
  384. printf("bind-\"\"-Error: Unknown key (%s)\n", key);
  385. delete [] tmp;
  386. return -2;
  387. }
  388. delete [] tmp;
  389. } else {
  390. printf("bind-Error: Unknown key (%s)\n", key);
  391. return -3;
  392. }
  393. return 0;
  394. }
  395. void OpenRaider::loadPakFolderRecursive(const char *dir) {
  396. struct dirent *ep;
  397. DIR *pakDir;
  398. pakDir = opendir(dir);
  399. if (pakDir != NULL) {
  400. while ((ep = readdir(pakDir)) != NULL) {
  401. if (ep->d_type == DT_DIR) {
  402. if ((strcmp(".", ep->d_name) != 0)
  403. && (strcmp("..", ep->d_name) != 0)) {
  404. char *tmp = bufferString("%s%s", dir, ep->d_name);
  405. char *next = fullPath(tmp, '/');
  406. loadPakFolderRecursive(next);
  407. delete next;
  408. delete tmp;
  409. }
  410. } else {
  411. char *fullPathMap = bufferString("%s%s", dir, ep->d_name);
  412. char *lowerPath = bufferString("%s", fullPathMap);
  413. for (char *p = lowerPath; *p; ++p) *p = (char)tolower(*p);
  414. // Check for valid extension
  415. if (stringEndsWith(lowerPath, ".phd")
  416. || stringEndsWith(lowerPath, ".tr2")
  417. || stringEndsWith(lowerPath, ".tr4")
  418. || stringEndsWith(lowerPath, ".trc")) {
  419. //if (m_tombraider.checkMime(fullPathMap) == 0) {
  420. // printf("Validated pak: '%s'\n", fullPathMap);
  421. // Just load relative filename
  422. mMapList.push_back(bufferString("%s", (fullPathMap + strlen(mPakDir))));
  423. //} else {
  424. // printf("ERROR: pak file '%s' not found or invalid\n", fullPathMap);
  425. //}
  426. }
  427. delete [] lowerPath;
  428. delete [] fullPathMap;
  429. }
  430. }
  431. closedir(pakDir);
  432. } else {
  433. printf("Could not open PAK dir %s!\n", dir);
  434. }
  435. }
  436. void OpenRaider::fillMapList() {
  437. char *tmp = fullPath(mPakDir, '/');
  438. loadPakFolderRecursive(tmp);
  439. delete [] tmp;
  440. mMapListFilled = true;
  441. }
  442. int OpenRaider::initialize() {
  443. assert(mInit == false);
  444. assert(mRunning == false);
  445. // Initialize Windowing
  446. if (mWindow->initialize() != 0)
  447. return -1;
  448. // Initialize OpenGL
  449. if (mWindow->initializeGL() != 0)
  450. return -2;
  451. // Initialize window font
  452. if (mWindow->initializeFont() != 0)
  453. return -3;
  454. // Initialize sound
  455. if (mSound->initialize() != 0)
  456. return -4;
  457. mMenu->setVisible(true);
  458. mInit = true;
  459. return 0;
  460. }
  461. void OpenRaider::run() {
  462. assert(mInit == true);
  463. assert(mRunning == false);
  464. mRunning = true;
  465. while (mRunning) {
  466. clock_t startTime = systemTimerGet();
  467. mWindow->eventHandling();
  468. // Temp Debug
  469. glClearColor(0.25f, 0.75f, 0.25f, 1.0f);
  470. glClear(GL_COLOR_BUFFER_BIT);
  471. mWindow->glEnter2D();
  472. mConsole->display();
  473. mMenu->display();
  474. mWindow->glExit2D();
  475. // Put on screen
  476. mWindow->swapBuffersGL();
  477. // Fill map list after first render pass,
  478. // so menu *loading screen* is visible
  479. if (!mMapListFilled)
  480. fillMapList();
  481. clock_t stopTime = systemTimerGet();
  482. if (MAX_MS_PER_FRAME > (stopTime - startTime))
  483. mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));
  484. }
  485. }
  486. void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
  487. if ((keyBindings[menu] == key) && pressed) {
  488. mMenu->setVisible(!mMenu->isVisible());
  489. } else if (!mMenu->isVisible()) {
  490. if ((keyBindings[console] == key) && pressed) {
  491. mConsole->setVisible(!mConsole->isVisible());
  492. } else if (!mConsole->isVisible()) {
  493. if (keyBindings[forward] == key) {
  494. } else if (keyBindings[backward] == key) {
  495. } else if (keyBindings[left] == key) {
  496. } else if (keyBindings[right] == key) {
  497. } else if (keyBindings[jump] == key) {
  498. } else if (keyBindings[crouch] == key) {
  499. } else if (keyBindings[use] == key) {
  500. } else if (keyBindings[holster] == key) {
  501. }
  502. } else {
  503. mConsole->handleKeyboard(key, pressed);
  504. }
  505. } else {
  506. mMenu->handleKeyboard(key, pressed);
  507. }
  508. }
  509. void OpenRaider::handleText(char *text, bool notFinished) {
  510. if ((mConsole->isVisible()) && (!mMenu->isVisible())) {
  511. mConsole->handleText(text, notFinished);
  512. }
  513. }
  514. void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, MouseButton button, bool released) {
  515. if (mMenu->isVisible()) {
  516. mMenu->handleMouseClick(x, y, button, released);
  517. }
  518. }
  519. void OpenRaider::handleMouseMotion(int xrel, int yrel) {
  520. }