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