]> git.tdb.fi Git - libs/game.git/blob - source/game/camera.h
Decorate things which constitute the public API of the library
[libs/game.git] / source / game / camera.h
1 #ifndef MSP_GAME_CAMERA_H_
2 #define MSP_GAME_CAMERA_H_
3
4 #include <msp/geometry/angle.h>
5 #include <msp/linal/vector.h>
6 #include "component.h"
7 #include "mspgame_api.h"
8
9 namespace Msp::Game {
10
11 enum class CameraScaling
12 {
13         ORIGINAL_SIZE,
14         SCALE_TO_FIT,
15         SCALE_TO_COVER,
16         STRETCH_TO_FIT
17 };
18
19 struct CameraSetup
20 {
21         Geometry::Angle<float> field_of_view_y = Geometry::Angle<float>::from_degrees(60);
22         LinAl::Vector<float, 2> size = { 16.0f/9.0f, 1.0f };
23         float near_clip = 0.1f;
24         float far_clip = 100.0f;
25         CameraScaling scaling = CameraScaling::SCALE_TO_FIT;
26         std::string sequence_name;
27 };
28
29 struct CameraData
30 {
31         Geometry::Angle<float> fov_y;
32         LinAl::Vector<float, 2> size;
33         float height;
34         float near_clip;
35         float far_clip;
36
37         bool is_orthographic() const { return fov_y==Geometry::Angle<float>::zero(); }
38         Geometry::Angle<float> get_fov_horizontal() const { return Geometry::atan(tan(fov_y/2.0f)*get_aspect())*2.0f; }
39         float get_aspect() const { return size.x/size.y; }
40 };
41
42 class MSPGAME_API Camera: public BufferedComponent<CameraData>
43 {
44 public:
45         using Setup = CameraSetup;
46
47 private:
48         const Setup &setup;
49
50 public:
51         Camera(Handle<Entity>, const Setup &);
52
53         void set_field_of_view(Geometry::Angle<float>, float);
54         void set_size(const LinAl::Vector<float, 2> &);
55         bool is_orthographic() const { return read().is_orthographic(); }
56         Geometry::Angle<float> get_fov_vertical() const { return read().fov_y; }
57         Geometry::Angle<float> get_fov_horizontal() const { return read().get_fov_horizontal(); }
58         const LinAl::Vector<float, 2> &get_size() const { return read().size; }
59         float get_aspect() const { return read().get_aspect(); }
60         float get_near_clip() const { return read().near_clip; }
61         float get_far_clip() const { return read().far_clip; }
62         CameraScaling get_scaling() const { return setup.scaling; }
63         const std::string &get_sequence_name() const { return setup.sequence_name; }
64 };
65
66 } // namespace Msp::Game
67
68 #endif