Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
Public Member Functions | Protected Member Functions | List of all members
gf::View Class Reference

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

#include <gf/View.h>

Inheritance diagram for gf::View:
Inheritance graph
[legend]

Public Member Functions

 View ()
 Default constructor. More...
 
 View (const RectF &rect)
 Construct the view from a rectangle. More...
 
 View (Vector2f center, Vector2f size)
 Construct the view from its center and size. More...
 
virtual ~View ()
 Destructor. More...
 
void setCenter (Vector2f center)
 Set the center of the view. More...
 
Vector2f getCenter () const
 Get the center of the view. More...
 
void setSize (Vector2f size)
 Set the size of the view. More...
 
Vector2f getSize () const
 Get the size of the view. More...
 
void setRotation (float rotation)
 Set the orientation of the view. More...
 
float getRotation () const
 Get the current orientation of the view. More...
 
void setViewport (const RectF &viewport)
 Set the target viewport. More...
 
const RectFgetViewport () const
 Get the target viewport rectangle of the view. More...
 
void reset (const RectF &rect)
 Reset the view to the given rectangle. More...
 
void move (Vector2f offset)
 Move the view relatively to its current position. More...
 
void rotate (float angle)
 Rotate the view relatively to its current orientation. More...
 
void zoom (float factor)
 Resize the view rectangle relatively to its current size. More...
 
void zoom (float factor, Vector2f fixed)
 Resize the view rectangle relatively to its current size and a fixed point. More...
 
Matrix3f getTransform () const
 Get the projection transform of the view. More...
 
Matrix3f getInverseTransform () const
 Get the inverse projection transform of the view. More...
 

Protected Member Functions

void setSizeNoCallback (Vector2f size)
 Set the world size, without calling onSizeChange() More...
 
virtual void onSizeChange (Vector2f size)
 Callback when the world has just been resized. More...
 
void setViewportNoCallback (const RectF &viewport)
 Set the viewport, without calling onViewportChange() More...
 
virtual void onViewportChange (const RectF &viewport)
 Callback when the viewport has just been changed. More...
 

Detailed Description

2D camera that defines what region is shown on screen

gf::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 has not 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, every objects drawn in this render target will be affected by the view until you use another view.

Usage example:

gf::View view;
// Initialize the view to a rectangle located at (100, 100) and
// with a size of 400x200
view.reset({ 100.0f, 100.0f, 400.0f, 200.0f });
// Rotate it by 45 degrees
view.rotate(gf::Pi / 4);
// Set its target viewport to be half of the window
view.setViewport({ 0.f, 0.f, 0.5f, 1.f });
// Apply it
renderer.setView(view);
// Render stuff
renderer.draw(someSprite);
// Set the default view back
renderer.setView(renderer.getDefaultView());
// Render stuff not affected by the view
renderer.draw(someOtherSprite);
See also
gf::RenderTarget, gf::AdaptiveView

Constructor & Destructor Documentation

◆ View() [1/3]

gf::View::View ( )

Default constructor.

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

◆ View() [2/3]

gf::View::View ( const RectF rect)
explicit

Construct the view from a rectangle.

Parameters
rectRectangle defining the zone to display

◆ View() [3/3]

gf::View::View ( Vector2f  center,
Vector2f  size 
)

Construct the view from its center and size.

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

◆ ~View()

virtual gf::View::~View ( )
virtual

Destructor.

Member Function Documentation

◆ getCenter()

Vector2f gf::View::getCenter ( ) const
inline

Get the center of the view.

Returns
Center of the view
See also
setCenter()

◆ getInverseTransform()

Matrix3f gf::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 gf::View::getRotation ( ) const
inline

Get the current orientation of the view.

Returns
Rotation angle of the view, in radians
See also
setRotation()

◆ getSize()

Vector2f gf::View::getSize ( ) const
inline

Get the size of the view.

Returns
Size of the view
See also
setSize()

◆ getTransform()

Matrix3f gf::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 RectF& gf::View::getViewport ( ) const
inline

Get the target viewport rectangle of the view.

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

◆ move()

void gf::View::move ( Vector2f  offset)

Move the view relatively to its current position.

Parameters
offsetMove offset
See also
setCenter(), rotate(), zoom()

◆ onSizeChange()

virtual void gf::View::onSizeChange ( Vector2f  size)
protectedvirtual

Callback when the world has just been resized.

This callback is called when setSize() is called.

Parameters
sizeThe new size of the visible world

Reimplemented in gf::ExtendView, gf::FillView, and gf::FitView.

◆ onViewportChange()

virtual void gf::View::onViewportChange ( const RectF viewport)
protectedvirtual

Callback when the viewport has just been changed.

Parameters
viewportThe new viewport

Reimplemented in gf::ScreenView, gf::ExtendView, gf::FillView, and gf::FitView.

◆ reset()

void gf::View::reset ( const RectF rect)

Reset the view to the given rectangle.

Note that this function resets the rotation angle to 0.

Parameters
rectRectangle defining the zone to display
See also
setCenter(), setSize(), setRotation()

◆ rotate()

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

Rotate the view relatively to its current orientation.

Parameters
angleAngle to rotate, in radians
See also
setRotation(), move(), zoom()

◆ setCenter()

void gf::View::setCenter ( Vector2f  center)
inline

Set the center of the view.

Parameters
centerNew center
See also
getCenter()

◆ setRotation()

void gf::View::setRotation ( float  rotation)
inline

Set the orientation of the view.

The default rotation of a view is 0 degree.

Parameters
rotationNew angle, in radians
See also
getRotation()

◆ setSize()

void gf::View::setSize ( Vector2f  size)
inline

Set the size of the view.

Parameters
sizeNew size
See also
getSize()

◆ setSizeNoCallback()

void gf::View::setSizeNoCallback ( Vector2f  size)
inlineprotected

Set the world size, without calling onSizeChange()

This function is meant for adaptative views so that they can adapt the world size without having a callback infinite loop.

Parameters
sizeThe new world size

◆ setViewport()

void gf::View::setViewport ( const RectF 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({0.0f, 0.0f, 0.5f, 1.0f}).

By default, a view has a viewport which covers the entire target.

Parameters
viewportNew viewport rectangle
See also
getViewport()

◆ setViewportNoCallback()

void gf::View::setViewportNoCallback ( const RectF viewport)
protected

Set the viewport, without calling onViewportChange()

This function is meant for adaptative views so that they can adapt the viewport without having a callback infinite loop.

Parameters
viewportThe new viewport

◆ zoom() [1/2]

void gf::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()

◆ zoom() [2/2]

void gf::View::zoom ( float  factor,
Vector2f  fixed 
)

Resize the view rectangle relatively to its current size and a fixed point.

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)

Additionally, a fixed point is used as the center of the zoom. It is the only point that stays at the same place in the view.

Parameters
factorZoom factor to apply
fixedThe center of the zoom
See also
setSize(), move(), rotate()