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

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