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

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