View

sfml.graphics.View
See theView companion object
trait View(using ctor: ctor)

2D camera that defines what region is shown on screen

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.

val window: RenderWindow = ???
val view: View = ???

val someSprite: Sprite = ???
val someText: Text = ???


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

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

// Set its target viewport to be half of the window
view.viewport = (0f, 0f, 0.5f, 1f)

// Apply it
window.view = view

// Render stuff
window.draw(someSprite)

// Set the default view back
window.view = window.defaultView

// Render stuff not affected by the view
window.draw(someText)

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

Attributes

See also
Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def center: Vector2[Float]

Get the center of the view.

Get the center of the view.

Attributes

Returns

Center of the view

See also
def center_=(x: Float, y: Float): Unit

Set the center of the view.

Set the center of the view.

Value parameters

x

X coordinate of the new center

y

Y coordinate of the new center

Attributes

See also
def center_=(center: Vector2[Float]): Unit

Set the center of the view.

Set the center of the view.

Value parameters

center

New center

Attributes

See also
def copy(): View

Get the inverse projection transform of the view.

Get the inverse projection transform of the view.

This function is meant for internal use only.

Attributes

Returns

Inverse of the projection transform defining the view

See also
def move(offsetX: Float, offsetY: Float): Unit

Move the view relatively to its current position.

Move the view relatively to its current position.

Value parameters

offsetX

X coordinate of the move offset

offsetY

Y coordinate of the move offset

Attributes

See also
def move(offset: Vector2[Float]): Unit

Move the view relatively to its current position.

Move the view relatively to its current position.

Value parameters

offset

Move offset

Attributes

See also
def reset(rect: Rect[Float]): Unit

Reset the view to the given rectangle.

Reset the view to the given rectangle.

Note that this function resets the rotation angle to 0.

Value parameters

rectangle

Rectangle defining the zone to display

Attributes

See also
def rotate(angle: Float): Unit

Rotate the view relatively to its current orientation.

Rotate the view relatively to its current orientation.

Value parameters

angle

Angle to rotate, in degrees

Attributes

See also
def rotation: Float

Get the current orientation of the view.

Get the current orientation of the view.

Attributes

Returns

Rotation angle of the view, in degrees

See also
def rotation_=(angle: Float): Unit

Set the orientation of the view.

Set the orientation of the view.

The default rotation of a view is 0 degree.

Value parameters

angle

New angle, in degrees

Attributes

See also
def size: Vector2[Float]

Get the size of the view.

Get the size of the view.

Attributes

Returns

Size of the view

See also
def size_=(width: Float, height: Float): Unit

Set the size of the view.

Set the size of the view.

Value parameters

height

New height of the view

width

New width of the view

Attributes

See also
def size_=(size: Vector2[Float]): Unit

Set the size of the view.

Set the size of the view.

Value parameters

size

New size

Attributes

See also

Get the projection transform of the view.

Get the projection transform of the view.

This function is meant for internal use only.

Attributes

Returns

Projection transform defining the view

See also
def viewport: Rect[Float]

Get the target viewport rectangle of the view.

Get the target viewport rectangle of the view.

Attributes

Returns

Viewport rectangle, expressed as a factor of the target size

See also
def viewport_=(viewport: Rect[Float]): Unit

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

Value parameters

viewport

New viewport rectangle

Attributes

See also
def zoom(factor: Float): Unit

Resize the view rectangle relatively to its current size.

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)

Value parameters

factor

Zoom factor to apply

Attributes

See also