Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:14

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_RENDERTARGET_HPP
0026 #define SFML_RENDERTARGET_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Graphics/Export.hpp>
0032 #include <SFML/Graphics/Color.hpp>
0033 #include <SFML/Graphics/Rect.hpp>
0034 #include <SFML/Graphics/View.hpp>
0035 #include <SFML/Graphics/Transform.hpp>
0036 #include <SFML/Graphics/BlendMode.hpp>
0037 #include <SFML/Graphics/RenderStates.hpp>
0038 #include <SFML/Graphics/PrimitiveType.hpp>
0039 #include <SFML/Graphics/Vertex.hpp>
0040 #include <SFML/System/NonCopyable.hpp>
0041 
0042 
0043 namespace sf
0044 {
0045 class Drawable;
0046 class VertexBuffer;
0047 
0048 ////////////////////////////////////////////////////////////
0049 /// \brief Base class for all render targets (window, texture, ...)
0050 ///
0051 ////////////////////////////////////////////////////////////
0052 class SFML_GRAPHICS_API RenderTarget : NonCopyable
0053 {
0054 public:
0055 
0056     ////////////////////////////////////////////////////////////
0057     /// \brief Destructor
0058     ///
0059     ////////////////////////////////////////////////////////////
0060     virtual ~RenderTarget();
0061 
0062     ////////////////////////////////////////////////////////////
0063     /// \brief Clear the entire target with a single color
0064     ///
0065     /// This function is usually called once every frame,
0066     /// to clear the previous contents of the target.
0067     ///
0068     /// \param color Fill color to use to clear the render target
0069     ///
0070     ////////////////////////////////////////////////////////////
0071     void clear(const Color& color = Color(0, 0, 0, 255));
0072 
0073     ////////////////////////////////////////////////////////////
0074     /// \brief Change the current active view
0075     ///
0076     /// The view is like a 2D camera, it controls which part of
0077     /// the 2D scene is visible, and how it is viewed in the
0078     /// render target.
0079     /// The new view will affect everything that is drawn, until
0080     /// another view is set.
0081     /// The render target keeps its own copy of the view object,
0082     /// so it is not necessary to keep the original one alive
0083     /// after calling this function.
0084     /// To restore the original view of the target, you can pass
0085     /// the result of getDefaultView() to this function.
0086     ///
0087     /// \param view New view to use
0088     ///
0089     /// \see getView, getDefaultView
0090     ///
0091     ////////////////////////////////////////////////////////////
0092     void setView(const View& view);
0093 
0094     ////////////////////////////////////////////////////////////
0095     /// \brief Get the view currently in use in the render target
0096     ///
0097     /// \return The view object that is currently used
0098     ///
0099     /// \see setView, getDefaultView
0100     ///
0101     ////////////////////////////////////////////////////////////
0102     const View& getView() const;
0103 
0104     ////////////////////////////////////////////////////////////
0105     /// \brief Get the default view of the render target
0106     ///
0107     /// The default view has the initial size of the render target,
0108     /// and never changes after the target has been created.
0109     ///
0110     /// \return The default view of the render target
0111     ///
0112     /// \see setView, getView
0113     ///
0114     ////////////////////////////////////////////////////////////
0115     const View& getDefaultView() const;
0116 
0117     ////////////////////////////////////////////////////////////
0118     /// \brief Get the viewport of a view, applied to this render target
0119     ///
0120     /// The viewport is defined in the view as a ratio, this function
0121     /// simply applies this ratio to the current dimensions of the
0122     /// render target to calculate the pixels rectangle that the viewport
0123     /// actually covers in the target.
0124     ///
0125     /// \param view The view for which we want to compute the viewport
0126     ///
0127     /// \return Viewport rectangle, expressed in pixels
0128     ///
0129     ////////////////////////////////////////////////////////////
0130     IntRect getViewport(const View& view) const;
0131 
0132     ////////////////////////////////////////////////////////////
0133     /// \brief Convert a point from target coordinates to world
0134     ///        coordinates, using the current view
0135     ///
0136     /// This function is an overload of the mapPixelToCoords
0137     /// function that implicitly uses the current view.
0138     /// It is equivalent to:
0139     /// \code
0140     /// target.mapPixelToCoords(point, target.getView());
0141     /// \endcode
0142     ///
0143     /// \param point Pixel to convert
0144     ///
0145     /// \return The converted point, in "world" coordinates
0146     ///
0147     /// \see mapCoordsToPixel
0148     ///
0149     ////////////////////////////////////////////////////////////
0150     Vector2f mapPixelToCoords(const Vector2i& point) const;
0151 
0152     ////////////////////////////////////////////////////////////
0153     /// \brief Convert a point from target coordinates to world coordinates
0154     ///
0155     /// This function finds the 2D position that matches the
0156     /// given pixel of the render target. In other words, it does
0157     /// the inverse of what the graphics card does, to find the
0158     /// initial position of a rendered pixel.
0159     ///
0160     /// Initially, both coordinate systems (world units and target pixels)
0161     /// match perfectly. But if you define a custom view or resize your
0162     /// render target, this assertion is not true anymore, i.e. a point
0163     /// located at (10, 50) in your render target may map to the point
0164     /// (150, 75) in your 2D world -- if the view is translated by (140, 25).
0165     ///
0166     /// For render-windows, this function is typically used to find
0167     /// which point (or object) is located below the mouse cursor.
0168     ///
0169     /// This version uses a custom view for calculations, see the other
0170     /// overload of the function if you want to use the current view of the
0171     /// render target.
0172     ///
0173     /// \param point Pixel to convert
0174     /// \param view The view to use for converting the point
0175     ///
0176     /// \return The converted point, in "world" units
0177     ///
0178     /// \see mapCoordsToPixel
0179     ///
0180     ////////////////////////////////////////////////////////////
0181     Vector2f mapPixelToCoords(const Vector2i& point, const View& view) const;
0182 
0183     ////////////////////////////////////////////////////////////
0184     /// \brief Convert a point from world coordinates to target
0185     ///        coordinates, using the current view
0186     ///
0187     /// This function is an overload of the mapCoordsToPixel
0188     /// function that implicitly uses the current view.
0189     /// It is equivalent to:
0190     /// \code
0191     /// target.mapCoordsToPixel(point, target.getView());
0192     /// \endcode
0193     ///
0194     /// \param point Point to convert
0195     ///
0196     /// \return The converted point, in target coordinates (pixels)
0197     ///
0198     /// \see mapPixelToCoords
0199     ///
0200     ////////////////////////////////////////////////////////////
0201     Vector2i mapCoordsToPixel(const Vector2f& point) const;
0202 
0203     ////////////////////////////////////////////////////////////
0204     /// \brief Convert a point from world coordinates to target coordinates
0205     ///
0206     /// This function finds the pixel of the render target that matches
0207     /// the given 2D point. In other words, it goes through the same process
0208     /// as the graphics card, to compute the final position of a rendered point.
0209     ///
0210     /// Initially, both coordinate systems (world units and target pixels)
0211     /// match perfectly. But if you define a custom view or resize your
0212     /// render target, this assertion is not true anymore, i.e. a point
0213     /// located at (150, 75) in your 2D world may map to the pixel
0214     /// (10, 50) of your render target -- if the view is translated by (140, 25).
0215     ///
0216     /// This version uses a custom view for calculations, see the other
0217     /// overload of the function if you want to use the current view of the
0218     /// render target.
0219     ///
0220     /// \param point Point to convert
0221     /// \param view The view to use for converting the point
0222     ///
0223     /// \return The converted point, in target coordinates (pixels)
0224     ///
0225     /// \see mapPixelToCoords
0226     ///
0227     ////////////////////////////////////////////////////////////
0228     Vector2i mapCoordsToPixel(const Vector2f& point, const View& view) const;
0229 
0230     ////////////////////////////////////////////////////////////
0231     /// \brief Draw a drawable object to the render target
0232     ///
0233     /// \param drawable Object to draw
0234     /// \param states   Render states to use for drawing
0235     ///
0236     ////////////////////////////////////////////////////////////
0237     void draw(const Drawable& drawable, const RenderStates& states = RenderStates::Default);
0238 
0239     ////////////////////////////////////////////////////////////
0240     /// \brief Draw primitives defined by an array of vertices
0241     ///
0242     /// \param vertices    Pointer to the vertices
0243     /// \param vertexCount Number of vertices in the array
0244     /// \param type        Type of primitives to draw
0245     /// \param states      Render states to use for drawing
0246     ///
0247     ////////////////////////////////////////////////////////////
0248     void draw(const Vertex* vertices, std::size_t vertexCount,
0249               PrimitiveType type, const RenderStates& states = RenderStates::Default);
0250 
0251     ////////////////////////////////////////////////////////////
0252     /// \brief Draw primitives defined by a vertex buffer
0253     ///
0254     /// \param vertexBuffer Vertex buffer
0255     /// \param states       Render states to use for drawing
0256     ///
0257     ////////////////////////////////////////////////////////////
0258     void draw(const VertexBuffer& vertexBuffer, const RenderStates& states = RenderStates::Default);
0259 
0260     ////////////////////////////////////////////////////////////
0261     /// \brief Draw primitives defined by a vertex buffer
0262     ///
0263     /// \param vertexBuffer Vertex buffer
0264     /// \param firstVertex  Index of the first vertex to render
0265     /// \param vertexCount  Number of vertices to render
0266     /// \param states       Render states to use for drawing
0267     ///
0268     ////////////////////////////////////////////////////////////
0269     void draw(const VertexBuffer& vertexBuffer, std::size_t firstVertex, std::size_t vertexCount, const RenderStates& states = RenderStates::Default);
0270 
0271     ////////////////////////////////////////////////////////////
0272     /// \brief Return the size of the rendering region of the target
0273     ///
0274     /// \return Size in pixels
0275     ///
0276     ////////////////////////////////////////////////////////////
0277     virtual Vector2u getSize() const = 0;
0278 
0279     ////////////////////////////////////////////////////////////
0280     /// \brief Tell if the render target will use sRGB encoding when drawing on it
0281     ///
0282     /// \return True if the render target use sRGB encoding, false otherwise
0283     ///
0284     ////////////////////////////////////////////////////////////
0285     virtual bool isSrgb() const;
0286 
0287     ////////////////////////////////////////////////////////////
0288     /// \brief Activate or deactivate the render target for rendering
0289     ///
0290     /// This function makes the render target's context current for
0291     /// future OpenGL rendering operations (so you shouldn't care
0292     /// about it if you're not doing direct OpenGL stuff).
0293     /// A render target's context is active only on the current thread,
0294     /// if you want to make it active on another thread you have
0295     /// to deactivate it on the previous thread first if it was active.
0296     /// Only one context can be current in a thread, so if you
0297     /// want to draw OpenGL geometry to another render target
0298     /// don't forget to activate it again. Activating a render
0299     /// target will automatically deactivate the previously active
0300     /// context (if any).
0301     ///
0302     /// \param active True to activate, false to deactivate
0303     ///
0304     /// \return True if operation was successful, false otherwise
0305     ///
0306     ////////////////////////////////////////////////////////////
0307     virtual bool setActive(bool active = true);
0308 
0309     ////////////////////////////////////////////////////////////
0310     /// \brief Save the current OpenGL render states and matrices
0311     ///
0312     /// This function can be used when you mix SFML drawing
0313     /// and direct OpenGL rendering. Combined with popGLStates,
0314     /// it ensures that:
0315     /// \li SFML's internal states are not messed up by your OpenGL code
0316     /// \li your OpenGL states are not modified by a call to a SFML function
0317     ///
0318     /// More specifically, it must be used around code that
0319     /// calls Draw functions. Example:
0320     /// \code
0321     /// // OpenGL code here...
0322     /// window.pushGLStates();
0323     /// window.draw(...);
0324     /// window.draw(...);
0325     /// window.popGLStates();
0326     /// // OpenGL code here...
0327     /// \endcode
0328     ///
0329     /// Note that this function is quite expensive: it saves all the
0330     /// possible OpenGL states and matrices, even the ones you
0331     /// don't care about. Therefore it should be used wisely.
0332     /// It is provided for convenience, but the best results will
0333     /// be achieved if you handle OpenGL states yourself (because
0334     /// you know which states have really changed, and need to be
0335     /// saved and restored). Take a look at the resetGLStates
0336     /// function if you do so.
0337     ///
0338     /// \see popGLStates
0339     ///
0340     ////////////////////////////////////////////////////////////
0341     void pushGLStates();
0342 
0343     ////////////////////////////////////////////////////////////
0344     /// \brief Restore the previously saved OpenGL render states and matrices
0345     ///
0346     /// See the description of pushGLStates to get a detailed
0347     /// description of these functions.
0348     ///
0349     /// \see pushGLStates
0350     ///
0351     ////////////////////////////////////////////////////////////
0352     void popGLStates();
0353 
0354     ////////////////////////////////////////////////////////////
0355     /// \brief Reset the internal OpenGL states so that the target is ready for drawing
0356     ///
0357     /// This function can be used when you mix SFML drawing
0358     /// and direct OpenGL rendering, if you choose not to use
0359     /// pushGLStates/popGLStates. It makes sure that all OpenGL
0360     /// states needed by SFML are set, so that subsequent draw()
0361     /// calls will work as expected.
0362     ///
0363     /// Example:
0364     /// \code
0365     /// // OpenGL code here...
0366     /// glPushAttrib(...);
0367     /// window.resetGLStates();
0368     /// window.draw(...);
0369     /// window.draw(...);
0370     /// glPopAttrib(...);
0371     /// // OpenGL code here...
0372     /// \endcode
0373     ///
0374     ////////////////////////////////////////////////////////////
0375     void resetGLStates();
0376 
0377 protected:
0378 
0379     ////////////////////////////////////////////////////////////
0380     /// \brief Default constructor
0381     ///
0382     ////////////////////////////////////////////////////////////
0383     RenderTarget();
0384 
0385     ////////////////////////////////////////////////////////////
0386     /// \brief Performs the common initialization step after creation
0387     ///
0388     /// The derived classes must call this function after the
0389     /// target is created and ready for drawing.
0390     ///
0391     ////////////////////////////////////////////////////////////
0392     void initialize();
0393 
0394 private:
0395 
0396     ////////////////////////////////////////////////////////////
0397     /// \brief Apply the current view
0398     ///
0399     ////////////////////////////////////////////////////////////
0400     void applyCurrentView();
0401 
0402     ////////////////////////////////////////////////////////////
0403     /// \brief Apply a new blending mode
0404     ///
0405     /// \param mode Blending mode to apply
0406     ///
0407     ////////////////////////////////////////////////////////////
0408     void applyBlendMode(const BlendMode& mode);
0409 
0410     ////////////////////////////////////////////////////////////
0411     /// \brief Apply a new transform
0412     ///
0413     /// \param transform Transform to apply
0414     ///
0415     ////////////////////////////////////////////////////////////
0416     void applyTransform(const Transform& transform);
0417 
0418     ////////////////////////////////////////////////////////////
0419     /// \brief Apply a new texture
0420     ///
0421     /// \param texture Texture to apply
0422     ///
0423     ////////////////////////////////////////////////////////////
0424     void applyTexture(const Texture* texture);
0425 
0426     ////////////////////////////////////////////////////////////
0427     /// \brief Apply a new shader
0428     ///
0429     /// \param shader Shader to apply
0430     ///
0431     ////////////////////////////////////////////////////////////
0432     void applyShader(const Shader* shader);
0433 
0434     ////////////////////////////////////////////////////////////
0435     /// \brief Setup environment for drawing
0436     ///
0437     /// \param useVertexCache Are we going to use the vertex cache?
0438     /// \param states         Render states to use for drawing
0439     ///
0440     ////////////////////////////////////////////////////////////
0441     void setupDraw(bool useVertexCache, const RenderStates& states);
0442 
0443     ////////////////////////////////////////////////////////////
0444     /// \brief Draw the primitives
0445     ///
0446     /// \param type        Type of primitives to draw
0447     /// \param firstVertex Index of the first vertex to use when drawing
0448     /// \param vertexCount Number of vertices to use when drawing
0449     ///
0450     ////////////////////////////////////////////////////////////
0451     void drawPrimitives(PrimitiveType type, std::size_t firstVertex, std::size_t vertexCount);
0452 
0453     ////////////////////////////////////////////////////////////
0454     /// \brief Clean up environment after drawing
0455     ///
0456     /// \param states Render states used for drawing
0457     ///
0458     ////////////////////////////////////////////////////////////
0459     void cleanupDraw(const RenderStates& states);
0460 
0461     ////////////////////////////////////////////////////////////
0462     /// \brief Render states cache
0463     ///
0464     ////////////////////////////////////////////////////////////
0465     struct StatesCache
0466     {
0467         enum {VertexCacheSize = 4};
0468 
0469         bool      enable;         //!< Is the cache enabled?
0470         bool      glStatesSet;    //!< Are our internal GL states set yet?
0471         bool      viewChanged;    //!< Has the current view changed since last draw?
0472         BlendMode lastBlendMode;  //!< Cached blending mode
0473         Uint64    lastTextureId;  //!< Cached texture
0474         bool      texCoordsArrayEnabled; //!< Is GL_TEXTURE_COORD_ARRAY client state enabled?
0475         bool      useVertexCache; //!< Did we previously use the vertex cache?
0476         Vertex    vertexCache[VertexCacheSize]; //!< Pre-transformed vertices cache
0477     };
0478 
0479     ////////////////////////////////////////////////////////////
0480     // Member data
0481     ////////////////////////////////////////////////////////////
0482     View        m_defaultView; //!< Default view
0483     View        m_view;        //!< Current view
0484     StatesCache m_cache;       //!< Render states cache
0485     Uint64      m_id;          //!< Unique number that identifies the RenderTarget
0486 };
0487 
0488 } // namespace sf
0489 
0490 
0491 #endif // SFML_RENDERTARGET_HPP
0492 
0493 
0494 ////////////////////////////////////////////////////////////
0495 /// \class sf::RenderTarget
0496 /// \ingroup graphics
0497 ///
0498 /// sf::RenderTarget defines the common behavior of all the
0499 /// 2D render targets usable in the graphics module. It makes
0500 /// it possible to draw 2D entities like sprites, shapes, text
0501 /// without using any OpenGL command directly.
0502 ///
0503 /// A sf::RenderTarget is also able to use views (sf::View),
0504 /// which are a kind of 2D cameras. With views you can globally
0505 /// scroll, rotate or zoom everything that is drawn,
0506 /// without having to transform every single entity. See the
0507 /// documentation of sf::View for more details and sample pieces of
0508 /// code about this class.
0509 ///
0510 /// On top of that, render targets are still able to render direct
0511 /// OpenGL stuff. It is even possible to mix together OpenGL calls
0512 /// and regular SFML drawing commands. When doing so, make sure that
0513 /// OpenGL states are not messed up by calling the
0514 /// pushGLStates/popGLStates functions.
0515 ///
0516 /// \see sf::RenderWindow, sf::RenderTexture, sf::View
0517 ///
0518 ////////////////////////////////////////////////////////////