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 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 "Console.h"
  14. #include "Game.h"
  15. #include "main.h"
  16. #include "math/math.h"
  17. #include "Menu.h"
  18. #include "Sound.h"
  19. #include "TombRaider.h"
  20. #include "utils/strings.h"
  21. #include "utils/time.h"
  22. #include "OpenRaider.h"
  23. OpenRaider::OpenRaider() {
  24. mRunning = false;
  25. mFPS = false;
  26. mBaseDir = NULL;
  27. mPakDir = NULL;
  28. mAudioDir = NULL;
  29. mDataDir = NULL;
  30. mMapListFilled = false;
  31. for (int i = 0; i < ActionEventCount; i++)
  32. keyBindings[i] = unknown;
  33. mCameraRotationDeltaX = OR_DEG_TO_RAD(1.0f);
  34. mCameraRotationDeltaY = OR_DEG_TO_RAD(1.0f);
  35. }
  36. OpenRaider::~OpenRaider() {
  37. if (mBaseDir)
  38. delete mBaseDir;
  39. if (mPakDir)
  40. delete mPakDir;
  41. if (mAudioDir)
  42. delete mAudioDir;
  43. if (mDataDir)
  44. delete mDataDir;
  45. while (mMapList.size() > 0) {
  46. delete [] mMapList.back();
  47. mMapList.pop_back();
  48. }
  49. }
  50. int OpenRaider::loadConfig(const char *config) {
  51. assert(config != NULL);
  52. assert(config[0] != '\0');
  53. char *configFile = fullPath(config, 0);
  54. getConsole().print("Loading config from \"%s\"...", configFile);
  55. FILE *f = fopen(configFile, "r");
  56. if (f == NULL) {
  57. getConsole().print("Could not open file!");
  58. return -1;
  59. }
  60. char buffer[256];
  61. while (fgets(buffer, 256, f) != NULL) {
  62. int error = command(buffer);
  63. if (error != 0) {
  64. getConsole().print("Error Code: %d", error);
  65. }
  66. }
  67. fclose(f);
  68. return 0;
  69. }
  70. int OpenRaider::command(const char *command) {
  71. assert(command != NULL);
  72. assert(command[0] != '\0');
  73. int returnValue = 0;
  74. char *cmd = bufferString("%s", command);
  75. size_t length = strlen(cmd);
  76. // Command ends at '\n' or # when a comment begins
  77. for (size_t i = 0; i < length; i++) {
  78. if (cmd[i] == '\n' || cmd[i] == '#') {
  79. cmd[i] = '\0';
  80. break;
  81. }
  82. }
  83. char *token = strtok(cmd, " \t");
  84. if (token != NULL) {
  85. // token is the command to execute
  86. // get arguments
  87. std::vector<char *> args;
  88. char *next;
  89. while ((next = strtok(NULL, " \t")) != NULL) {
  90. args.push_back(next);
  91. }
  92. // Execute
  93. returnValue = this->command(token, &args);
  94. }
  95. free(cmd);
  96. return returnValue;
  97. }
  98. int OpenRaider::command(const char *command, std::vector<char *> *args) {
  99. assert(command != NULL);
  100. assert(command[0] != '\0');
  101. assert(args != NULL);
  102. if (strcmp(command, "set") == 0) {
  103. if (args->size() != 2) {
  104. getConsole().print("Invalid use of set-command");
  105. return -1;
  106. } else {
  107. return set(args->at(0), args->at(1));
  108. }
  109. } else if (strcmp(command, "bind") == 0) {
  110. if (args->size() != 2) {
  111. getConsole().print("Invalid use of bind-command");
  112. return -2;
  113. } else {
  114. return bind(args->at(0), args->at(1));
  115. }
  116. } else if (strcmp(command, "quit") == 0) {
  117. exit(0);
  118. } else if (strcmp(command, "load") == 0) {
  119. char *tmp = bufferString("%s/%s", mPakDir, args->at(0));
  120. int error = getGame().loadLevel(tmp);
  121. delete [] tmp;
  122. return error;
  123. } else if (strcmp(command, "sshot") == 0) {
  124. char *filename = bufferString("%s/sshots/%s", mBaseDir, VERSION);
  125. bool console = (args->size() > 0) && (strcmp(args->at(0), "console") == 0);
  126. bool menu = (args->size() > 0) && (strcmp(args->at(0), "menu") == 0);
  127. if (!console) {
  128. getConsole().setVisible(false);
  129. if (menu)
  130. getMenu().setVisible(true);
  131. frame();
  132. frame(); // Double buffered
  133. }
  134. getRender().screenShot(filename);
  135. if (!console) {
  136. getConsole().setVisible(true);
  137. if (menu)
  138. getMenu().setVisible(false);
  139. }
  140. getConsole().print("Screenshot stored...");
  141. delete filename;
  142. } else if (strcmp(command, "mode") == 0) {
  143. if (args->size() > 0) {
  144. char *mode = args->at(0);
  145. if (strcmp(mode, "wireframe") == 0) {
  146. if (getGame().mLoaded) {
  147. getRender().setMode(Render::modeWireframe);
  148. getConsole().print("Wireframe mode");
  149. } else {
  150. getConsole().print("Load a level to set this mode!");
  151. return -3;
  152. }
  153. } else if (strcmp(mode, "solid") == 0) {
  154. if (getGame().mLoaded) {
  155. getRender().setMode(Render::modeSolid);
  156. getConsole().print("Solid mode");
  157. } else {
  158. getConsole().print("Load a level to set this mode!");
  159. return -4;
  160. }
  161. } else if (strcmp(mode, "texture") == 0) {
  162. if (getGame().mLoaded) {
  163. getRender().setMode(Render::modeTexture);
  164. getConsole().print("Texture mode");
  165. } else {
  166. getConsole().print("Load a level to set this mode!");
  167. return -5;
  168. }
  169. } else if (strcmp(mode, "vertexlight") == 0) {
  170. if (getGame().mLoaded) {
  171. getRender().setMode(Render::modeVertexLight);
  172. getConsole().print("Vertexlight mode");
  173. } else {
  174. getConsole().print("Load a level to set this mode!");
  175. return -6;
  176. }
  177. } else if (strcmp(mode, "titlescreen") == 0) {
  178. getRender().setMode(Render::modeLoadScreen);
  179. getConsole().print("Titlescreen mode");
  180. } else {
  181. getConsole().print("Invalid use of mode command (%s)!", mode);
  182. return -7;
  183. }
  184. } else {
  185. getConsole().print("Invalid use of mode command!");
  186. return -8;
  187. }
  188. } else if (strcmp(command, "move") == 0) {
  189. if (args->size() > 0) {
  190. if (getGame().mLoaded) {
  191. char *move = args->at(0);
  192. if (strcmp(move, "walk") == 0) {
  193. getGame().mLara->moveType = worldMoveType_walk;
  194. getConsole().print("Lara is walking...");
  195. } else if (strcmp(move, "fly") == 0) {
  196. getGame().mLara->moveType = worldMoveType_fly;
  197. getConsole().print("Lara is flying...");
  198. } else if (strcmp(move, "noclip") == 0) {
  199. getGame().mLara->moveType = worldMoveType_noClipping;
  200. getConsole().print("Lara is noclipping...");
  201. } else {
  202. getConsole().print("Invalid use of move command (%s)!", move);
  203. return -9;
  204. }
  205. } else {
  206. getConsole().print("Load a level to change the movement type!");
  207. return -10;
  208. }
  209. } else {
  210. getConsole().print("Invalid use of move command!");
  211. return -11;
  212. }
  213. } else if (strcmp(command, "sound") == 0) {
  214. if (args->size() > 0) {
  215. getSound().play(atoi(args->at(0)));
  216. } else {
  217. getConsole().print("Invalid use of sound command!");
  218. return -12;
  219. }
  220. } else if (strcmp(command, "animate") == 0) {
  221. if (args->size() > 0) {
  222. char c = args->at(0)[0];
  223. if (c == 'n') {
  224. // Step all skeletal models to their next animation
  225. if (getRender().getFlags() & Render::fAnimateAllModels) {
  226. for (unsigned int i = 0; i < getRender().mModels.size(); i++) {
  227. SkeletalModel *m = getRender().mModels[i];
  228. if (m->getAnimation() < ((int)m->model->animation.size() - 1))
  229. m->setAnimation(m->getAnimation() + 1);
  230. else
  231. if (m->getAnimation() != 0)
  232. m->setAnimation(0);
  233. }
  234. } else {
  235. getConsole().print("Animations need to be enabled!");
  236. }
  237. } else if (c == 'p') {
  238. // Step all skeletal models to their previous animation
  239. if (getRender().getFlags() & Render::fAnimateAllModels) {
  240. for (unsigned int i = 0; i < getRender().mModels.size(); i++) {
  241. SkeletalModel *m = getRender().mModels[i];
  242. if (m->getAnimation() > 0)
  243. m->setAnimation(m->getAnimation() - 1);
  244. else
  245. if (m->model->animation.size() > 0)
  246. m->setAnimation(m->model->animation.size() - 1);
  247. }
  248. } else {
  249. getConsole().print("Animations need to be enabled!");
  250. }
  251. } else {
  252. // Enable or disable animating all skeletal models
  253. bool b;
  254. if (readBool(args->at(0), &b) < 0) {
  255. getConsole().print("Pass BOOL to animate command!");
  256. return -13;
  257. }
  258. if (b)
  259. getRender().setFlags(Render::fAnimateAllModels);
  260. else
  261. getRender().clearFlags(Render::fAnimateAllModels);
  262. getConsole().print(b ? "Animating all models" : "No longer animating all models");
  263. }
  264. } else {
  265. getConsole().print("Invalid use of animate command!");
  266. return -14;
  267. }
  268. } else if (strcmp(command, "light") == 0) {
  269. if (args->size() > 0) {
  270. bool b;
  271. if (readBool(args->at(0), &b) < 0) {
  272. getConsole().print("Pass BOOL to light command!");
  273. return -15;
  274. }
  275. if (b)
  276. getRender().setFlags(Render::fGL_Lights);
  277. else
  278. getRender().clearFlags(Render::fGL_Lights);
  279. getConsole().print("GL-Lights are now %s", b ? "on" : "off");
  280. } else {
  281. getConsole().print("Invalid use of light-command!");
  282. return -16;
  283. }
  284. } else if (strcmp(command, "fog") == 0) {
  285. if (args->size() > 0) {
  286. bool b;
  287. if (readBool(args->at(0), &b) < 0) {
  288. getConsole().print("Pass BOOL to fog command!");
  289. return -17;
  290. }
  291. if (b)
  292. getRender().setFlags(Render::fFog);
  293. else
  294. getRender().clearFlags(Render::fFog);
  295. getConsole().print("Fog is now %s", b ? "on" : "off");
  296. } else {
  297. getConsole().print("Invalid use of fog-command!");
  298. return -18;
  299. }
  300. } else if (strcmp(command, "hop") == 0) {
  301. if (args->size() > 0) {
  302. bool b;
  303. if (readBool(args->at(0), &b) < 0) {
  304. getConsole().print("Pass BOOL to hop command!");
  305. return -19;
  306. }
  307. if (b)
  308. getWorld().setFlag(World::fEnableHopping);
  309. else
  310. getWorld().clearFlag(World::fEnableHopping);
  311. getConsole().print("Room hopping is now %s", b ? "on" : "off");
  312. } else {
  313. getConsole().print("Invalid use of hop-command!");
  314. return -20;
  315. }
  316. } else if (strcmp(command, "viewmodel") == 0) {
  317. if (getGame().mLara) {
  318. SkeletalModel *smdl = static_cast<SkeletalModel *>(getGame().mLara->tmpHook);
  319. skeletal_model_t *mdl = getWorld().getModel(atoi(args->at(0)));
  320. if (smdl)
  321. smdl->setModel(mdl);
  322. }
  323. //m_render.ViewModel(LARA, atoi(cmd));
  324. } else if (strcmp(command, "pos") == 0) {
  325. if (getGame().mLara) {
  326. getConsole().print("Position:");
  327. getConsole().print(" Room %i (0x%X)", getGame().mLara->room, getWorld().getRoomInfo(getGame().mLara->room));
  328. getConsole().print(" %.1fx %.1fy %.1fz", getGame().mLara->pos[0], getGame().mLara->pos[1], getGame().mLara->pos[2]);
  329. getConsole().print(" %.1f Yaw %.1f Pitch", OR_RAD_TO_DEG(getGame().mLara->angles[1]), OR_RAD_TO_DEG(getGame().mLara->angles[2]));
  330. } else {
  331. getConsole().print("Load a level to get Laras position!");
  332. return -21;
  333. }
  334. } else if (strcmp(command, "help") == 0) {
  335. if (args->size() == 0) {
  336. getConsole().print("Available commands:");
  337. getConsole().print(" load - load a level");
  338. getConsole().print(" set - set a parameter");
  339. getConsole().print(" bind - bind a keyboard/mouse action");
  340. getConsole().print(" sshot - make a screenshot");
  341. getConsole().print(" move - [walk|fly|noclip]");
  342. getConsole().print(" sound - INT - Test play sound");
  343. getConsole().print(" mode - MODE - Render mode");
  344. getConsole().print(" animate - [BOOL|n|p] - Animate models");
  345. getConsole().print(" light - BOOL - GL Lights");
  346. getConsole().print(" fog - BOOL - GL Fog");
  347. getConsole().print(" hop - BOOL - Room hop");
  348. getConsole().print(" viewmodel - INT - Change Laras model");
  349. getConsole().print(" pos - Print position info");
  350. getConsole().print(" help - print command help");
  351. getConsole().print(" quit - exit OpenRaider");
  352. getConsole().print("Use help COMMAND to get additional info");
  353. } else if (args->size() == 1) {
  354. return help(args->at(0));
  355. } else {
  356. getConsole().print("Invalid use of help-command");
  357. return -22;
  358. }
  359. } else {
  360. getConsole().print("Unknown command: %s ", command);
  361. return -23;
  362. }
  363. return 0;
  364. }
  365. int OpenRaider::help(const char *cmd) {
  366. assert(cmd != NULL);
  367. assert(cmd[0] != '\0');
  368. if (strcmp(cmd, "set") == 0) {
  369. getConsole().print("set-Command Usage:");
  370. getConsole().print(" set VAR VAL");
  371. getConsole().print("Available Variables:");
  372. getConsole().print(" basedir STRING");
  373. getConsole().print(" pakdir STRING");
  374. getConsole().print(" audiodir STRING");
  375. getConsole().print(" datadir STRING");
  376. getConsole().print(" font STRING");
  377. getConsole().print(" gldriver STRING");
  378. getConsole().print(" size \"INTxINT\"");
  379. getConsole().print(" fullscreen BOOL");
  380. getConsole().print(" audio BOOL");
  381. getConsole().print(" volume BOOL");
  382. getConsole().print(" mouse_x FLOAT");
  383. getConsole().print(" mouse_y FLOAT");
  384. getConsole().print(" fps BOOL");
  385. getConsole().print("Enclose STRINGs with \"\"!");
  386. getConsole().print("size expects a STRING in the specified format");
  387. } else if (strcmp(cmd, "bind") == 0) {
  388. getConsole().print("bind-Command Usage:");
  389. getConsole().print(" bind ACTION KEY");
  390. getConsole().print("Available Actions:");
  391. getConsole().print(" menu");
  392. getConsole().print(" console");
  393. getConsole().print(" forward");
  394. getConsole().print(" backward");
  395. getConsole().print(" left");
  396. getConsole().print(" right");
  397. getConsole().print(" jump");
  398. getConsole().print(" crouch");
  399. getConsole().print(" use");
  400. getConsole().print(" holster");
  401. getConsole().print("Key-Format:");
  402. getConsole().print(" 'a' or '1' for character/number keys");
  403. getConsole().print(" \"leftctrl\" for symbols and special keys");
  404. } else if (strcmp(cmd, "load") == 0) {
  405. getConsole().print("load-Command Usage:");
  406. getConsole().print(" load levelfile.name");
  407. } else if (strcmp(cmd, "game") == 0) {
  408. getConsole().print("Use \"game help\" for more info");
  409. } else if (strcmp(cmd, "sshot") == 0) {
  410. getConsole().print("sshot-Command Usage:");
  411. getConsole().print(" sshot [console|menu]");
  412. getConsole().print("Add console/menu to capture them too");
  413. } else if (strcmp(cmd, "sound") == 0) {
  414. getConsole().print("sound-Command Usage:");
  415. getConsole().print(" sound INT");
  416. getConsole().print("Where INT is a valid sound ID integer");
  417. } else if (strcmp(cmd, "move") == 0) {
  418. getConsole().print("move-Command Usage:");
  419. getConsole().print(" move COMMAND");
  420. getConsole().print("Where COMMAND is one of the following:");
  421. getConsole().print(" walk");
  422. getConsole().print(" fly");
  423. getConsole().print(" noclip");
  424. } else if (strcmp(cmd, "mode") == 0) {
  425. getConsole().print("mode-Command Usage:");
  426. getConsole().print(" mode MODE");
  427. getConsole().print("Where MODE is one of the following:");
  428. getConsole().print(" wireframe");
  429. getConsole().print(" solid");
  430. getConsole().print(" texture");
  431. getConsole().print(" vertexlight");
  432. getConsole().print(" titlescreen");
  433. } else if (strcmp(cmd, "animate") == 0) {
  434. getConsole().print("animate-Command Usage:");
  435. getConsole().print(" animate [n|p|BOOL]");
  436. getConsole().print("Where the commands have the following meaning:");
  437. getConsole().print(" BOOL to (de)activate animating all models");
  438. getConsole().print(" n to step all models to their next animation");
  439. getConsole().print(" p to step all models to their previous animation");
  440. } else {
  441. getConsole().print("No help available for %s", cmd);
  442. return -1;
  443. }
  444. return 0;
  445. }
  446. char *OpenRaider::expandDirectoryNames(const char *s) {
  447. const char *base = "$(basedir)";
  448. const char *pak = "$(pakdir)";
  449. const char *audio = "$(audiodir)";
  450. const char *data = "$(datadir)";
  451. assert(s != NULL);
  452. assert(s[0] != '\0');
  453. if (mBaseDir != NULL) {
  454. if (strstr(s, base) != NULL) {
  455. return stringReplace(s, base, mBaseDir);
  456. }
  457. }
  458. if (mPakDir != NULL) {
  459. if (strstr(s, pak) != NULL) {
  460. return stringReplace(s, pak, mPakDir);
  461. }
  462. }
  463. if (mAudioDir != NULL) {
  464. if (strstr(s, audio) != NULL) {
  465. return stringReplace(s, audio, mAudioDir);
  466. }
  467. }
  468. if (mDataDir != NULL) {
  469. if (strstr(s, data) != NULL) {
  470. return stringReplace(s, data, mDataDir);
  471. }
  472. }
  473. return NULL;
  474. }
  475. #define CHANGE_DIR_WITH_EXPANSION(a) do { \
  476. char *quotes = stringRemoveQuotes(value); \
  477. char *tmp = expandDirectoryNames(quotes); \
  478. if (tmp == NULL) { \
  479. a = fullPath(quotes, 0); \
  480. } else { \
  481. a = fullPath(tmp, 0); \
  482. delete [] tmp; \
  483. } \
  484. delete [] quotes; \
  485. } while(false)
  486. int OpenRaider::set(const char *var, const char *value) {
  487. assert(var != NULL);
  488. assert(var[0] != '\0');
  489. assert(value != NULL);
  490. assert(value[0] != '\0');
  491. if (strcmp(var, "size") == 0) {
  492. // value has format like "\"1024x768\""
  493. unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
  494. if (sscanf(value, "\"%dx%d\"", &w, &h) != 2) {
  495. getConsole().print("set-size-Error: Invalid value (%s)", value);
  496. return -2;
  497. }
  498. getWindow().setSize(w, h);
  499. } else if (strcmp(var, "fullscreen") == 0) {
  500. bool fullscreen = false;
  501. if (readBool(value, &fullscreen) != 0) {
  502. getConsole().print("set-fullscreen-Error: Invalid value (%s)", value);
  503. return -3;
  504. }
  505. getWindow().setFullscreen(fullscreen);
  506. } else if (strcmp(var, "gldriver") == 0) {
  507. getWindow().setDriver(value);
  508. } else if (strcmp(var, "audio") == 0) {
  509. bool audio = false;
  510. if (readBool(value, &audio) != 0) {
  511. getConsole().print("set-audio-Error: Invalid value (%s)", value);
  512. return -4;
  513. }
  514. getSound().setEnabled(audio);
  515. } else if (strcmp(var, "volume") == 0) {
  516. float vol = 1.0f;
  517. if (sscanf(value, "%f", &vol) != 1) {
  518. getConsole().print("set-volume-Error: Invalid value (%s)", value);
  519. return -5;
  520. }
  521. getSound().setVolume(vol);
  522. } else if (strcmp(var, "mouse_x") == 0) {
  523. float sense = 1.0f;
  524. if (sscanf(value, "%f", &sense) != 1) {
  525. getConsole().print("set-mouse_x-Error: Invalid value (%s)", value);
  526. return -6;
  527. }
  528. mCameraRotationDeltaX = OR_DEG_TO_RAD(sense);
  529. } else if (strcmp(var, "mouse_y") == 0) {
  530. float sense = 1.0f;
  531. if (sscanf(value, "%f", &sense) != 1) {
  532. getConsole().print("set-mouse_y-Error: Invalid value (%s)", value);
  533. return -7;
  534. }
  535. mCameraRotationDeltaY = OR_DEG_TO_RAD(sense);
  536. } else if (strcmp(var, "fps") == 0) {
  537. bool fps = false;
  538. if (readBool(value, &fps) != 0) {
  539. getConsole().print("set-fps-Error: Invalid value (%s)", value);
  540. return -8;
  541. }
  542. mFPS = fps;
  543. } else if (strcmp(var, "basedir") == 0) {
  544. CHANGE_DIR_WITH_EXPANSION(mBaseDir);
  545. } else if (strcmp(var, "pakdir") == 0) {
  546. CHANGE_DIR_WITH_EXPANSION(mPakDir);
  547. } else if (strcmp(var, "audiodir") == 0) {
  548. CHANGE_DIR_WITH_EXPANSION(mAudioDir);
  549. } else if (strcmp(var, "datadir") == 0) {
  550. CHANGE_DIR_WITH_EXPANSION(mDataDir);
  551. } else if (strcmp(var, "font") == 0) {
  552. char *quotes = stringReplace(value, "\"", "");
  553. char *tmp = expandDirectoryNames(quotes);
  554. if (tmp == NULL) {
  555. getWindow().setFont(quotes);
  556. } else {
  557. getWindow().setFont(tmp);
  558. delete [] tmp;
  559. }
  560. delete [] quotes;
  561. } else {
  562. getConsole().print("set-Error: Unknown variable (%s = %s)", var, value);
  563. return -1;
  564. }
  565. return 0;
  566. }
  567. int OpenRaider::bind(const char *action, const char *key) {
  568. assert(action != NULL);
  569. assert(action[0] != '\0');
  570. assert(key != NULL);
  571. assert(key[0] != '\0');
  572. const char *tmp = action;
  573. if (action[0] == '+')
  574. tmp++;
  575. if (strcmp(tmp, "menu") == 0) {
  576. return bind(menuAction, key);
  577. } else if (strcmp(tmp, "console") == 0) {
  578. return bind(consoleAction, key);
  579. } else if (strcmp(tmp, "forward") == 0) {
  580. return bind(forwardAction, key);
  581. } else if (strcmp(tmp, "backward") == 0) {
  582. return bind(backwardAction, key);
  583. } else if (strcmp(tmp, "left") == 0) {
  584. return bind(leftAction, key);
  585. } else if (strcmp(tmp, "right") == 0) {
  586. return bind(rightAction, key);
  587. } else if (strcmp(tmp, "jump") == 0) {
  588. return bind(jumpAction, key);
  589. } else if (strcmp(tmp, "crouch") == 0) {
  590. return bind(crouchAction, key);
  591. } else if (strcmp(tmp, "use") == 0) {
  592. return bind(useAction, key);
  593. } else if (strcmp(tmp, "holster") == 0) {
  594. return bind(holsterAction, key);
  595. } else {
  596. getConsole().print("bind-Error: Unknown action (%s --> %s)", key, action);
  597. return -1;
  598. }
  599. }
  600. int OpenRaider::bind(ActionEvents action, const char *key) {
  601. assert(action < ActionEventCount);
  602. assert(key != NULL);
  603. assert(key[0] != '\0');
  604. size_t length = strlen(key);
  605. if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
  606. // Literal character like w, a, s, d, 0, 1...
  607. char c = key[1];
  608. if (((c >= '0') && (c <= '9'))
  609. || ((c >= 'a') && (c <= 'z'))) {
  610. keyBindings[action] = (KeyboardButton)c;
  611. } else {
  612. getConsole().print("bind-\'\'-Error: Unknown key (%s)", key);
  613. return -1;
  614. }
  615. } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
  616. // Special characters like tilde, esc, quote...
  617. char *tmp = stringRemoveQuotes(key);
  618. if (strcmp(tmp, "quote") == 0) {
  619. keyBindings[action] = quote;
  620. } else if (strcmp(tmp, "backslash") == 0) {
  621. keyBindings[action] = quote;
  622. } else if (strcmp(tmp, "backspace") == 0) {
  623. keyBindings[action] = backspace;
  624. } else if (strcmp(tmp, "capslock") == 0) {
  625. keyBindings[action] = capslock;
  626. } else if (strcmp(tmp, "comma") == 0) {
  627. keyBindings[action] = comma;
  628. } else if (strcmp(tmp, "del") == 0) {
  629. keyBindings[action] = del;
  630. } else if (strcmp(tmp, "up") == 0) {
  631. keyBindings[action] = up;
  632. } else if (strcmp(tmp, "down") == 0) {
  633. keyBindings[action] = down;
  634. } else if (strcmp(tmp, "left") == 0) {
  635. keyBindings[action] = left;
  636. } else if (strcmp(tmp, "right") == 0) {
  637. keyBindings[action] = right;
  638. } else if (strcmp(tmp, "end") == 0) {
  639. keyBindings[action] = end;
  640. } else if (strcmp(tmp, "equals") == 0) {
  641. keyBindings[action] = equals;
  642. } else if (strcmp(tmp, "escape") == 0) {
  643. keyBindings[action] = escape;
  644. } else if (strcmp(tmp, "f1") == 0) {
  645. keyBindings[action] = f1;
  646. } else if (strcmp(tmp, "f2") == 0) {
  647. keyBindings[action] = f2;
  648. } else if (strcmp(tmp, "f3") == 0) {
  649. keyBindings[action] = f3;
  650. } else if (strcmp(tmp, "f4") == 0) {
  651. keyBindings[action] = f4;
  652. } else if (strcmp(tmp, "f5") == 0) {
  653. keyBindings[action] = f5;
  654. } else if (strcmp(tmp, "f6") == 0) {
  655. keyBindings[action] = f6;
  656. } else if (strcmp(tmp, "f7") == 0) {
  657. keyBindings[action] = f7;
  658. } else if (strcmp(tmp, "f8") == 0) {
  659. keyBindings[action] = f8;
  660. } else if (strcmp(tmp, "f9") == 0) {
  661. keyBindings[action] = f9;
  662. } else if (strcmp(tmp, "f10") == 0) {
  663. keyBindings[action] = f10;
  664. } else if (strcmp(tmp, "f11") == 0) {
  665. keyBindings[action] = f11;
  666. } else if (strcmp(tmp, "f12") == 0) {
  667. keyBindings[action] = f12;
  668. } else if (strcmp(tmp, "backquote") == 0) {
  669. keyBindings[action] = backquote;
  670. } else if (strcmp(tmp, "home") == 0) {
  671. keyBindings[action] = home;
  672. } else if (strcmp(tmp, "insert") == 0) {
  673. keyBindings[action] = insert;
  674. } else if (strcmp(tmp, "leftalt") == 0) {
  675. keyBindings[action] = leftalt;
  676. } else if (strcmp(tmp, "leftctrl") == 0) {
  677. keyBindings[action] = leftctrl;
  678. } else if (strcmp(tmp, "leftbracket") == 0) {
  679. keyBindings[action] = leftbracket;
  680. } else if (strcmp(tmp, "leftgui") == 0) {
  681. keyBindings[action] = leftgui;
  682. } else if (strcmp(tmp, "leftshift") == 0) {
  683. keyBindings[action] = leftshift;
  684. } else if (strcmp(tmp, "minus") == 0) {
  685. keyBindings[action] = minus;
  686. } else if (strcmp(tmp, "numlock") == 0) {
  687. keyBindings[action] = numlock;
  688. } else if (strcmp(tmp, "pagedown") == 0) {
  689. keyBindings[action] = pagedown;
  690. } else if (strcmp(tmp, "pageup") == 0) {
  691. keyBindings[action] = pageup;
  692. } else if (strcmp(tmp, "pause") == 0) {
  693. keyBindings[action] = pause;
  694. } else if (strcmp(tmp, "dot") == 0) {
  695. keyBindings[action] = dot;
  696. } else if (strcmp(tmp, "rightalt") == 0) {
  697. keyBindings[action] = rightalt;
  698. } else if (strcmp(tmp, "rightctrl") == 0) {
  699. keyBindings[action] = rightctrl;
  700. } else if (strcmp(tmp, "enter") == 0) {
  701. keyBindings[action] = enter;
  702. } else if (strcmp(tmp, "rightgui") == 0) {
  703. keyBindings[action] = rightgui;
  704. } else if (strcmp(tmp, "rightbracket") == 0) {
  705. keyBindings[action] = rightbracket;
  706. } else if (strcmp(tmp, "rightshift") == 0) {
  707. keyBindings[action] = rightshift;
  708. } else if (strcmp(tmp, "scrolllock") == 0) {
  709. keyBindings[action] = scrolllock;
  710. } else if (strcmp(tmp, "semicolon") == 0) {
  711. keyBindings[action] = semicolon;
  712. } else if (strcmp(tmp, "slash") == 0) {
  713. keyBindings[action] = slash;
  714. } else if (strcmp(tmp, "space") == 0) {
  715. keyBindings[action] = space;
  716. } else if (strcmp(tmp, "tab") == 0) {
  717. keyBindings[action] = tab;
  718. } else if (strcmp(tmp, "leftmouse") == 0) {
  719. keyBindings[action] = leftmouse;
  720. } else if (strcmp(tmp, "middlemouse") == 0) {
  721. keyBindings[action] = middlemouse;
  722. } else if (strcmp(tmp, "rightmouse") == 0) {
  723. keyBindings[action] = rightmouse;
  724. } else {
  725. getConsole().print("bind-\"\"-Error: Unknown key (%s)", key);
  726. delete [] tmp;
  727. return -2;
  728. }
  729. delete [] tmp;
  730. } else {
  731. getConsole().print("bind-Error: Unknown key (%s)", key);
  732. return -3;
  733. }
  734. return 0;
  735. }
  736. void OpenRaider::loadPakFolderRecursive(const char *dir) {
  737. struct dirent *ep;
  738. DIR *pakDir;
  739. assert(dir != NULL);
  740. assert(dir[0] != '\0');
  741. assert(mRunning == true);
  742. pakDir = opendir(dir);
  743. if (pakDir != NULL) {
  744. while ((ep = readdir(pakDir)) != NULL) {
  745. if (ep->d_type == DT_DIR) {
  746. if ((strcmp(".", ep->d_name) != 0)
  747. && (strcmp("..", ep->d_name) != 0)) {
  748. char *tmp = bufferString("%s%s", dir, ep->d_name);
  749. char *next = fullPath(tmp, '/');
  750. loadPakFolderRecursive(next);
  751. delete next;
  752. delete tmp;
  753. }
  754. } else {
  755. char *fullPathMap = bufferString("%s%s", dir, ep->d_name);
  756. char *lowerPath = bufferString("%s", fullPathMap);
  757. for (char *p = lowerPath; *p; ++p) *p = (char)tolower(*p);
  758. // Check for valid extension
  759. if (stringEndsWith(lowerPath, ".phd")
  760. || stringEndsWith(lowerPath, ".tr2")
  761. || stringEndsWith(lowerPath, ".tr4")
  762. || stringEndsWith(lowerPath, ".trc")) {
  763. int error = TombRaider::checkMime(fullPathMap);
  764. if (error == 0) {
  765. // Just load relative filename
  766. mMapList.push_back(bufferString("%s", (fullPathMap + strlen(mPakDir) + 1)));
  767. } else {
  768. getConsole().print("Error: pak file '%s' %s",
  769. fullPathMap, (error == -1) ? "not found" : "invalid");
  770. }
  771. }
  772. delete [] lowerPath;
  773. delete [] fullPathMap;
  774. }
  775. }
  776. closedir(pakDir);
  777. } else {
  778. getConsole().print("Could not open PAK dir %s!", dir);
  779. }
  780. }
  781. void OpenRaider::fillMapList() {
  782. assert(mRunning == true);
  783. char *tmp = fullPath(mPakDir, '/');
  784. loadPakFolderRecursive(tmp);
  785. delete [] tmp;
  786. mMapListFilled = true;
  787. }
  788. void OpenRaider::run() {
  789. assert(mRunning == false);
  790. mRunning = true;
  791. while (mRunning) {
  792. frame();
  793. }
  794. }
  795. void OpenRaider::frame() {
  796. assert(mRunning == true);
  797. static clock_t fpsSum = 0, fpsCount = 0;
  798. static int fps = 0;
  799. clock_t startTime = systemTimerGet();
  800. // Get keyboard and mouse input
  801. getWindow().eventHandling();
  802. // Clear screen
  803. glClearColor(0.00f, 0.00f, 0.00f, 1.0f);
  804. glClear(GL_COLOR_BUFFER_BIT);
  805. // Draw game scene
  806. getRender().display();
  807. // Draw 2D overlays (console and menu)
  808. getWindow().glEnter2D();
  809. getConsole().display();
  810. getMenu().display();
  811. // Draw FPS counter
  812. if (mFPS)
  813. getWindow().drawText(10, getWindow().mHeight - 20, 0.5f, OR_BLUE, "%dFPS", fps);
  814. getWindow().glExit2D();
  815. // Put new frame on screen
  816. getWindow().swapBuffersGL();
  817. // Fill map list after first render pass,
  818. // so menu *loading screen* is visible
  819. if (!mMapListFilled)
  820. fillMapList();
  821. // Calculate FPS display value
  822. fpsCount++;
  823. fpsSum += (systemTimerGet() - startTime);
  824. if (fpsSum >= 500) {
  825. // Update every 500ms
  826. fps = (int)((float)fpsCount * (1000.0f / (float)fpsSum));
  827. fpsCount = fpsSum = 0;
  828. }
  829. }
  830. void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
  831. assert(key < unknown);
  832. assert(mRunning == true);
  833. if ((keyBindings[menuAction] == key) && pressed) {
  834. getMenu().setVisible(!getMenu().isVisible());
  835. } else if (!getMenu().isVisible()) {
  836. if ((keyBindings[consoleAction] == key) && pressed) {
  837. getConsole().setVisible(!getConsole().isVisible());
  838. } else if (!getConsole().isVisible()) {
  839. for (int i = forwardAction; i < ActionEventCount; i++) {
  840. if (keyBindings[i] == key) {
  841. getGame().handleAction((ActionEvents)i, pressed);
  842. }
  843. }
  844. } else {
  845. getConsole().handleKeyboard(key, pressed);
  846. }
  847. } else {
  848. getMenu().handleKeyboard(key, pressed);
  849. }
  850. getWindow().setMousegrab(!(getMenu().isVisible() || getConsole().isVisible()));
  851. }
  852. void OpenRaider::handleText(char *text, bool notFinished) {
  853. assert(text != NULL);
  854. assert(text[0] != '\0');
  855. assert(mRunning == true);
  856. if ((getConsole().isVisible()) && (!getMenu().isVisible())) {
  857. getConsole().handleText(text, notFinished);
  858. }
  859. }
  860. void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  861. assert(button < unknown);
  862. assert(mRunning == true);
  863. if (getMenu().isVisible()) {
  864. getMenu().handleMouseClick(x, y, button, released);
  865. } else if (!getConsole().isVisible()) {
  866. for (int i = forwardAction; i < ActionEventCount; i++) {
  867. if (keyBindings[i] == button) {
  868. getGame().handleAction((ActionEvents)i, !released);
  869. }
  870. }
  871. }
  872. }
  873. void OpenRaider::handleMouseMotion(int xrel, int yrel) {
  874. assert((xrel != 0) || (yrel != 0));
  875. assert(mRunning == true);
  876. if ((!getConsole().isVisible()) && (!getMenu().isVisible())) {
  877. getGame().handleMouseMotion(xrel, yrel);
  878. }
  879. }
  880. void OpenRaider::handleMouseScroll(int xrel, int yrel) {
  881. assert((xrel != 0) || (yrel != 0));
  882. assert(mRunning == true);
  883. if ((getConsole().isVisible()) && (!getMenu().isVisible())) {
  884. getConsole().handleMouseScroll(xrel, yrel);
  885. }
  886. }