Gamedev Framework (gf)  0.3.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...
 
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 setWorldSize (Vector2f size)
 Set the world size, without calling onWorldResize() More...
 
virtual void onWorldResize (Vector2f worldSize)
 Callback when the world has just been resized. 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

gf::View::View ( )

Default constructor.

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

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

Construct the view from a rectangle.

Parameters
rectRectangle defining the zone to display
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
virtual gf::View::~View ( )
virtual

Destructor.

Member Function Documentation

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

Get the center of the view.

Returns
Center of the view
See also
setCenter()
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
float gf::View::getRotation ( ) const
inline

Get the current orientation of the view.

Returns
Rotation angle of the view, in radians
See also
setRotation()
Vector2f gf::View::getSize ( ) const
inline

Get the size of the view.

Returns
Size of the view
See also
setSize()
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()
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()
void gf::View::move ( Vector2f  offset)

Move the view relatively to its current position.

Parameters
offsetMove offset
See also
setCenter(), rotate(), zoom()
virtual void gf::View::onWorldResize ( Vector2f  worldSize)
protectedvirtual

Callback when the world has just been resized.

This callback is called when setSize() is called.

Parameters
worldSizeThe new size of the visible world

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

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()
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()
void gf::View::setCenter ( Vector2f  center)
inline

Set the center of the view.

Parameters
centerNew center
See also
getCenter()
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()
void gf::View::setSize ( Vector2f  size)
inline

Set the size of the view.

Parameters
sizeNew size
See also
getSize()
void gf::View::setViewport ( const RectF viewport)
inline

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()
void gf::View::setWorldSize ( Vector2f  size)
inlineprotected

Set the world size, without calling onWorldResize()

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
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()