|
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++.
|
2D camera that follows a target, stays inside the world and shakes. More...
#include <camera.h>
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 } |
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.
|
default |
Create a camera with no view size. Call set_view_size before use.
|
explicit |
Create a camera.
| view_size | Size of the area the camera shows, usually the logical screen size. |
Definition at line 18 of file camera.cpp.
|
private |
Definition at line 135 of file camera.cpp.
| void asw::Camera::clear_anchor | ( | ) |
Keep the followed target in the middle of the view again.
Definition at line 47 of file camera.cpp.
| void asw::Camera::clear_bounds | ( | ) |
Let the view move anywhere.
Definition at line 36 of file camera.cpp.
| void asw::Camera::follow | ( | const Vec2< float > & | target, |
| float | dt ) |
Ease towards a target. Call once per update.
| target | The world position to keep at the anchor. |
| dt | The time in seconds since the last update. |
Definition at line 71 of file camera.cpp.
| Vec2< float > asw::Camera::get_position | ( | ) | const |
Get the top left of the view in world space, without shake.
Definition at line 110 of file camera.cpp.
| Quad< float > asw::Camera::get_view | ( | ) | const |
Get the visible world area, including shake.
Definition at line 115 of file camera.cpp.
Convert a screen position, such as the mouse, to world space.
| screen | The screen position. |
Definition at line 130 of file camera.cpp.
| 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.
| anchor | The point, relative to the top left of the view. |
Definition at line 41 of file camera.cpp.
| 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.
| bounds | The world area. |
Definition at line 29 of file camera.cpp.
| void asw::Camera::set_follow_speed | ( | float | speed | ) |
Set how quickly follow catches up with its target.
| speed | Higher is faster. 0 jumps straight to the target. |
Definition at line 52 of file camera.cpp.
| void asw::Camera::set_position | ( | const Vec2< float > & | position | ) |
Set the top left of the view in world space.
| position | The world position. |
Definition at line 104 of file camera.cpp.
| void asw::Camera::set_shake_decay | ( | float | decay | ) |
Set how quickly screen shake fades.
| decay | Shake strength lost per second, in pixels. |
Definition at line 57 of file camera.cpp.
| void asw::Camera::set_view_size | ( | const Vec2< float > & | view_size | ) |
Set the size of the area the camera shows.
| view_size | The view size in pixels. |
Definition at line 23 of file camera.cpp.
| void asw::Camera::shake | ( | float | strength | ) |
Start a screen shake. A weaker shake does not cut short a stronger one already running.
| strength | Largest offset in pixels. |
Definition at line 99 of file camera.cpp.
| void asw::Camera::snap_to | ( | const Vec2< float > & | target | ) |
Move straight to a target and stop any shake.
| target | The world position to keep at the anchor. |
Definition at line 62 of file camera.cpp.
| void asw::Camera::update | ( | float | dt | ) |
Advance screen shake. Call once per update.
| dt | The time in seconds since the last update. |
Definition at line 87 of file camera.cpp.
Convert a world area to screen space.
| world | The world area. |
Definition at line 125 of file camera.cpp.
Convert a world position to screen space.
| world | The world position. |
Definition at line 120 of file camera.cpp.