Documentation de SFML 2.6.2

Loading...
Searching...
No Matches

2D camera that defines what region is shown on screen More...

#include <SFML/Graphics/View.hpp>

Public Member Functions

 View ()
 Default constructor.
 
 View (const FloatRect &rectangle)
 Construct the view from a rectangle.
 
 View (const Vector2f &center, const Vector2f &size)
 Construct the view from its center and size.
 
void setCenter (float x, float y)
 Set the center of the view.
 
void setCenter (const Vector2f &center)
 Set the center of the view.
 
void setSize (float width, float height)
 Set the size of the view.
 
void setSize (const Vector2f &size)
 Set the size of the view.
 
void setRotation (float angle)
 Set the orientation of the view.
 
void setViewport (const FloatRect &viewport)
 Set the target viewport.
 
void reset (const FloatRect &rectangle)
 Reset the view to the given rectangle.
 
const Vector2fgetCenter () const
 Get the center of the view.
 
const Vector2fgetSize () const
 Get the size of the view.
 
float getRotation () const
 Get the current orientation of the view.
 
const FloatRectgetViewport () const
 Get the target viewport rectangle of the view.
 
void move (float offsetX, float offsetY)
 Move the view relatively to its current position.
 
void move (const Vector2f &offset)
 Move the view relatively to its current position.
 
void rotate (float angle)
 Rotate the view relatively to its current orientation.
 
void zoom (float factor)
 Resize the view rectangle relatively to its current size.
 
const TransformgetTransform () const
 Get the projection transform of the view.
 
const TransformgetInverseTransform () const
 Get the inverse projection transform of the view.
 

Detailed Description

2D camera that defines what region is shown on screen

sf::View defines a camera in the 2D scene.

This is a very powerful concept: you can scroll, rotate or zoom the entire scene without altering the way that your drawable objects are drawn.

A view is composed of a source rectangle, which defines what part of the 2D scene is shown, and a target viewport, which defines where the contents of the source rectangle will be displayed on the render target (window or texture).

The viewport allows to map the scene to a custom part of the render target, and can be used for split-screen or for displaying a minimap, for example. If the source rectangle doesn't have the same size as the viewport, its contents will be stretched to fit in.

To apply a view, you have to assign it to the render target. Then, objects drawn in this render target will be affected by the view until you use another view.

Usage example:

sf::View view;
// Initialize the view to a rectangle located at (100, 100) and with a size of 400x200
view.reset(sf::FloatRect(100, 100, 400, 200));
// Rotate it by 45 degrees
view.rotate(45);
// Set its target viewport to be half of the window
view.setViewport(sf::FloatRect(0.f, 0.f, 0.5f, 1.f));
// Apply it
window.setView(view);
// Render stuff
window.draw(someSprite);
// Set the default view back
window.setView(window.getDefaultView());
// Render stuff not affected by the view
window.draw(someText);
void setView(const View &view)
Change the current active view.
void draw(const Drawable &drawable, const RenderStates &states=RenderStates::Default)
Draw a drawable object to the render target.
const View & getDefaultView() const
Get the default view of the render target.
Window that can serve as a target for 2D drawing.
2D camera that defines what region is shown on screen
Definition View.hpp:44
void rotate(float angle)
Rotate the view relatively to its current orientation.
void setViewport(const FloatRect &viewport)
Set the target viewport.
void reset(const FloatRect &rectangle)
Reset the view to the given rectangle.

See also the note on coordinates and undistorted rendering in sf::Transformable.

See also
sf::RenderWindow, sf::RenderTexture

Definition at line 43 of file View.hpp.

Constructor & Destructor Documentation

◆ View() [1/3]

sf::View::View ( )

Default constructor.

This constructor creates a default view of (0, 0, 1000, 1000)

◆ View() [2/3]

sf::View::View ( const FloatRect & rectangle)
explicit

Construct the view from a rectangle.

Parameters
rectangleRectangle defining the zone to display

◆ View() [3/3]

sf::View::View ( const Vector2f & center,
const Vector2f & size )

Construct the view from its center and size.

Parameters
centerCenter of the zone to display
sizeSize of zone to display

Member Function Documentation

◆ getCenter()

const Vector2f & sf::View::getCenter ( ) const

Get the center of the view.

Returns
Center of the view
See also
getSize, setCenter

◆ getInverseTransform()

const Transform & sf::View::getInverseTransform ( ) const

Get the inverse projection transform of the view.

This function is meant for internal use only.

Returns
Inverse of the projection transform defining the view
See also
getTransform

◆ getRotation()

float sf::View::getRotation ( ) const

Get the current orientation of the view.

Returns
Rotation angle of the view, in degrees
See also
setRotation

◆ getSize()

const Vector2f & sf::View::getSize ( ) const

Get the size of the view.

Returns
Size of the view
See also
getCenter, setSize

◆ getTransform()

const Transform & sf::View::getTransform ( ) const

Get the projection transform of the view.

This function is meant for internal use only.

Returns
Projection transform defining the view
See also
getInverseTransform

◆ getViewport()

const FloatRect & sf::View::getViewport ( ) const

Get the target viewport rectangle of the view.

Returns
Viewport rectangle, expressed as a factor of the target size
See also
setViewport

◆ move() [1/2]

void sf::View::move ( const Vector2f & offset)

Move the view relatively to its current position.

Parameters
offsetMove offset
See also
setCenter, rotate, zoom

◆ move() [2/2]

void sf::View::move ( float offsetX,
float offsetY )

Move the view relatively to its current position.

Parameters
offsetXX coordinate of the move offset
offsetYY coordinate of the move offset
See also
setCenter, rotate, zoom

◆ reset()

void sf::View::reset ( const FloatRect & rectangle)

Reset the view to the given rectangle.

Note that this function resets the rotation angle to 0.

Parameters
rectangleRectangle defining the zone to display
See also
setCenter, setSize, setRotation

◆ rotate()

void sf::View::rotate ( float angle)

Rotate the view relatively to its current orientation.

Parameters
angleAngle to rotate, in degrees
See also
setRotation, move, zoom

◆ setCenter() [1/2]

void sf::View::setCenter ( const Vector2f & center)

Set the center of the view.

Parameters
centerNew center
See also
setSize, getCenter

◆ setCenter() [2/2]

void sf::View::setCenter ( float x,
float y )

Set the center of the view.

Parameters
xX coordinate of the new center
yY coordinate of the new center
See also
setSize, getCenter

◆ setRotation()

void sf::View::setRotation ( float angle)

Set the orientation of the view.

The default rotation of a view is 0 degree.

Parameters
angleNew angle, in degrees
See also
getRotation

◆ setSize() [1/2]

void sf::View::setSize ( const Vector2f & size)

Set the size of the view.

Parameters
sizeNew size
See also
setCenter, getCenter

◆ setSize() [2/2]

void sf::View::setSize ( float width,
float height )

Set the size of the view.

Parameters
widthNew width of the view
heightNew height of the view
See also
setCenter, getCenter

◆ setViewport()

void sf::View::setViewport ( const FloatRect & viewport)

Set the target viewport.

The viewport is the rectangle into which the contents of the view are displayed, expressed as a factor (between 0 and 1) of the size of the RenderTarget to which the view is applied. For example, a view which takes the left side of the target would be defined with View.setViewport(sf::FloatRect(0, 0, 0.5, 1)). By default, a view has a viewport which covers the entire target.

Parameters
viewportNew viewport rectangle
See also
getViewport

◆ zoom()

void sf::View::zoom ( float factor)

Resize the view rectangle relatively to its current size.

Resizing the view simulates a zoom, as the zone displayed on screen grows or shrinks. factor is a multiplier:

  • 1 keeps the size unchanged
  • > 1 makes the view bigger (objects appear smaller)
  • < 1 makes the view smaller (objects appear bigger)
Parameters
factorZoom factor to apply
See also
setSize, move, rotate

The documentation for this class was generated from the following file: