1 ////////////////////////////////////////////////////////////
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
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.
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:
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.
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
21 // 3. This notice may not be removed or altered from any source distribution.
23 ////////////////////////////////////////////////////////////
25 #ifndef SFML_RENDERTEXTURE_HPP
26 #define SFML_RENDERTEXTURE_HPP
28 ////////////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////
31 #include <SFML/Graphics/Export.hpp>
32 #include <SFML/Graphics/Texture.hpp>
33 #include <SFML/Graphics/RenderTarget.hpp>
40 class RenderTextureImpl;
43 ////////////////////////////////////////////////////////////
44 /// \brief Target for off-screen 2D rendering into a texture
46 ////////////////////////////////////////////////////////////
47 class SFML_GRAPHICS_API RenderTexture : public RenderTarget
51 ////////////////////////////////////////////////////////////
52 /// \brief Default constructor
54 /// Constructs an empty, invalid render-texture. You must
55 /// call create to have a valid render-texture.
59 ////////////////////////////////////////////////////////////
62 ////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////
66 virtual ~RenderTexture();
68 ////////////////////////////////////////////////////////////
69 /// \brief Create the render-texture
71 /// Before calling this function, the render-texture is in
72 /// an invalid state, thus it is mandatory to call it before
73 /// doing anything with the render-texture.
74 /// The last parameter, \a depthBuffer, is useful if you want
75 /// to use the render-texture for 3D OpenGL rendering that requires
76 /// a depth-buffer. Otherwise it is unnecessary, and you should
77 /// leave this parameter to false (which is its default value).
79 /// \param width Width of the render-texture
80 /// \param height Height of the render-texture
81 /// \param depthBuffer Do you want this render-texture to have a depth buffer?
83 /// \return True if creation has been successful
85 ////////////////////////////////////////////////////////////
86 bool create(unsigned int width, unsigned int height, bool depthBuffer = false);
88 ////////////////////////////////////////////////////////////
89 /// \brief Enable or disable texture smoothing
91 /// This function is similar to Texture::setSmooth.
92 /// This parameter is disabled by default.
94 /// \param smooth True to enable smoothing, false to disable it
98 ////////////////////////////////////////////////////////////
99 void setSmooth(bool smooth);
101 ////////////////////////////////////////////////////////////
102 /// \brief Tell whether the smooth filtering is enabled or not
104 /// \return True if texture smoothing is enabled
108 ////////////////////////////////////////////////////////////
109 bool isSmooth() const;
111 ////////////////////////////////////////////////////////////
112 /// \brief Enable or disable texture repeating
114 /// This function is similar to Texture::setRepeated.
115 /// This parameter is disabled by default.
117 /// \param repeated True to enable repeating, false to disable it
121 ////////////////////////////////////////////////////////////
122 void setRepeated(bool repeated);
124 ////////////////////////////////////////////////////////////
125 /// \brief Tell whether the texture is repeated or not
127 /// \return True if texture is repeated
131 ////////////////////////////////////////////////////////////
132 bool isRepeated() const;
134 ////////////////////////////////////////////////////////////
135 /// \brief Activate of deactivate the render-texture for rendering
137 /// This function makes the render-texture's context current for
138 /// future OpenGL rendering operations (so you shouldn't care
139 /// about it if you're not doing direct OpenGL stuff).
140 /// Only one context can be current in a thread, so if you
141 /// want to draw OpenGL geometry to another render target
142 /// (like a RenderWindow) don't forget to activate it again.
144 /// \param active True to activate, false to deactivate
146 /// \return True if operation was successful, false otherwise
148 ////////////////////////////////////////////////////////////
149 bool setActive(bool active = true);
151 ////////////////////////////////////////////////////////////
152 /// \brief Update the contents of the target texture
154 /// This function updates the target texture with what
155 /// has been drawn so far. Like for windows, calling this
156 /// function is mandatory at the end of rendering. Not calling
157 /// it may leave the texture in an undefined state.
159 ////////////////////////////////////////////////////////////
162 ////////////////////////////////////////////////////////////
163 /// \brief Return the size of the rendering region of the texture
165 /// The returned value is the size that you passed to
166 /// the create function.
168 /// \return Size in pixels
170 ////////////////////////////////////////////////////////////
171 virtual Vector2u getSize() const;
173 ////////////////////////////////////////////////////////////
174 /// \brief Get a read-only reference to the target texture
176 /// After drawing to the render-texture and calling Display,
177 /// you can retrieve the updated texture using this function,
178 /// and draw it using a sprite (for example).
179 /// The internal sf::Texture of a render-texture is always the
180 /// same instance, so that it is possible to call this function
181 /// once and keep a reference to the texture even after it is
184 /// \return Const reference to the texture
186 ////////////////////////////////////////////////////////////
187 const Texture& getTexture() const;
191 ////////////////////////////////////////////////////////////
192 /// \brief Activate the target for rendering
194 /// This function is called by the base class
195 /// everytime it's going to use OpenGL calls.
197 /// \param active True to make the target active, false to deactivate it
199 /// \return True if the function succeeded
201 ////////////////////////////////////////////////////////////
202 virtual bool activate(bool active);
204 ////////////////////////////////////////////////////////////
206 ////////////////////////////////////////////////////////////
207 priv::RenderTextureImpl* m_impl; ///< Platform/hardware specific implementation
208 Texture m_texture; ///< Target texture to draw on
214 #endif // SFML_RENDERTEXTURE_HPP
217 ////////////////////////////////////////////////////////////
218 /// \class sf::RenderTexture
219 /// \ingroup graphics
221 /// sf::RenderTexture is the little brother of sf::RenderWindow.
222 /// It implements the same 2D drawing and OpenGL-related functions
223 /// (see their base class sf::RenderTarget for more details),
224 /// the difference is that the result is stored in an off-screen
225 /// texture rather than being show in a window.
227 /// Rendering to a texture can be useful in a variety of situations:
228 /// \li precomputing a complex static texture (like a level's background from multiple tiles)
229 /// \li applying post-effects to the whole scene with shaders
230 /// \li creating a sprite from a 3D object rendered with OpenGL
236 /// // Create a new render-window
237 /// sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
239 /// // Create a new render-texture
240 /// sf::RenderTexture texture;
241 /// if (!texture.create(500, 500))
245 /// while (window.isOpen())
247 /// // Event processing
250 /// // Clear the whole texture with red color
251 /// texture.clear(sf::Color::Red);
253 /// // Draw stuff to the texture
254 /// texture.draw(sprite); // sprite is a sf::Sprite
255 /// texture.draw(shape); // shape is a sf::Shape
256 /// texture.draw(text); // text is a sf::Text
258 /// // We're done drawing to the texture
259 /// texture.display();
261 /// // Now we start rendering to the window, clear it first
264 /// // Draw the texture
265 /// sf::Sprite sprite(texture.getTexture());
266 /// window.draw(sprite);
268 /// // End the current frame and display its contents on screen
269 /// window.display();
273 /// Like sf::RenderWindow, sf::RenderTexture is still able to render direct
274 /// OpenGL stuff. It is even possible to mix together OpenGL calls
275 /// and regular SFML drawing commands. If you need a depth buffer for
276 /// 3D rendering, don't forget to request it when calling RenderTexture::create.
278 /// \see sf::RenderTarget, sf::RenderWindow, sf::View, sf::Texture
280 ////////////////////////////////////////////////////////////