Calamity Engine 1.0.0
A cross-platform 2D game engine written in C++ and SDL3.
Loading...
Searching...
No Matches
definitions.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <vector>
4#include <string>
5#include <SDL3/SDL.h>
6#include "../utils/logger.hpp"
7#include <cereal/archives/json.hpp>
8#include <box2d/types.h>
9
10// Forward declarations
11class Graphics;
12class Services;
13class Window;
14
20{
21 static constexpr float scale = 0.01f; // 100 pixels = 1 meter
22};
23
40class Color
41{
42public:
43 Uint8 r; // Red value. Goes up to 255.
44 Uint8 g; // Green value. Goes up to 255.
45 Uint8 b; // Blue value. Goes up to 255.
46 Uint8 a; // Alpha value. Goes up to 255.
47
48 Color(int r, int g, int b);
49 Color(int r, int g, int b, int a);
50 Color(int hexCode);
51 Color(int hexCode, int a);
52 Color(std::string hexCode);
53 Color(const std::string &hexCode, int a);
54
55 operator SDL_Color() const { return {r, g, b, a}; };
56 operator SDL_FColor() const { return {r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f}; };
57
58 static const Color WHITE;
59 static const Color BLACK;
60 static const Color RED;
61 static const Color GREEN;
62 static const Color BLUE;
63 static const Color TRANSPARENT;
64
65 template <class Archive>
66 void serialize(Archive &ar)
67 {
68 ar(r, g, b, a);
69 }
70};
71
76struct Vector2
77{
78 float x;
79 float y;
80
81 Vector2() : x(0), y(0) {}
82 Vector2(float x, float y) : x(x), y(y) {}
83 explicit Vector2(const b2Vec2 &v)
84 {
85 x = v.x;
86 y = v.y;
87 };
88 explicit Vector2(const SDL_FPoint &v)
89 {
90 x = v.x;
91 y = v.y;
92 }
93
94 bool operator==(Vector2 o) const { return (x == o.x && y == o.y); };
95 Vector2 operator*(float s) const { return {x * s, y * s}; }
96 Vector2 operator/(float s) const { return {x / s, y / s}; }
97 Vector2 operator/(Vector2 s) const { return {x / s.x, y / s.y}; };
98 Vector2 operator*(Vector2 s) const { return {x * s.x, y * s.y}; }
99 Vector2 operator+(const Vector2 &v) const { return {x + v.x, y + v.y}; }
100 Vector2 operator-(const Vector2 &v) const { return {x - v.x, y - v.y}; }
101 bool operator!=(Vector2 o) const { return (x != o.x || y != o.y); };
102 operator b2Vec2() const { return {x, y}; };
103 float distanceTo(const Vector2 &other) const
104 {
105 return std::sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
106 }
107
108 template <class Archive>
109 void serialize(Archive &ar)
110 {
111 ar(CEREAL_NVP(x), CEREAL_NVP(y));
112 }
113};
114
122{
123 INVALID = -1,
124 NEAREST,
125 LINEAR,
127};
128
154{
155public:
156 Texture() : handle(nullptr), width(0), height(0), textureWidth(0), textureHeight(0) {};
157 Texture(const std::string &path, std::shared_ptr<Window> window, TextureScaling scaling = TextureScaling::NEAREST);
158 ~Texture();
159 Texture(const Texture &) = delete;
160 Texture &operator=(const Texture &) = delete;
161 Texture(Texture &&other) noexcept : handle(other.handle), window(other.window), width(other.width), height(other.height), textureWidth(other.textureWidth), textureHeight(other.textureHeight), path(std::move(other.path))
162 {
163 other.handle = nullptr;
164 }
165
166 Texture &operator=(Texture &&other) noexcept
167 {
168 if (this != &other)
169 {
170 SDL_DestroyTexture(handle);
171 handle = other.handle;
172 window = other.window;
173 width = other.width;
174 height = other.height;
175 textureWidth = other.textureWidth;
176 textureHeight = other.textureHeight;
177 path = std::move(other.path);
178 other.handle = nullptr;
179 }
180 return *this;
181 }
182
183 template <class Archive>
184 void load(Archive &ar)
185 {
186 ar(CEREAL_NVP(path), CEREAL_NVP(scaling), CEREAL_NVP(width), CEREAL_NVP(height), CEREAL_NVP(textureWidth), CEREAL_NVP(textureHeight));
187 }
188
189 template <class Archive>
190 void save(Archive &ar) const
191 {
192 ar(CEREAL_NVP(path), CEREAL_NVP(scaling), CEREAL_NVP(width), CEREAL_NVP(height), CEREAL_NVP(textureWidth), CEREAL_NVP(textureHeight));
193 }
194 void initialize(bool set = true);
195
196 void setWindow(std::shared_ptr<Window> window)
197 {
198 this->window = window;
199 }
200
201 SDL_Texture *handle;
202 int width;
206 std::string path;
207
208private:
210 std::shared_ptr<Window> window;
211};
212
221{
222 float m[2][2];
223
224 Matrix2 operator*(const Matrix2 &other) const;
225 Vector2 operator*(const Vector2 &other) const;
226 Matrix2 operator+(const Matrix2 &other) const;
227 Matrix2 operator-(const Matrix2 &other) const;
228 Matrix2 operator/(float scalar) const;
229
230 static Matrix2 rotation(float angle);
231 static Matrix2 scale(Vector2 s);
232
233 template <class Archive>
234 void serialize(Archive &ar)
235 {
236 ar(cereal::make_nvp("matrix", m));
237 }
238};
239
259{
261 Matrix2 transformation = {{{1, 0}, {0, 1}}};
262
263 void rotate(float angle);
264 void rotateRadians(float radians);
265 void setScale(Vector2 scale);
266 void scale(Vector2 scale);
267 void setAngle(float angle);
268 void setAngleRadians(float radians);
269
270 Vector2 applyTo(const Vector2 &point) const;
271 Transform applyTo(const Transform &other) const;
272 Transform inverse() const;
273 float getAngle() const;
274 float getAngleRadians() const;
275 float getDegrees() const;
276 Vector2 getScale() const;
277 void lookAt(const Vector2 &point);
278
279 static float degToRad(float degrees);
280 static float radToDeg(float radians);
281
282 template <class Archive>
283 void serialize(Archive &ar)
284 {
285 ar(CEREAL_NVP(position), CEREAL_NVP(transformation));
286 }
287};
288
293struct Rect
294{
295 Rect() {};
296 Rect(const Vector2 _position, const Vector2 _size) : position(_position), size(_size) {};
298 Vector2 size = {0, 0};
299
300 template <class Archive>
301 void serialize(Archive &ar)
302 {
303 ar(CEREAL_NVP(position), CEREAL_NVP(size));
304 }
305
306 operator SDL_Rect() const
307 {
308 return SDL_Rect{static_cast<int>(position.x), static_cast<int>(position.y), static_cast<int>(size.x), static_cast<int>(size.y)};
309 };
310};
311
323struct Frame
324{
325 Frame() = default;
326 Frame(const Rect _rect, const Vector2 _origin = {0.5, 0.5f}, const Color _modulate = Color::WHITE) : rect(_rect), origin(_origin), modulate(_modulate) {};
327
329 Vector2 origin = {0.5f, 0.5f};
331
332 template <class Archive>
333 void save(Archive &ar) const
334 {
335 ar(CEREAL_NVP(rect), CEREAL_NVP(origin), CEREAL_NVP(modulate));
336 }
337
338 template <class Archive>
339 void load(Archive &ar)
340 {
341 ar(CEREAL_NVP(rect), CEREAL_NVP(origin), CEREAL_NVP(modulate));
342 }
343};
344
372{
373public:
374 Animation(std::string _name = "Animation", int _fps = 30, Vector2 _size = {0, 0}, bool _loop = true, bool _autoplay = false) : name(_name), fps(_fps), size(_size), loop(_loop), autoplay(_autoplay) {};
375
376 std::string name;
377 int fps = 15;
378 std::vector<Frame> frames;
379
380 std::string texturePath;
382
384 bool loop = true;
385 bool autoplay = false;
386
387 template <typename... Args>
388 void addFrames(Args... frames)
389 {
390 (this->frames.push_back(frames), ...);
391 }
392
393 template <class Archive>
394 void save(Archive &ar) const
395 {
396 ar(CEREAL_NVP(fps), CEREAL_NVP(frames), CEREAL_NVP(texturePath), CEREAL_NVP(textureScaling), CEREAL_NVP(loop), CEREAL_NVP(autoplay));
397 }
398
399 template <class Archive>
400 void load(Archive &ar)
401 {
402 ar(CEREAL_NVP(fps), CEREAL_NVP(frames), CEREAL_NVP(texturePath), CEREAL_NVP(textureScaling), CEREAL_NVP(loop), CEREAL_NVP(autoplay));
403 }
404};
405
413{
414 Polygon();
415 explicit Polygon(const b2Polygon &polygon);
416
418 int count;
419 Vector2 normals[B2_MAX_POLYGON_VERTICES];
420 float radius;
421 Vector2 vertices[B2_MAX_POLYGON_VERTICES];
422
423 operator b2Polygon() const;
424
425 template <class Archive>
426 void serialize(Archive &ar)
427 {
428 ar(CEREAL_NVP(centroid), CEREAL_NVP(count), CEREAL_NVP(normals), CEREAL_NVP(radius), CEREAL_NVP(vertices));
429 }
430};
431
437struct Circle
438{
439 Circle();
440 explicit Circle(const b2Circle &circle);
441
442 Vector2 center = {0.5f, 0.5f};
443 float radius;
444
445 operator b2Circle() const;
446
447 template <class Archive>
448 void serialize(Archive &ar)
449 {
450 ar(CEREAL_NVP(center), CEREAL_NVP(radius));
451 }
452};
453
459{
460 Capsule();
461 explicit Capsule(const b2Capsule &capsule);
462
465 float radius;
466
467 operator b2Capsule() const;
468
469 template <class Archive>
470 void serialize(Archive &ar)
471 {
472 ar(CEREAL_NVP(center1), CEREAL_NVP(center2), CEREAL_NVP(radius));
473 }
474};
475
481{
482 Segment();
483 explicit Segment(const b2Segment &segment);
484
487
488 operator b2Segment() const;
489
490 template <class Archive>
491 void serialize(Archive &ar)
492 {
493 ar(CEREAL_NVP(point1), CEREAL_NVP(point2));
494 }
495};
496
508struct Tile
509{
511
515
516 template <class Archive>
517 void save(Archive &ar) const
518 {
520 }
521
522 template <class Archive>
523 void load(Archive &ar)
524 {
526 }
527};
Definition definitions.hpp:372
std::string texturePath
Definition definitions.hpp:380
std::string name
Definition definitions.hpp:376
TextureScaling textureScaling
Definition definitions.hpp:381
std::vector< Frame > frames
Definition definitions.hpp:378
Animation(std::string _name="Animation", int _fps=30, Vector2 _size={0, 0}, bool _loop=true, bool _autoplay=false)
Definition definitions.hpp:374
bool loop
Definition definitions.hpp:384
void load(Archive &ar)
Definition definitions.hpp:400
int fps
Definition definitions.hpp:377
void addFrames(Args... frames)
Definition definitions.hpp:388
bool autoplay
Definition definitions.hpp:385
Vector2 size
Definition definitions.hpp:383
void save(Archive &ar) const
Definition definitions.hpp:394
Definition definitions.hpp:41
Uint8 b
Definition definitions.hpp:45
static const Color GREEN
Definition definitions.hpp:61
static const Color WHITE
Definition definitions.hpp:58
Uint8 g
Definition definitions.hpp:44
static const Color BLACK
Definition definitions.hpp:59
static const Color BLUE
Definition definitions.hpp:62
Uint8 r
Definition definitions.hpp:43
void serialize(Archive &ar)
Definition definitions.hpp:66
static const Color TRANSPARENT
Definition definitions.hpp:63
Uint8 a
Definition definitions.hpp:46
static const Color RED
Definition definitions.hpp:60
Definition graphics.hpp:26
Definition services.hpp:34
Definition definitions.hpp:154
void save(Archive &ar) const
Definition definitions.hpp:190
int width
Definition definitions.hpp:202
Texture(Texture &&other) noexcept
Definition definitions.hpp:161
~Texture()
Definition definitions.cpp:232
int textureWidth
Definition definitions.hpp:204
int textureHeight
Definition definitions.hpp:205
Texture & operator=(const Texture &)=delete
void load(Archive &ar)
Definition definitions.hpp:184
Texture()
Definition definitions.hpp:156
Texture(const Texture &)=delete
Texture & operator=(Texture &&other) noexcept
Definition definitions.hpp:166
SDL_Texture * handle
Definition definitions.hpp:201
std::string path
Definition definitions.hpp:206
void initialize(bool set=true)
Definition definitions.cpp:219
int height
Definition definitions.hpp:203
void setWindow(std::shared_ptr< Window > window)
Definition definitions.hpp:196
Definition definitions.hpp:64
TextureScaling
Definition definitions.hpp:122
Definition definitions.hpp:459
Capsule()
Definition definitions.cpp:301
Vector2 center1
Definition definitions.hpp:463
Vector2 center2
Definition definitions.hpp:464
float radius
Definition definitions.hpp:465
void serialize(Archive &ar)
Definition definitions.hpp:470
Definition definitions.hpp:438
float radius
Definition definitions.hpp:443
Vector2 center
Definition definitions.hpp:442
void serialize(Archive &ar)
Definition definitions.hpp:448
Circle()
Definition definitions.cpp:279
Definition definitions.hpp:324
Color modulate
Definition definitions.hpp:330
void load(Archive &ar)
Definition definitions.hpp:339
void save(Archive &ar) const
Definition definitions.hpp:333
Vector2 origin
Definition definitions.hpp:329
Frame(const Rect _rect, const Vector2 _origin={0.5, 0.5f}, const Color _modulate=Color::WHITE)
Definition definitions.hpp:326
Rect rect
Definition definitions.hpp:328
Frame()=default
Definition definitions.hpp:221
Matrix2 operator+(const Matrix2 &other) const
Definition definitions.cpp:27
Matrix2 operator-(const Matrix2 &other) const
Definition definitions.cpp:40
static Matrix2 rotation(float angle)
Definition definitions.cpp:74
void serialize(Archive &ar)
Definition definitions.hpp:234
Matrix2 operator*(const Matrix2 &other) const
Definition definitions.cpp:16
Matrix2 operator/(float scalar) const
Definition definitions.cpp:53
float m[2][2]
Definition definitions.hpp:222
static Matrix2 scale(Vector2 s)
Definition definitions.cpp:87
Definition definitions.hpp:20
static constexpr float scale
Definition definitions.hpp:21
Definition definitions.hpp:413
Vector2 vertices[B2_MAX_POLYGON_VERTICES]
Definition definitions.hpp:421
Vector2 centroid
Definition definitions.hpp:417
int count
Definition definitions.hpp:418
void serialize(Archive &ar)
Definition definitions.hpp:426
float radius
Definition definitions.hpp:420
Vector2 normals[B2_MAX_POLYGON_VERTICES]
Definition definitions.hpp:419
Polygon()
Definition definitions.cpp:265
Definition definitions.hpp:294
void serialize(Archive &ar)
Definition definitions.hpp:301
Vector2 size
Definition definitions.hpp:298
Rect()
Definition definitions.hpp:295
Rect(const Vector2 _position, const Vector2 _size)
Definition definitions.hpp:296
Vector2 position
Definition definitions.hpp:297
Definition definitions.hpp:481
Vector2 point2
Definition definitions.hpp:486
void serialize(Archive &ar)
Definition definitions.hpp:491
Segment()
Definition definitions.cpp:326
Vector2 point1
Definition definitions.hpp:485
Definition definitions.hpp:509
void load(Archive &ar)
Definition definitions.hpp:523
Color modulate
Definition definitions.hpp:514
Tile(Vector2 gridPosition, Rect sourceRect={{0, 0}, {64, 64}}, Color modulate=Color::WHITE)
Definition definitions.hpp:510
Vector2 gridPosition
Definition definitions.hpp:512
void save(Archive &ar) const
Definition definitions.hpp:517
Rect sourceRect
Definition definitions.hpp:513
Definition definitions.hpp:259
Vector2 position
Definition definitions.hpp:260
void lookAt(const Vector2 &point)
Definition definitions.cpp:207
static float degToRad(float degrees)
Definition definitions.cpp:102
float getDegrees() const
Definition definitions.cpp:147
void rotateRadians(float radians)
Definition definitions.cpp:117
void setScale(Vector2 scale)
Definition definitions.cpp:122
float getAngle() const
Definition definitions.cpp:137
void setAngleRadians(float radians)
Definition definitions.cpp:158
Vector2 applyTo(const Vector2 &point) const
Definition definitions.cpp:174
float getAngleRadians() const
Definition definitions.cpp:142
static float radToDeg(float radians)
Definition definitions.cpp:107
void rotate(float angle)
Definition definitions.cpp:112
void setAngle(float angle)
Definition definitions.cpp:152
Transform inverse() const
Definition definitions.cpp:187
Vector2 getScale() const
Definition definitions.cpp:164
Matrix2 transformation
Definition definitions.hpp:261
void serialize(Archive &ar)
Definition definitions.hpp:283
void scale(Vector2 scale)
Definition definitions.cpp:129
Definition definitions.hpp:77
float x
Definition definitions.hpp:78
Vector2(float x, float y)
Definition definitions.hpp:82
float y
Definition definitions.hpp:79
Vector2 operator+(const Vector2 &v) const
Definition definitions.hpp:99
Vector2()
Definition definitions.hpp:81
Vector2 operator/(Vector2 s) const
Definition definitions.hpp:97
Vector2 operator-(const Vector2 &v) const
Definition definitions.hpp:100
Vector2 operator*(float s) const
Definition definitions.hpp:95
Vector2(const b2Vec2 &v)
Definition definitions.hpp:83
bool operator==(Vector2 o) const
Definition definitions.hpp:94
float distanceTo(const Vector2 &other) const
Definition definitions.hpp:103
Vector2(const SDL_FPoint &v)
Definition definitions.hpp:88
Vector2 operator*(Vector2 s) const
Definition definitions.hpp:98
Vector2 operator/(float s) const
Definition definitions.hpp:96
bool operator!=(Vector2 o) const
Definition definitions.hpp:101
void serialize(Archive &ar)
Definition definitions.hpp:109