clean up datatypes a bit

This commit is contained in:
zetaPRIME 2017-03-04 07:50:08 -05:00
parent e7e9ed7de7
commit 017fd97d46
4 changed files with 21 additions and 22 deletions

View File

@ -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::invalid = VRect(Vector2::invalid, Vector2::invalid);
const VRect VRect::zero = VRect(); // should initialize to 0,0,0,0

View File

@ -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;
};
}

View File

@ -1,17 +1,12 @@
#include <cstdlib>
#include <cmath>
#include <fastmath.h>
#include <limits>
#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,6 +30,8 @@ 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<double>::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);

View File

@ -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;