From 6767dae7411519edffd9db6467fe80b87d3f591e Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 29 Oct 2011 15:37:53 +0000 Subject: [PATCH] core: disallow casting eg. a vec3 into a vec2. Use the xy() getter instead. --- src/input.cpp | 2 +- src/matrix.h | 112 +++++++++++++++++++++++++++++++++++++------ test/unit/matrix.cpp | 8 ++-- 3 files changed, 102 insertions(+), 20 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 9276c830..ae832e40 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -130,7 +130,7 @@ void Input::SetMousePos(ivec2 coord) { if (data->entities[n] == top) { - data->entities[n]->mousepos = (ivec2)((ivec3)coord - top->bbox[0]); + data->entities[n]->mousepos = coord - top->bbox[0].xy(); if (top != data->lastfocus) data->entities[n]->pressed = data->buttons; else diff --git a/src/matrix.h b/src/matrix.h index 0a9cf033..310e8dfc 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -63,17 +63,6 @@ namespace lol return *this = (*this) op val; \ } -#define CAST_OP(elems, dest) \ - inline operator Vec##dest() const \ - { \ - Vec##dest ret; \ - for (int n = 0; n < elems && n < dest; n++) \ - ret[n] = (*this)[n]; \ - for (int n = elems; n < dest; n++) \ - ret[n] = 0; \ - return ret; \ - } - #define OPERATORS(elems) \ inline T& operator[](int n) { return *(&x + n); } \ inline T const& operator[](int n) const { return *(&x + n); } \ @@ -95,10 +84,6 @@ namespace lol SCALAR_OP(elems, *) \ SCALAR_OP(elems, /) \ \ - CAST_OP(elems, 2) \ - CAST_OP(elems, 3) \ - CAST_OP(elems, 4) \ - \ template \ inline operator Vec##elems() const \ { \ @@ -130,10 +115,75 @@ namespace lol return sqrtf((float)sqlen()); \ } +#define SWIZZLE2(e1, e2) \ + inline Vec2 e1##e2() const \ + { \ + return Vec2(e1, e2); \ + } + +#define SWIZZLE3(e1, e2, e3) \ + inline Vec3 e1##e2##e3() const \ + { \ + return Vec3(e1, e2, e3); \ + } + +#define SWIZZLE4(e1, e2, e3, e4) \ + inline Vec4 e1##e2##e3##e4() const \ + { \ + return Vec4(e1, e2, e3, e4); \ + } + +#define SWIZZLE22(e1) \ + SWIZZLE2(e1, x); SWIZZLE2(e1, y); +#define SWIZZLE23(e1) \ + SWIZZLE2(e1, x); SWIZZLE2(e1, y); SWIZZLE2(e1, z); +#define SWIZZLE24(e1) \ + SWIZZLE2(e1, x); SWIZZLE2(e1, y); SWIZZLE2(e1, z); SWIZZLE2(e1, w); + +#define SWIZZLE32(e1, e2) \ + SWIZZLE3(e1, e2, x); SWIZZLE3(e1, e2, y); +#define SWIZZLE322(e1) \ + SWIZZLE32(e1, x); SWIZZLE32(e1, y); +#define SWIZZLE33(e1, e2) \ + SWIZZLE3(e1, e2, x); SWIZZLE3(e1, e2, y); SWIZZLE3(e1, e2, z); +#define SWIZZLE333(e1) \ + SWIZZLE33(e1, x); SWIZZLE33(e1, y); SWIZZLE33(e1, z); +#define SWIZZLE34(e1, e2) \ + SWIZZLE3(e1, e2, x); SWIZZLE3(e1, e2, y); \ + SWIZZLE3(e1, e2, z); SWIZZLE3(e1, e2, w); +#define SWIZZLE344(e1) \ + SWIZZLE34(e1, x); SWIZZLE34(e1, y); \ + SWIZZLE34(e1, z); SWIZZLE34(e1, w); + +#define SWIZZLE42(e1, e2, e3) \ + SWIZZLE4(e1, e2, e3, x); SWIZZLE4(e1, e2, e3, y); +#define SWIZZLE422(e1, e2) \ + SWIZZLE42(e1, e2, x); SWIZZLE42(e1, e2, y); +#define SWIZZLE4222(e1) \ + SWIZZLE422(e1, x); SWIZZLE422(e1, y); +#define SWIZZLE43(e1, e2, e3) \ + SWIZZLE4(e1, e2, e3, x); SWIZZLE4(e1, e2, e3, y); SWIZZLE4(e1, e2, e3, z); +#define SWIZZLE433(e1, e2) \ + SWIZZLE43(e1, e2, x); SWIZZLE43(e1, e2, y); SWIZZLE43(e1, e2, z); +#define SWIZZLE4333(e1) \ + SWIZZLE433(e1, x); SWIZZLE433(e1, y); SWIZZLE433(e1, z); +#define SWIZZLE44(e1, e2, e3) \ + SWIZZLE4(e1, e2, e3, x); SWIZZLE4(e1, e2, e3, y); \ + SWIZZLE4(e1, e2, e3, z); SWIZZLE4(e1, e2, e3, w); +#define SWIZZLE444(e1, e2) \ + SWIZZLE44(e1, e2, x); SWIZZLE44(e1, e2, y); \ + SWIZZLE44(e1, e2, z); SWIZZLE44(e1, e2, w); +#define SWIZZLE4444(e1) \ + SWIZZLE444(e1, x); SWIZZLE444(e1, y); SWIZZLE444(e1, z); SWIZZLE444(e1, w); + template struct Vec2; template struct Vec3; template struct Vec4; +/* + * 2-element vectors + */ + template struct Vec2 { inline Vec2() { } @@ -142,6 +192,10 @@ template struct Vec2 OPERATORS(2) + SWIZZLE22(x); SWIZZLE22(y); + SWIZZLE322(x); SWIZZLE322(y); + SWIZZLE4222(x); SWIZZLE4222(y); + #if !defined __ANDROID__ template friend std::ostream &operator<<(std::ostream &stream, Vec2 const &v); @@ -154,14 +208,24 @@ template struct Vec2 typedef Vec2 vec2; typedef Vec2 ivec2; +/* + * 3-element vectors + */ + template struct Vec3 { inline Vec3() { } inline Vec3(T val) { x = y = z = val; } inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; } + inline Vec3(Vec2 _xy, T _z) { x = _xy.x; y = _xy.y; z = _z; } + inline Vec3(T _x, Vec2 _yz) { x = _x; y = _yz.x; z = _yz.y; } OPERATORS(3) + SWIZZLE23(x); SWIZZLE23(y); SWIZZLE23(z); + SWIZZLE333(x); SWIZZLE333(y); SWIZZLE333(z); + SWIZZLE4333(x); SWIZZLE4333(y); SWIZZLE4333(z); + #if !defined __ANDROID__ template friend std::ostream &operator<<(std::ostream &stream, Vec3 const &v); @@ -175,14 +239,28 @@ template struct Vec3 typedef Vec3 vec3; typedef Vec3 ivec3; +/* + * 4-element vectors + */ + template struct Vec4 { inline Vec4() { } inline Vec4(T val) { x = y = z = w = val; } inline Vec4(T _x, T _y, T _z, T _w) { x = _x; y = _y; z = _z; w = _w; } + inline Vec4(Vec2 _xy, T _z, T _w) { x = _xy.x; y = _xy.y; z = _z; w = _w; } + inline Vec4(T _x, Vec2 _yz, T _w) { x = _x; y = _yz.x; z = _yz.y; w = _w; } + inline Vec4(T _x, T _y, Vec2 _zw) { x = _x; y = _y; z = _zw.x; w = _zw.y; } + inline Vec4(Vec2 _xy, Vec2 _zw) { x = _xy.x; y = _xy.y; z = _zw.x; w = _zw.y; } + inline Vec4(Vec3 _xyz, T _w) { x = _xyz.x; y = _xyz.y; z = _xyz.z; w = _w; } + inline Vec4(T _x, Vec3 _yzw) { x = _x; y = _yzw.x; z = _yzw.y; w = _yzw.z; } OPERATORS(4) + SWIZZLE24(x); SWIZZLE24(y); SWIZZLE24(z); SWIZZLE24(w); + SWIZZLE344(x); SWIZZLE344(y); SWIZZLE344(z); SWIZZLE344(w); + SWIZZLE4444(x); SWIZZLE4444(y); SWIZZLE4444(z); SWIZZLE4444(w); + #if !defined __ANDROID__ template friend std::ostream &operator<<(std::ostream &stream, Vec4 const &v); @@ -222,6 +300,10 @@ GLOBALS(2) GLOBALS(3) GLOBALS(4) +/* + * 4×4-element matrices + */ + template struct Mat4 { inline Mat4() { } diff --git a/test/unit/matrix.cpp b/test/unit/matrix.cpp index a6c5e135..4e6db4e0 100644 --- a/test/unit/matrix.cpp +++ b/test/unit/matrix.cpp @@ -120,15 +120,15 @@ LOLUNIT_FIXTURE(MatrixTest) { vec2 a1(1.0f, 3.0f); - vec3 b(a1); - vec2 a2(b); + vec3 b(a1, 0.0f); + vec2 a2(b.xy()); LOLUNIT_ASSERT_EQUAL(b.x, a1.x); LOLUNIT_ASSERT_EQUAL(b.y, a1.y); LOLUNIT_ASSERT_EQUAL(b.z, 0.0f); LOLUNIT_ASSERT_EQUAL(a2, a1); - vec4 c(a1); - vec2 a3(c); + vec4 c(a1, 0.0f, 0.0f); + vec2 a3(c.xy()); LOLUNIT_ASSERT_EQUAL(c.x, a1.x); LOLUNIT_ASSERT_EQUAL(c.y, a1.y); LOLUNIT_ASSERT_EQUAL(c.z, 0.0f);