Skip to content

class SF::View
inherits Reference #

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:

window = SF::RenderWindow.new
view = SF::View.new

# Initialize the view to a rectangle located at (100, 100) and with a size of 400x200
view.reset(SF.float_rect(100, 100, 400, 200))

# Rotate it by 45 degrees
view.rotate(45)

# Set its target viewport to be half of the window
view.viewport = SF.float_rect(0.0, 0.0, 0.5, 1.0)

# Apply it
window.view = view

# Render stuff
window.draw some_sprite

# Set the default view back
window.view = window.default_view

# Render stuff not affected by the view
window.draw some_text

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

See also: SF::RenderWindow, SF::RenderTexture

Class methods#

.new(center : Vector2 | Tuple, size : Vector2 | Tuple) #

Construct the view from its center and size

  • center - Center of the zone to display
  • size - Size of zone to display
View source

.new(rectangle : FloatRect) #

Construct the view from a rectangle

  • rectangle - Rectangle defining the zone to display
View source

.new #

Default constructor

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

View source

Methods#

#center : Vector2f #

Get the center of the view

Returns: Center of the view

See also: size, center=

View source

#center=(center : Vector2 | Tuple) #

Set the center of the view

  • center - New center

See also: size=, center

View source

#dup : View #

Returns a shallow copy of this object.

This allocates a new object and copies the contents of self into it.

View source

#finalize #

View source

#inverse_transform : Transform #

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: transform

View source

#move(offset_x : Number, offset_y : Number) #

Move the view relatively to its current position

  • offset_x - X coordinate of the move offset
  • offset_y - Y coordinate of the move offset

See also: center=, rotate, zoom

View source

#move(offset : Vector2 | Tuple) #

Move the view relatively to its current position

  • offset - Move offset

See also: center=, rotate, zoom

View source

#reset(rectangle : FloatRect) #

Reset the view to the given rectangle

Note that this function resets the rotation angle to 0.

  • rectangle - Rectangle defining the zone to display

See also: center=, size=, rotation=

View source

#rotate(angle : Number) #

Rotate the view relatively to its current orientation

  • angle - Angle to rotate, in degrees

See also: rotation=, move, zoom

View source

#rotation : Float32 #

Get the current orientation of the view

Returns: Rotation angle of the view, in degrees

See also: rotation=

View source

#rotation=(angle : Number) #

Set the orientation of the view

The default rotation of a view is 0 degree.

  • angle - New angle, in degrees

See also: rotation

View source

#set_center(x : Number, y : Number) #

Set the center of the view

  • x - X coordinate of the new center
  • y - Y coordinate of the new center

See also: size=, center

View source

#set_size(width : Number, height : Number) #

Set the size of the view

  • width - New width of the view
  • height - New height of the view

See also: center=, center

View source

#size : Vector2f #

Get the size of the view

Returns: Size of the view

See also: center, size=

View source

#size=(size : Vector2 | Tuple) #

Set the size of the view

  • size - New size

See also: center=, center

View source

#transform : Transform #

Get the projection transform of the view

This function is meant for internal use only.

Returns: Projection transform defining the view

See also: inverse_transform

View source

#viewport : FloatRect #

Get the target viewport rectangle of the view

Returns: Viewport rectangle, expressed as a factor of the target size

See also: viewport=

View source

#viewport=(viewport : FloatRect) #

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.viewport=(SF::FloatRect(0, 0, 0.5, 1)). By default, a view has a viewport which covers the entire target.

  • viewport - New viewport rectangle

See also: viewport

View source

#zoom(factor : Number) #

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)

  • factor - Zoom factor to apply

See also: size=, move, rotate

View source