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
camera.h
Go to the documentation of this file.
1
8
9#ifndef ASW_CAMERA_H
10#define ASW_CAMERA_H
11
12#include "./geometry.h"
13
14namespace asw {
15
21class Camera {
22public:
25 Camera() = default;
26
32 explicit Camera(const Vec2<float>& view_size);
33
39
45 void set_bounds(const Quad<float>& bounds);
46
49 void clear_bounds();
50
56 void set_anchor(const Vec2<float>& anchor);
57
60 void clear_anchor();
61
66 void set_follow_speed(float speed);
67
72 void set_shake_decay(float decay);
73
78 void snap_to(const Vec2<float>& target);
79
85 void follow(const Vec2<float>& target, float dt);
86
91 void update(float dt);
92
98 void shake(float strength);
99
104 void set_position(const Vec2<float>& position);
105
111
116 Quad<float> get_view() const;
117
123 Vec2<float> world_to_screen(const Vec2<float>& world) const;
124
130 Quad<float> world_to_screen(const Quad<float>& world) const;
131
137 Vec2<float> screen_to_world(const Vec2<float>& screen) const;
138
139private:
140 void clamp_to_bounds();
141
147 bool has_bounds { false };
148 bool has_anchor { false };
149 float follow_speed { 8.0F };
150 float shake_strength { 0.0F };
151 float shake_decay { 60.0F };
152};
153
154} // namespace asw
155
156#endif // ASW_CAMERA_H
2D camera that follows a target, stays inside the world and shakes.
Definition camera.h:21
void snap_to(const Vec2< float > &target)
Move straight to a target and stop any shake.
Definition camera.cpp:62
void follow(const Vec2< float > &target, float dt)
Ease towards a target. Call once per update.
Definition camera.cpp:71
Vec2< float > shake_offset
Definition camera.h:145
Vec2< float > world_to_screen(const Vec2< float > &world) const
Convert a world position to screen space.
Definition camera.cpp:120
Camera()=default
Create a camera with no view size. Call set_view_size before use.
Quad< float > bounds
Definition camera.h:146
void set_shake_decay(float decay)
Set how quickly screen shake fades.
Definition camera.cpp:57
Vec2< float > get_position() const
Get the top left of the view in world space, without shake.
Definition camera.cpp:110
void shake(float strength)
Start a screen shake. A weaker shake does not cut short a stronger one already running.
Definition camera.cpp:99
Vec2< float > view_size
Definition camera.h:143
float follow_speed
Definition camera.h:149
bool has_bounds
Definition camera.h:147
Vec2< float > position
Definition camera.h:142
bool has_anchor
Definition camera.h:148
void set_position(const Vec2< float > &position)
Set the top left of the view in world space.
Definition camera.cpp:104
Vec2< float > screen_to_world(const Vec2< float > &screen) const
Convert a screen position, such as the mouse, to world space.
Definition camera.cpp:130
void set_follow_speed(float speed)
Set how quickly follow catches up with its target.
Definition camera.cpp:52
float shake_decay
Definition camera.h:151
void update(float dt)
Advance screen shake. Call once per update.
Definition camera.cpp:87
float shake_strength
Definition camera.h:150
void clamp_to_bounds()
Definition camera.cpp:135
void clear_bounds()
Let the view move anywhere.
Definition camera.cpp:36
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 ...
Definition camera.cpp:29
Vec2< float > anchor
Definition camera.h:144
Quad< float > get_view() const
Get the visible world area, including shake.
Definition camera.cpp:115
void clear_anchor()
Keep the followed target in the middle of the view again.
Definition camera.cpp:47
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.
Definition camera.cpp:41
void set_view_size(const Vec2< float > &view_size)
Set the size of the area the camera shows.
Definition camera.cpp:23
A 2D rectangle in space.
Definition geometry.h:504
A 2D vector in space.
Definition geometry.h:27
Common geometry types.
Definition camera.cpp:16