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

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