Open Source Tomb Raider Engine
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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