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_VIEW_HPP
0026 #define SFML_VIEW_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Graphics/Export.hpp>
0032 #include <SFML/Graphics/Rect.hpp>
0033 #include <SFML/Graphics/Transform.hpp>
0034 #include <SFML/System/Vector2.hpp>
0035 
0036 
0037 namespace sf
0038 {
0039 ////////////////////////////////////////////////////////////
0040 /// \brief 2D camera that defines what region is shown on screen
0041 ///
0042 ////////////////////////////////////////////////////////////
0043 class SFML_GRAPHICS_API View
0044 {
0045 public:
0046 
0047     ////////////////////////////////////////////////////////////
0048     /// \brief Default constructor
0049     ///
0050     /// This constructor creates a default view of (0, 0, 1000, 1000)
0051     ///
0052     ////////////////////////////////////////////////////////////
0053     View();
0054 
0055     ////////////////////////////////////////////////////////////
0056     /// \brief Construct the view from a rectangle
0057     ///
0058     /// \param rectangle Rectangle defining the zone to display
0059     ///
0060     ////////////////////////////////////////////////////////////
0061     explicit View(const FloatRect& rectangle);
0062 
0063     ////////////////////////////////////////////////////////////
0064     /// \brief Construct the view from its center and size
0065     ///
0066     /// \param center Center of the zone to display
0067     /// \param size   Size of zone to display
0068     ///
0069     ////////////////////////////////////////////////////////////
0070     View(const Vector2f& center, const Vector2f& size);
0071 
0072     ////////////////////////////////////////////////////////////
0073     /// \brief Set the center of the view
0074     ///
0075     /// \param x X coordinate of the new center
0076     /// \param y Y coordinate of the new center
0077     ///
0078     /// \see setSize, getCenter
0079     ///
0080     ////////////////////////////////////////////////////////////
0081     void setCenter(float x, float y);
0082 
0083     ////////////////////////////////////////////////////////////
0084     /// \brief Set the center of the view
0085     ///
0086     /// \param center New center
0087     ///
0088     /// \see setSize, getCenter
0089     ///
0090     ////////////////////////////////////////////////////////////
0091     void setCenter(const Vector2f& center);
0092 
0093     ////////////////////////////////////////////////////////////
0094     /// \brief Set the size of the view
0095     ///
0096     /// \param width  New width of the view
0097     /// \param height New height of the view
0098     ///
0099     /// \see setCenter, getCenter
0100     ///
0101     ////////////////////////////////////////////////////////////
0102     void setSize(float width, float height);
0103 
0104     ////////////////////////////////////////////////////////////
0105     /// \brief Set the size of the view
0106     ///
0107     /// \param size New size
0108     ///
0109     /// \see setCenter, getCenter
0110     ///
0111     ////////////////////////////////////////////////////////////
0112     void setSize(const Vector2f& size);
0113 
0114     ////////////////////////////////////////////////////////////
0115     /// \brief Set the orientation of the view
0116     ///
0117     /// The default rotation of a view is 0 degree.
0118     ///
0119     /// \param angle New angle, in degrees
0120     ///
0121     /// \see getRotation
0122     ///
0123     ////////////////////////////////////////////////////////////
0124     void setRotation(float angle);
0125 
0126     ////////////////////////////////////////////////////////////
0127     /// \brief Set the target viewport
0128     ///
0129     /// The viewport is the rectangle into which the contents of the
0130     /// view are displayed, expressed as a factor (between 0 and 1)
0131     /// of the size of the RenderTarget to which the view is applied.
0132     /// For example, a view which takes the left side of the target would
0133     /// be defined with View.setViewport(sf::FloatRect(0, 0, 0.5, 1)).
0134     /// By default, a view has a viewport which covers the entire target.
0135     ///
0136     /// \param viewport New viewport rectangle
0137     ///
0138     /// \see getViewport
0139     ///
0140     ////////////////////////////////////////////////////////////
0141     void setViewport(const FloatRect& viewport);
0142 
0143     ////////////////////////////////////////////////////////////
0144     /// \brief Reset the view to the given rectangle
0145     ///
0146     /// Note that this function resets the rotation angle to 0.
0147     ///
0148     /// \param rectangle Rectangle defining the zone to display
0149     ///
0150     /// \see setCenter, setSize, setRotation
0151     ///
0152     ////////////////////////////////////////////////////////////
0153     void reset(const FloatRect& rectangle);
0154 
0155     ////////////////////////////////////////////////////////////
0156     /// \brief Get the center of the view
0157     ///
0158     /// \return Center of the view
0159     ///
0160     /// \see getSize, setCenter
0161     ///
0162     ////////////////////////////////////////////////////////////
0163     const Vector2f& getCenter() const;
0164 
0165     ////////////////////////////////////////////////////////////
0166     /// \brief Get the size of the view
0167     ///
0168     /// \return Size of the view
0169     ///
0170     /// \see getCenter, setSize
0171     ///
0172     ////////////////////////////////////////////////////////////
0173     const Vector2f& getSize() const;
0174 
0175     ////////////////////////////////////////////////////////////
0176     /// \brief Get the current orientation of the view
0177     ///
0178     /// \return Rotation angle of the view, in degrees
0179     ///
0180     /// \see setRotation
0181     ///
0182     ////////////////////////////////////////////////////////////
0183     float getRotation() const;
0184 
0185     ////////////////////////////////////////////////////////////
0186     /// \brief Get the target viewport rectangle of the view
0187     ///
0188     /// \return Viewport rectangle, expressed as a factor of the target size
0189     ///
0190     /// \see setViewport
0191     ///
0192     ////////////////////////////////////////////////////////////
0193     const FloatRect& getViewport() const;
0194 
0195     ////////////////////////////////////////////////////////////
0196     /// \brief Move the view relatively to its current position
0197     ///
0198     /// \param offsetX X coordinate of the move offset
0199     /// \param offsetY Y coordinate of the move offset
0200     ///
0201     /// \see setCenter, rotate, zoom
0202     ///
0203     ////////////////////////////////////////////////////////////
0204     void move(float offsetX, float offsetY);
0205 
0206     ////////////////////////////////////////////////////////////
0207     /// \brief Move the view relatively to its current position
0208     ///
0209     /// \param offset Move offset
0210     ///
0211     /// \see setCenter, rotate, zoom
0212     ///
0213     ////////////////////////////////////////////////////////////
0214     void move(const Vector2f& offset);
0215 
0216     ////////////////////////////////////////////////////////////
0217     /// \brief Rotate the view relatively to its current orientation
0218     ///
0219     /// \param angle Angle to rotate, in degrees
0220     ///
0221     /// \see setRotation, move, zoom
0222     ///
0223     ////////////////////////////////////////////////////////////
0224     void rotate(float angle);
0225 
0226     ////////////////////////////////////////////////////////////
0227     /// \brief Resize the view rectangle relatively to its current size
0228     ///
0229     /// Resizing the view simulates a zoom, as the zone displayed on
0230     /// screen grows or shrinks.
0231     /// \a factor is a multiplier:
0232     /// \li 1 keeps the size unchanged
0233     /// \li > 1 makes the view bigger (objects appear smaller)
0234     /// \li < 1 makes the view smaller (objects appear bigger)
0235     ///
0236     /// \param factor Zoom factor to apply
0237     ///
0238     /// \see setSize, move, rotate
0239     ///
0240     ////////////////////////////////////////////////////////////
0241     void zoom(float factor);
0242 
0243     ////////////////////////////////////////////////////////////
0244     /// \brief Get the projection transform of the view
0245     ///
0246     /// This function is meant for internal use only.
0247     ///
0248     /// \return Projection transform defining the view
0249     ///
0250     /// \see getInverseTransform
0251     ///
0252     ////////////////////////////////////////////////////////////
0253     const Transform& getTransform() const;
0254 
0255     ////////////////////////////////////////////////////////////
0256     /// \brief Get the inverse projection transform of the view
0257     ///
0258     /// This function is meant for internal use only.
0259     ///
0260     /// \return Inverse of the projection transform defining the view
0261     ///
0262     /// \see getTransform
0263     ///
0264     ////////////////////////////////////////////////////////////
0265     const Transform& getInverseTransform() const;
0266 
0267 private:
0268 
0269     ////////////////////////////////////////////////////////////
0270     // Member data
0271     ////////////////////////////////////////////////////////////
0272     Vector2f          m_center;              //!< Center of the view, in scene coordinates
0273     Vector2f          m_size;                //!< Size of the view, in scene coordinates
0274     float             m_rotation;            //!< Angle of rotation of the view rectangle, in degrees
0275     FloatRect         m_viewport;            //!< Viewport rectangle, expressed as a factor of the render-target's size
0276     mutable Transform m_transform;           //!< Precomputed projection transform corresponding to the view
0277     mutable Transform m_inverseTransform;    //!< Precomputed inverse projection transform corresponding to the view
0278     mutable bool      m_transformUpdated;    //!< Internal state telling if the transform needs to be updated
0279     mutable bool      m_invTransformUpdated; //!< Internal state telling if the inverse transform needs to be updated
0280 };
0281 
0282 } // namespace sf
0283 
0284 
0285 #endif // SFML_VIEW_HPP
0286 
0287 
0288 ////////////////////////////////////////////////////////////
0289 /// \class sf::View
0290 /// \ingroup graphics
0291 ///
0292 /// sf::View defines a camera in the 2D scene. This is a
0293 /// very powerful concept: you can scroll, rotate or zoom
0294 /// the entire scene without altering the way that your
0295 /// drawable objects are drawn.
0296 ///
0297 /// A view is composed of a source rectangle, which defines
0298 /// what part of the 2D scene is shown, and a target viewport,
0299 /// which defines where the contents of the source rectangle
0300 /// will be displayed on the render target (window or texture).
0301 ///
0302 /// The viewport allows to map the scene to a custom part
0303 /// of the render target, and can be used for split-screen
0304 /// or for displaying a minimap, for example. If the source
0305 /// rectangle doesn't have the same size as the viewport, its
0306 /// contents will be stretched to fit in.
0307 ///
0308 /// To apply a view, you have to assign it to the render target.
0309 /// Then, objects drawn in this render target will be
0310 /// affected by the view until you use another view.
0311 ///
0312 /// Usage example:
0313 /// \code
0314 /// sf::RenderWindow window;
0315 /// sf::View view;
0316 ///
0317 /// // Initialize the view to a rectangle located at (100, 100) and with a size of 400x200
0318 /// view.reset(sf::FloatRect(100, 100, 400, 200));
0319 ///
0320 /// // Rotate it by 45 degrees
0321 /// view.rotate(45);
0322 ///
0323 /// // Set its target viewport to be half of the window
0324 /// view.setViewport(sf::FloatRect(0.f, 0.f, 0.5f, 1.f));
0325 ///
0326 /// // Apply it
0327 /// window.setView(view);
0328 ///
0329 /// // Render stuff
0330 /// window.draw(someSprite);
0331 ///
0332 /// // Set the default view back
0333 /// window.setView(window.getDefaultView());
0334 ///
0335 /// // Render stuff not affected by the view
0336 /// window.draw(someText);
0337 /// \endcode
0338 ///
0339 /// See also the note on coordinates and undistorted rendering in sf::Transformable.
0340 ///
0341 /// \see sf::RenderWindow, sf::RenderTexture
0342 ///
0343 ////////////////////////////////////////////////////////////