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

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