galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Camera.cpp
Go to the documentation of this file.
1
7
8#include <glm/gtc/matrix_transform.hpp>
9#include <nlohmann/json.hpp>
10
11#include "Camera.hpp"
12
13namespace galaxy
14{
15 namespace graphics
16 {
18 : m_allow_rotation {false}
19 , m_translation_speed {180.0f}
20 , m_rotation_speed {180.0f}
21 {
22 }
23
24 Camera::Camera(const nlohmann::json& json)
25 : m_allow_rotation {false}
26 , m_translation_speed {180.0f}
27 , m_rotation_speed {180.0f}
28 {
29 deserialize(json);
30 }
31
33 : Transform {std::move(c)}
34 {
35 this->m_allow_rotation = c.m_allow_rotation;
36 this->m_translation_speed = c.m_translation_speed;
37 this->m_rotation_speed = c.m_rotation_speed;
38 this->m_data.m_model_view = std::move(c.m_data.m_model_view);
39 this->m_data.m_projection = std::move(c.m_data.m_projection);
40 this->m_viewport = std::move(c.m_viewport);
41 }
42
44 {
45 if (this != &c)
46 {
47 Transform::operator=(std::move(c));
48
49 this->m_allow_rotation = c.m_allow_rotation;
50 this->m_translation_speed = c.m_translation_speed;
51 this->m_rotation_speed = c.m_rotation_speed;
52 this->m_data.m_model_view = std::move(c.m_data.m_model_view);
53 this->m_data.m_projection = std::move(c.m_data.m_projection);
54 this->m_viewport = std::move(c.m_viewport);
55 }
56
57 return *this;
58 }
59
70
72 {
73 if (this != &c)
74 {
76
82 this->m_viewport = c.m_viewport;
83 }
84
85 return *this;
86 }
87
89 {
90 }
91
92 void Camera::set_viewport(const float width, const float height)
93 {
94 set_projection(0.0f, width, height, 0.0f);
95 }
96
97 void Camera::translate(const float x, const float y)
98 {
100 m_pos.y += y * m_translation_speed;
101
102 m_dirty = true;
103 }
104
105 void Camera::rotate(const float degrees)
106 {
107 m_rotation += degrees * m_rotation_speed;
108 m_rotation = std::clamp(m_rotation, -360.0f, 360.0f);
109
110 m_dirty = true;
111 }
112
113 const glm::vec2& Camera::get_viewport() const
114 {
115 return m_viewport;
116 }
117
118 const glm::mat4& Camera::get_model_view()
119 {
120 recalculate();
121 return m_data.m_model_view;
122 }
123
124 const glm::mat4& Camera::get_proj()
125 {
126 recalculate();
127 return m_data.m_projection;
128 }
129
131 {
132 recalculate();
133 return m_data;
134 }
135
136 void Camera::set_projection(const float left, const float right, const float bottom, const float top)
137 {
138 m_dirty = true;
139
140 m_origin.x = right * 0.5f;
141 m_origin.y = bottom * 0.5f;
142
143 m_viewport.x = right;
144 m_viewport.y = bottom;
145
146 m_data.m_projection = glm::ortho(left, right, bottom, top, -1.0f, 1.0f);
147 }
148
150 {
151 if (m_dirty)
152 {
153 m_data.m_model_view = glm::inverse(get_transform());
154 m_dirty = false;
155 }
156 }
157
158 nlohmann::json Camera::serialize()
159 {
160 nlohmann::json json = "{}"_json;
161 json["pos"]["x"] = m_pos.x;
162 json["pos"]["y"] = m_pos.y;
163 json["rotation"] = m_rotation;
164 json["scale"]["x"] = m_scale.x;
165 json["scale"]["y"] = m_scale.y;
166 json["allow_rotation"] = m_allow_rotation;
167 json["rotation_speed"] = m_rotation_speed;
168 json["translation_speed"] = m_translation_speed;
169
170 return json;
171 }
172
173 void Camera::deserialize(const nlohmann::json& json)
174 {
175 const auto& pos = json.at("pos");
176 m_pos.x = pos.at("x");
177 m_pos.y = pos.at("y");
178
179 m_rotation = json.at("rotation");
180
181 const auto& scale = json.at("scale");
182 m_scale.x = scale.at("x");
183 m_scale.y = scale.at("y");
184
185 m_allow_rotation = json.at("allow_rotation");
186 m_rotation_speed = json.at("rotation_speed");
187 m_translation_speed = json.at("translation_speed");
188
189 m_dirty = true;
190 }
191 } // namespace graphics
192} // namespace galaxy
Orthographic 2D camera.
Definition Camera.hpp:28
const glm::mat4 & get_proj()
Get the Camera projection.
Definition Camera.cpp:124
void set_viewport(const float width, const float height)
Set Viewport.
Definition Camera.cpp:92
const glm::vec2 & get_viewport() const
Get camera viewport.
Definition Camera.cpp:113
float m_translation_speed
Movement speed.
Definition Camera.hpp:182
Data m_data
Camera data.
Definition Camera.hpp:193
void set_projection(const float left, const float right, const float bottom, const float top)
Set camera projection.
Definition Camera.cpp:136
nlohmann::json serialize() override
Serializes object.
Definition Camera.cpp:158
void translate(const float x, const float y) override
Translate (move) position.
Definition Camera.cpp:97
Camera & operator=(Camera &&)
Move assignment operator.
Definition Camera.cpp:43
void deserialize(const nlohmann::json &json) override
Deserializes from object.
Definition Camera.cpp:173
glm::vec2 m_viewport
Camera viewport size.
Definition Camera.hpp:198
virtual ~Camera()
Destructor.
Definition Camera.cpp:88
void recalculate()
Recalculates the model view matrix.
Definition Camera.cpp:149
Data & get_data()
Get camera view and proj.
Definition Camera.cpp:130
Camera()
Constructor.
Definition Camera.cpp:17
void rotate(const float degrees) override
Rotate entity.
Definition Camera.cpp:105
bool m_allow_rotation
Allow camera to rotate.
Definition Camera.hpp:177
const glm::mat4 & get_model_view()
Retrieve internal transformation matrix.
Definition Camera.cpp:118
float m_rotation_speed
Rotational speed.
Definition Camera.hpp:187
Defines the 2D transformation of an entity.
Definition Transform.hpp:21
glm::vec2 m_origin
Transform origin point.
Transform & operator=(Transform &&)
Move assignment operator.
Definition Transform.cpp:42
bool m_dirty
Flag to see if transform needs to be recalculated.
float m_rotation
Cached for easy retrieval. Rotation.
glm::vec2 m_scale
Cached for easy retrieval. Scale.
void scale(const float scale)
Set entity scale.
glm::mat4 & get_transform()
Retrieve internal transformation matrix.
glm::vec2 m_pos
Cached for easy retrieval. Pos.
Animated.cpp galaxy.
Definition Animated.cpp:16
STL namespace.
glm::mat4 m_model_view
Combined transformation matrix.
Definition Camera.hpp:40
glm::mat4 m_projection
Camera projection matrix.
Definition Camera.hpp:45