SFML 3.0.2
Loading...
Searching...
No Matches
RenderTarget.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2025 Laurent Gomila (laurent@sfml-dev.org)
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//
24
25#pragma once
26
28// Headers
30#include <SFML/Graphics/Export.hpp>
31
32#include <SFML/Graphics/BlendMode.hpp>
33#include <SFML/Graphics/Color.hpp>
34#include <SFML/Graphics/CoordinateType.hpp>
35#include <SFML/Graphics/PrimitiveType.hpp>
36#include <SFML/Graphics/Rect.hpp>
37#include <SFML/Graphics/RenderStates.hpp>
38#include <SFML/Graphics/StencilMode.hpp>
39#include <SFML/Graphics/Vertex.hpp>
40#include <SFML/Graphics/View.hpp>
41
42#include <SFML/System/Vector2.hpp>
43
44#include <array>
45
46#include <cstddef>
47#include <cstdint>
48
49
50 namespace sf
51{
52 class Drawable;
53 class Shader;
54 class Texture;
55 class Transform;
56 class VertexBuffer;
57
62 class SFML_GRAPHICS_API RenderTarget
63{
64 public:
69 virtual ~RenderTarget() = default;
70
75 RenderTarget(const RenderTarget&) = delete;
76
81 RenderTarget& operator=(const RenderTarget&) = delete;
82
87 RenderTarget(RenderTarget&&) noexcept = default;
88
93 RenderTarget& operator=(RenderTarget&&) noexcept = default;
94
104 void clear(Color color = Color::Black);
105
115 void clearStencil(StencilValue stencilValue);
116
127 void clear(Color color, StencilValue stencilValue);
128
148 void setView(const View& view);
149
158 [[nodiscard]] const View& getView() const;
159
171 [[nodiscard]] const View& getDefaultView() const;
172
186 [[nodiscard]] IntRect getViewport(const View& view) const;
187
201 [[nodiscard]] IntRect getScissor(const View& view) const;
202
221 [[nodiscard]] Vector2f mapPixelToCoords(Vector2i point) const;
222
252 [[nodiscard]] Vector2f mapPixelToCoords(Vector2i point, const View& view) const;
253
272 [[nodiscard]] Vector2i mapCoordsToPixel(Vector2f point) const;
273
299 [[nodiscard]] Vector2i mapCoordsToPixel(Vector2f point, const View& view) const;
300
308 void draw(const Drawable& drawable, const RenderStates& states = RenderStates::Default);
309
319 void draw(const Vertex* vertices,
320 std::size_t vertexCount,
321 PrimitiveType type,
322 const RenderStates& states = RenderStates::Default);
323
331 void draw(const VertexBuffer& vertexBuffer, const RenderStates& states = RenderStates::Default);
332
342 void draw(const VertexBuffer& vertexBuffer,
343 std::size_t firstVertex,
344 std::size_t vertexCount,
345 const RenderStates& states = RenderStates::Default);
346
353 [[nodiscard]] virtual Vector2u getSize() const = 0;
354
361 [[nodiscard]] virtual bool isSrgb() const;
362
383 [[nodiscard]] virtual bool setActive(bool active = true);
384
417 void pushGLStates();
418
428 void popGLStates();
429
451 void resetGLStates();
452
453protected:
458 RenderTarget() = default;
459
467 void initialize();
468
469private:
474 void applyCurrentView();
475
482 void applyBlendMode(const BlendMode& mode);
483
490 void applyStencilMode(const StencilMode& mode);
491
498 void applyTransform(const Transform& transform);
499
507 void applyTexture(const Texture* texture, CoordinateType coordinateType = CoordinateType::Pixels);
508
515 void applyShader(const Shader* shader);
516
524 void setupDraw(bool useVertexCache, const RenderStates& states);
525
534 void drawPrimitives(PrimitiveType type, std::size_t firstVertex, std::size_t vertexCount);
535
542 void cleanupDraw(const RenderStates& states);
543
548 struct StatesCache
549 {
550 bool enable{};
551 bool glStatesSet{};
552 bool viewChanged{};
553 bool scissorEnabled{};
554 bool stencilEnabled{};
555 BlendMode lastBlendMode;
556 StencilMode lastStencilMode;
557 std::uint64_t lastTextureId{};
558 CoordinateType lastCoordinateType{};
559 bool texCoordsArrayEnabled{};
560 bool useVertexCache{};
561 std::array<Vertex, 4> vertexCache{};
562 };
563
565 // Member data
567 View m_defaultView;
568 View m_view;
569 StatesCache m_cache{};
570 std::uint64_t m_id{};
571};
572
573} // namespace sf
574
575
BlendMode.hpp
Color.hpp
CoordinateType.hpp
Export.hpp
SFML_GRAPHICS_API
#define SFML_GRAPHICS_API
Definition Graphics/Export.hpp:42
PrimitiveType.hpp
Rect.hpp
RenderStates.hpp
StencilMode.hpp
Vector2.hpp
Vertex.hpp
View.hpp
sf::Color
Utility class for manipulating RGBA colors.
Definition Color.hpp:40
sf::Drawable
Abstract base class for objects that can be drawn to a render target.
Definition Drawable.hpp:44
sf::RenderTarget::setView
void setView(const View &view)
Change the current active view.
sf::RenderTarget::draw
void draw(const Drawable &drawable, const RenderStates &states=RenderStates::Default)
Draw a drawable object to the render target.
sf::RenderTarget::getScissor
IntRect getScissor(const View &view) const
Get the scissor rectangle of a view, applied to this render target.
sf::RenderTarget::getSize
virtual Vector2u getSize() const =0
Return the size of the rendering region of the target.
sf::RenderTarget::clearStencil
void clearStencil(StencilValue stencilValue)
Clear the stencil buffer to a specific value.
sf::RenderTarget::mapPixelToCoords
Vector2f mapPixelToCoords(Vector2i point) const
Convert a point from target coordinates to world coordinates, using the current view.
sf::RenderTarget::getDefaultView
const View & getDefaultView() const
Get the default view of the render target.
sf::RenderTarget::getViewport
IntRect getViewport(const View &view) const
Get the viewport of a view, applied to this render target.
sf::RenderTarget::pushGLStates
void pushGLStates()
Save the current OpenGL render states and matrices.
sf::RenderTarget::RenderTarget
RenderTarget(RenderTarget &&) noexcept=default
Move constructor.
sf::RenderTarget::resetGLStates
void resetGLStates()
Reset the internal OpenGL states so that the target is ready for drawing.
sf::RenderTarget::operator=
RenderTarget & operator=(const RenderTarget &)=delete
Deleted copy assignment.
sf::RenderTarget::mapCoordsToPixel
Vector2i mapCoordsToPixel(Vector2f point) const
Convert a point from world coordinates to target coordinates, using the current view.
sf::RenderTarget::~RenderTarget
virtual ~RenderTarget()=default
Destructor.
sf::RenderTarget::popGLStates
void popGLStates()
Restore the previously saved OpenGL render states and matrices.
sf::RenderTarget::getView
const View & getView() const
Get the view currently in use in the render target.
sf::RenderTarget::setActive
virtual bool setActive(bool active=true)
Activate or deactivate the render target for rendering.
sf::RenderTarget::isSrgb
virtual bool isSrgb() const
Tell if the render target will use sRGB encoding when drawing on it.
sf::RenderTarget::RenderTarget
RenderTarget(const RenderTarget &)=delete
Deleted copy constructor.
sf::RenderTarget::clear
void clear(Color color=Color::Black)
Clear the entire target with a single color.
sf::RenderTarget::initialize
void initialize()
Performs the common initialization step after creation.
sf::Shader
Shader class (vertex, geometry and fragment)
Definition Shader.hpp:54
sf::Texture
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:56
sf::Transform
3x3 transform matrix
Definition Transform.hpp:48
sf::VertexBuffer
Vertex buffer storage for one or more 2D primitives.
Definition VertexBuffer.hpp:51
sf::View
2D camera that defines what region is shown on screen
Definition View.hpp:46
sf::CoordinateType
CoordinateType
Types of texture coordinates that can be used for rendering.
Definition CoordinateType.hpp:38
sf::PrimitiveType
PrimitiveType
Types of primitives that a sf::VertexArray can render.
Definition PrimitiveType.hpp:39
sf::CoordinateType::Pixels
@ Pixels
Texture coordinates in range [0 .. size].
Definition CoordinateType.hpp:40
sf
Definition AudioResource.hpp:36
sf::Vector2u
Vector2< unsigned int > Vector2u
Definition Vector2.hpp:209
sf::IntRect
Rect< int > IntRect
Definition Rect.hpp:146
sf::Vector2i
Vector2< int > Vector2i
Definition Vector2.hpp:208
sf::Vector2f
Vector2< float > Vector2f
Definition Vector2.hpp:210
sf::BlendMode
Blending modes for drawing.
Definition BlendMode.hpp:41
sf::RenderStates
Define the states used for drawing to a RenderTarget
Definition RenderStates.hpp:48
sf::StencilMode
Stencil modes for drawing.
Definition StencilMode.hpp:107
sf::StencilValue
Stencil value type (also used as a mask)
Definition StencilMode.hpp:75
sf::Vertex
Point with color and texture coordinates.
Definition Vertex.hpp:44