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

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