From 8ffb27ab09ff567a159e5be5a243fd3967084977 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 3 Feb 2019 16:10:44 -0500 Subject: Very basic ECS --- src/vector.h | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/vector.h (limited to 'src/vector.h') 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 @@ +#ifndef COORDINATES_H_A45D34FB +#define COORDINATES_H_A45D34FB + +#include + +template +class vec2 { +public: + + T coords[2]; + + constexpr vec2() : coords{0, 0} + { + } + + constexpr vec2(T x, T y) : coords{x, y} + { + } + + inline T& x() + { + return coords[0]; + } + + constexpr inline const T& x() const + { + return coords[0]; + } + + inline T& w() + { + return coords[0]; + } + + constexpr inline const T& w() const + { + return coords[0]; + } + + inline T& y() + { + return coords[1]; + } + + constexpr inline const T& y() const + { + return coords[1]; + } + + inline T& h() + { + return coords[1]; + } + + constexpr inline const T& h() const + { + return coords[1]; + } + + template + constexpr operator vec2() const + { + return vec2(x(), y()); + } + + constexpr vec2 operator+(const vec2& other) const + { + return vec2(x() + other.x(), y() + other.y()); + } + + vec2& operator+=(const vec2& other) + { + x() += other.x(); + y() += other.y(); + + return *this; + } + + constexpr vec2 operator-(const vec2& other) const + { + return vec2(x() - other.x(), y() - other.y()); + } + + vec2 operator-=(const vec2& other) + { + x() -= other.x(); + y() -= other.y(); + + return *this; + } + + constexpr vec2 operator-() const + { + return vec2(-x(), -y()); + } + + constexpr vec2 operator*(T s) const + { + return vec2(x() * s, y() * s); + } + + vec2& operator*=(T s) + { + x() *= s; + y() *= s; + + return *this; + } + + constexpr vec2 operator*(const vec2& other) const + { + return vec2(x() * other.x(), y() * other.y()); + } + +}; + +using vec2s = vec2; + +#endif /* end of include guard: COORDINATES_H_A45D34FB */ -- cgit 1.4.1