From e0703476b9855deb5a0426836a2359fa0b7edd9b Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 15 Jun 2020 20:16:49 +0200 Subject: [PATCH] Added Vector2 structure --- src/sdlf/CMakeLists.txt | 1 + src/sdlf/maths/Vector2.hpp | 89 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/sdlf/maths/Vector2.hpp diff --git a/src/sdlf/CMakeLists.txt b/src/sdlf/CMakeLists.txt index 51a2e1c..e60b566 100644 --- a/src/sdlf/CMakeLists.txt +++ b/src/sdlf/CMakeLists.txt @@ -5,6 +5,7 @@ add_library(sdlf STATIC target_include_directories(sdlf PRIVATE "${CMAKE_SOURCE_DIR}/3rdparty/include/SDL" + "maths" ) target_link_libraries(sdlf PRIVATE diff --git a/src/sdlf/maths/Vector2.hpp b/src/sdlf/maths/Vector2.hpp new file mode 100644 index 0000000..c64b8c2 --- /dev/null +++ b/src/sdlf/maths/Vector2.hpp @@ -0,0 +1,89 @@ +#pragma once + +#include + +namespace sf +{ + template::value, T>> + struct Vector2 + { + T x, y; + + Vector2() : + x(0), y(0) + { /* Empty */ } + + Vector2(T x, T y) : + x(x), y(y) + { /* Empty */ } + + Vector2(const Vector2& other) : + x(other.x), y(other.y) + { /* Empty */ } + + + //////////////////////// OPERATOR OVERLOADS //////////////////////// + friend Vector2 operator-(const Vector2& other) + { + return Vector2(-other.x, -other.y); + } + + friend Vector2 operator+(const Vector2& left, const Vector2& right) + { + return Vector2(left.x + right.x, left.y + right.y); + } + + Vector2& operator+=(const Vector2& right) + { + x += right.x; + y += right.y; + return this; + } + + friend Vector2 operator-(const Vector2& left, const Vector2& right) + { + return Vector2(left.x - right.x, left.y - right.y); + } + + Vector2& operator-=(const Vector2& right) + { + x -= right.x; + y -= right.y; + return this; + } + + friend Vector2 operator*(const Vector2& left, T right) + { + return Vector2(left.x * right, left.y * right); + } + + friend Vector2 operator*(T left, const Vector2& right) + { + return Vector2(left * right.x, left * right.x); + } + + Vector2& operator*=(T right) + { + x *= right; + y *= left; + return this; + } + + friend Vector2 operator/(const Vector2& left, T right) + { + return Vector2(left.x / right, left.y / right); + } + + Vector2& operator/=(T right) + { + x /= right; + y /= left; + return this; + } + }; + + typedef Vector2 Vector2i, Vec2i; + typedef Vector2 Vector2u, Vec2u; + typedef Vector2 Vector2f, Vec2f; + typedef Vector2 Vector2d, Vec2d; +} \ No newline at end of file