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.

main.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*!
  2. * \file src/main.cpp
  3. * \brief Where main() is
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdlib>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include "global.h"
  11. #include "Camera.h"
  12. #include "Console.h"
  13. #include "Game.h"
  14. #include "Menu.h"
  15. #include "OpenRaider.h"
  16. #include "Render.h"
  17. #include "World.h"
  18. #include "utils/strings.h"
  19. #include "utils/time.h"
  20. #ifdef USING_AL
  21. #include "SoundAL.h"
  22. #else
  23. #include "SoundNull.h"
  24. #endif
  25. #ifdef USING_SDL
  26. #include "WindowSDL.h"
  27. #ifdef USING_TRLE
  28. #include "FontTRLE.h"
  29. #else
  30. #include "FontSDL.h"
  31. #endif
  32. #else
  33. #error No Windowing Library selected!
  34. #endif
  35. Camera gCamera;
  36. Console gConsole;
  37. Game gGame;
  38. Menu gMenu;
  39. OpenRaider gOpenRaider;
  40. Render gRender;
  41. World gWorld;
  42. #ifdef USING_AL
  43. SoundAL gSound;
  44. #else
  45. SoundNull gSound;
  46. #endif
  47. #ifdef USING_SDL
  48. WindowSDL gWindow;
  49. #ifdef USING_TRLE
  50. FontTRLE gFont;
  51. #else
  52. FontSDL gFont;
  53. #endif
  54. #endif
  55. Camera &getCamera() {
  56. return gCamera;
  57. }
  58. Console &getConsole() {
  59. return gConsole;
  60. }
  61. Font &getFont() {
  62. return gFont;
  63. }
  64. Game &getGame() {
  65. return gGame;
  66. }
  67. Menu &getMenu() {
  68. return gMenu;
  69. }
  70. OpenRaider &getOpenRaider() {
  71. return gOpenRaider;
  72. }
  73. Render &getRender() {
  74. return gRender;
  75. }
  76. Sound &getSound() {
  77. return gSound;
  78. }
  79. Window &getWindow() {
  80. return gWindow;
  81. }
  82. World &getWorld() {
  83. return gWorld;
  84. }
  85. void cleanupHandler(void) {
  86. #ifdef DEBUG
  87. printf("\nThanks for testing %s\n", VERSION);
  88. printf("Build date: %s @ %s\n", __DATE__, __TIME__);
  89. printf("Build host: %s\n", BUILD_HOST);
  90. printf("Web site : http://github.com/xythobuz/OpenRaider\n");
  91. printf("Contact : xythobuz@xythobuz.de\n");
  92. #endif
  93. }
  94. int main(int argc, char *argv[]) {
  95. const char *config = NULL;
  96. // Handle arguments
  97. if (argc == 1) {
  98. // Use default rc file path
  99. config = DEFAULT_CONFIG_PATH "/" DEFAULT_CONFIG_FILE;
  100. } else if (argc == 2) {
  101. // Check for command line switches
  102. if ((strcmp("-h", argv[1]) == 0)
  103. || (strcmp("--help", argv[1]) == 0)) {
  104. // Display help text
  105. printf("%s [OPTIONS | /path/to/config]\n"
  106. "Options:\n"
  107. "\t--help\n\t-h\tDisplay this help text\n"
  108. "\t--version\n\t-v\tDisplay version information\n"
  109. "If no options are given, the default config will be loaded from:\n"
  110. "\t" DEFAULT_CONFIG_PATH "/" DEFAULT_CONFIG_FILE "\n", argv[0]);
  111. return 0;
  112. } else if ((strcmp("-v", argv[1]) == 0)
  113. || (strcmp("--version", argv[1]) == 0)) {
  114. // Display version
  115. printf(VERSION "\n");
  116. return 0;
  117. } else {
  118. // Interpret as rc file name
  119. config = argv[1];
  120. }
  121. } else {
  122. printf("Usage:\n%s -h\n", argv[0]);
  123. return 1;
  124. }
  125. #ifdef DEBUG
  126. printf("Initializing %s\n", VERSION);
  127. #endif
  128. atexit(cleanupHandler);
  129. // Try to load a configuration
  130. if (gOpenRaider.loadConfig(config) != 0) {
  131. if (gOpenRaider.loadConfig(DEFAULT_CONFIG_PATH "/" DEFAULT_CONFIG_FILE) != 0) {
  132. if (gOpenRaider.loadConfig(DEFAULT_CONFIG_FILE) != 0) {
  133. printf("Could not find a config file. Aborting...\n");
  134. return 2;
  135. }
  136. }
  137. }
  138. // Initialize everything
  139. int error = gOpenRaider.initialize();
  140. if (error != 0) {
  141. printf("Could not initialize OpenRaider (%d)!\n", error);
  142. return 3;
  143. }
  144. // Enter Main loop
  145. gConsole.print("Starting %s", VERSION);
  146. gOpenRaider.run();
  147. return 0;
  148. }
  149. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS) && (!defined(NDEBUG))
  150. #include <execinfo.h>
  151. void assertImplementation(const char *exp, const char *file, int line) {
  152. const unsigned int maxSize = 128;
  153. void *callstack[maxSize];
  154. int frames = backtrace(callstack, maxSize);
  155. char **strs = backtrace_symbols(callstack, frames);
  156. printf("\nassertion failed:\n");
  157. printf("\t%s\n", exp);
  158. printf("in %s:%d\n\n", file, line);
  159. for (int i = 0; i < frames; i++)
  160. printf("%s\n", strs[i]);
  161. delete [] strs;
  162. abort();
  163. }
  164. #endif
  165. ActionEvents stringToActionEvent(const char *action) {
  166. if (strcmp(action, "menu") == 0) {
  167. return menuAction;
  168. } else if (strcmp(action, "console") == 0) {
  169. return consoleAction;
  170. } else if (strcmp(action, "forward") == 0) {
  171. return forwardAction;
  172. } else if (strcmp(action, "backward") == 0) {
  173. return backwardAction;
  174. } else if (strcmp(action, "left") == 0) {
  175. return leftAction;
  176. } else if (strcmp(action, "right") == 0) {
  177. return rightAction;
  178. } else if (strcmp(action, "jump") == 0) {
  179. return jumpAction;
  180. } else if (strcmp(action, "crouch") == 0) {
  181. return crouchAction;
  182. } else if (strcmp(action, "use") == 0) {
  183. return useAction;
  184. } else if (strcmp(action, "holster") == 0) {
  185. return holsterAction;
  186. } else if (strcmp(action, "walk") == 0) {
  187. return walkAction;
  188. } else {
  189. return ActionEventCount;
  190. }
  191. }
  192. KeyboardButton stringToKeyboardButton(const char *key) {
  193. size_t length = strlen(key);
  194. if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
  195. // Literal character like w, a, s, d, 0, 1...
  196. char c = key[1];
  197. if (((c >= '0') && (c <= '9'))
  198. || ((c >= 'a') && (c <= 'z')))
  199. return (KeyboardButton)c;
  200. } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
  201. // Special characters like tilde, esc, quote...
  202. char *tmp = stringRemoveQuotes(key);
  203. if (strcmp(tmp, "quote") == 0) {
  204. delete [] tmp;
  205. return quoteKey;
  206. } else if (strcmp(tmp, "backslash") == 0) {
  207. delete [] tmp;
  208. return backslashKey;
  209. } else if (strcmp(tmp, "backspace") == 0) {
  210. delete [] tmp;
  211. return backspaceKey;
  212. } else if (strcmp(tmp, "capslock") == 0) {
  213. delete [] tmp;
  214. return capslockKey;
  215. } else if (strcmp(tmp, "comma") == 0) {
  216. delete [] tmp;
  217. return commaKey;
  218. } else if (strcmp(tmp, "del") == 0) {
  219. delete [] tmp;
  220. return delKey;
  221. } else if (strcmp(tmp, "up") == 0) {
  222. delete [] tmp;
  223. return upKey;
  224. } else if (strcmp(tmp, "down") == 0) {
  225. delete [] tmp;
  226. return downKey;
  227. } else if (strcmp(tmp, "left") == 0) {
  228. delete [] tmp;
  229. return leftKey;
  230. } else if (strcmp(tmp, "right") == 0) {
  231. delete [] tmp;
  232. return rightKey;
  233. } else if (strcmp(tmp, "end") == 0) {
  234. delete [] tmp;
  235. return endKey;
  236. } else if (strcmp(tmp, "equals") == 0) {
  237. delete [] tmp;
  238. return equalsKey;
  239. } else if (strcmp(tmp, "escape") == 0) {
  240. delete [] tmp;
  241. return escapeKey;
  242. } else if (strcmp(tmp, "f1") == 0) {
  243. delete [] tmp;
  244. return f1Key;
  245. } else if (strcmp(tmp, "f2") == 0) {
  246. delete [] tmp;
  247. return f2Key;
  248. } else if (strcmp(tmp, "f3") == 0) {
  249. delete [] tmp;
  250. return f3Key;
  251. } else if (strcmp(tmp, "f4") == 0) {
  252. delete [] tmp;
  253. return f4Key;
  254. } else if (strcmp(tmp, "f5") == 0) {
  255. delete [] tmp;
  256. return f5Key;
  257. } else if (strcmp(tmp, "f6") == 0) {
  258. delete [] tmp;
  259. return f6Key;
  260. } else if (strcmp(tmp, "f7") == 0) {
  261. delete [] tmp;
  262. return f7Key;
  263. } else if (strcmp(tmp, "f8") == 0) {
  264. delete [] tmp;
  265. return f8Key;
  266. } else if (strcmp(tmp, "f9") == 0) {
  267. delete [] tmp;
  268. return f9Key;
  269. } else if (strcmp(tmp, "f10") == 0) {
  270. delete [] tmp;
  271. return f10Key;
  272. } else if (strcmp(tmp, "f11") == 0) {
  273. delete [] tmp;
  274. return f11Key;
  275. } else if (strcmp(tmp, "f12") == 0) {
  276. delete [] tmp;
  277. return f12Key;
  278. } else if (strcmp(tmp, "backquote") == 0) {
  279. delete [] tmp;
  280. return backquoteKey;
  281. } else if (strcmp(tmp, "home") == 0) {
  282. delete [] tmp;
  283. return homeKey;
  284. } else if (strcmp(tmp, "insert") == 0) {
  285. delete [] tmp;
  286. return insertKey;
  287. } else if (strcmp(tmp, "leftalt") == 0) {
  288. delete [] tmp;
  289. return leftaltKey;
  290. } else if (strcmp(tmp, "leftctrl") == 0) {
  291. delete [] tmp;
  292. return leftctrlKey;
  293. } else if (strcmp(tmp, "leftbracket") == 0) {
  294. delete [] tmp;
  295. return leftbracketKey;
  296. } else if (strcmp(tmp, "leftgui") == 0) {
  297. delete [] tmp;
  298. return leftguiKey;
  299. } else if (strcmp(tmp, "leftshift") == 0) {
  300. delete [] tmp;
  301. return leftshiftKey;
  302. } else if (strcmp(tmp, "minus") == 0) {
  303. delete [] tmp;
  304. return minusKey;
  305. } else if (strcmp(tmp, "numlock") == 0) {
  306. delete [] tmp;
  307. return numlockKey;
  308. } else if (strcmp(tmp, "pagedown") == 0) {
  309. delete [] tmp;
  310. return pagedownKey;
  311. } else if (strcmp(tmp, "pageup") == 0) {
  312. delete [] tmp;
  313. return pageupKey;
  314. } else if (strcmp(tmp, "pause") == 0) {
  315. delete [] tmp;
  316. return pauseKey;
  317. } else if (strcmp(tmp, "dot") == 0) {
  318. delete [] tmp;
  319. return dotKey;
  320. } else if (strcmp(tmp, "rightalt") == 0) {
  321. delete [] tmp;
  322. return rightaltKey;
  323. } else if (strcmp(tmp, "rightctrl") == 0) {
  324. delete [] tmp;
  325. return rightctrlKey;
  326. } else if (strcmp(tmp, "enter") == 0) {
  327. delete [] tmp;
  328. return enterKey;
  329. } else if (strcmp(tmp, "rightgui") == 0) {
  330. delete [] tmp;
  331. return rightguiKey;
  332. } else if (strcmp(tmp, "rightbracket") == 0) {
  333. delete [] tmp;
  334. return rightbracketKey;
  335. } else if (strcmp(tmp, "rightshift") == 0) {
  336. delete [] tmp;
  337. return rightshiftKey;
  338. } else if (strcmp(tmp, "scrolllock") == 0) {
  339. delete [] tmp;
  340. return scrolllockKey;
  341. } else if (strcmp(tmp, "semicolon") == 0) {
  342. delete [] tmp;
  343. return semicolonKey;
  344. } else if (strcmp(tmp, "slash") == 0) {
  345. delete [] tmp;
  346. return slashKey;
  347. } else if (strcmp(tmp, "space") == 0) {
  348. delete [] tmp;
  349. return spaceKey;
  350. } else if (strcmp(tmp, "tab") == 0) {
  351. delete [] tmp;
  352. return tabKey;
  353. } else if (strcmp(tmp, "leftmouse") == 0) {
  354. delete [] tmp;
  355. return leftmouseKey;
  356. } else if (strcmp(tmp, "middlemouse") == 0) {
  357. delete [] tmp;
  358. return middlemouseKey;
  359. } else if (strcmp(tmp, "rightmouse") == 0) {
  360. delete [] tmp;
  361. return rightmouseKey;
  362. }
  363. delete [] tmp;
  364. }
  365. return unknownKey;
  366. }