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

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