From 017fd97d4678062ee8722e8cda1b4a6e574b685f Mon Sep 17 00:00:00 2001 From: zetaPRIME Date: Sat, 4 Mar 2017 07:50:08 -0500 Subject: [PATCH] clean up datatypes a bit --- libstarlight/source/starlight/datatypes/VRect.cpp | 11 +++-------- libstarlight/source/starlight/datatypes/VRect.h | 11 +++++++---- libstarlight/source/starlight/datatypes/Vector2.cpp | 11 ++++------- libstarlight/source/starlight/datatypes/Vector2.h | 10 +++++++--- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/libstarlight/source/starlight/datatypes/VRect.cpp b/libstarlight/source/starlight/datatypes/VRect.cpp index 521d4d3..7d3e393 100644 --- a/libstarlight/source/starlight/datatypes/VRect.cpp +++ b/libstarlight/source/starlight/datatypes/VRect.cpp @@ -8,13 +8,6 @@ using starlight::Vector2; using starlight::VRect; -VRect::VRect() : pos(Vector2()), size(Vector2()) { } - -VRect::~VRect() { } - -VRect::VRect(float x, float y, float w, float h) : pos(x, y), size(w, h) { } -VRect::VRect(Vector2 pos, Vector2 size) : pos(pos), size(size) { } - Vector2 VRect::Center() const { return pos + (size * 0.5f); } Vector2 VRect::TopLeft() const { return pos; } Vector2 VRect::TopRight() const { return Vector2(pos.x + size.x, pos.y); } @@ -62,4 +55,6 @@ VRect VRect::LeftEdge(float width) const { return VRect(pos.x, pos.y, width, VRect VRect::RightEdge(float width) const { return VRect(pos.x + size.x - width, pos.y, width, size.y); } // constants -const VRect VRect::zero = VRect(); // should initialize to 0,0,0,0 \ No newline at end of file +const VRect VRect::invalid = VRect(Vector2::invalid, Vector2::invalid); + +const VRect VRect::zero = VRect(); // should initialize to 0,0,0,0 diff --git a/libstarlight/source/starlight/datatypes/VRect.h b/libstarlight/source/starlight/datatypes/VRect.h index 79049d0..a149bc7 100644 --- a/libstarlight/source/starlight/datatypes/VRect.h +++ b/libstarlight/source/starlight/datatypes/VRect.h @@ -12,10 +12,10 @@ namespace starlight { Vector2 pos; Vector2 size; - VRect(); - VRect(float x, float y, float w, float h); - VRect(Vector2 pos, Vector2 size); - ~VRect(); + constexpr VRect() = default; + constexpr VRect(float x, float y, float w, float h) : pos(x, y), size(w, h) { } + constexpr VRect(Vector2 pos, Vector2 size) : pos(pos), size(size) { } + ~VRect() = default; Vector2 Center() const; Vector2 TopLeft() const; @@ -54,6 +54,9 @@ namespace starlight { inline VRect & operator += (const Vector2 & vec) { pos += vec; return *this; } inline VRect & operator -= (const Vector2 & vec) { pos -= vec; return *this; } + inline explicit operator bool() { return pos && size; } + + static const VRect invalid; static const VRect zero; }; } diff --git a/libstarlight/source/starlight/datatypes/Vector2.cpp b/libstarlight/source/starlight/datatypes/Vector2.cpp index 5d29ec3..b92b77c 100644 --- a/libstarlight/source/starlight/datatypes/Vector2.cpp +++ b/libstarlight/source/starlight/datatypes/Vector2.cpp @@ -1,17 +1,12 @@ #include #include #include +#include #include "Vector2.h" using starlight::Vector2; -Vector2::Vector2() : x(0.0f), y(0.0f) { } - -Vector2::~Vector2() { } - -Vector2::Vector2(float x, float y) : x(x), y(y) { } - // maths float Vector2::Length() const { return sqrtf(x * x + y * y); } Vector2 Vector2::Normalized() const { float m = Length(); return m == 0.0f ? Vector2::zero : Vector2(x / m, y / m); } @@ -35,9 +30,11 @@ Vector2 Vector2::RotateAround(const Vector2& anchor, float angle) const { return //inline Vector2 operator *(const Vector2* vec, const float scalar) { return Vector2(vec.x * scalar, vec.y * scalar); } // constants +const Vector2 Vector2::invalid = Vector2(std::numeric_limits::quiet_NaN()); + const Vector2 Vector2::zero = Vector2(0.0f, 0.0f); const Vector2 Vector2::one = Vector2(1.0f, 1.0f); const Vector2 Vector2::half = Vector2(0.5f, 0.5f); const Vector2 Vector2::h = Vector2(1, 0); -const Vector2 Vector2::v = Vector2(0, 1); \ No newline at end of file +const Vector2 Vector2::v = Vector2(0, 1); diff --git a/libstarlight/source/starlight/datatypes/Vector2.h b/libstarlight/source/starlight/datatypes/Vector2.h index fe78cc2..c28bc0c 100644 --- a/libstarlight/source/starlight/datatypes/Vector2.h +++ b/libstarlight/source/starlight/datatypes/Vector2.h @@ -10,9 +10,10 @@ namespace starlight { float x = 0.0f; float y = 0.0f; - Vector2(); - Vector2(float x, float y); - ~Vector2(); + constexpr Vector2() = default; + constexpr Vector2(float x, float y) : x(x), y(y) { } + constexpr Vector2(float mag) : x(mag), y(mag) { } + ~Vector2() = default; float Length() const; Vector2 Normalized() const; @@ -47,6 +48,9 @@ namespace starlight { inline Vector2 & operator -= (const Vector2 & o) { x -= o.x; y -= o.y; return *this; } inline Vector2 & operator *= (const Vector2 & o) { x *= o.x; y *= o.y; return *this; } + inline explicit operator bool() const { return x == x && y == y; } + + static const Vector2 invalid; static const Vector2 zero; static const Vector2 one; static const Vector2 half;