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.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>
  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. static char *bufferString(const char *string, ...) __attribute__((format(printf, 1, 2)));
  67. /*!
  68. * \brief Expansion of unix home enviroment char.
  69. * Also makes sure string ends in "end" char.
  70. * \param path path string
  71. * \param end end character. 0 appends no additional char
  72. * \returns allocated string of path with expansions
  73. */
  74. static char *fullPath(const char *path, char end);
  75. /*!
  76. * \brief Only returns last part of a path string.
  77. * \param filename Path to a file
  78. * \returns Name of the file in filename, without path in front
  79. */
  80. static char *getFileFromFullPath(char *filename);
  81. /*!
  82. * \brief Gets the game tick
  83. * \returns number of milliseconds since start of program
  84. */
  85. virtual unsigned int getTicks();
  86. /*!
  87. * \brief Created a directory
  88. * \param path Directory to create
  89. * \returns -1 on error
  90. */
  91. static int createDir(char *path);
  92. /*!
  93. * \brief Created a new Command Mode.
  94. * \param command valid command mode for the resource file, eg "[Engine.OpenGL.Driver]"
  95. * \returns id given to mode
  96. */
  97. virtual unsigned int addCommandMode(const char *command);
  98. /*!
  99. * \brief Binds a key to a command
  100. * \param cmd valid command string for event
  101. * \param key valid keyboard code
  102. * \param event valid game event id
  103. */
  104. virtual void bindKeyCommand(const char *cmd, unsigned int key, int event);
  105. /*!
  106. * \brief Executes a command string
  107. * \param cmd valid command string, cmd sets its var
  108. */
  109. virtual void command(const char *cmd);
  110. virtual void gameFrame() = 0;
  111. virtual void handleMouseMotionEvent(float x, float y) = 0;
  112. /*!
  113. * \brief Receives the event bound to the command from the key press
  114. * \param key key pressed
  115. */
  116. virtual void handleBoundKeyPressEvent(unsigned int key) = 0;
  117. /*!
  118. * \brief Receives the event bound to the command from the key release
  119. * \param key key released
  120. */
  121. virtual void handleBoundKeyReleaseEvent(unsigned int key) = 0;
  122. /*!
  123. * \brief Executes valid command based on keyword
  124. * \param command valid keyword, optionally followed by space separated arguments
  125. * \param mode current type or resource mode
  126. */
  127. virtual void handleCommand(char *command, unsigned int mode) = 0;
  128. /*!
  129. * \brief Receives key code from text input in console mode
  130. * \param key is a valid keyboard code
  131. * \param mod modifier key
  132. */
  133. virtual void handleConsoleKeyPressEvent(unsigned int key, unsigned int mod) = 0;
  134. virtual void handleKeyPressEvent(unsigned int key, unsigned int mod) = 0;
  135. virtual void handleKeyReleaseEvent(unsigned int key, unsigned int mod) = 0;
  136. virtual void initGL();
  137. virtual void initVideo(unsigned int width, unsigned int height, bool fullscreen) = 0;
  138. /*!
  139. * \brief Init the resource vars
  140. * \param filename resource file
  141. * \returns < 0 on error
  142. */
  143. virtual int loadResourceFile(const char *filename);
  144. static void resetTicks();
  145. virtual void resizeGL(unsigned int width, unsigned int height);
  146. virtual void runGame() = 0;
  147. /*!
  148. * \brief Turns console key events on/off
  149. * Mostly for allowing text entry vs key impulse commands
  150. * \param on new state
  151. */
  152. void setConsoleMode(bool on);
  153. void setDriverGL(const char *driver);
  154. virtual void shutdown(int code) = 0;
  155. virtual void swapBuffersGL() = 0;
  156. virtual void toggleFullscreen() = 0;
  157. protected:
  158. unsigned int m_width; //!< Width of the viewport
  159. unsigned int m_height; //!< Height of the viewport
  160. char *m_driver; //!< String for dynamic use of GL library
  161. float m_clipNear; //!< Clip near distance
  162. float m_clipFar; //!< Clip far distance
  163. float m_fovY; //!< Field of vision
  164. std::map<unsigned int, int> mKeyEvents; //!< Single key press event mappings
  165. bool mConsoleMode; //!< Using text (console) event handler?
  166. Vector<const char *> mCmdModes; //!< Dynamic resource command collection
  167. unsigned int mCommandMode; //!< Current resource command mode
  168. unsigned int mConsoleKey; //!< Console toggle event now handled lower
  169. };
  170. //! \todo Could make these static methods later, depends on API evolution
  171. /*!
  172. * \brief Checks if Command matches Symbol.
  173. * Returns the rest of the argument list back in command buffer, if any
  174. * \param symbol command string
  175. * \param command with arguments
  176. * \returns true if command matches symbol
  177. */
  178. bool rc_command(const char *symbol, char *command);
  179. /*!
  180. * \brief Interpret a string as a bool
  181. * \param buffer "true" or "false"
  182. * \param val is set to boolean interpretation of buffer
  183. * \returns -1 for null string, -2 if string is not "true" or "false"
  184. */
  185. int rc_get_bool(char *buffer, bool *val);
  186. /*!
  187. * \brief Sets timer state and returns number of ticks
  188. * \param state 0 - reset, 1 - get number of ticks
  189. * \returns number of ticks
  190. */
  191. unsigned int system_timer(int state);
  192. #endif