Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
types.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4#include <SDL3/SDL_rect.h>
5
6#include <algorithm>
7#include <optional>
8
9namespace Flint {
15 template <typename T>
16 struct Vec2 {
17 T x, y;
18
19 Vec2 operator+(T scalar) {
20 return {x + scalar, y + scalar};
21 }
22
23 Vec2 operator+(const Vec2 &other) const {
24 return {x + other.x, y + other.y};
25 }
26
27 Vec2 operator-(T scalar) {
28 return {x - scalar, y - scalar};
29 }
30
31 Vec2 operator-(const Vec2 &other) const {
32 return {x - other.x, y - other.y};
33 }
34
35 Vec2 operator*(T scalar) const {
36 return {x * scalar, y * scalar};
37 }
38
39 Vec2 operator*(const Vec2 &other) const {
40 return {x * other.x, y * other.y};
41 }
42
43 Vec2 operator/(T scalar) const {
44 return {x / scalar, y / scalar};
45 }
46
47 Vec2 operator/(const Vec2 &other) const {
48 return {x / other.x, y / other.y};
49 }
50
51 Vec2 &operator+=(const Vec2 &other) {
52 x += other.x;
53 y += other.y;
54 return *this;
55 }
56
57 Vec2 &operator-=(const Vec2 &other) {
58 x -= other.x;
59 y -= other.y;
60 return *this;
61 }
62
63 Vec2 &operator*=(T scalar) {
64 x *= scalar;
65 y *= scalar;
66 return *this;
67 }
68
69 Vec2 &operator/=(T scalar) {
70 x /= scalar;
71 y /= scalar;
72 return *this;
73 }
74
75 bool operator==(const Vec2 &other) const = default;
76 };
77
78 using Vec2i = Vec2<int>;
79 using Vec2f = Vec2<float>;
80
86 template <typename T>
87 struct Rect {
88 union {
89 struct {
92 };
93
94 struct {
95 T x, y, w, h;
96 };
97 };
98
99 Rect() = default;
100
111
112 Rect(T x, T y, T w, T h)
113 : x(x),
114 y(y),
115 w(w),
116 h(h) {
117 }
118
125 std::optional<Rect<T>> findIntersection(const Rect<T> &other) const {
126 T x1 = std::max(position.x, other.position.x);
127 T y1 = std::max(position.y, other.position.y);
128 T x2 = std::min(position.x + size.x, other.position.x + other.size.x);
129 T y2 = std::min(position.y + size.y, other.position.y + other.size.y);
130
131 if (x2 > x1 && y2 > y1) {
132 return Rect<T>{
133 { x1, y1},
134 {x2 - x1, y2 - y1}
135 };
136 }
137 return std::nullopt;
138 }
139
146 bool contains(const Vec2<T> &point) const {
147 return point.x >= position.x && point.x < position.x + size.x && point.y >= position.y &&
148 point.y < position.y + size.y;
149 }
150
156 auto toNative() const {
157 if constexpr (std::is_same_v<T, int>) {
158 return SDL_Rect{static_cast<int>(position.x), static_cast<int>(position.y),
159 static_cast<int>(size.x), static_cast<int>(size.y)};
160 } else {
161 return SDL_FRect{position.x, position.y, size.x, size.y};
162 }
163 }
164
170 static auto fromNative(const SDL_Rect &rect) {
171 if constexpr (std::is_same_v<T, int>) {
172 return Rect<T>{
173 {rect.x, rect.y},
174 {rect.w, rect.h}
175 };
176 } else {
177 return Rect<T>{
178 {static_cast<T>(rect.x), static_cast<T>(rect.y)},
179 {static_cast<T>(rect.w), static_cast<T>(rect.h)}
180 };
181 }
182 }
183
189 std::optional<Rect> getGlobalBounds() const {
190 if (size.x <= 0 || size.y <= 0) {
191 return std::nullopt;
192 }
193 return *this;
194 }
195 };
196
197 using IntRect = Rect<int>;
198 using FloatRect = Rect<float>;
199}
auto toNative() const
Convert to the corresponding SDL rectangle type.
Definition types.h:156
std::optional< Rect< T > > findIntersection(const Rect< T > &other) const
Compute the intersection with another rectangle.
Definition types.h:125
std::optional< Rect > getGlobalBounds() const
Return this rect as an optional, or nullopt if it has zero/negative area.
Definition types.h:189
Vec2< T > position
Definition types.h:90
static auto fromNative(const SDL_Rect &rect)
Construct a Rect from an SDL_Rect.
Definition types.h:170
Rect(Vec2< T > position, Vec2< T > size)
Construct a Rect from explicit position and size.
Definition types.h:107
bool contains(const Vec2< T > &point) const
Test whether point lies inside the rectangle (half-open interval).
Definition types.h:146
Vec2< T > size
Definition types.h:91
Generic axis-aligned rectangle defined by a position and size.
Definition types.h:87
Generic 2-D vector with arithmetic operators.
Definition types.h:16