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

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