Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

OpenRaider.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*!
  2. * \file src/OpenRaider.cpp
  3. * \brief Main Game Object
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <assert.h>
  10. #include "WindowSDL.h"
  11. #include "config.h"
  12. #include "utils/strings.h"
  13. #include "utils/time.h"
  14. #include "OpenRaider.h"
  15. #define MAX_MS_PER_FRAME (1000 / MAXIMUM_FPS)
  16. OpenRaider::OpenRaider() {
  17. mInit = false;
  18. mRunning = false;
  19. mBaseDir = NULL;
  20. mPakDir = NULL;
  21. mAudioDir = NULL;
  22. mDataDir = NULL;
  23. mMenu = new Menu();
  24. mSound = new Sound();
  25. mWindow = new WindowSDL();
  26. }
  27. OpenRaider::~OpenRaider() {
  28. if (mMenu)
  29. delete mMenu;
  30. if (mSound)
  31. delete mSound;
  32. if (mWindow)
  33. delete mWindow;
  34. if (mBaseDir)
  35. delete mBaseDir;
  36. if (mPakDir)
  37. delete mPakDir;
  38. if (mAudioDir)
  39. delete mAudioDir;
  40. if (mDataDir)
  41. delete mDataDir;
  42. }
  43. int OpenRaider::loadConfig(const char *config) {
  44. assert(config != NULL);
  45. assert(config[0] != '\0');
  46. char *configFile = fullPath(config, 0);
  47. printf("Loading config from \"%s\"...\n", configFile);
  48. FILE *f = fopen(configFile, "r");
  49. if (f == NULL) {
  50. printf("Could not open file!\n");
  51. return -1;
  52. }
  53. char buffer[256];
  54. while (fgets(buffer, 256, f) != NULL) {
  55. command(buffer);
  56. }
  57. fclose(f);
  58. return 0;
  59. }
  60. int OpenRaider::command(const char *command) {
  61. assert(command != NULL);
  62. assert(command[0] != '\0');
  63. int returnValue = 0;
  64. char *cmd = bufferString("%s", command);
  65. size_t length = strlen(cmd);
  66. // Command ends at '\n' or # when a comment begins
  67. for (size_t i = 0; i < length; i++) {
  68. if (cmd[i] == '\n' || cmd[i] == '#') {
  69. cmd[i] = '\0';
  70. break;
  71. }
  72. }
  73. char *token = strtok(cmd, " \t");
  74. if (token != NULL) {
  75. // token is the command to execute
  76. // get arguments
  77. std::vector<char *> args;
  78. char *next;
  79. while ((next = strtok(NULL, " \t")) != NULL) {
  80. args.push_back(next);
  81. }
  82. // Execute
  83. returnValue = this->command(token, &args);
  84. }
  85. free(cmd);
  86. return returnValue;
  87. }
  88. int OpenRaider::command(const char *command, std::vector<char *> *args) {
  89. assert(command != NULL);
  90. assert(command[0] != '\0');
  91. assert(args != NULL);
  92. if (strcmp(command, "set") == 0) {
  93. if (args->size() != 2) {
  94. printf("Invalid use of set-command ");
  95. printStringVector(args);
  96. printf("\n");
  97. return -2;
  98. } else {
  99. return set(args->at(0), args->at(1));
  100. }
  101. } else if (strcmp(command, "bind") == 0) {
  102. if (args->size() != 2) {
  103. printf("Invalid use of bind-command ");
  104. printStringVector(args);
  105. printf("\n");
  106. return -3;
  107. } else {
  108. return bind(args->at(0), args->at(1));
  109. }
  110. } else {
  111. printf("Unknown command: %s ", command);
  112. printStringVector(args);
  113. printf("\n");
  114. return -1;
  115. }
  116. }
  117. char *OpenRaider::expandDirectoryNames(const char *s) {
  118. const char *base = "$(basedir)";
  119. const char *pak = "$(pakdir)";
  120. const char *audio = "$(audiodir)";
  121. const char *data = "$(datadir)";
  122. if (mBaseDir != NULL) {
  123. if (strstr(s, base) != NULL) {
  124. return stringReplace(s, base, mBaseDir);
  125. }
  126. }
  127. if (mPakDir != NULL) {
  128. if (strstr(s, pak) != NULL) {
  129. return stringReplace(s, pak, mPakDir);
  130. }
  131. }
  132. if (mAudioDir != NULL) {
  133. if (strstr(s, audio) != NULL) {
  134. return stringReplace(s, audio, mAudioDir);
  135. }
  136. }
  137. if (mDataDir != NULL) {
  138. if (strstr(s, data) != NULL) {
  139. return stringReplace(s, data, mDataDir);
  140. }
  141. }
  142. return NULL;
  143. }
  144. #define CHANGE_DIR_WITH_EXPANSION(a) do { \
  145. char *quotes = stringRemoveQuotes(value); \
  146. char *tmp = expandDirectoryNames(quotes); \
  147. if (tmp == NULL) { \
  148. a = fullPath(quotes, 0); \
  149. } else { \
  150. a = fullPath(tmp, 0); \
  151. delete [] tmp; \
  152. } \
  153. delete [] quotes; \
  154. } while(false)
  155. int OpenRaider::set(const char *var, const char *value) {
  156. if (strcmp(var, "size") == 0) {
  157. // value has format like "\"1024x768\""
  158. unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
  159. if (sscanf(value, "\"%dx%d\"", &w, &h) != 2) {
  160. printf("set-size-Error: Invalid value (%s)\n", value);
  161. return -2;
  162. }
  163. mWindow->setSize(w, h);
  164. } else if (strcmp(var, "fullscreen") == 0) {
  165. bool fullscreen = false;
  166. if (readBool(value, &fullscreen) != 0) {
  167. printf("set-fullscreen-Error: Invalid value (%s)\n", value);
  168. return -3;
  169. }
  170. mWindow->setFullscreen(fullscreen);
  171. } else if (strcmp(var, "gldriver") == 0) {
  172. mWindow->setDriver(value);
  173. } else if (strcmp(var, "audio") == 0) {
  174. bool audio = false;
  175. if (readBool(value, &audio) != 0) {
  176. printf("set-audio-Error: Invalid value (%s)\n", value);
  177. return -4;
  178. }
  179. mSound->setEnabled(audio);
  180. } else if (strcmp(var, "volume") == 0) {
  181. float vol = 1.0f;
  182. if (sscanf(value, "%f", &vol) != 1) {
  183. printf("set-volume-Error: Invalid value (%s)\n", value);
  184. return -5;
  185. }
  186. mSound->setVolume(vol);
  187. } else if (strcmp(var, "mouse_x") == 0) {
  188. float sense = 1.0f;
  189. if (sscanf(value, "%f", &sense) != 1) {
  190. printf("set-mouse_x-Error: Invalid value (%s)\n", value);
  191. return -6;
  192. }
  193. //! \todo mouse support
  194. } else if (strcmp(var, "mouse_y") == 0) {
  195. float sense = 1.0f;
  196. if (sscanf(value, "%f", &sense) != 1) {
  197. printf("set-mouse_y-Error: Invalid value (%s)\n", value);
  198. return -7;
  199. }
  200. //! \todo mouse support
  201. } else if (strcmp(var, "basedir") == 0) {
  202. CHANGE_DIR_WITH_EXPANSION(mBaseDir);
  203. } else if (strcmp(var, "pakdir") == 0) {
  204. CHANGE_DIR_WITH_EXPANSION(mPakDir);
  205. } else if (strcmp(var, "audiodir") == 0) {
  206. CHANGE_DIR_WITH_EXPANSION(mAudioDir);
  207. } else if (strcmp(var, "datadir") == 0) {
  208. CHANGE_DIR_WITH_EXPANSION(mDataDir);
  209. } else if (strcmp(var, "font") == 0) {
  210. char *quotes = stringReplace(value, "\"", "");
  211. char *tmp = expandDirectoryNames(quotes);
  212. if (tmp == NULL) {
  213. mWindow->setFont(quotes);
  214. } else {
  215. mWindow->setFont(tmp);
  216. delete [] tmp;
  217. }
  218. delete [] quotes;
  219. } else {
  220. printf("set-Error: Unknown variable (%s = %s)\n", var, value);
  221. return -1;
  222. }
  223. return 0;
  224. }
  225. int OpenRaider::bind(const char *action, const char *key) {
  226. const char *tmp = action;
  227. if (action[0] == '+')
  228. tmp++;
  229. if (strcmp(tmp, "console") == 0) {
  230. } else if (strcmp(tmp, "forward") == 0) {
  231. } else if (strcmp(tmp, "backward") == 0) {
  232. } else if (strcmp(tmp, "jump") == 0) {
  233. } else if (strcmp(tmp, "crouch") == 0) {
  234. } else if (strcmp(tmp, "left") == 0) {
  235. } else if (strcmp(tmp, "right") == 0) {
  236. } else {
  237. printf("bind-Error: Unknown action (%s --> %s)\n", key, action);
  238. return -1;
  239. }
  240. return 0;
  241. }
  242. int OpenRaider::initialize() {
  243. assert(mInit == false);
  244. assert(mRunning == false);
  245. // Initialize Windowing
  246. if (mWindow->initialize() != 0)
  247. return -1;
  248. // Initialize OpenGL
  249. if (mWindow->initializeGL() != 0)
  250. return -2;
  251. // Initialize window font
  252. if (mWindow->initializeFont() != 0)
  253. return -3;
  254. // Initialize sound
  255. if (mSound->initialize() != 0)
  256. return -4;
  257. mMenu->setVisible(true);
  258. mInit = true;
  259. return 0;
  260. }
  261. void OpenRaider::run() {
  262. assert(mInit == true);
  263. assert(mRunning == false);
  264. mRunning = true;
  265. while (mRunning) {
  266. clock_t startTime = systemTimerGet();
  267. mWindow->eventHandling();
  268. // Temp Debug
  269. glClearColor(0.25f, 0.75f, 0.25f, 1.0f);
  270. glClear(GL_COLOR_BUFFER_BIT);
  271. mWindow->glEnter2D();
  272. mMenu->display();
  273. mWindow->glExit2D();
  274. mWindow->swapBuffersGL();
  275. clock_t stopTime = systemTimerGet();
  276. if (MAX_MS_PER_FRAME > (stopTime - startTime))
  277. mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));
  278. }
  279. }