SF::View
Inherits Reference / Object
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
Constructors
Construct the view from its center and size
- center - Center of the zone to display
- size - Size of zone to display
Construct the view from a rectangle
- rectangle - Rectangle defining the zone to display
Instance methods
Set the center of the view
- center - New center
See also: size=, center
Returns a shallow copy of this object.
This allocates a new object and copies the contents of
self into it.
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
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
Move the view relatively to its current position
- offset - Move offset
See also: center=, rotate, zoom
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=
Rotate the view relatively to its current orientation
- angle - Angle to rotate, in degrees
See also: rotation=, move, zoom
Get the current orientation of the view
Returns: Rotation angle of the view, in degrees
See also: rotation=
Set the orientation of the view
The default rotation of a view is 0 degree.
- angle - New angle, in degrees
See also: rotation
Set the center of the view
- x - X coordinate of the new center
- y - Y coordinate of the new center
See also: size=, center
Set the size of the view
- width - New width of the view
- height - New height of the view
See also: center=, center
Set the size of the view
- size - New size
See also: center=, center
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
Get the target viewport rectangle of the view
Returns: Viewport rectangle, expressed as a factor of the target size
See also: 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.viewport = SF::FloatRect.new(0, 0, 0.5, 1).
By default, a view has a viewport which covers the entire target.
- viewport - New viewport rectangle
See also: viewport
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