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.

OpenRaider.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. mWindow = new WindowSDL();
  20. }
  21. OpenRaider::~OpenRaider() {
  22. if (mWindow)
  23. delete mWindow;
  24. }
  25. int OpenRaider::loadConfig(const char *config) {
  26. assert(config != NULL);
  27. assert(config[0] != '\0');
  28. char *configFile = fullPath(config, 0);
  29. printf("Loading config from \"%s\"...\n", configFile);
  30. FILE *f = fopen(configFile, "r");
  31. if (f == NULL) {
  32. printf("Could not open file!\n");
  33. return -1;
  34. }
  35. char buffer[256];
  36. while (fgets(buffer, 256, f) != NULL) {
  37. command(buffer);
  38. }
  39. fclose(f);
  40. return 0;
  41. }
  42. int OpenRaider::command(const char *command) {
  43. assert(command != NULL);
  44. assert(command[0] != '\0');
  45. int returnValue = 0;
  46. char *cmd = bufferString("%s", command);
  47. size_t length = strlen(cmd);
  48. // Command ends at '\n' or # when a comment begins
  49. for (size_t i = 0; i < length; i++) {
  50. if (cmd[i] == '\n' || cmd[i] == '#') {
  51. cmd[i] = '\0';
  52. break;
  53. }
  54. }
  55. char *token = strtok(cmd, " \t");
  56. if (token != NULL) {
  57. // token is the command to execute
  58. // get arguments
  59. std::vector<char *> args;
  60. char *next;
  61. while ((next = strtok(NULL, " \t")) != NULL) {
  62. args.push_back(next);
  63. }
  64. // Execute
  65. returnValue = this->command(token, &args);
  66. }
  67. free(cmd);
  68. return returnValue;
  69. }
  70. int OpenRaider::command(const char *command, std::vector<char *> *args) {
  71. assert(command != NULL);
  72. assert(command[0] != '\0');
  73. assert(args != NULL);
  74. if (strcmp(command, "set") == 0) {
  75. if (args->size() != 2) {
  76. printf("Invalid use of set-command ");
  77. printStringVector(args);
  78. printf("\n");
  79. return -2;
  80. } else {
  81. return set(args->at(0), args->at(1));
  82. }
  83. } else if (strcmp(command, "bind") == 0) {
  84. if (args->size() != 2) {
  85. printf("Invalid use of bind-command ");
  86. printStringVector(args);
  87. printf("\n");
  88. return -3;
  89. } else {
  90. return bind(args->at(0), args->at(1));
  91. }
  92. } else {
  93. printf("Unknown command: %s ", command);
  94. printStringVector(args);
  95. printf("\n");
  96. return -1;
  97. }
  98. }
  99. int OpenRaider::set(const char *var, const char *value) {
  100. if (strcmp(var, "size") == 0) {
  101. // value has format like "\"1024x768\""
  102. unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
  103. if (sscanf(value, "\"%dx%d\"", &w, &h) != 2) {
  104. printf("set-size-Error: Invalid value (%s)\n", value);
  105. return -2;
  106. }
  107. mWindow->setSize(w, h);
  108. } else if (strcmp(var, "fullscreen") == 0) {
  109. } else if (strcmp(var, "gldriver") == 0) {
  110. } else if (strcmp(var, "audio") == 0) {
  111. } else if (strcmp(var, "volume") == 0) {
  112. } else if (strcmp(var, "mouse_x") == 0) {
  113. } else if (strcmp(var, "mouse_y") == 0) {
  114. } else if (strcmp(var, "basedir") == 0) {
  115. } else if (strcmp(var, "pakdir") == 0) {
  116. } else if (strcmp(var, "audiodir") == 0) {
  117. } else if (strcmp(var, "datadir") == 0) {
  118. } else if (strcmp(var, "font") == 0) {
  119. } else {
  120. printf("set-Error: Unknown variable (%s = %s)\n", var, value);
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. int OpenRaider::bind(const char *action, const char *key) {
  126. if (strcmp(action, "console") == 0) {
  127. } else if (strcmp(action, "forward") == 0) {
  128. } else if (strcmp(action, "backward") == 0) {
  129. } else if (strcmp(action, "jump") == 0) {
  130. } else if (strcmp(action, "crouch") == 0) {
  131. } else if (strcmp(action, "left") == 0) {
  132. } else if (strcmp(action, "right") == 0) {
  133. } else {
  134. printf("bind-Error: Unknown action (%s --> %s)\n", key, action);
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. int OpenRaider::initialize() {
  140. assert(mInit == false);
  141. assert(mRunning == false);
  142. // Initialize Windowing
  143. if (mWindow->initialize() != 0)
  144. return -1;
  145. // Initialize OpenGL
  146. if (mWindow->initializeGL() != 0)
  147. return -2;
  148. mWindow->setFont("~/.OpenRaider/data/test.ttf");
  149. // Initialize window font
  150. if (mWindow->initializeFont() != 0)
  151. return -3;
  152. mInit = true;
  153. return 0;
  154. }
  155. void OpenRaider::run() {
  156. assert(mInit == true);
  157. assert(mRunning == false);
  158. mRunning = true;
  159. while (mRunning) {
  160. clock_t startTime = systemTimerGet();
  161. mWindow->eventHandling();
  162. glClear(GL_COLOR_BUFFER_BIT);
  163. WindowString s;
  164. s.text = bufferString("This text is not fixed-width...");
  165. s.x = 100;
  166. s.y = 100;
  167. s.scale = 1.5f;
  168. s.color[0] = s.color[1] = s.color[2] = 0xFF;
  169. mWindow->writeString(&s);
  170. mWindow->swapBuffersGL();
  171. clock_t stopTime = systemTimerGet();
  172. if (MAX_MS_PER_FRAME > (stopTime - startTime))
  173. mWindow->delay(MAX_MS_PER_FRAME - (stopTime - startTime));
  174. }
  175. }