Open Source Tomb Raider Engine
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

System.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*!
  2. * \file include/System.h
  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. * \author xythobuz
  9. */
  10. #ifndef _SYSTEM_H_
  11. #define _SYSTEM_H_
  12. #include <Map.h>
  13. #include <Vector.h>
  14. //! \todo Replace with unicode compatible key codes
  15. #define SYS_MOUSE_LEFT 6000
  16. #define SYS_MOUSE_RIGHT 6001
  17. #define SYS_MOUSE_MIDDLE 6002
  18. #define SYS_KEY_ESC 27
  19. #define SYS_KEY_ENTER 13
  20. #define SYS_KEY_UP 5000
  21. #define SYS_KEY_DOWN 5001
  22. #define SYS_KEY_RIGHT 5002
  23. #define SYS_KEY_LEFT 5003
  24. #define SYS_KEY_PAGEDOWN 5004
  25. #define SYS_KEY_PAGEUP 5005
  26. #define SYS_KEY_F1 1000
  27. #define SYS_KEY_F2 1001
  28. #define SYS_KEY_F3 1002
  29. #define SYS_KEY_F4 1003
  30. #define SYS_KEY_F5 1004
  31. #define SYS_KEY_F6 1005
  32. #define SYS_KEY_F7 1006
  33. #define SYS_KEY_F8 1007
  34. #define SYS_KEY_F9 1008
  35. #define SYS_KEY_F10 1009
  36. #define SYS_KEY_F11 1010
  37. #define SYS_KEY_F12 1011
  38. typedef enum {
  39. SYS_MOD_KEY_LSHIFT = 1,
  40. SYS_MOD_KEY_RSHIFT = 2,
  41. SYS_MOD_KEY_LCTRL = 4,
  42. SYS_MOD_KEY_RCTRL = 8,
  43. SYS_MOD_KEY_LALT = 16,
  44. SYS_MOD_KEY_RALT = 32,
  45. SYS_MOD_KEY_LMETA = 64,
  46. SYS_MOD_KEY_RMETA = 128
  47. } sdl_sys_mod_key_t;
  48. /*!
  49. * \brief Basic Interface for System implementations (SDLSystem)
  50. */
  51. class System {
  52. public:
  53. /*!
  54. * \brief Constructs an object of System
  55. */
  56. System();
  57. /*!
  58. * \brief Deconstructs an object of System
  59. */
  60. virtual ~System();
  61. /*!
  62. * \brief Generates a buffered string for the printf call
  63. * \param string Format string like for printf
  64. * \returns string in a buffer
  65. */
  66. __attribute__((format(printf, 1, 2)))
  67. static char *bufferString(const char *string, ...);
  68. /*!
  69. * \brief Expansion of unix home enviroment char.
  70. * Also makes sure string ends in "end" char.
  71. * \param path path string
  72. * \param end end character. 0 appends no additional char
  73. * \returns allocated string of path with expansions
  74. */
  75. static char *fullPath(const char *path, char end);
  76. /*!
  77. * \brief Only returns last part of a path string.
  78. * \param filename Path to a file
  79. * \returns Name of the file in filename, without path in front
  80. */
  81. static char *getFileFromFullPath(char *filename);
  82. /*!
  83. * \brief Gets the game tick
  84. * \returns number of milliseconds since start of program
  85. */
  86. virtual unsigned int getTicks();
  87. /*!
  88. * \brief Created a directory
  89. * \param path Directory to create
  90. * \returns -1 on error
  91. */
  92. static int createDir(char *path);
  93. /*!
  94. * \brief Created a new Command Mode.
  95. * \param command valid command mode for the resource file, eg "[Engine.OpenGL.Driver]"
  96. * \returns id given to mode
  97. */
  98. virtual unsigned int addCommandMode(const char *command);
  99. /*!
  100. * \brief Binds a key to a command
  101. * \param cmd valid command string for event
  102. * \param key valid keyboard code
  103. * \param event valid game event id
  104. */
  105. virtual void bindKeyCommand(const char *cmd, unsigned int key, int event);
  106. /*!
  107. * \brief Executes a command string
  108. * \param cmd valid command string, cmd sets its var
  109. */
  110. virtual void command(const char *cmd);
  111. virtual void gameFrame() = 0;
  112. virtual void handleMouseMotionEvent(float x, float y) = 0;
  113. /*!
  114. * \brief Receives the event bound to the command from the key press
  115. * \param key key pressed
  116. */
  117. virtual void handleBoundKeyPressEvent(unsigned int key) = 0;
  118. /*!
  119. * \brief Receives the event bound to the command from the key release
  120. * \param key key released
  121. */
  122. virtual void handleBoundKeyReleaseEvent(unsigned int key) = 0;
  123. /*!
  124. * \brief Executes valid command based on keyword
  125. * \param command valid keyword, optionally followed by space separated arguments
  126. * \param mode current type or resource mode
  127. */
  128. virtual void handleCommand(char *command, unsigned int mode) = 0;
  129. /*!
  130. * \brief Receives key code from text input in console mode
  131. * \param key is a valid keyboard code
  132. * \param mod modifier key
  133. */
  134. virtual void handleConsoleKeyPressEvent(unsigned int key, unsigned int mod) = 0;
  135. virtual void handleKeyPressEvent(unsigned int key, unsigned int mod) = 0;
  136. virtual void handleKeyReleaseEvent(unsigned int key, unsigned int mod) = 0;
  137. virtual void initGL();
  138. virtual void initVideo(unsigned int width, unsigned int height, bool fullscreen) = 0;
  139. /*!
  140. * \brief Init the resource vars
  141. * \param filename resource file
  142. * \returns < 0 on error
  143. */
  144. virtual int loadResourceFile(const char *filename);
  145. static void resetTicks();
  146. virtual void resizeGL(unsigned int width, unsigned int height);
  147. virtual void runGame() = 0;
  148. /*!
  149. * \brief Turns console key events on/off
  150. * Mostly for allowing text entry vs key impulse commands
  151. * \param on new state
  152. */
  153. void setConsoleMode(bool on);
  154. void setDriverGL(const char *driver);
  155. virtual void shutdown(int code) = 0;
  156. virtual void swapBuffersGL() = 0;
  157. virtual void toggleFullscreen() = 0;
  158. protected:
  159. unsigned int m_width; //!< Width of the viewport
  160. unsigned int m_height; //!< Height of the viewport
  161. char *m_driver; //!< String for dynamic use of GL library
  162. float m_clipNear; //!< Clip near distance
  163. float m_clipFar; //!< Clip far distance
  164. float m_fovY; //!< Field of vision
  165. Map<unsigned int, int> mKeyEvents; //!< Single key press event mappings
  166. bool mConsoleMode; //!< Using text (console) event handler?
  167. Vector<const char *> mCmdModes; //!< Dynamic resource command collection
  168. unsigned int mCommandMode; //!< Current resource command mode
  169. unsigned int mConsoleKey; //!< Console toggle event now handled lower
  170. };
  171. //! \todo Could make these static methods later, depends on API evolution
  172. /*!
  173. * \brief Checks if Command matches Symbol.
  174. * Returns the rest of the argument list back in command buffer, if any
  175. * \param symbol command string
  176. * \param command with arguments
  177. * \returns true if command matches symbol
  178. */
  179. bool rc_command(const char *symbol, char *command);
  180. /*!
  181. * \brief Interpret a string as a bool
  182. * \param buffer "true" or "false"
  183. * \param val is set to boolean interpretation of buffer
  184. * \returns -1 for null string, -2 if string is not "true" or "false"
  185. */
  186. int rc_get_bool(char *buffer, bool *val);
  187. /*!
  188. * \brief Sets timer state and returns number of ticks
  189. * \param state 0 - reset, 1 - get number of ticks
  190. * \returns number of ticks
  191. */
  192. unsigned int system_timer(int state);
  193. #endif