Open Source Tomb Raider Engine
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 "MenuFolder.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 MenuFolder 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() << "Initializing " << VERSION << Console::endl;
  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() << "Starting " << VERSION << Console::endl;
  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. return ActionEventCount;
  152. }
  153. }
  154. KeyboardButton stringToKeyboardButton(const char *key) {
  155. size_t length = strlen(key);
  156. if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
  157. // Literal character like w, a, s, d, 0, 1...
  158. char c = key[1];
  159. if (((c >= '0') && (c <= '9'))
  160. || ((c >= 'a') && (c <= 'z')))
  161. return (KeyboardButton)c;
  162. } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
  163. // Special characters like tilde, esc, quote...
  164. char *tmp = stringRemoveQuotes(key);
  165. if (strcmp(tmp, "quote") == 0) {
  166. delete [] tmp;
  167. return quoteKey;
  168. } else if (strcmp(tmp, "backslash") == 0) {
  169. delete [] tmp;
  170. return backslashKey;
  171. } else if (strcmp(tmp, "backspace") == 0) {
  172. delete [] tmp;
  173. return backspaceKey;
  174. } else if (strcmp(tmp, "capslock") == 0) {
  175. delete [] tmp;
  176. return capslockKey;
  177. } else if (strcmp(tmp, "comma") == 0) {
  178. delete [] tmp;
  179. return commaKey;
  180. } else if (strcmp(tmp, "del") == 0) {
  181. delete [] tmp;
  182. return delKey;
  183. } else if (strcmp(tmp, "up") == 0) {
  184. delete [] tmp;
  185. return upKey;
  186. } else if (strcmp(tmp, "down") == 0) {
  187. delete [] tmp;
  188. return downKey;
  189. } else if (strcmp(tmp, "left") == 0) {
  190. delete [] tmp;
  191. return leftKey;
  192. } else if (strcmp(tmp, "right") == 0) {
  193. delete [] tmp;
  194. return rightKey;
  195. } else if (strcmp(tmp, "end") == 0) {
  196. delete [] tmp;
  197. return endKey;
  198. } else if (strcmp(tmp, "equals") == 0) {
  199. delete [] tmp;
  200. return equalsKey;
  201. } else if (strcmp(tmp, "escape") == 0) {
  202. delete [] tmp;
  203. return escapeKey;
  204. } else if (strcmp(tmp, "f1") == 0) {
  205. delete [] tmp;
  206. return f1Key;
  207. } else if (strcmp(tmp, "f2") == 0) {
  208. delete [] tmp;
  209. return f2Key;
  210. } else if (strcmp(tmp, "f3") == 0) {
  211. delete [] tmp;
  212. return f3Key;
  213. } else if (strcmp(tmp, "f4") == 0) {
  214. delete [] tmp;
  215. return f4Key;
  216. } else if (strcmp(tmp, "f5") == 0) {
  217. delete [] tmp;
  218. return f5Key;
  219. } else if (strcmp(tmp, "f6") == 0) {
  220. delete [] tmp;
  221. return f6Key;
  222. } else if (strcmp(tmp, "f7") == 0) {
  223. delete [] tmp;
  224. return f7Key;
  225. } else if (strcmp(tmp, "f8") == 0) {
  226. delete [] tmp;
  227. return f8Key;
  228. } else if (strcmp(tmp, "f9") == 0) {
  229. delete [] tmp;
  230. return f9Key;
  231. } else if (strcmp(tmp, "f10") == 0) {
  232. delete [] tmp;
  233. return f10Key;
  234. } else if (strcmp(tmp, "f11") == 0) {
  235. delete [] tmp;
  236. return f11Key;
  237. } else if (strcmp(tmp, "f12") == 0) {
  238. delete [] tmp;
  239. return f12Key;
  240. } else if (strcmp(tmp, "backquote") == 0) {
  241. delete [] tmp;
  242. return backquoteKey;
  243. } else if (strcmp(tmp, "home") == 0) {
  244. delete [] tmp;
  245. return homeKey;
  246. } else if (strcmp(tmp, "insert") == 0) {
  247. delete [] tmp;
  248. return insertKey;
  249. } else if (strcmp(tmp, "leftalt") == 0) {
  250. delete [] tmp;
  251. return leftaltKey;
  252. } else if (strcmp(tmp, "leftctrl") == 0) {
  253. delete [] tmp;
  254. return leftctrlKey;
  255. } else if (strcmp(tmp, "leftbracket") == 0) {
  256. delete [] tmp;
  257. return leftbracketKey;
  258. } else if (strcmp(tmp, "leftgui") == 0) {
  259. delete [] tmp;
  260. return leftguiKey;
  261. } else if (strcmp(tmp, "leftshift") == 0) {
  262. delete [] tmp;
  263. return leftshiftKey;
  264. } else if (strcmp(tmp, "minus") == 0) {
  265. delete [] tmp;
  266. return minusKey;
  267. } else if (strcmp(tmp, "numlock") == 0) {
  268. delete [] tmp;
  269. return numlockKey;
  270. } else if (strcmp(tmp, "pagedown") == 0) {
  271. delete [] tmp;
  272. return pagedownKey;
  273. } else if (strcmp(tmp, "pageup") == 0) {
  274. delete [] tmp;
  275. return pageupKey;
  276. } else if (strcmp(tmp, "pause") == 0) {
  277. delete [] tmp;
  278. return pauseKey;
  279. } else if (strcmp(tmp, "dot") == 0) {
  280. delete [] tmp;
  281. return dotKey;
  282. } else if (strcmp(tmp, "rightalt") == 0) {
  283. delete [] tmp;
  284. return rightaltKey;
  285. } else if (strcmp(tmp, "rightctrl") == 0) {
  286. delete [] tmp;
  287. return rightctrlKey;
  288. } else if (strcmp(tmp, "enter") == 0) {
  289. delete [] tmp;
  290. return enterKey;
  291. } else if (strcmp(tmp, "rightgui") == 0) {
  292. delete [] tmp;
  293. return rightguiKey;
  294. } else if (strcmp(tmp, "rightbracket") == 0) {
  295. delete [] tmp;
  296. return rightbracketKey;
  297. } else if (strcmp(tmp, "rightshift") == 0) {
  298. delete [] tmp;
  299. return rightshiftKey;
  300. } else if (strcmp(tmp, "scrolllock") == 0) {
  301. delete [] tmp;
  302. return scrolllockKey;
  303. } else if (strcmp(tmp, "semicolon") == 0) {
  304. delete [] tmp;
  305. return semicolonKey;
  306. } else if (strcmp(tmp, "slash") == 0) {
  307. delete [] tmp;
  308. return slashKey;
  309. } else if (strcmp(tmp, "space") == 0) {
  310. delete [] tmp;
  311. return spaceKey;
  312. } else if (strcmp(tmp, "tab") == 0) {
  313. delete [] tmp;
  314. return tabKey;
  315. } else if (strcmp(tmp, "leftmouse") == 0) {
  316. delete [] tmp;
  317. return leftmouseKey;
  318. } else if (strcmp(tmp, "middlemouse") == 0) {
  319. delete [] tmp;
  320. return middlemouseKey;
  321. } else if (strcmp(tmp, "rightmouse") == 0) {
  322. delete [] tmp;
  323. return rightmouseKey;
  324. } else if (strcmp(tmp, "fourthmouse") == 0) {
  325. delete [] tmp;
  326. return fourthmouseKey;
  327. } else if (strcmp(tmp, "fifthmouse") == 0) {
  328. delete [] tmp;
  329. return fifthmouseKey;
  330. }
  331. delete [] tmp;
  332. }
  333. return unknownKey;
  334. }
  335. #endif // UNIT_TEST
  336. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
  337. #ifndef NDEBUG
  338. #include <exception>
  339. #include <execinfo.h>
  340. namespace {
  341. extern std::terminate_handler oldTerminateHandler;
  342. [[noreturn]] void terminateHandler() {
  343. const unsigned int maxSize = 128;
  344. void *callstack[maxSize];
  345. int frames = backtrace(callstack, maxSize);
  346. char **strs = backtrace_symbols(callstack, frames);
  347. std::cout << std::endl;
  348. for (int i = 0; i < frames; i++)
  349. std::cout << strs[i] << std::endl;
  350. delete [] strs;
  351. std::cout << std::endl << "Last custom Exception:" << std::endl;
  352. std::cout << " " << Exception::getLastException() << std::endl << std::endl;
  353. oldTerminateHandler();
  354. abort();
  355. }
  356. std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
  357. }
  358. #endif // NDEBUG
  359. #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS