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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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::addCommandMode(const char *command) {
  49. if (command && command[0] == '[') {
  50. mCmdModes.pushBack(command);
  51. return (mCmdModes.size() - 1);
  52. } else {
  53. return 0;
  54. }
  55. }
  56. //! \fixme Modifer support later
  57. void System::bindKeyCommand(const char *cmd, unsigned int key, int event) {
  58. printf("Bound command '%s' -> event %i (0x%x key)\n", cmd, event, key);
  59. mKeyEvents[key] = event;
  60. }
  61. void System::command(const char *cmd) {
  62. bool modeFound = false;
  63. char *cmdbuf;
  64. if (!cmd || !cmd[0]) // Null command string
  65. return;
  66. if (cmd[0] == '[') { // Set a mode, eg "[Engine.OpenGL.Driver]"
  67. for (mCmdModes.start(); mCmdModes.forward(); mCmdModes.next()) {
  68. if (strcmp(cmd, mCmdModes.current()) == 0) {
  69. mCommandMode = mCmdModes.getCurrentIndex();
  70. modeFound = true;
  71. }
  72. }
  73. if (!modeFound) {
  74. // mCommandMode = 0;
  75. printf("Command> Unknown mode '%s'\n", cmd);
  76. }
  77. } else { // Execute a command in current mode, eg "stat fps"
  78. cmdbuf = new char[strlen(cmd) + 1];
  79. strncpy(cmdbuf, cmd, strlen(cmd) + 1);
  80. handleCommand(cmdbuf, mCommandMode);
  81. }
  82. }
  83. int System::loadResourceFile(const char *filename) {
  84. char buffer[256];
  85. bool line_comment = false;
  86. FILE *f;
  87. char c;
  88. int i, j;
  89. f = fopen(filename, "r");
  90. if (!f) {
  91. perror(filename);
  92. return -1;
  93. }
  94. printf("Loading %s...\n", filename);
  95. i = 0;
  96. buffer[0] = 0;
  97. // Strip out whitespace and comments
  98. while (fscanf(f, "%c", &c) != EOF) {
  99. if (line_comment && c != '\n')
  100. continue;
  101. if (i > 254) {
  102. printf("loadResourceFile> Overflow handled\n");
  103. i = 254;
  104. }
  105. switch (c) {
  106. case '\v':
  107. case '\t':
  108. break;
  109. case '#':
  110. buffer[i++] = 0;
  111. line_comment = true;
  112. break;
  113. case '\n':
  114. if (line_comment)
  115. line_comment = false;
  116. if (buffer[0] == 0) {
  117. i = 0;
  118. continue;
  119. }
  120. buffer[i] = 0;
  121. //printf("'%s'\n", buffer);
  122. // 'Preprocessor' commands
  123. if (buffer[0] == '@') {
  124. if (strncmp((buffer + 1), "include ", 8) == 0) {
  125. for (j = 9; j < i; ++j) {
  126. buffer[j-9] = buffer[j];
  127. buffer[j-8] = 0;
  128. }
  129. printf("Importing '%s'\n", buffer);
  130. loadResourceFile(fullPath(buffer, 0));
  131. }
  132. } else {
  133. command(buffer);
  134. }
  135. i = 0;
  136. buffer[0] = 0;
  137. break;
  138. default:
  139. buffer[i++] = c;
  140. }
  141. }
  142. fclose(f);
  143. return 0;
  144. }
  145. void System::setDriverGL(const char *driver) {
  146. unsigned int len;
  147. if (m_driver)
  148. delete [] m_driver;
  149. if (driver && driver[0]) {
  150. len = strlen(driver);
  151. m_driver = new char[len+1];
  152. strncpy(m_driver, driver, len);
  153. m_driver[len] = 0;
  154. }
  155. }
  156. void System::resizeGL(unsigned int w, unsigned int h) {
  157. if (!w || !h) {
  158. printf("resizeGL> ERROR assertions 'w > 0', 'h > 0' failed\n");
  159. return;
  160. }
  161. glViewport(0, 0, w, h);
  162. glMatrixMode(GL_PROJECTION);
  163. glLoadIdentity();
  164. // Adjust clipping
  165. // gluPerspective is deprecated!
  166. // gluPerspective(m_fovY, ((GLdouble)w)/((GLdouble)h), m_clipNear, m_clipFar);
  167. // Fix: http://stackoverflow.com/a/2417756
  168. GLfloat fH = tanf(m_fovY * HEL_PI / 360.0f) * m_clipNear;
  169. GLfloat fW = fH * ((GLfloat)w)/((GLfloat)h);
  170. glFrustum(-fW, fW, -fH, fH, m_clipNear, m_clipFar);
  171. glMatrixMode(GL_MODELVIEW);
  172. glLoadIdentity();
  173. }