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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 "math/math.h"
  16. #include "Menu.h"
  17. #include "Sound.h"
  18. #include "TombRaider.h"
  19. #include "Window.h"
  20. #include "utils/strings.h"
  21. #include "utils/time.h"
  22. #include "OpenRaider.h"
  23. #define MAX_MS_PER_FRAME (1000 / MAXIMUM_FPS)
  24. OpenRaider::OpenRaider() {
  25. mInit = false;
  26. mRunning = false;
  27. mBaseDir = NULL;
  28. mPakDir = NULL;
  29. mAudioDir = NULL;
  30. mDataDir = NULL;
  31. mMapListFilled = false;
  32. mMenu = new Menu();
  33. mConsole = new Console();
  34. mSound = new Sound();
  35. mWindow = new WindowSDL();
  36. mGame = NULL;
  37. for (int i = 0; i < ActionEventCount; i++)
  38. keyBindings[i] = unknown;
  39. mCameraRotationDeltaX = OR_DEG_TO_RAD(1.0f);
  40. mCameraRotationDeltaY = OR_DEG_TO_RAD(1.0f);
  41. }
  42. OpenRaider::~OpenRaider() {
  43. if (mGame)
  44. delete mGame;
  45. if (mMenu)
  46. delete mMenu;
  47. if (mConsole)
  48. delete mConsole;
  49. if (mSound)
  50. delete mSound;
  51. if (mWindow)
  52. delete mWindow;
  53. if (mBaseDir)
  54. delete mBaseDir;
  55. if (mPakDir)
  56. delete mPakDir;
  57. if (mAudioDir)
  58. delete mAudioDir;
  59. if (mDataDir)
  60. delete mDataDir;
  61. while (mMapList.size() > 0) {
  62. delete [] mMapList.back();
  63. mMapList.pop_back();
  64. }
  65. }
  66. int OpenRaider::loadConfig(const char *config) {
  67. assert(config != NULL);
  68. assert(config[0] != '\0');
  69. char *configFile = fullPath(config, 0);
  70. mConsole->print("Loading config from \"%s\"...", configFile);
  71. FILE *f = fopen(configFile, "r");
  72. if (f == NULL) {
  73. mConsole->print("Could not open file!");
  74. return -1;
  75. }
  76. char buffer[256];
  77. while (fgets(buffer, 256, f) != NULL) {
  78. int error = command(buffer);
  79. if (error != 0) {
  80. mConsole->print("Error Code: %d", error);
  81. }
  82. }
  83. fclose(f);
  84. return 0;
  85. }
  86. int OpenRaider::command(const char *command) {
  87. assert(command != NULL);
  88. assert(command[0] != '\0');
  89. int returnValue = 0;
  90. char *cmd = bufferString("%s", command);
  91. size_t length = strlen(cmd);
  92. // Command ends at '\n' or # when a comment begins
  93. for (size_t i = 0; i < length; i++) {
  94. if (cmd[i] == '\n' || cmd[i] == '#') {
  95. cmd[i] = '\0';
  96. break;
  97. }
  98. }
  99. char *token = strtok(cmd, " \t");
  100. if (token != NULL) {
  101. // token is the command to execute
  102. // get arguments
  103. std::vector<char *> args;
  104. char *next;
  105. while ((next = strtok(NULL, " \t")) != NULL) {
  106. args.push_back(next);
  107. }
  108. // Execute
  109. returnValue = this->command(token, &args);
  110. }
  111. free(cmd);
  112. return returnValue;
  113. }
  114. int OpenRaider::command(const char *command, std::vector<char *> *args) {
  115. assert(command != NULL);
  116. assert(command[0] != '\0');
  117. assert(args != NULL);
  118. if (strcmp(command, "set") == 0) {
  119. if (args->size() != 2) {
  120. mConsole->print("Invalid use of set-command");
  121. return -2;
  122. } else {
  123. return set(args->at(0), args->at(1));
  124. }
  125. } else if (strcmp(command, "bind") == 0) {
  126. if (args->size() != 2) {
  127. mConsole->print("Invalid use of bind-command");
  128. return -3;
  129. } else {
  130. return bind(args->at(0), args->at(1));
  131. }
  132. } else if (strcmp(command, "quit") == 0) {
  133. exit(0);
  134. } else if (strcmp(command, "help") == 0) {
  135. if (args->size() == 0) {
  136. mConsole->print("Available commands:");
  137. mConsole->print(" load");
  138. mConsole->print(" set");
  139. mConsole->print(" bind");
  140. mConsole->print(" help");
  141. mConsole->print(" quit");
  142. mConsole->print("Use help COMMAND to get additional info");
  143. } else if (args->size() == 1) {
  144. return help(args->at(0));
  145. } else {
  146. mConsole->print("Invalid use of help-command");
  147. return -4;
  148. }
  149. } else if (strcmp(command, "load") == 0) {
  150. if (mGame)
  151. delete mGame;
  152. char *tmp = bufferString("%s/%s", mPakDir, args->at(0));
  153. try {
  154. mGame = new Game(tmp);
  155. delete [] tmp;
  156. return 0;
  157. } catch (int error) {
  158. delete [] tmp;
  159. return error;
  160. }
  161. } else {
  162. mConsole->print("Unknown command: %s ", command);
  163. return -1;
  164. }
  165. return 0;
  166. }
  167. int OpenRaider::help(const char *cmd) {
  168. if (strcmp(cmd, "set") == 0) {
  169. mConsole->print("set-Command Usage:");
  170. mConsole->print(" set VAR VAL");
  171. mConsole->print("Available Variables:");
  172. mConsole->print(" basedir STRING");
  173. mConsole->print(" pakdir STRING");
  174. mConsole->print(" audiodir STRING");
  175. mConsole->print(" datadir STRING");
  176. mConsole->print(" font STRING");
  177. mConsole->print(" gldriver STRING");
  178. mConsole->print(" size \"INTxINT\"");
  179. mConsole->print(" fullscreen BOOL");
  180. mConsole->print(" audio BOOL");
  181. mConsole->print(" volume BOOL");
  182. mConsole->print(" mouse_x FLOAT");
  183. mConsole->print(" mouse_y FLOAT");
  184. mConsole->print("Enclose STRINGs with \"\"!");
  185. mConsole->print("size expects a STRING in the specified format");
  186. } else if (strcmp(cmd, "bind") == 0) {
  187. mConsole->print("bind-Command Usage:");
  188. mConsole->print(" bind ACTION KEY");
  189. mConsole->print("Available Actions:");
  190. mConsole->print(" menu");
  191. mConsole->print(" console");
  192. mConsole->print(" forward");
  193. mConsole->print(" backward");
  194. mConsole->print(" left");
  195. mConsole->print(" right");
  196. mConsole->print(" jump");
  197. mConsole->print(" crouch");
  198. mConsole->print(" use");
  199. mConsole->print(" holster");
  200. mConsole->print("Key-Format:");
  201. mConsole->print(" 'a' or '1' for character/number keys");
  202. mConsole->print(" \"leftctrl\" for symbols and special keys");
  203. } else if (strcmp(cmd, "load") == 0) {
  204. mConsole->print("load-Command Usage:");
  205. mConsole->print(" load levelfile.name");
  206. } else {
  207. mConsole->print("No help available for %s", cmd);
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. char *OpenRaider::expandDirectoryNames(const char *s) {
  213. const char *base = "$(basedir)";
  214. const char *pak = "$(pakdir)";
  215. const char *audio = "$(audiodir)";
  216. const char *data = "$(datadir)";
  217. if (mBaseDir != NULL) {
  218. if (strstr(s, base) != NULL) {
  219. return stringReplace(s, base, mBaseDir);
  220. }
  221. }
  222. if (mPakDir != NULL) {
  223. if (strstr(s, pak) != NULL) {
  224. return stringReplace(s, pak, mPakDir);
  225. }
  226. }
  227. if (mAudioDir != NULL) {
  228. if (strstr(s, audio) != NULL) {
  229. return stringReplace(s, audio, mAudioDir);
  230. }
  231. }
  232. if (mDataDir != NULL) {
  233. if (strstr(s, data) != NULL) {
  234. return stringReplace(s, data, mDataDir);
  235. }
  236. }
  237. return NULL;
  238. }
  239. #define CHANGE_DIR_WITH_EXPANSION(a) do { \
  240. char *quotes = stringRemoveQuotes(value); \
  241. char *tmp = expandDirectoryNames(quotes); \
  242. if (tmp == NULL) { \
  243. a = fullPath(quotes, 0); \
  244. } else { \
  245. a = fullPath(tmp, 0); \
  246. delete [] tmp; \
  247. } \
  248. delete [] quotes; \
  249. } while(false)
  250. int OpenRaider::set(const char *var, const char *value) {
  251. if (strcmp(var, "size") == 0) {
  252. // value has format like "\"1024x768\""
  253. unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
  254. if (sscanf(value, "\"%dx%d\"", &w, &h) != 2) {
  255. mConsole->print("set-size-Error: Invalid value (%s)", value);
  256. return -2;
  257. }
  258. mWindow->setSize(w, h);
  259. } else if (strcmp(var, "fullscreen") == 0) {
  260. bool fullscreen = false;
  261. if (readBool(value, &fullscreen) != 0) {
  262. mConsole->print("set-fullscreen-Error: Invalid value (%s)", value);
  263. return -3;
  264. }
  265. mWindow->setFullscreen(fullscreen);
  266. } else if (strcmp(var, "gldriver") == 0) {
  267. mWindow->setDriver(value);
  268. } else if (strcmp(var, "audio") == 0) {
  269. bool audio = false;
  270. if (readBool(value, &audio) != 0) {
  271. mConsole->print("set-audio-Error: Invalid value (%s)", value);
  272. return -4;
  273. }
  274. mSound->setEnabled(audio);
  275. } else if (strcmp(var, "volume") == 0) {
  276. float vol = 1.0f;
  277. if (sscanf(value, "%f", &vol) != 1) {
  278. mConsole->print("set-volume-Error: Invalid value (%s)", value);
  279. return -5;
  280. }
  281. mSound->setVolume(vol);
  282. } else if (strcmp(var, "mouse_x") == 0) {
  283. float sense = 1.0f;
  284. if (sscanf(value, "%f", &sense) != 1) {
  285. mConsole->print("set-mouse_x-Error: Invalid value (%s)", value);
  286. return -6;
  287. }
  288. mCameraRotationDeltaX = OR_DEG_TO_RAD(sense);
  289. } else if (strcmp(var, "mouse_y") == 0) {
  290. float sense = 1.0f;
  291. if (sscanf(value, "%f", &sense) != 1) {
  292. mConsole->print("set-mouse_y-Error: Invalid value (%s)", value);
  293. return -7;
  294. }
  295. mCameraRotationDeltaY = OR_DEG_TO_RAD(sense);
  296. } else if (strcmp(var, "basedir") == 0) {
  297. CHANGE_DIR_WITH_EXPANSION(mBaseDir);
  298. } else if (strcmp(var, "pakdir") == 0) {
  299. CHANGE_DIR_WITH_EXPANSION(mPakDir);
  300. } else if (strcmp(var, "audiodir") == 0) {
  301. CHANGE_DIR_WITH_EXPANSION(mAudioDir);
  302. } else if (strcmp(var, "datadir") == 0) {
  303. CHANGE_DIR_WITH_EXPANSION(mDataDir);
  304. } else if (strcmp(var, "font") == 0) {
  305. char *quotes = stringReplace(value, "\"", "");
  306. char *tmp = expandDirectoryNames(quotes);
  307. if (tmp == NULL) {
  308. mWindow->setFont(quotes);
  309. } else {
  310. mWindow->setFont(tmp);
  311. delete [] tmp;
  312. }
  313. delete [] quotes;
  314. } else {
  315. mConsole->print("set-Error: Unknown variable (%s = %s)", var, value);
  316. return -1;
  317. }
  318. return 0;
  319. }
  320. int OpenRaider::bind(const char *action, const char *key) {
  321. const char *tmp = action;
  322. if (action[0] == '+')
  323. tmp++;
  324. if (strcmp(tmp, "menu") == 0) {
  325. return bind(menuAction, key);
  326. } else if (strcmp(tmp, "console") == 0) {
  327. return bind(consoleAction, key);
  328. } else if (strcmp(tmp, "forward") == 0) {
  329. return bind(forwardAction, key);
  330. } else if (strcmp(tmp, "backward") == 0) {
  331. return bind(backwardAction, key);
  332. } else if (strcmp(tmp, "left") == 0) {
  333. return bind(leftAction, key);
  334. } else if (strcmp(tmp, "right") == 0) {
  335. return bind(rightAction, key);
  336. } else if (strcmp(tmp, "jump") == 0) {
  337. return bind(jumpAction, key);
  338. } else if (strcmp(tmp, "crouch") == 0) {
  339. return bind(crouchAction, key);
  340. } else if (strcmp(tmp, "use") == 0) {
  341. return bind(useAction, key);
  342. } else if (strcmp(tmp, "holster") == 0) {
  343. return bind(holsterAction, key);
  344. } else {
  345. mConsole->print("bind-Error: Unknown action (%s --> %s)", key, action);
  346. return -1;
  347. }
  348. }
  349. int OpenRaider::bind(ActionEvents action, const char *key) {
  350. assert(action != ActionEventCount);
  351. assert(key != NULL);
  352. assert(key[0] != '\0');
  353. size_t length = strlen(key);
  354. if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
  355. // Literal character like w, a, s, d, 0, 1...
  356. char c = key[1];
  357. if (((c >= '0') && (c <= '9'))
  358. || ((c >= 'a') && (c <= 'z'))) {
  359. keyBindings[action] = (KeyboardButton)c;
  360. } else {
  361. mConsole->print("bind-\'\'-Error: Unknown key (%s)", key);
  362. return -1;
  363. }
  364. } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
  365. // Special characters like tilde, esc, quote...
  366. char *tmp = stringRemoveQuotes(key);
  367. if (strcmp(tmp, "quote") == 0) {
  368. keyBindings[action] = quote;
  369. } else if (strcmp(tmp, "backslash") == 0) {
  370. keyBindings[action] = quote;
  371. } else if (strcmp(tmp, "backspace") == 0) {
  372. keyBindings[action] = backspace;
  373. } else if (strcmp(tmp, "capslock") == 0) {
  374. keyBindings[action] = capslock;
  375. } else if (strcmp(tmp, "comma") == 0) {
  376. keyBindings[action] = comma;
  377. } else if (strcmp(tmp, "del") == 0) {
  378. keyBindings[action] = del;
  379. } else if (strcmp(tmp, "up") == 0) {
  380. keyBindings[action] = up;
  381. } else if (strcmp(tmp, "down") == 0) {
  382. keyBindings[action] = down;
  383. } else if (strcmp(tmp, "left") == 0) {
  384. keyBindings[action] = left;
  385. } else if (strcmp(tmp, "right") == 0) {
  386. keyBindings[action] = right;
  387. } else if (strcmp(tmp, "end") == 0) {
  388. keyBindings[action] = end;
  389. } else if (strcmp(tmp, "equals") == 0) {
  390. keyBindings[action] = equals;
  391. } else if (strcmp(tmp, "escape") == 0) {
  392. keyBindings[action] = escape;
  393. } else if (strcmp(tmp, "f1") == 0) {
  394. keyBindings[action] = f1;
  395. } else if (strcmp(tmp, "f2") == 0) {
  396. keyBindings[action] = f2;
  397. } else if (strcmp(tmp, "f3") == 0) {
  398. keyBindings[action] = f3;
  399. } else if (strcmp(tmp, "f4") == 0) {
  400. keyBindings[action] = f4;
  401. } else if (strcmp(tmp, "f5") == 0) {
  402. keyBindings[action] = f5;
  403. } else if (strcmp(tmp, "f6") == 0) {
  404. keyBindings[action] = f6;
  405. } else if (strcmp(tmp, "f7") == 0) {
  406. keyBindings[action] = f7;
  407. } else if (strcmp(tmp, "f8") == 0) {
  408. keyBindings[action] = f8;
  409. } else if (strcmp(tmp, "f9") == 0) {
  410. keyBindings[action] = f9;
  411. } else if (strcmp(tmp, "f10") == 0) {
  412. keyBindings[action] = f10;
  413. } else if (strcmp(tmp, "f11") == 0) {
  414. keyBindings[action] = f11;
  415. } else if (strcmp(tmp, "f12") == 0) {
  416. keyBindings[action] = f12;
  417. } else if (strcmp(tmp, "backquote") == 0) {
  418. keyBindings[action] = backquote;
  419. } else if (strcmp(tmp, "home") == 0) {
  420. keyBindings[action] = home;
  421. } else if (strcmp(tmp, "insert") == 0) {
  422. keyBindings[action] = insert;
  423. } else if (strcmp(tmp, "leftalt") == 0) {
  424. keyBindings[action] = leftalt;
  425. } else if (strcmp(tmp, "leftctrl") == 0) {
  426. keyBindings[action] = leftctrl;
  427. } else if (strcmp(tmp, "leftbracket") == 0) {
  428. keyBindings[action] = leftbracket;
  429. } else if (strcmp(tmp, "leftgui") == 0) {
  430. keyBindings[action] = leftgui;
  431. } else if (strcmp(tmp, "leftshift") == 0) {
  432. keyBindings[action] = leftshift;
  433. } else if (strcmp(tmp, "minus") == 0) {
  434. keyBindings[action] = minus;
  435. } else if (strcmp(tmp, "numlock") == 0) {
  436. keyBindings[action] = numlock;
  437. } else if (strcmp(tmp, "pagedown") == 0) {
  438. keyBindings[action] = pagedown;
  439. } else if (strcmp(tmp, "pageup") == 0) {
  440. keyBindings[action] = pageup;
  441. } else if (strcmp(tmp, "pause") == 0) {
  442. keyBindings[action] = pause;
  443. } else if (strcmp(tmp, "dot") == 0) {
  444. keyBindings[action] = dot;
  445. } else if (strcmp(tmp, "rightalt") == 0) {
  446. keyBindings[action] = rightalt;
  447. } else if (strcmp(tmp, "rightctrl") == 0) {
  448. keyBindings[action] = rightctrl;
  449. } else if (strcmp(tmp, "enter") == 0) {
  450. keyBindings[action] = enter;
  451. } else if (strcmp(tmp, "rightgui") == 0) {
  452. keyBindings[action] = rightgui;
  453. } else if (strcmp(tmp, "rightbracket") == 0) {
  454. keyBindings[action] = rightbracket;
  455. } else if (strcmp(tmp, "rightshift") == 0) {
  456. keyBindings[action] = rightshift;
  457. } else if (strcmp(tmp, "scrolllock") == 0) {
  458. keyBindings[action] = scrolllock;
  459. } else if (strcmp(tmp, "semicolon") == 0) {
  460. keyBindings[action] = semicolon;
  461. } else if (strcmp(tmp, "slash") == 0) {
  462. keyBindings[action] = slash;
  463. } else if (strcmp(tmp, "space") == 0) {
  464. keyBindings[action] = space;
  465. } else if (strcmp(tmp, "tab") == 0) {
  466. keyBindings[action] = tab;
  467. } else if (strcmp(tmp, "leftmouse") == 0) {
  468. keyBindings[action] = leftmouse;
  469. } else if (strcmp(tmp, "middlemouse") == 0) {
  470. keyBindings[action] = middlemouse;
  471. } else if (strcmp(tmp, "rightmouse") == 0) {
  472. keyBindings[action] = rightmouse;
  473. } else {
  474. mConsole->print("bind-\"\"-Error: Unknown key (%s)", key);
  475. delete [] tmp;
  476. return -2;
  477. }
  478. delete [] tmp;
  479. } else {
  480. mConsole->print("bind-Error: Unknown key (%s)", key);
  481. return -3;
  482. }
  483. return 0;
  484. }
  485. void OpenRaider::loadPakFolderRecursive(const char *dir) {
  486. struct dirent *ep;
  487. DIR *pakDir;
  488. pakDir = opendir(dir);
  489. if (pakDir != NULL) {
  490. while ((ep = readdir(pakDir)) != NULL) {
  491. if (ep->d_type == DT_DIR) {
  492. if ((strcmp(".", ep->d_name) != 0)
  493. && (strcmp("..", ep->d_name) != 0)) {
  494. char *tmp = bufferString("%s%s", dir, ep->d_name);
  495. char *next = fullPath(tmp, '/');
  496. loadPakFolderRecursive(next);
  497. delete next;
  498. delete tmp;
  499. }
  500. } else {
  501. char *fullPathMap = bufferString("%s%s", dir, ep->d_name);
  502. char *lowerPath = bufferString("%s", fullPathMap);
  503. for (char *p = lowerPath; *p; ++p) *p = (char)tolower(*p);
  504. // Check for valid extension
  505. if (stringEndsWith(lowerPath, ".phd")
  506. || stringEndsWith(lowerPath, ".tr2")
  507. || stringEndsWith(lowerPath, ".tr4")
  508. || stringEndsWith(lowerPath, ".trc")) {
  509. int error = TombRaider::checkMime(fullPathMap);
  510. if (error == 0) {
  511. // Just load relative filename
  512. mMapList.push_back(bufferString("%s", (fullPathMap + strlen(mPakDir) + 1)));
  513. } else {
  514. mConsole->print("Error: pak file '%s' %s",
  515. fullPathMap, (error == -1) ? "not found" : "invalid");
  516. }
  517. }
  518. delete [] lowerPath;
  519. delete [] fullPathMap;
  520. }
  521. }
  522. closedir(pakDir);
  523. } else {
  524. mConsole->print("Could not open PAK dir %s!", dir);
  525. }
  526. }
  527. void OpenRaider::fillMapList() {
  528. char *tmp = fullPath(mPakDir, '/');
  529. loadPakFolderRecursive(tmp);
  530. delete [] tmp;
  531. mMapListFilled = true;
  532. }
  533. int OpenRaider::initialize() {
  534. assert(mInit == false);
  535. assert(mRunning == false);
  536. // Initialize Windowing
  537. if (mWindow->initialize() != 0)
  538. return -1;
  539. // Initialize OpenGL
  540. if (mWindow->initializeGL() != 0)
  541. return -2;
  542. // Initialize window font
  543. if (mWindow->initializeFont() != 0)
  544. return -3;
  545. // Initialize sound
  546. if (mSound->initialize() != 0)
  547. return -4;
  548. mMenu->setVisible(true);
  549. mInit = true;
  550. return 0;
  551. }
  552. void OpenRaider::run() {
  553. assert(mInit == true);
  554. assert(mRunning == false);
  555. mRunning = true;
  556. while (mRunning) {
  557. clock_t startTime = systemTimerGet();
  558. // Get keyboard and mouse input
  559. mWindow->eventHandling();
  560. // Clear screen
  561. glClearColor(0.00f, 0.00f, 0.00f, 1.0f);
  562. glClear(GL_COLOR_BUFFER_BIT);
  563. // Draw game scene
  564. if (mGame)
  565. mGame->display();
  566. // Draw 2D overlays (console and menu)
  567. mWindow->glEnter2D();
  568. mConsole->display();
  569. mMenu->display();
  570. mWindow->glExit2D();
  571. // Put new frame on screen
  572. mWindow->swapBuffersGL();
  573. // Fill map list after first render pass,
  574. // so menu *loading screen* is visible
  575. if (!mMapListFilled)
  576. fillMapList();
  577. // Check time it took to compute the last frame
  578. // and delay for an appropriate amount of time
  579. clock_t stopTime = systemTimerGet();
  580. if (MAX_MS_PER_FRAME > (stopTime - startTime))
  581. mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));
  582. }
  583. }
  584. void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
  585. if ((keyBindings[menuAction] == key) && pressed) {
  586. mMenu->setVisible(!mMenu->isVisible());
  587. } else if (!mMenu->isVisible()) {
  588. if ((keyBindings[consoleAction] == key) && pressed) {
  589. mConsole->setVisible(!mConsole->isVisible());
  590. } else if (!mConsole->isVisible()) {
  591. for (int i = forwardAction; i < ActionEventCount; i++) {
  592. if (keyBindings[i] == key) {
  593. if (mGame)
  594. mGame->handleAction((ActionEvents)i, pressed);
  595. }
  596. }
  597. } else {
  598. mConsole->handleKeyboard(key, pressed);
  599. }
  600. } else {
  601. mMenu->handleKeyboard(key, pressed);
  602. }
  603. }
  604. void OpenRaider::handleText(char *text, bool notFinished) {
  605. if ((mConsole->isVisible()) && (!mMenu->isVisible())) {
  606. mConsole->handleText(text, notFinished);
  607. }
  608. }
  609. void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
  610. if (mMenu->isVisible()) {
  611. mMenu->handleMouseClick(x, y, button, released);
  612. } else if (!mConsole->isVisible()) {
  613. for (int i = forwardAction; i < ActionEventCount; i++) {
  614. if (keyBindings[i] == button) {
  615. if (mGame)
  616. mGame->handleAction((ActionEvents)i, !released);
  617. }
  618. }
  619. }
  620. }
  621. void OpenRaider::handleMouseMotion(int xrel, int yrel) {
  622. if ((!mConsole->isVisible()) && (!mMenu->isVisible())) {
  623. if (mGame)
  624. mGame->handleMouseMotion(xrel, yrel);
  625. }
  626. }
  627. void OpenRaider::handleMouseScroll(int xrel, int yrel) {
  628. if ((mConsole->isVisible()) && (!mMenu->isVisible())) {
  629. mConsole->handleMouseScroll(xrel, yrel);
  630. }
  631. }