1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2025 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Export.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Window/ContextSettings.hpp>
#include <SFML/System/Vector2.hpp>
#include <memory>
namespace sf
{
namespace priv
{
class RenderTextureImpl;
}
////////////////////////////////////////////////////////////
/// \brief Target for off-screen 2D rendering into a texture
///
////////////////////////////////////////////////////////////
class SFML_GRAPHICS_API RenderTexture : public RenderTarget
{
public:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
/// Constructs a render-texture with width 0 and height 0.
///
/// \see `resize`
///
////////////////////////////////////////////////////////////
RenderTexture();
////////////////////////////////////////////////////////////
/// \brief Construct a render-texture
///
/// The last parameter, `settings`, is useful if you want to enable
/// multi-sampling or use the render-texture for OpenGL rendering that
/// requires a depth or stencil buffer. Otherwise it is unnecessary, and
/// you should leave this parameter at its default value.
///
/// After creation, the contents of the render-texture are undefined.
/// Call `RenderTexture::clear` first to ensure a single color fill.
///
/// \param size Width and height of the render-texture
/// \param settings Additional settings for the underlying OpenGL texture and context
///
/// \throws sf::Exception if creation was unsuccessful
///
////////////////////////////////////////////////////////////
RenderTexture(Vector2u size, const ContextSettings& settings = {});
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~RenderTexture() override;
////////////////////////////////////////////////////////////
/// \brief Deleted copy constructor
///
////////////////////////////////////////////////////////////
RenderTexture(const RenderTexture&) = delete;
////////////////////////////////////////////////////////////
/// \brief Deleted copy assignment
///
////////////////////////////////////////////////////////////
RenderTexture& operator=(const RenderTexture&) = delete;
////////////////////////////////////////////////////////////
/// \brief Move constructor
///
////////////////////////////////////////////////////////////
RenderTexture(RenderTexture&&) noexcept;
////////////////////////////////////////////////////////////
/// \brief Move assignment operator
///
////////////////////////////////////////////////////////////
RenderTexture& operator=(RenderTexture&&) noexcept;
////////////////////////////////////////////////////////////
/// \brief Resize the render-texture
///
/// The last parameter, `settings`, is useful if you want to enable
/// multi-sampling or use the render-texture for OpenGL rendering that
/// requires a depth or stencil buffer. Otherwise it is unnecessary, and
/// you should leave this parameter at its default value.
///
/// After resizing, the contents of the render-texture are undefined.
/// Call `RenderTexture::clear` first to ensure a single color fill.
///
/// \param size Width and height of the render-texture
/// \param settings Additional settings for the underlying OpenGL texture and context
///
/// \return `true` if resizing has been successful, `false` if it failed
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool resize(Vector2u size, const ContextSettings& settings = {});
////////////////////////////////////////////////////////////
/// \brief Get the maximum anti-aliasing level supported by the system
///
/// \return The maximum anti-aliasing level supported by the system
///
////////////////////////////////////////////////////////////
[[nodiscard]] static unsigned int getMaximumAntiAliasingLevel();
////////////////////////////////////////////////////////////
/// \brief Enable or disable texture smoothing
///
/// This function is similar to `Texture::setSmooth`.
/// This parameter is disabled by default.
///
/// \param smooth `true` to enable smoothing, `false` to disable it
///
/// \see `isSmooth`
///
////////////////////////////////////////////////////////////
void setSmooth(bool smooth);
////////////////////////////////////////////////////////////
/// \brief Tell whether the smooth filtering is enabled or not
///
/// \return `true` if texture smoothing is enabled
///
/// \see `setSmooth`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool isSmooth() const;
////////////////////////////////////////////////////////////
/// \brief Enable or disable texture repeating
///
/// This function is similar to `Texture::setRepeated`.
/// This parameter is disabled by default.
///
/// \param repeated `true` to enable repeating, `false` to disable it
///
/// \see `isRepeated`
///
////////////////////////////////////////////////////////////
void setRepeated(bool repeated);
////////////////////////////////////////////////////////////
/// \brief Tell whether the texture is repeated or not
///
/// \return `true` if texture is repeated
///
/// \see `setRepeated`
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool isRepeated() const;
////////////////////////////////////////////////////////////
/// \brief Generate a mipmap using the current texture data
///
/// This function is similar to `Texture::generateMipmap` and operates
/// on the texture used as the target for drawing.
/// Be aware that any draw operation may modify the base level image data.
/// For this reason, calling this function only makes sense after all
/// drawing is completed and display has been called. Not calling display
/// after subsequent drawing will lead to undefined behavior if a mipmap
/// had been previously generated.
///
/// \return `true` if mipmap generation was successful, `false` if unsuccessful
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool generateMipmap();
////////////////////////////////////////////////////////////
/// \brief Activate or deactivate the render-texture for rendering
///
/// This function makes the render-texture's context current for
/// future OpenGL rendering operations (so you shouldn't care
/// about it if you're not doing direct OpenGL stuff).
/// Only one context can be current in a thread, so if you
/// want to draw OpenGL geometry to another render target
/// (like a RenderWindow) don't forget to activate it again.
///
/// \param active `true` to activate, `false` to deactivate
///
/// \return `true` if operation was successful, `false` otherwise
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool setActive(bool active = true) override;
////////////////////////////////////////////////////////////
/// \brief Update the contents of the target texture
///
/// This function updates the target texture with what
/// has been drawn so far. Like for windows, calling this
/// function is mandatory at the end of rendering. Not calling
/// it may leave the texture in an undefined state.
///
////////////////////////////////////////////////////////////
void display();
////////////////////////////////////////////////////////////
/// \brief Return the size of the rendering region of the texture
///
/// The returned value is the size that you passed to
/// the create function.
///
/// \return Size in pixels
///
////////////////////////////////////////////////////////////
[[nodiscard]] Vector2u getSize() const override;
////////////////////////////////////////////////////////////
/// \brief Tell if the render-texture will use sRGB encoding when drawing on it
///
/// You can request sRGB encoding for a render-texture
/// by having the sRgbCapable flag set for the context parameter of `create()` method
///
/// \return `true` if the render-texture use sRGB encoding, `false` otherwise
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool isSrgb() const override;
////////////////////////////////////////////////////////////
/// \brief Get a read-only reference to the target texture
///
/// After drawing to the render-texture and calling Display,
/// you can retrieve the updated texture using this function,
/// and draw it using a sprite (for example).
/// The internal `sf::Texture` of a render-texture is always the
/// same instance, so that it is possible to call this function
/// once and keep a reference to the texture even after it is
/// modified.
///
/// \return Const reference to the texture
///
////////////////////////////////////////////////////////////
[[nodiscard]] const Texture& getTexture() const;
private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
std::unique_ptr<priv::RenderTextureImpl> m_impl; //!< Platform/hardware specific implementation
Texture m_texture; //!< Target texture to draw on
};
} // namespace sf
////////////////////////////////////////////////////////////
/// \class sf::RenderTexture
/// \ingroup graphics
///
/// `sf::RenderTexture` is the little brother of `sf::RenderWindow`.
/// It implements the same 2D drawing and OpenGL-related functions
/// (see their base class `sf::RenderTarget` for more details),
/// the difference is that the result is stored in an off-screen
/// texture rather than being show in a window.
///
/// Rendering to a texture can be useful in a variety of situations:
/// \li precomputing a complex static texture (like a level's background from multiple tiles)
/// \li applying post-effects to the whole scene with shaders
/// \li creating a sprite from a 3D object rendered with OpenGL
/// \li etc.
///
/// Usage example:
///
/// \code
/// // Create a new render-window
/// sf::RenderWindow window(sf::VideoMode({800, 600}), "SFML window");
///
/// // Create a new render-texture
/// sf::RenderTexture texture({500, 500});
///
/// // The main loop
/// while (window.isOpen())
/// {
/// // Event processing
/// // ...
///
/// // Clear the whole texture with red color
/// texture.clear(sf::Color::Red);
///
/// // Draw stuff to the texture
/// texture.draw(sprite); // sprite is a sf::Sprite
/// texture.draw(shape); // shape is a sf::Shape
/// texture.draw(text); // text is a sf::Text
///
/// // We're done drawing to the texture
/// texture.display();
///
/// // Now we start rendering to the window, clear it first
/// window.clear();
///
/// // Draw the texture
/// sf::Sprite sprite(texture.getTexture());
/// window.draw(sprite);
///
/// // End the current frame and display its contents on screen
/// window.display();
/// }
/// \endcode
///
/// Like `sf::RenderWindow`, `sf::RenderTexture` is still able to render direct
/// OpenGL stuff. It is even possible to mix together OpenGL calls
/// and regular SFML drawing commands. If you need a depth buffer for
/// 3D rendering, don't forget to request it when calling `RenderTexture::create`.
///
/// \see `sf::RenderTarget`, `sf::RenderWindow`, `sf::View`, `sf::Texture`
///
////////////////////////////////////////////////////////////
|