/extlibs/SFML/include/SFML/Graphics/Renderer.hpp

https://bitbucket.org/hugoruscitti/pilascpp · C++ Header · 392 lines · 67 code · 40 blank · 285 comment · 0 complexity · df723360992e904cf5d0ad0356ee048a MD5 · raw file

  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
  5. //
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it freely,
  11. // subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented;
  14. // you must not claim that you wrote the original software.
  15. // If you use this software in a product, an acknowledgment
  16. // in the product documentation would be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such,
  19. // and must not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. ////////////////////////////////////////////////////////////
  24. #ifndef SFML_RENDERER_HPP
  25. #define SFML_RENDERER_HPP
  26. ////////////////////////////////////////////////////////////
  27. // Headers
  28. ////////////////////////////////////////////////////////////
  29. #include <SFML/Config.hpp>
  30. #include <SFML/System/NonCopyable.hpp>
  31. #include <SFML/Graphics/Color.hpp>
  32. #include <SFML/Graphics/Drawable.hpp>
  33. #include <SFML/Graphics/Matrix3.hpp>
  34. namespace sf
  35. {
  36. class Image;
  37. class Shader;
  38. ////////////////////////////////////////////////////////////
  39. /// \brief Handles the low-level rendering (states and geometry)
  40. ///
  41. ////////////////////////////////////////////////////////////
  42. class SFML_API Renderer : NonCopyable
  43. {
  44. public :
  45. ////////////////////////////////////////////////////////////
  46. /// \brief Types of primitives to be rendererd
  47. ///
  48. ////////////////////////////////////////////////////////////
  49. enum PrimitiveType
  50. {
  51. TriangleList, ///< Simple list of triangles
  52. TriangleStrip, ///< Triangle strip (consecutive triangles always share two points)
  53. TriangleFan, ///< Triangle fan (one center point + outline points)
  54. QuadList ///< Simple list of quads
  55. };
  56. public :
  57. ////////////////////////////////////////////////////////////
  58. /// \brief Construct the renderer with its owner render target
  59. ///
  60. /// \param target Owner render target
  61. ///
  62. ////////////////////////////////////////////////////////////
  63. Renderer(RenderTarget& target);
  64. ////////////////////////////////////////////////////////////
  65. /// \brief Destructor
  66. ///
  67. ////////////////////////////////////////////////////////////
  68. ~Renderer();
  69. ////////////////////////////////////////////////////////////
  70. /// \brief Initialize the renderer (set the default states, etc.)
  71. ///
  72. ////////////////////////////////////////////////////////////
  73. void Initialize();
  74. ////////////////////////////////////////////////////////////
  75. /// \brief Save the current OpenGL render states and matrices
  76. ///
  77. /// \see RestoreGLStates
  78. ///
  79. ////////////////////////////////////////////////////////////
  80. void SaveGLStates();
  81. ////////////////////////////////////////////////////////////
  82. /// \brief Restore the previously saved OpenGL render states and matrices
  83. ///
  84. /// \see SaveGLStates
  85. ///
  86. ////////////////////////////////////////////////////////////
  87. void RestoreGLStates();
  88. ////////////////////////////////////////////////////////////
  89. /// Clear the color buffer
  90. ///
  91. /// \param color Color to use to clear the color buffer
  92. ///
  93. ////////////////////////////////////////////////////////////
  94. void Clear(const Color& color);
  95. ////////////////////////////////////////////////////////////
  96. /// \brief Save the current render states
  97. ///
  98. /// \see PopStates
  99. ///
  100. ////////////////////////////////////////////////////////////
  101. void PushStates();
  102. ////////////////////////////////////////////////////////////
  103. /// \brief Restore the previously saved render states
  104. ///
  105. /// \see PushStates
  106. ///
  107. ////////////////////////////////////////////////////////////
  108. void PopStates();
  109. ////////////////////////////////////////////////////////////
  110. /// \brief Set a new model-view matrix
  111. ///
  112. /// Note: any call to this function after a call to BeginBatch
  113. /// will be ignored, and delayed until BeginBatch is called again.
  114. ///
  115. /// \param matrix New model-view matrix
  116. ///
  117. /// \see ApplyModelView
  118. ///
  119. ////////////////////////////////////////////////////////////
  120. void SetModelView(const Matrix3& matrix);
  121. ////////////////////////////////////////////////////////////
  122. /// \brief Combine a new model-view matrix with the current one
  123. ///
  124. /// Note: any call to this function after a call to BeginBatch
  125. /// will be ignored, and delayed until BeginBatch is called again.
  126. ///
  127. /// \param matrix Model-view matrix to combine
  128. ///
  129. /// \see SetModelView
  130. ///
  131. ////////////////////////////////////////////////////////////
  132. void ApplyModelView(const Matrix3& matrix);
  133. ////////////////////////////////////////////////////////////
  134. /// \brief Set a new projection matrix
  135. ///
  136. /// Note: any call to this function after a call to BeginBatch
  137. /// will be ignored, and delayed until BeginBatch is called again.
  138. ///
  139. /// \param matrix New projection matrix
  140. ///
  141. /// \see ApplyProjection
  142. ///
  143. ////////////////////////////////////////////////////////////
  144. void SetProjection(const Matrix3& matrix);
  145. ////////////////////////////////////////////////////////////
  146. /// \brief Set the current global color
  147. ///
  148. /// This color will be modulated with each vertex's color.
  149. /// Note: any call to this function after a call to BeginBatch
  150. /// will be ignored, and delayed until BeginBatch is called again.
  151. ///
  152. /// \param color New global color
  153. ///
  154. /// \see ApplyColor
  155. ///
  156. ////////////////////////////////////////////////////////////
  157. void SetColor(const Color& color);
  158. ////////////////////////////////////////////////////////////
  159. /// \brief Modulate the current global color with a new one
  160. ///
  161. /// This color will be modulated with each vertex's color.
  162. /// Note: any call to this function after a call to BeginBatch
  163. /// will be ignored, and delayed until BeginBatch is called again.
  164. ///
  165. /// \param color Color to modulate
  166. ///
  167. /// \see SetColor
  168. ///
  169. ////////////////////////////////////////////////////////////
  170. void ApplyColor(const Color& color);
  171. ////////////////////////////////////////////////////////////
  172. /// \brief Set the current viewport
  173. ///
  174. /// Note: any call to this function after a call to BeginBatch
  175. /// will be ignored, and delayed until BeginBatch is called again.
  176. ///
  177. /// \param viewport New viewport to apply
  178. ///
  179. ////////////////////////////////////////////////////////////
  180. void SetViewport(const IntRect& viewport);
  181. ////////////////////////////////////////////////////////////
  182. /// \brief Set the current alpha-blending mode
  183. ///
  184. /// Note: any call to this function after a call to BeginBatch
  185. /// will be ignored, and delayed until BeginBatch is called again.
  186. ///
  187. /// \param mode New blending mode
  188. ///
  189. ////////////////////////////////////////////////////////////
  190. void SetBlendMode(Blend::Mode mode);
  191. ////////////////////////////////////////////////////////////
  192. /// \brief Set the current texture
  193. ///
  194. /// Note: any call to this function after a call to BeginBatch
  195. /// will be ignored, and delayed until BeginBatch is called again.
  196. ///
  197. /// \param texture New texture
  198. ///
  199. ////////////////////////////////////////////////////////////
  200. void SetTexture(const Image* texture);
  201. ////////////////////////////////////////////////////////////
  202. /// \brief Set the current shader
  203. ///
  204. /// Note: any call to this function after a call to BeginBatch
  205. /// will be ignored, and delayed until BeginBatch is called again.
  206. ///
  207. /// \param shader New Shader
  208. ///
  209. ////////////////////////////////////////////////////////////
  210. void SetShader(const Shader* shader);
  211. ////////////////////////////////////////////////////////////
  212. /// \brief Begin rendering a new geometry batch
  213. ///
  214. /// You need to call End() to complete the batch and trigger
  215. /// the actual rendering of the geometry that you passed
  216. /// between Begin() and End().
  217. ///
  218. /// Usage:
  219. /// \code
  220. /// renderer.Begin(Renderer::TriangleList);
  221. /// renderer.AddVertex(...);
  222. /// renderer.AddVertex(...);
  223. /// renderer.AddVertex(...);
  224. /// renderer.End();
  225. /// \endcode
  226. ///
  227. /// \param type Type of the primitives that are going to be rendered
  228. ///
  229. /// \see End
  230. ///
  231. ////////////////////////////////////////////////////////////
  232. void Begin(PrimitiveType type);
  233. ////////////////////////////////////////////////////////////
  234. /// \brief End the current geometry batch and render it
  235. ///
  236. /// \see Begin
  237. ///
  238. ////////////////////////////////////////////////////////////
  239. void End();
  240. ////////////////////////////////////////////////////////////
  241. /// \brief Add a new vertex (position only)
  242. ///
  243. /// This function adds a new vertex to the current batch.
  244. /// This is equivalent to calling AddVertex(x, y, 0, 0, Color::White).
  245. ///
  246. /// \param x X coordinate of the vertex
  247. /// \param y Y coordinate of the vertex
  248. ///
  249. ////////////////////////////////////////////////////////////
  250. void AddVertex(float x, float y);
  251. ////////////////////////////////////////////////////////////
  252. /// \brief Add a new vertex (position + texture coordinates)
  253. ///
  254. /// This function adds a new vertex to the current batch.
  255. /// This is equivalent to calling AddVertex(x, y, u, v, Color::White).
  256. ///
  257. /// \param x X coordinate of the vertex
  258. /// \param y Y coordinate of the vertex
  259. /// \param u X texture coordinate of the vertex
  260. /// \param v Y texture coordinate of the vertex
  261. ///
  262. ////////////////////////////////////////////////////////////
  263. void AddVertex(float x, float y, float u, float v);
  264. ////////////////////////////////////////////////////////////
  265. /// \brief Add a new vertex (position + color)
  266. ///
  267. /// This function adds a new vertex to the current batch.
  268. /// This is equivalent to calling AddVertex(x, y, 0, 0, color).
  269. ///
  270. /// \param x X coordinate of the vertex
  271. /// \param y Y coordinate of the vertex
  272. /// \param color Color of the vertex
  273. ///
  274. ////////////////////////////////////////////////////////////
  275. void AddVertex(float x, float y, const Color& color);
  276. ////////////////////////////////////////////////////////////
  277. /// \brief Add a new vertex (position + texture coordinates + color)
  278. ///
  279. /// This function adds a new vertex to the current batch.
  280. ///
  281. /// \param x X coordinate of the vertex
  282. /// \param y Y coordinate of the vertex
  283. /// \param u X texture coordinate of the vertex
  284. /// \param v Y texture coordinate of the vertex
  285. /// \param color Color of the vertex
  286. ///
  287. ////////////////////////////////////////////////////////////
  288. void AddVertex(float x, float y, float u, float v, const Color& color);
  289. private :
  290. ////////////////////////////////////////////////////////////
  291. /// \brief Process a new vertex
  292. ///
  293. /// This function is called by all the public overloads of AddVertex,
  294. /// it processes a new vertex to be rendered.
  295. ///
  296. /// \param x X coordinate of the vertex
  297. /// \param y Y coordinate of the vertex
  298. /// \param u X texture coordinate of the vertex
  299. /// \param v Y texture coordinate of the vertex
  300. /// \param r Red component of the vertex color (normalized)
  301. /// \param g Green component of the vertex color (normalized)
  302. /// \param b Blue component of the vertex color (normalized)
  303. /// \param a Alpha component of the vertex color (normalized)
  304. ///
  305. ////////////////////////////////////////////////////////////
  306. void ProcessVertex(float x, float y, float u, float v, float r, float g, float b, float a);
  307. ////////////////////////////////////////////////////////////
  308. // Structure holding the render states that can be stacked
  309. ////////////////////////////////////////////////////////////
  310. struct States
  311. {
  312. States() : r(1.f), g(1.f), b(1.f), a(1.f) {}
  313. Matrix3 modelView; ///< Model-view matrix
  314. float r, g, b, a; ///< Vertex color (normalized components for faster operations)
  315. };
  316. ////////////////////////////////////////////////////////////
  317. // Member data
  318. ////////////////////////////////////////////////////////////
  319. RenderTarget& myTarget; ///< Reference to the render target owning this renderer
  320. States myStatesStack[64]; ///< Stack of render states
  321. States* myStates; ///< Current set of render states
  322. const Image* myTexture; ///< Current texture
  323. const Shader* myShader; ///< Current pixel shader
  324. Blend::Mode myBlendMode; ///< Current blending mode
  325. IntRect myViewport; ///< Current target viewport
  326. bool myTextureIsValid; ///< Is the cached texture valid? (if not, the cached value is ignored)
  327. bool myShaderIsValid; ///< Is the cached shader valid? (if not, the cached value is ignored)
  328. bool myBlendModeIsValid; ///< Is the cached blend mode valid? (if not, the cached value is ignored)
  329. bool myViewportIsValid; ///< Is the cached viewport valid? (if not, the cached value is ignored)
  330. };
  331. } // namespace sf
  332. #endif // SFML_RENDERER_HPP
  333. ////////////////////////////////////////////////////////////
  334. /// \class sf::Renderer
  335. /// \ingroup graphics
  336. ///
  337. /// sf::Renderer is the abstraction layer between SFML code
  338. /// and the low-level drawing API (OpenGL). It manages
  339. /// render states efficiently, and provides a lightweight
  340. /// abstraction for rendering geometry.
  341. ///
  342. /// The purpose of this class is to provide a single abstract
  343. /// entry point for everything related to low-level rendering.
  344. /// Hiding everything behind sf::Renderer makes optimizing
  345. /// easy, as well as porting to other technologies in the future
  346. /// (like OpenGL ES or OpenGL 3.x).
  347. ///
  348. /// This class is mainly meant for internal usage, you should
  349. /// never care about it unless you write your own sf::Drawable
  350. /// class that uses raw geometry in its Render function.
  351. ///
  352. ////////////////////////////////////////////////////////////