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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. ActionEvents stringToActionEvent(const char *action) {
  126. if (strcmp(action, "menu") == 0) {
  127. return menuAction;
  128. } else if (strcmp(action, "console") == 0) {
  129. return consoleAction;
  130. } else if (strcmp(action, "forward") == 0) {
  131. return forwardAction;
  132. } else if (strcmp(action, "backward") == 0) {
  133. return backwardAction;
  134. } else if (strcmp(action, "left") == 0) {
  135. return leftAction;
  136. } else if (strcmp(action, "right") == 0) {
  137. return rightAction;
  138. } else if (strcmp(action, "jump") == 0) {
  139. return jumpAction;
  140. } else if (strcmp(action, "crouch") == 0) {
  141. return crouchAction;
  142. } else if (strcmp(action, "use") == 0) {
  143. return useAction;
  144. } else if (strcmp(action, "holster") == 0) {
  145. return holsterAction;
  146. } else if (strcmp(action, "walk") == 0) {
  147. return walkAction;
  148. } else {
  149. getConsole().print("Unknown action: \"%s\"", action);
  150. return ActionEventCount;
  151. }
  152. }
  153. KeyboardButton stringToKeyboardButton(const char *key) {
  154. size_t length = strlen(key);
  155. if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
  156. // Literal character like w, a, s, d, 0, 1...
  157. char c = key[1];
  158. if (((c >= '0') && (c <= '9'))
  159. || ((c >= 'a') && (c <= 'z')))
  160. return (KeyboardButton)c;
  161. } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
  162. // Special characters like tilde, esc, quote...
  163. char *tmp = stringRemoveQuotes(key);
  164. if (strcmp(tmp, "quote") == 0) {
  165. delete [] tmp;
  166. return quoteKey;
  167. } else if (strcmp(tmp, "backslash") == 0) {
  168. delete [] tmp;
  169. return backslashKey;
  170. } else if (strcmp(tmp, "backspace") == 0) {
  171. delete [] tmp;
  172. return backspaceKey;
  173. } else if (strcmp(tmp, "capslock") == 0) {
  174. delete [] tmp;
  175. return capslockKey;
  176. } else if (strcmp(tmp, "comma") == 0) {
  177. delete [] tmp;
  178. return commaKey;
  179. } else if (strcmp(tmp, "del") == 0) {
  180. delete [] tmp;
  181. return delKey;
  182. } else if (strcmp(tmp, "up") == 0) {
  183. delete [] tmp;
  184. return upKey;
  185. } else if (strcmp(tmp, "down") == 0) {
  186. delete [] tmp;
  187. return downKey;
  188. } else if (strcmp(tmp, "left") == 0) {
  189. delete [] tmp;
  190. return leftKey;
  191. } else if (strcmp(tmp, "right") == 0) {
  192. delete [] tmp;
  193. return rightKey;
  194. } else if (strcmp(tmp, "end") == 0) {
  195. delete [] tmp;
  196. return endKey;
  197. } else if (strcmp(tmp, "equals") == 0) {
  198. delete [] tmp;
  199. return equalsKey;
  200. } else if (strcmp(tmp, "escape") == 0) {
  201. delete [] tmp;
  202. return escapeKey;
  203. } else if (strcmp(tmp, "f1") == 0) {
  204. delete [] tmp;
  205. return f1Key;
  206. } else if (strcmp(tmp, "f2") == 0) {
  207. delete [] tmp;
  208. return f2Key;
  209. } else if (strcmp(tmp, "f3") == 0) {
  210. delete [] tmp;
  211. return f3Key;
  212. } else if (strcmp(tmp, "f4") == 0) {
  213. delete [] tmp;
  214. return f4Key;
  215. } else if (strcmp(tmp, "f5") == 0) {
  216. delete [] tmp;
  217. return f5Key;
  218. } else if (strcmp(tmp, "f6") == 0) {
  219. delete [] tmp;
  220. return f6Key;
  221. } else if (strcmp(tmp, "f7") == 0) {
  222. delete [] tmp;
  223. return f7Key;
  224. } else if (strcmp(tmp, "f8") == 0) {
  225. delete [] tmp;
  226. return f8Key;
  227. } else if (strcmp(tmp, "f9") == 0) {
  228. delete [] tmp;
  229. return f9Key;
  230. } else if (strcmp(tmp, "f10") == 0) {
  231. delete [] tmp;
  232. return f10Key;
  233. } else if (strcmp(tmp, "f11") == 0) {
  234. delete [] tmp;
  235. return f11Key;
  236. } else if (strcmp(tmp, "f12") == 0) {
  237. delete [] tmp;
  238. return f12Key;
  239. } else if (strcmp(tmp, "backquote") == 0) {
  240. delete [] tmp;
  241. return backquoteKey;
  242. } else if (strcmp(tmp, "home") == 0) {
  243. delete [] tmp;
  244. return homeKey;
  245. } else if (strcmp(tmp, "insert") == 0) {
  246. delete [] tmp;
  247. return insertKey;
  248. } else if (strcmp(tmp, "leftalt") == 0) {
  249. delete [] tmp;
  250. return leftaltKey;
  251. } else if (strcmp(tmp, "leftctrl") == 0) {
  252. delete [] tmp;
  253. return leftctrlKey;
  254. } else if (strcmp(tmp, "leftbracket") == 0) {
  255. delete [] tmp;
  256. return leftbracketKey;
  257. } else if (strcmp(tmp, "leftgui") == 0) {
  258. delete [] tmp;
  259. return leftguiKey;
  260. } else if (strcmp(tmp, "leftshift") == 0) {
  261. delete [] tmp;
  262. return leftshiftKey;
  263. } else if (strcmp(tmp, "minus") == 0) {
  264. delete [] tmp;
  265. return minusKey;
  266. } else if (strcmp(tmp, "numlock") == 0) {
  267. delete [] tmp;
  268. return numlockKey;
  269. } else if (strcmp(tmp, "pagedown") == 0) {
  270. delete [] tmp;
  271. return pagedownKey;
  272. } else if (strcmp(tmp, "pageup") == 0) {
  273. delete [] tmp;
  274. return pageupKey;
  275. } else if (strcmp(tmp, "pause") == 0) {
  276. delete [] tmp;
  277. return pauseKey;
  278. } else if (strcmp(tmp, "dot") == 0) {
  279. delete [] tmp;
  280. return dotKey;
  281. } else if (strcmp(tmp, "rightalt") == 0) {
  282. delete [] tmp;
  283. return rightaltKey;
  284. } else if (strcmp(tmp, "rightctrl") == 0) {
  285. delete [] tmp;
  286. return rightctrlKey;
  287. } else if (strcmp(tmp, "enter") == 0) {
  288. delete [] tmp;
  289. return enterKey;
  290. } else if (strcmp(tmp, "rightgui") == 0) {
  291. delete [] tmp;
  292. return rightguiKey;
  293. } else if (strcmp(tmp, "rightbracket") == 0) {
  294. delete [] tmp;
  295. return rightbracketKey;
  296. } else if (strcmp(tmp, "rightshift") == 0) {
  297. delete [] tmp;
  298. return rightshiftKey;
  299. } else if (strcmp(tmp, "scrolllock") == 0) {
  300. delete [] tmp;
  301. return scrolllockKey;
  302. } else if (strcmp(tmp, "semicolon") == 0) {
  303. delete [] tmp;
  304. return semicolonKey;
  305. } else if (strcmp(tmp, "slash") == 0) {
  306. delete [] tmp;
  307. return slashKey;
  308. } else if (strcmp(tmp, "space") == 0) {
  309. delete [] tmp;
  310. return spaceKey;
  311. } else if (strcmp(tmp, "tab") == 0) {
  312. delete [] tmp;
  313. return tabKey;
  314. } else if (strcmp(tmp, "leftmouse") == 0) {
  315. delete [] tmp;
  316. return leftmouseKey;
  317. } else if (strcmp(tmp, "middlemouse") == 0) {
  318. delete [] tmp;
  319. return middlemouseKey;
  320. } else if (strcmp(tmp, "rightmouse") == 0) {
  321. delete [] tmp;
  322. return rightmouseKey;
  323. } else if (strcmp(tmp, "fourthmouse") == 0) {
  324. delete [] tmp;
  325. return fourthmouseKey;
  326. } else if (strcmp(tmp, "fifthmouse") == 0) {
  327. delete [] tmp;
  328. return fifthmouseKey;
  329. }
  330. delete [] tmp;
  331. }
  332. getConsole().print("Unknown key: %s", key);
  333. return unknownKey;
  334. }