Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

System.h 7.0KB

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