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