Open Source Tomb Raider Engine
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

System.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*!
  2. * \file src/System.cpp
  3. * \brief Mostly defines the interface of System implementations.
  4. *
  5. * Currently only SDL is used, but there was a GLUT implementation.
  6. *
  7. * \author Mongoose
  8. */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <string.h>
  14. #include <stdarg.h>
  15. #include <cmath>
  16. #ifdef __APPLE__
  17. #include <OpenGL/gl.h>
  18. #include <OpenGL/glu.h>
  19. #else
  20. #include <GL/gl.h>
  21. #include <GL/glu.h>
  22. #endif
  23. #include "utils/math.h"
  24. #include "utils/time.h"
  25. #include "System.h"
  26. System::System() {
  27. m_width = 800;
  28. m_height = 600;
  29. m_driver = NULL;
  30. m_clipFar = 4000.0f;
  31. m_clipNear = 4.0f;
  32. m_fovY = 45.0f;
  33. mConsoleMode = false;
  34. mCommandMode = 0;
  35. printf("[System.Core]\n");
  36. // Hack for bad Map class, as well as reserved commands
  37. addCommandMode("[System.Console]");
  38. mConsoleKey = '`';
  39. bindKeyCommand("+console", mConsoleKey, 0);
  40. #ifdef WIN32
  41. setDriverGL("libGL32.dll");
  42. #else
  43. setDriverGL("/usr/lib/libGL.so.1");
  44. #endif
  45. }
  46. System::~System() {
  47. }
  48. unsigned int System::getTicks() {
  49. return system_timer();
  50. }
  51. void System::resetTicks() {
  52. system_timer_reset();
  53. }
  54. int System::createDir(char *path) {
  55. #ifdef WIN32
  56. return _mkdir(path);
  57. #else
  58. return mkdir(path, S_IRWXU | S_IRWXG);
  59. #endif
  60. }
  61. unsigned int System::addCommandMode(const char *command) {
  62. if (command && command[0] == '[') {
  63. mCmdModes.pushBack(command);
  64. return (mCmdModes.size() - 1);
  65. } else {
  66. return 0;
  67. }
  68. }
  69. //! \fixme Modifer support later
  70. void System::bindKeyCommand(const char *cmd, unsigned int key, int event) {
  71. printf("Bound command '%s' -> event %i (0x%x key)\n", cmd, event, key);
  72. mKeyEvents[key] = event;
  73. }
  74. void System::command(const char *cmd) {
  75. bool modeFound = false;
  76. char *cmdbuf;
  77. if (!cmd || !cmd[0]) // Null command string
  78. return;
  79. if (cmd[0] == '[') { // Set a mode, eg "[Engine.OpenGL.Driver]"
  80. for (mCmdModes.start(); mCmdModes.forward(); mCmdModes.next()) {
  81. if (strcmp(cmd, mCmdModes.current()) == 0) {
  82. mCommandMode = mCmdModes.getCurrentIndex();
  83. modeFound = true;
  84. }
  85. }
  86. if (!modeFound) {
  87. // mCommandMode = 0;
  88. printf("Command> Unknown mode '%s'\n", cmd);
  89. }
  90. } else { // Execute a command in current mode, eg "stat fps"
  91. cmdbuf = new char[strlen(cmd) + 1];
  92. strncpy(cmdbuf, cmd, strlen(cmd) + 1);
  93. handleCommand(cmdbuf, mCommandMode);
  94. }
  95. }
  96. int System::loadResourceFile(const char *filename) {
  97. char buffer[256];
  98. bool line_comment = false;
  99. FILE *f;
  100. char c;
  101. int i, j;
  102. f = fopen(filename, "r");
  103. if (!f) {
  104. perror(filename);
  105. return -1;
  106. }
  107. printf("Loading %s...\n", filename);
  108. i = 0;
  109. buffer[0] = 0;
  110. // Strip out whitespace and comments
  111. while (fscanf(f, "%c", &c) != EOF) {
  112. if (line_comment && c != '\n')
  113. continue;
  114. if (i > 254) {
  115. printf("loadResourceFile> Overflow handled\n");
  116. i = 254;
  117. }
  118. switch (c) {
  119. case '\v':
  120. case '\t':
  121. break;
  122. case '#':
  123. buffer[i++] = 0;
  124. line_comment = true;
  125. break;
  126. case '\n':
  127. if (line_comment)
  128. line_comment = false;
  129. if (buffer[0] == 0) {
  130. i = 0;
  131. continue;
  132. }
  133. buffer[i] = 0;
  134. //printf("'%s'\n", buffer);
  135. // 'Preprocessor' commands
  136. if (buffer[0] == '@') {
  137. if (strncmp((buffer + 1), "include ", 8) == 0) {
  138. for (j = 9; j < i; ++j) {
  139. buffer[j-9] = buffer[j];
  140. buffer[j-8] = 0;
  141. }
  142. printf("Importing '%s'\n", buffer);
  143. loadResourceFile(fullPath(buffer, 0));
  144. }
  145. } else {
  146. command(buffer);
  147. }
  148. i = 0;
  149. buffer[0] = 0;
  150. break;
  151. default:
  152. buffer[i++] = c;
  153. }
  154. }
  155. fclose(f);
  156. return 0;
  157. }
  158. void System::setDriverGL(const char *driver) {
  159. unsigned int len;
  160. if (m_driver)
  161. delete [] m_driver;
  162. if (driver && driver[0]) {
  163. len = strlen(driver);
  164. m_driver = new char[len+1];
  165. strncpy(m_driver, driver, len);
  166. m_driver[len] = 0;
  167. }
  168. }
  169. void System::resizeGL(unsigned int w, unsigned int h) {
  170. if (!w || !h) {
  171. printf("resizeGL> ERROR assertions 'w > 0', 'h > 0' failed\n");
  172. return;
  173. }
  174. glViewport(0, 0, w, h);
  175. glMatrixMode(GL_PROJECTION);
  176. glLoadIdentity();
  177. // Adjust clipping
  178. // gluPerspective is deprecated!
  179. // gluPerspective(m_fovY, ((GLdouble)w)/((GLdouble)h), m_clipNear, m_clipFar);
  180. // Fix: http://stackoverflow.com/a/2417756
  181. GLfloat fH = tanf(m_fovY * HEL_PI / 360.0f) * m_clipNear;
  182. GLfloat fW = fH * ((GLfloat)w)/((GLfloat)h);
  183. glFrustum(-fW, fW, -fH, fH, m_clipNear, m_clipFar);
  184. glMatrixMode(GL_MODELVIEW);
  185. glLoadIdentity();
  186. }