class SF::View

Overview

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

Defined in:

graphics/obj.cr

Constructors

Instance Method Summary

Constructor Detail

def self.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]
def self.new(rectangle : FloatRect) #

Construct the view from a rectangle

  • rectangle - Rectangle defining the zone to display

[View source]
def self.new #

Default constructor

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


[View source]

Instance Method Detail

def center : Vector2f #

Get the center of the view

Returns: Center of the view

See also: #size, #center=


[View source]
def center=(center : Vector2 | Tuple) #

Set the center of the view

  • center - New center

See also: #size=, #center


[View source]
def dup : View #
Description copied from class Reference

Returns a shallow copy of this object.

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


[View source]
def finalize #

[View source]
def 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]
def 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]
def move(offset : Vector2 | Tuple) #

Move the view relatively to its current position

  • offset - Move offset

See also: #center=, #rotate, #zoom


[View source]
def 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]
def rotate(angle : Number) #

Rotate the view relatively to its current orientation

  • angle - Angle to rotate, in degrees

See also: #rotation=, #move, #zoom


[View source]
def rotation : Float32 #

Get the current orientation of the view

Returns: Rotation angle of the view, in degrees

See also: #rotation=


[View source]
def 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]
def 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]
def 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]
def size : Vector2f #

Get the size of the view

Returns: Size of the view

See also: #center, #size=


[View source]
def size=(size : Vector2 | Tuple) #

Set the size of the view

  • size - New size

See also: #center=, #center


[View source]
def 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]
def 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]
def 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.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


[View source]
def 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]