summary refs log tree commit diff stats
path: root/src/vector.h
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2019-02-03 16:10:44 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2019-02-03 16:10:44 -0500
commit8ffb27ab09ff567a159e5be5a243fd3967084977 (patch)
treec7c1be31a4074a0d58191b9cc5ed880271c65b91 /src/vector.h
downloaddispatcher-8ffb27ab09ff567a159e5be5a243fd3967084977.tar.gz
dispatcher-8ffb27ab09ff567a159e5be5a243fd3967084977.tar.bz2
dispatcher-8ffb27ab09ff567a159e5be5a243fd3967084977.zip
Very basic ECS
Diffstat (limited to 'src/vector.h')
-rw-r--r--src/vector.h119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..10fe4da --- /dev/null +++ b/src/vector.h
@@ -0,0 +1,119 @@
1#ifndef COORDINATES_H_A45D34FB
2#define COORDINATES_H_A45D34FB
3
4#include <cstddef>
5
6template <typename T>
7class vec2 {
8public:
9
10 T coords[2];
11
12 constexpr vec2() : coords{0, 0}
13 {
14 }
15
16 constexpr vec2(T x, T y) : coords{x, y}
17 {
18 }
19
20 inline T& x()
21 {
22 return coords[0];
23 }
24
25 constexpr inline const T& x() const
26 {
27 return coords[0];
28 }
29
30 inline T& w()
31 {
32 return coords[0];
33 }
34
35 constexpr inline const T& w() const
36 {
37 return coords[0];
38 }
39
40 inline T& y()
41 {
42 return coords[1];
43 }
44
45 constexpr inline const T& y() const
46 {
47 return coords[1];
48 }
49
50 inline T& h()
51 {
52 return coords[1];
53 }
54
55 constexpr inline const T& h() const
56 {
57 return coords[1];
58 }
59
60 template <typename R>
61 constexpr operator vec2<R>() const
62 {
63 return vec2<R>(x(), y());
64 }
65
66 constexpr vec2 operator+(const vec2& other) const
67 {
68 return vec2(x() + other.x(), y() + other.y());
69 }
70
71 vec2& operator+=(const vec2& other)
72 {
73 x() += other.x();
74 y() += other.y();
75
76 return *this;
77 }
78
79 constexpr vec2 operator-(const vec2& other) const
80 {
81 return vec2(x() - other.x(), y() - other.y());
82 }
83
84 vec2 operator-=(const vec2& other)
85 {
86 x() -= other.x();
87 y() -= other.y();
88
89 return *this;
90 }
91
92 constexpr vec2 operator-() const
93 {
94 return vec2(-x(), -y());
95 }
96
97 constexpr vec2 operator*(T s) const
98 {
99 return vec2(x() * s, y() * s);
100 }
101
102 vec2& operator*=(T s)
103 {
104 x() *= s;
105 y() *= s;
106
107 return *this;
108 }
109
110 constexpr vec2 operator*(const vec2& other) const
111 {
112 return vec2(x() * other.x(), y() * other.y());
113 }
114
115};
116
117using vec2s = vec2<size_t>;
118
119#endif /* end of include guard: COORDINATES_H_A45D34FB */