Open Source Tomb Raider Engine
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

OpenRaider.cpp 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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, "help") == 0) {
  325. if (args->size() == 0) {
  326. getConsole().print("Available commands:");
  327. getConsole().print(" load - load a level");
  328. getConsole().print(" set - set a parameter");
  329. getConsole().print(" bind - bind a keyboard/mouse action");
  330. getConsole().print(" sshot - make a screenshot");
  331. getConsole().print(" move - [walk|fly|noclip]");
  332. getConsole().print(" sound - INT - Test play sound");
  333. getConsole().print(" mode - MODE - Render mode");
  334. getConsole().print(" animate - [BOOL|n|p] - Animate models");
  335. getConsole().print(" light - BOOL - GL Lights");
  336. getConsole().print(" fog - BOOL - GL Fog");
  337. getConsole().print(" hop - BOOL - Room hop");
  338. getConsole().print(" viewmodel - INT - Change Laras model");
  339. getConsole().print(" help - print command help");
  340. getConsole().print(" quit - exit OpenRaider");
  341. getConsole().print("Use help COMMAND to get additional info");
  342. } else if (args->size() == 1) {
  343. return help(args->at(0));
  344. } else {
  345. getConsole().print("Invalid use of help-command");
  346. return -21;
  347. }
  348. } else {
  349. getConsole().print("Unknown command: %s ", command);
  350. return -22;
  351. }
  352. return 0;
  353. }
  354. int OpenRaider::help(const char *cmd) {
  355. assert(cmd != NULL);
  356. assert(cmd[0] != '\0');
  357. if (strcmp(cmd, "set") == 0) {
  358. getConsole().print("set-Command Usage:");
  359. getConsole().print(" set VAR VAL");
  360. getConsole().print("Available Variables:");
  361. getConsole().print(" basedir STRING");
  362. getConsole().print(" pakdir STRING");
  363. getConsole().print(" audiodir STRING");
  364. getConsole().print(" datadir STRING");
  365. getConsole().print(" font STRING");
  366. getConsole().print(" gldriver STRING");
  367. getConsole().print(" size \"INTxINT\"");
  368. getConsole().print(" fullscreen BOOL");
  369. getConsole().print(" audio BOOL");
  370. getConsole().print(" volume BOOL");
  371. getConsole().print(" mouse_x FLOAT");
  372. getConsole().print(" mouse_y FLOAT");
  373. getConsole().print(" fps BOOL");
  374. getConsole().print("Enclose STRINGs with \"\"!");
  375. getConsole().print("size expects a STRING in the specified format");
  376. } else if (strcmp(cmd, "bind") == 0) {
  377. getConsole().print("bind-Command Usage:");
  378. getConsole().print(" bind ACTION KEY");
  379. getConsole().print("Available Actions:");
  380. getConsole().print(" menu");
  381. getConsole().print(" console");
  382. getConsole().print(" forward");
  383. getConsole().print(" backward");
  384. getConsole().print(" left");
  385. getConsole().print(" right");
  386. getConsole().print(" jump");
  387. getConsole().print(" crouch");
  388. getConsole().print(" use");
  389. getConsole().print(" holster");
  390. getConsole().print("Key-Format:");
  391. getConsole().print(" 'a' or '1' for character/number keys");
  392. getConsole().print(" \"leftctrl\" for symbols and special keys");
  393. } else if (strcmp(cmd, "load") == 0) {
  394. getConsole().print("load-Command Usage:");
  395. getConsole().print(" load levelfile.name");
  396. } else if (strcmp(cmd, "game") == 0) {
  397. getConsole().print("Use \"game help\" for more info");
  398. } else if (strcmp(cmd, "sshot") == 0) {
  399. getConsole().print("sshot-Command Usage:");
  400. getConsole().print(" sshot [console|menu]");
  401. getConsole().print("Add console/menu to capture them too");
  402. } else if (strcmp(cmd, "sound") == 0) {
  403. getConsole().print("sound-Command Usage:");
  404. getConsole().print(" sound INT");
  405. getConsole().print("Where INT is a valid sound ID integer");
  406. } else if (strcmp(cmd, "move") == 0) {
  407. getConsole().print("move-Command Usage:");
  408. getConsole().print(" move COMMAND");
  409. getConsole().print("Where COMMAND is one of the following:");
  410. getConsole().print(" walk");
  411. getConsole().print(" fly");
  412. getConsole().print(" noclip");
  413. } else if (strcmp(cmd, "mode") == 0) {
  414. getConsole().print("mode-Command Usage:");
  415. getConsole().print(" mode MODE");
  416. getConsole().print("Where MODE is one of the following:");
  417. getConsole().print(" wireframe");
  418. getConsole().print(" solid");
  419. getConsole().print(" texture");
  420. getConsole().print(" vertexlight");
  421. getConsole().print(" titlescreen");
  422. } else if (strcmp(cmd, "animate") == 0) {
  423. getConsole().print("animate-Command Usage:");
  424. getConsole().print(" animate [n|p|BOOL]");
  425. getConsole().print("Where the commands have the following meaning:");
  426. getConsole().print(" BOOL to (de)activate animating all models");
  427. getConsole().print(" n to step all models to their next animation");
  428. getConsole().print(" p to step all models to their previous animation");
  429. } else {
  430. getConsole().print("No help available for %s", cmd);
  431. return -1;
  432. }
  433. return 0;
  434. }
  435. char *OpenRaider::expandDirectoryNames(const char *s) {
  436. const char *base = "$(basedir)";
  437. const char *pak = "$(pakdir)";
  438. const char *audio = "$(audiodir)";
  439. const char *data = "$(datadir)";
  440. assert(s != NULL);
  441. assert(s[0] != '\0');
  442. if (mBaseDir != NULL) {
  443. if (strstr(s, base) != NULL) {
  444. return stringReplace(s, base, mBaseDir);
  445. }
  446. }
  447. if (mPakDir != NULL) {
  448. if (strstr(s, pak) != NULL) {
  449. return stringReplace(s, pak, mPakDir);
  450. }
  451. }
  452. if (mAudioDir != NULL) {
  453. if (strstr(s, audio) != NULL) {
  454. return stringReplace(s, audio, mAudioDir);
  455. }
  456. }
  457. if (mDataDir != NULL) {
  458. if (strstr(s, data) != NULL) {
  459. return stringReplace(s, data, mDataDir);
  460. }
  461. }
  462. return NULL;
  463. }
  464. #define CHANGE_DIR_WITH_EXPANSION(a) do { \
  465. char *quotes = stringRemoveQuotes(value); \
  466. char *tmp = expandDirectoryNames(quotes); \
  467. if (tmp == NULL) { \
  468. a = fullPath(quotes, 0); \
  469. } else { \
  470. a = fullPath(tmp, 0); \
  471. delete [] tmp; \
  472. } \
  473. delete [] quotes; \
  474. } while(false)
  475. int OpenRaider::set(const char *var, const char *value) {
  476. assert(var != NULL);
  477. assert(var[0] != '\0');
  478. assert(value != NULL);
  479. assert(value[0] != '\0');
  480. if (strcmp(var, "size") == 0) {
  481. // value has format like "\"1024x768\""
  482. unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
  483. if (sscanf(value, "\"%dx%d\"", &w, &h) != 2) {
  484. getConsole().print("set-size-Error: Invalid value (%s)", value);
  485. return -2;
  486. }
  487. getWindow().setSize(w, h);
  488. } else if (strcmp(var, "fullscreen") == 0) {
  489. bool fullscreen = false;
  490. if (readBool(value, &fullscreen) != 0) {
  491. getConsole().print("set-fullscreen-Error: Invalid value (%s)", value);
  492. return -3;
  493. }
  494. getWindow().setFullscreen(fullscreen);
  495. } else if (strcmp(var, "gldriver") == 0) {
  496. getWindow().setDriver(value);
  497. } else if (strcmp(var, "audio") == 0) {
  498. bool audio = false;
  499. if (readBool(value, &audio) != 0) {
  500. getConsole().print("set-audio-Error: Invalid value (%s)", value);
  501. return -4;
  502. }
  503. getSound().setEnabled(audio);
  504. } else if (strcmp(var, "volume") == 0) {
  505. float vol = 1.0f;
  506. if (sscanf(value, "%f", &vol) != 1) {
  507. getConsole().print("set-volume-Error: Invalid value (%s)", value);
  508. return -5;
  509. }
  510. getSound().setVolume(vol);
  511. } else if (strcmp(var, "mouse_x") == 0) {
  512. float sense = 1.0f;
  513. if (sscanf(value, "%f", &sense) != 1) {
  514. getConsole().print("set-mouse_x-Error: Invalid value (%s)", value);
  515. return -6;
  516. }
  517. mCameraRotationDeltaX = OR_DEG_TO_RAD(sense);
  518. } else if (strcmp(var, "mouse_y") == 0) {
  519. float sense = 1.0f;
  520. if (sscanf(value, "%f", &sense) != 1) {
  521. getConsole().print("set-mouse_y-Error: Invalid value (%s)", value);
  522. return -7;
  523. }
  524. mCameraRotationDeltaY = OR_DEG_TO_RAD(sense);
  525. } else if (strcmp(var, "fps") == 0) {
  526. bool fps = false;
  527. if (readBool(value, &fps) != 0) {
  528. getConsole().print("set-fps-Error: Invalid value (%s)", value);
  529. return -8;
  530. }
  531. mFPS = fps;
  532. } else if (strcmp(var, "basedir") == 0) {
  533. CHANGE_DIR_WITH_EXPANSION(mBaseDir);
  534. } else if (strcmp(var, "pakdir") == 0) {
  535. CHANGE_DIR_WITH_EXPANSION(mPakDir);
  536. } else if (strcmp(var, "audiodir") == 0) {
  537. CHANGE_DIR_WITH_EXPANSION(mAudioDir);
  538. } else if (strcmp(var, "datadir") == 0) {
  539. CHANGE_DIR_WITH_EXPANSION(mDataDir);
  540. } else if (strcmp(var, "font") == 0) {
  541. char *quotes = stringReplace(value, "\"", "");
  542. char *tmp = expandDirectoryNames(quotes);
  543. if (tmp == NULL) {
  544. getWindow().setFont(quotes);
  545. } else {
  546. getWindow().setFont(tmp);
  547. delete [] tmp;
  548. }
  549. delete [] quotes;
  550. } else {
  551. getConsole().print("set-Error: Unknown variable (%s = %s)", var, value);
  552. return -1;
  553. }
  554. return 0;
  555. }
  556. int OpenRaider::bind(const char *action, const char *key) {
  557. assert(action != NULL);
  558. assert(action[0] != '\0');
  559. assert(key != NULL);
  560. assert(key[0] != '\0');
  561. const char *tmp = action;
  562. if (action[0] == '+')
  563. tmp++;
  564. if (strcmp(tmp, "menu") == 0) {
  565. return bind(menuAction, key);
  566. } else if (strcmp(tmp, "console") == 0) {
  567. return bind(consoleAction, key);
  568. } else if (strcmp(tmp, "forward") == 0) {
  569. return bind(forwardAction, key);
  570. } else if (strcmp(tmp, "backward") == 0) {
  571. return bind(backwardAction, key);
  572. } else if (strcmp(tmp, "left") == 0) {
  573. return bind(leftAction, key);
  574. } else if (strcmp(tmp, "right") == 0) {
  575. return bind(rightAction, key);
  576. } else if (strcmp(tmp, "jump") == 0) {
  577. return bind(jumpAction, key);
  578. } else if (strcmp(tmp, "crouch") == 0) {
  579. return bind(crouchAction, key);
  580. } else if (strcmp(tmp, "use") == 0) {
  581. return bind(useAction, key);
  582. } else if (strcmp(tmp, "holster") == 0) {
  583. return bind(holsterAction, key);
  584. } else {
  585. getConsole().print("bind-Error: Unknown action (%s --> %s)", key, action);
  586. return -1;
  587. }
  588. }
  589. int OpenRaider::bind(ActionEvents action, const char *key) {
  590. assert(action < ActionEventCount);
  591. assert(key != NULL);
  592. assert(key[0] != '\0');
  593. size_t length = strlen(key);
  594. if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
  595. // Literal character like w, a, s, d, 0, 1...
  596. char c = key[1];
  597. if (((c >= '0') && (c <= '9'))
  598. || ((c >= 'a') && (c <= 'z'))) {
  599. keyBindings[action] = (KeyboardButton)c;
  600. } else {
  601. getConsole().print("bind-\'\'-Error: Unknown key (%s)", key);
  602. return -1;
  603. }
  604. } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
  605. // Special characters like tilde, esc, quote...
  606. char *tmp = stringRemoveQuotes(key);
  607. if (strcmp(tmp, "quote") == 0) {
  608. keyBindings[action] = quote;
  609. } else if (strcmp(tmp, "backslash") == 0) {
  610. keyBindings[action] = quote;
  611. } else if (strcmp(tmp, "backspace") == 0) {
  612. keyBindings[action] = backspace;
  613. } else if (strcmp(tmp, "capslock") == 0) {
  614. keyBindings[action] = capslock;
  615. } else if (strcmp(tmp, "comma") == 0) {
  616. keyBindings[action] = comma;
  617. } else if (strcmp(tmp, "del") == 0) {
  618. keyBindings[action] = del;
  619. } else if (strcmp(tmp, "up") == 0) {
  620. keyBindings[action] = up;
  621. } else if (strcmp(tmp, "down") == 0) {
  622. keyBindings[action] = down;
  623. } else if (strcmp(tmp, "left") == 0) {
  624. keyBindings[action] = left;
  625. } else if (strcmp(tmp, "right") == 0) {
  626. keyBindings[action] = right;
  627. } else if (strcmp(tmp, "end") == 0) {
  628. keyBindings[action] = end;
  629. } else if (strcmp(tmp, "equals") == 0) {
  630. keyBindings[action] = equals;
  631. } else if (strcmp(tmp, "escape") == 0) {
  632. keyBindings[action] = escape;
  633. } else if (strcmp(tmp, "f1") == 0) {
  634. keyBindings[action] = f1;
  635. } else if (strcmp(tmp, "f2") == 0) {
  636. keyBindings[action] = f2;
  637. } else if (strcmp(tmp, "f3") == 0) {
  638. keyBindings[action] = f3;
  639. } else if (strcmp(tmp, "f4") == 0) {
  640. keyBindings[action] = f4;
  641. } else if (strcmp(tmp, "f5") == 0) {
  642. keyBindings[action] = f5;
  643. } else if (strcmp(tmp, "f6") == 0) {
  644. keyBindings[action] = f6;
  645. } else if (strcmp(tmp, "f7") == 0) {
  646. keyBindings[action] = f7;
  647. } else if (strcmp(tmp, "f8") == 0) {
  648. keyBindings[action] = f8;
  649. } else if (strcmp(tmp, "f9") == 0) {
  650. keyBindings[action] = f9;
  651. } else if (strcmp(tmp, "f10") == 0) {
  652. keyBindings[action] = f10;
  653. } else if (strcmp(tmp, "f11") == 0) {
  654. keyBindings[action] = f11;
  655. } else if (strcmp(tmp, "f12") == 0) {
  656. keyBindings[action] = f12;
  657. } else if (strcmp(tmp, "backquote") == 0) {
  658. keyBindings[action] = backquote;
  659. } else if (strcmp(tmp, "home") == 0) {
  660. keyBindings[action] = home;
  661. } else if (strcmp(tmp, "insert") == 0) {
  662. keyBindings[action] = insert;
  663. } else if (strcmp(tmp, "leftalt") == 0) {
  664. keyBindings[action] = leftalt;
  665. } else if (strcmp(tmp, "leftctrl") == 0) {
  666. keyBindings[action] = leftctrl;
  667. } else if (strcmp(tmp, "leftbracket") == 0) {
  668. keyBindings[action] = leftbracket;
  669. } else if (strcmp(tmp, "leftgui") == 0) {
  670. keyBindings[action] = leftgui;
  671. } else if (strcmp(tmp, "leftshift") == 0) {
  672. keyBindings[action] = leftshift;
  673. } else if (strcmp(tmp, "minus") == 0) {
  674. keyBindings[action] = minus;
  675. } else if (strcmp(tmp, "numlock") == 0) {
  676. keyBindings[action] = numlock;
  677. } else if (strcmp(tmp, "pagedown") == 0) {
  678. keyBindings[action] = pagedown;
  679. } else if (strcmp(tmp, "pageup") == 0) {
  680. keyBindings[action] = pageup;
  681. } else if (strcmp(tmp, "pause") == 0) {
  682. keyBindings[action] = pause;
  683. } else if (strcmp(tmp, "dot") == 0) {
  684. keyBindings[action] = dot;
  685. } else if (strcmp(tmp, "rightalt") == 0) {
  686. keyBindings[action] = rightalt;
  687. } else if (strcmp(tmp, "rightctrl") == 0) {
  688. keyBindings[action] = rightctrl;
  689. } else if (strcmp(tmp, "enter") == 0) {
  690. keyBindings[action] = enter;
  691. } else if (strcmp(tmp, "rightgui") == 0) {
  692. keyBindings[action] = rightgui;
  693. } else if (strcmp(tmp, "rightbracket") == 0) {
  694. keyBindings[action] = rightbracket;
  695. } else if (strcmp(tmp, "rightshift") == 0) {
  696. keyBindings[action] = rightshift;
  697. } else if (strcmp(tmp, "scrolllock") == 0) {
  698. keyBindings[action] = scrolllock;
  699. } else if (strcmp(tmp, "semicolon") == 0) {
  700. keyBindings[action] = semicolon;
  701. } else if (strcmp(tmp, "slash") == 0) {
  702. keyBindings[action] = slash;
  703. } else if (strcmp(tmp, "space") == 0) {
  704. keyBindings[action] = space;
  705. } else if (strcmp(tmp, "tab") == 0) {
  706. keyBindings[action] = tab;
  707. } else if (strcmp(tmp, "leftmouse") == 0) {
  708. keyBindings[action] = leftmouse;
  709. } else if (strcmp(tmp, "middlemouse") == 0) {
  710. keyBindings[action] = middlemouse;
  711. } else if (strcmp(tmp, "rightmouse") == 0) {
  712. keyBindings[action] = rightmouse;
  713. } else {
  714. getConsole().print("bind-\"\"-Error: Unknown key (%s)", key);
  715. delete [] tmp;
  716. return -2;
  717. }
  718. delete [] tmp;
  719. } else {
  720. getConsole().print("bind-Error: Unknown key (%s)", key);
  721. return -3;
  722. }
  723. return 0;
  724. }
  725. void OpenRaider::loadPakFolderRecursive(const char *dir) {
  726. struct dirent *ep;
  727. DIR *pakDir;
  728. assert(dir != NULL);
  729. assert(dir[0] != '\0');
  730. assert(mRunning == true);
  731. pakDir = opendir(dir);
  732. if (pakDir != NULL) {
  733. while ((ep = readdir(pakDir)) != NULL) {
  734. if (ep->d_type == DT_DIR) {
  735. if ((strcmp(".", ep->d_name) != 0)
  736. && (strcmp("..", ep->d_name) != 0)) {
  737. char *tmp = bufferString("%s%s", dir, ep->d_name);
  738. char *next = fullPath(tmp, '/');
  739. loadPakFolderRecursive(next);
  740. delete next;
  741. delete tmp;
  742. }
  743. } else {
  744. char *fullPathMap = bufferString("%s%s", dir, ep->d_name);
  745. char *lowerPath = bufferString("%s", fullPathMap);
  746. for (char *p = lowerPath; *p; ++p) *p = (char)tolower(*p);
  747. // Check for valid extension
  748. if (stringEndsWith(lowerPath, ".phd")
  749. || stringEndsWith(lowerPath, ".tr2")
  750. || stringEndsWith(lowerPath, ".tr4")
  751. || stringEndsWith(lowerPath, ".trc")) {
  752. int error = TombRaider::checkMime(fullPathMap);
  753. if (error == 0) {
  754. // Just load relative filename
  755. mMapList.push_back(bufferString("%s", (fullPathMap + strlen(mPakDir) + 1)));
  756. } else {
  757. getConsole().print("Error: pak file '%s' %s",
  758. fullPathMap, (error == -1) ? "not found" : "invalid");
  759. }
  760. }
  761. delete [] lowerPath;
  762. delete [] fullPathMap;
  763. }
  764. }
  765. closedir(pakDir);
  766. } else {
  767. getConsole().print("Could not open PAK dir %s!", dir);
  768. }
  769. }
  770. void OpenRaider::fillMapList() {
  771. assert(mRunning == true);
  772. char *tmp = fullPath(mPakDir, '/');
  773. loadPakFolderRecursive(tmp);
  774. delete [] tmp;
  775. mMapListFilled = true;
  776. }
  777. void OpenRaider::run() {
  778. assert(mRunning == false);
  779. mRunning = true;
  780. while (mRunning) {
  781. frame();
  782. }
  783. }
  784. void OpenRaider::frame() {
  785. assert(mRunning == true);
  786. static clock_t fpsSum = 0, fpsCount = 0;
  787. static int fps = 0;
  788. clock_t startTime = systemTimerGet();
  789. // Get keyboard and mouse input
  790. getWindow().eventHandling();
  791. // Clear screen
  792. glClearColor(0.00f, 0.00f, 0.00f, 1.0f);
  793. glClear(GL_COLOR_BUFFER_BIT);
  794. // Draw game scene
  795. getRender().display();
  796. // Draw 2D overlays (console and menu)
  797. getWindow().glEnter2D();
  798. getConsole().display();
  799. getMenu().display();
  800. // Draw FPS counter
  801. if (mFPS)
  802. getWindow().drawText(10, getWindow().mHeight - 20, 0.5f, OR_BLUE, "%dFPS", fps);
  803. getWindow().glExit2D();
  804. // Put new frame on screen
  805. getWindow().swapBuffersGL();
  806. // Fill map list after first render pass,
  807. // so menu *loading screen* is visible
  808. if (!mMapListFilled)
  809. fillMapList();
  810. // Calculate FPS display value
  811. fpsCount++;
  812. fpsSum += (systemTimerGet() - startTime);
  813. if (fpsSum >= 500) {
  814. // Update every 500ms
  815. fps = (int)((float)fpsCount * (1000.0f / (float)fpsSum));
  816. fpsCount = fpsSum = 0;
  817. }
  818. }
  819. void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
  820. assert(key < unknown);
  821. assert(mRunning == true);
  822. if ((keyBindings[menuAction] == key) && pressed) {
  823. getMenu().setVisible(!getMenu().isVisible());
  824. } else if (!getMenu().isVisible()) {
  825. if ((keyBindings[consoleAction] == key) && pressed) {
  826. getConsole().setVisible(!getConsole().isVisible());
  827. } else if (!getConsole().isVisible()) {
  828. for (int i = forwardAction; i < ActionEventCount; i++) {
  829. if (keyBindings[i] == key) {
  830. getGame().handleAction((ActionEvents)i, pressed);
  831. }
  832. }
  833. } else {
  834. getConsole().handleKeyboard(key, pressed);
  835. }
  836. } else {
  837. getMenu().handleKeyboard(key, pressed);
  838. }
  839. getWindow().setMousegrab(!(getMenu().isVisible() || getConsole().isVisible()));
  840. }
  841. void OpenRaider::handleText(char *text, bool notFinished) {
  842. assert(text != NULL);
  843. assert(text[0] != '\0');
  844. assert(mRunning == true);
  845. if ((getConsole().isVisible()) && (!getMenu().isVisible())) {
  846. getConsole().handleText(text, notFinished);
  847. }
  848. }
  849. void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  850. assert(button < unknown);
  851. assert(mRunning == true);
  852. if (getMenu().isVisible()) {
  853. getMenu().handleMouseClick(x, y, button, released);
  854. } else if (!getConsole().isVisible()) {
  855. for (int i = forwardAction; i < ActionEventCount; i++) {
  856. if (keyBindings[i] == button) {
  857. getGame().handleAction((ActionEvents)i, !released);
  858. }
  859. }
  860. }
  861. }
  862. void OpenRaider::handleMouseMotion(int xrel, int yrel) {
  863. assert((xrel != 0) || (yrel != 0));
  864. assert(mRunning == true);
  865. if ((!getConsole().isVisible()) && (!getMenu().isVisible())) {
  866. getGame().handleMouseMotion(xrel, yrel);
  867. }
  868. }
  869. void OpenRaider::handleMouseScroll(int xrel, int yrel) {
  870. assert((xrel != 0) || (yrel != 0));
  871. assert(mRunning == true);
  872. if ((getConsole().isVisible()) && (!getMenu().isVisible())) {
  873. getConsole().handleMouseScroll(xrel, yrel);
  874. }
  875. }