ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
asw::Camera Class Reference

2D camera that follows a target, stays inside the world and shakes. More...

#include <camera.h>

Collaboration diagram for asw::Camera:
[legend]

Public Member Functions

 Camera ()=default
 Create a camera with no view size. Call set_view_size before use.
 
 Camera (const Vec2< float > &view_size)
 Create a camera.
 
void set_view_size (const Vec2< float > &view_size)
 Set the size of the area the camera shows.
 
void set_bounds (const Quad< float > &bounds)
 Keep the view inside an area of the world. If the area is smaller than the view, the view is centred on it.
 
void clear_bounds ()
 Let the view move anywhere.
 
void set_anchor (const Vec2< float > &anchor)
 Set the point in the view where the followed target is kept. Defaults to the middle of the view.
 
void clear_anchor ()
 Keep the followed target in the middle of the view again.
 
void set_follow_speed (float speed)
 Set how quickly follow catches up with its target.
 
void set_shake_decay (float decay)
 Set how quickly screen shake fades.
 
void snap_to (const Vec2< float > &target)
 Move straight to a target and stop any shake.
 
void follow (const Vec2< float > &target, float dt)
 Ease towards a target. Call once per update.
 
void update (float dt)
 Advance screen shake. Call once per update.
 
void shake (float strength)
 Start a screen shake. A weaker shake does not cut short a stronger one already running.
 
void set_position (const Vec2< float > &position)
 Set the top left of the view in world space.
 
Vec2< float > get_position () const
 Get the top left of the view in world space, without shake.
 
Quad< float > get_view () const
 Get the visible world area, including shake.
 
Vec2< float > world_to_screen (const Vec2< float > &world) const
 Convert a world position to screen space.
 
Quad< float > world_to_screen (const Quad< float > &world) const
 Convert a world area to screen space.
 
Vec2< float > screen_to_world (const Vec2< float > &screen) const
 Convert a screen position, such as the mouse, to world space.
 

Private Member Functions

void clamp_to_bounds ()
 

Private Attributes

Vec2< float > position
 
Vec2< float > view_size
 
Vec2< float > anchor
 
Vec2< float > shake_offset
 
Quad< float > bounds
 
bool has_bounds { false }
 
bool has_anchor { false }
 
float follow_speed { 8.0F }
 
float shake_strength { 0.0F }
 
float shake_decay { 60.0F }
 

Detailed Description

2D camera that follows a target, stays inside the world and shakes.

Drawing in asw is in screen space. Keep game objects in world space and convert with world_to_screen when drawing.

Definition at line 21 of file camera.h.

Constructor & Destructor Documentation

◆ Camera() [1/2]

asw::Camera::Camera ( )
default

Create a camera with no view size. Call set_view_size before use.

◆ Camera() [2/2]

asw::Camera::Camera ( const Vec2< float > & view_size)
explicit

Create a camera.

Parameters
view_sizeSize of the area the camera shows, usually the logical screen size.

Definition at line 18 of file camera.cpp.

Member Function Documentation

◆ clamp_to_bounds()

void asw::Camera::clamp_to_bounds ( )
private

Definition at line 135 of file camera.cpp.

◆ clear_anchor()

void asw::Camera::clear_anchor ( )

Keep the followed target in the middle of the view again.

Definition at line 47 of file camera.cpp.

◆ clear_bounds()

void asw::Camera::clear_bounds ( )

Let the view move anywhere.

Definition at line 36 of file camera.cpp.

◆ follow()

void asw::Camera::follow ( const Vec2< float > & target,
float dt )

Ease towards a target. Call once per update.

Parameters
targetThe world position to keep at the anchor.
dtThe time in seconds since the last update.

Definition at line 71 of file camera.cpp.

◆ get_position()

Vec2< float > asw::Camera::get_position ( ) const

Get the top left of the view in world space, without shake.

Returns
The world position.

Definition at line 110 of file camera.cpp.

◆ get_view()

Quad< float > asw::Camera::get_view ( ) const

Get the visible world area, including shake.

Returns
The view in world space.

Definition at line 115 of file camera.cpp.

◆ screen_to_world()

Vec2< float > asw::Camera::screen_to_world ( const Vec2< float > & screen) const

Convert a screen position, such as the mouse, to world space.

Parameters
screenThe screen position.
Returns
The world position.

Definition at line 130 of file camera.cpp.

◆ set_anchor()

void asw::Camera::set_anchor ( const Vec2< float > & anchor)

Set the point in the view where the followed target is kept. Defaults to the middle of the view.

Parameters
anchorThe point, relative to the top left of the view.

Definition at line 41 of file camera.cpp.

◆ set_bounds()

void asw::Camera::set_bounds ( const Quad< float > & bounds)

Keep the view inside an area of the world. If the area is smaller than the view, the view is centred on it.

Parameters
boundsThe world area.

Definition at line 29 of file camera.cpp.

◆ set_follow_speed()

void asw::Camera::set_follow_speed ( float speed)

Set how quickly follow catches up with its target.

Parameters
speedHigher is faster. 0 jumps straight to the target.

Definition at line 52 of file camera.cpp.

◆ set_position()

void asw::Camera::set_position ( const Vec2< float > & position)

Set the top left of the view in world space.

Parameters
positionThe world position.

Definition at line 104 of file camera.cpp.

◆ set_shake_decay()

void asw::Camera::set_shake_decay ( float decay)

Set how quickly screen shake fades.

Parameters
decayShake strength lost per second, in pixels.

Definition at line 57 of file camera.cpp.

◆ set_view_size()

void asw::Camera::set_view_size ( const Vec2< float > & view_size)

Set the size of the area the camera shows.

Parameters
view_sizeThe view size in pixels.

Definition at line 23 of file camera.cpp.

◆ shake()

void asw::Camera::shake ( float strength)

Start a screen shake. A weaker shake does not cut short a stronger one already running.

Parameters
strengthLargest offset in pixels.

Definition at line 99 of file camera.cpp.

◆ snap_to()

void asw::Camera::snap_to ( const Vec2< float > & target)

Move straight to a target and stop any shake.

Parameters
targetThe world position to keep at the anchor.

Definition at line 62 of file camera.cpp.

◆ update()

void asw::Camera::update ( float dt)

Advance screen shake. Call once per update.

Parameters
dtThe time in seconds since the last update.

Definition at line 87 of file camera.cpp.

◆ world_to_screen() [1/2]

Quad< float > asw::Camera::world_to_screen ( const Quad< float > & world) const

Convert a world area to screen space.

Parameters
worldThe world area.
Returns
The screen area.

Definition at line 125 of file camera.cpp.

◆ world_to_screen() [2/2]

Vec2< float > asw::Camera::world_to_screen ( const Vec2< float > & world) const

Convert a world position to screen space.

Parameters
worldThe world position.
Returns
The screen position.

Definition at line 120 of file camera.cpp.

Member Data Documentation

◆ anchor

Vec2<float> asw::Camera::anchor
private

Definition at line 144 of file camera.h.

◆ bounds

Quad<float> asw::Camera::bounds
private

Definition at line 146 of file camera.h.

◆ follow_speed

float asw::Camera::follow_speed { 8.0F }
private

Definition at line 149 of file camera.h.

◆ has_anchor

bool asw::Camera::has_anchor { false }
private

Definition at line 148 of file camera.h.

◆ has_bounds

bool asw::Camera::has_bounds { false }
private

Definition at line 147 of file camera.h.

◆ position

Vec2<float> asw::Camera::position
private

Definition at line 142 of file camera.h.

◆ shake_decay

float asw::Camera::shake_decay { 60.0F }
private

Definition at line 151 of file camera.h.

◆ shake_offset

Vec2<float> asw::Camera::shake_offset
private

Definition at line 145 of file camera.h.

◆ shake_strength

float asw::Camera::shake_strength { 0.0F }
private

Definition at line 150 of file camera.h.

◆ view_size

Vec2<float> asw::Camera::view_size
private

Definition at line 143 of file camera.h.


The documentation for this class was generated from the following files: