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 12KB

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