Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

System.cpp 5.0KB

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