From 01ebcec3b8db2bb30621b77536b58c480dda1638 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 1 Nov 2011 17:55:19 +0000 Subject: [PATCH] core: split vector operations into linear and non-linear so that we can reuse the linear operations in quaternions. Also mark some constructors explicit to better spot coding errors. --- src/debug/record.cpp | 2 +- src/font.cpp | 2 +- src/image/codec/dummy-image.cpp | 2 +- src/input.cpp | 8 ++-- src/map.cpp | 4 +- src/matrix.cpp | 17 ++++++- src/matrix.h | 81 +++++++++++++++++---------------- src/text.cpp | 2 +- src/tileset.cpp | 2 +- src/worldentity.cpp | 20 ++++---- 10 files changed, 80 insertions(+), 60 deletions(-) diff --git a/src/debug/record.cpp b/src/debug/record.cpp index 29a345dc..65c8e55f 100644 --- a/src/debug/record.cpp +++ b/src/debug/record.cpp @@ -53,7 +53,7 @@ DebugRecord::DebugRecord(char const *path, float fps) Ticker::StartRecording(); data->path = strdup(path); - data->size = 0; + data->size = ivec2(0); data->fps = (int)(fps + 0.5f); #if defined USE_PIPI data->sequence = NULL; diff --git a/src/font.cpp b/src/font.cpp index 4cfc82a1..28f4060f 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -46,7 +46,7 @@ Font::Font(char const *path) data->name = (char *)malloc(7 + strlen(path) + 1); sprintf(data->name, " %s", path); - data->tileset = Tiler::Register(path, 0, 16, 1.0f); + data->tileset = Tiler::Register(path, ivec2(0), ivec2(16), 1.0f); data->size = data->tileset->GetSize(0); drawgroup = DRAWGROUP_BEFORE; diff --git a/src/image/codec/dummy-image.cpp b/src/image/codec/dummy-image.cpp index 5995dade..406b4e6f 100644 --- a/src/image/codec/dummy-image.cpp +++ b/src/image/codec/dummy-image.cpp @@ -44,7 +44,7 @@ private: bool DummyImageData::Open(char const *path) { - size = 256; + size = ivec2(256); format = Image::FORMAT_RGBA; pixels = (uint8_t *)malloc(256 * 256 * 4 * sizeof(*pixels)); uint8_t *parser = pixels; diff --git a/src/input.cpp b/src/input.cpp index 58e84527..b3d4334d 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -138,15 +138,15 @@ void Input::SetMousePos(ivec2 coord) if (top != data->lastfocus) data->entities[n]->pressed = data->buttons; else - data->entities[n]->clicked = 0; + data->entities[n]->clicked = ivec3(0); } else { data->entities[n]->mousepos = ivec2(-1); /* FIXME */ - data->entities[n]->released = 0; - data->entities[n]->pressed = 0; - data->entities[n]->clicked = 0; + data->entities[n]->released = ivec3(0); + data->entities[n]->pressed = ivec3(0); + data->entities[n]->clicked = ivec3(0); } } diff --git a/src/map.cpp b/src/map.cpp index a9b0e92f..f088b6f6 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -135,8 +135,8 @@ Map::Map(char const *path) else if (sscanf(tmp, " tilesets[data->ntilers] = Tiler::Register(str, 32, 0, - sqrtf(2)); + data->tilesets[data->ntilers] = Tiler::Register(str, ivec2(32), + ivec2(0), sqrtf(2)); data->ntilers++; //Log::Debug("new tiler %s\n", str); } diff --git a/src/matrix.cpp b/src/matrix.cpp index bf71fd32..dce0c763 100644 --- a/src/matrix.cpp +++ b/src/matrix.cpp @@ -73,7 +73,7 @@ template<> vec4 normalize(vec4 v) { float norm = v.len(); if (!norm) - return vec4(0); + return vec4(0.0f); return v / norm; } @@ -126,16 +126,31 @@ template<> void vec2::printf() const Log::Debug("[ %6.6f %6.6f ]\n", x, y); } +template<> void ivec2::printf() const +{ + Log::Debug("[ %i %i ]\n", x, y); +} + template<> void vec3::printf() const { Log::Debug("[ %6.6f %6.6f %6.6f ]\n", x, y, z); } +template<> void ivec3::printf() const +{ + Log::Debug("[ %i %i %i ]\n", x, y, z); +} + template<> void vec4::printf() const { Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w); } +template<> void ivec4::printf() const +{ + Log::Debug("[ %i %i %i %i ]\n", x, y, z, w); +} + template<> void mat4::printf() const { mat4 const &p = *this; diff --git a/src/matrix.h b/src/matrix.h index 84331e5b..50fafc6e 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -63,36 +63,19 @@ namespace lol return *this = (*this) op val; \ } -#define OPERATORS(elems) \ +#define LINEAR_OPS(elems) \ inline T& operator[](int n) { return *(&x + n); } \ inline T const& operator[](int n) const { return *(&x + n); } \ \ VECTOR_OP(elems, -) \ VECTOR_OP(elems, +) \ - VECTOR_OP(elems, *) \ - VECTOR_OP(elems, /) \ \ BOOL_OP(elems, ==, ==, true) \ BOOL_OP(elems, !=, ==, false) \ - BOOL_OP(elems, <=, <=, true) \ - BOOL_OP(elems, >=, >=, true) \ - BOOL_OP(elems, <, <, true) \ - BOOL_OP(elems, >, >, true) \ \ - SCALAR_OP(elems, -) \ - SCALAR_OP(elems, +) \ SCALAR_OP(elems, *) \ SCALAR_OP(elems, /) \ \ - template \ - inline operator Vec##elems() const \ - { \ - Vec##elems ret; \ - for (int n = 0; n < elems; n++) \ - ret[n] = static_cast((*this)[n]); \ - return ret; \ - } \ - \ inline Vec##elems operator -() const \ { \ Vec##elems ret; \ @@ -116,28 +99,47 @@ namespace lol } \ \ template \ - friend U dot(Vec##elems, Vec##elems); \ - template \ friend Vec##elems normalize(Vec##elems); \ \ void printf() const; +#define OTHER_OPS(elems) \ + VECTOR_OP(elems, *) \ + VECTOR_OP(elems, /) \ + \ + BOOL_OP(elems, <=, <=, true) \ + BOOL_OP(elems, >=, >=, true) \ + BOOL_OP(elems, <, <, true) \ + BOOL_OP(elems, >, >, true) \ + \ + template \ + inline operator Vec##elems() const \ + { \ + Vec##elems ret; \ + for (int n = 0; n < elems; n++) \ + ret[n] = static_cast((*this)[n]); \ + return ret; \ + } \ + \ + template \ + friend U dot(Vec##elems, Vec##elems); + #define SWIZZLE2(e1, e2) \ inline Vec2 e1##e2() const \ { \ - return Vec2(e1, e2); \ + return Vec2(this->e1, this->e2); \ } #define SWIZZLE3(e1, e2, e3) \ inline Vec3 e1##e2##e3() const \ { \ - return Vec3(e1, e2, e3); \ + return Vec3(this->e1, this->e2, this->e3); \ } #define SWIZZLE4(e1, e2, e3, e4) \ inline Vec4 e1##e2##e3##e4() const \ { \ - return Vec4(e1, e2, e3, e4); \ + return Vec4(this->e1, this->e2, this->e3, this->e4); \ } #define SWIZZLE22(e1) \ @@ -194,10 +196,11 @@ template struct Vec4; template struct Vec2 { inline Vec2() { } - inline Vec2(T val) { x = y = val; } + explicit inline Vec2(T val) { x = y = val; } inline Vec2(T _x, T _y) { x = _x; y = _y; } - OPERATORS(2) + LINEAR_OPS(2) + OTHER_OPS(2) SWIZZLE22(x); SWIZZLE22(y); SWIZZLE322(x); SWIZZLE322(y); @@ -230,12 +233,13 @@ typedef Vec2 u64vec2; template struct Vec3 { inline Vec3() { } - inline Vec3(T val) { x = y = z = val; } + explicit 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) + LINEAR_OPS(3) + OTHER_OPS(3) SWIZZLE23(x); SWIZZLE23(y); SWIZZLE23(z); SWIZZLE333(x); SWIZZLE333(y); SWIZZLE333(z); @@ -272,16 +276,17 @@ typedef Vec3 u64vec3; 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) + explicit inline Vec4(T val) : x(val), y(val), z(val), 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) { } + + LINEAR_OPS(4) + OTHER_OPS(4) SWIZZLE24(x); SWIZZLE24(y); SWIZZLE24(z); SWIZZLE24(w); SWIZZLE344(x); SWIZZLE344(y); SWIZZLE344(z); SWIZZLE344(w); @@ -341,7 +346,7 @@ GLOBALS(4) template struct Mat4 { inline Mat4() { } - inline Mat4(T val) + explicit inline Mat4(T val) { for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++) diff --git a/src/text.cpp b/src/text.cpp index e118830b..d74d7d3d 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -87,7 +87,7 @@ void Text::TickDraw(float deltams) if (data->text) { Font *font = Forge::GetFont(data->font); - vec3 delta = 0; + vec3 delta(0.0f); if (data->align == ALIGN_RIGHT) delta.x -= data->length * font->GetSize().x; else if (data->align == ALIGN_CENTER) diff --git a/src/tileset.cpp b/src/tileset.cpp index 76a69605..3f734639 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -72,7 +72,7 @@ TileSet::TileSet(char const *path, ivec2 size, ivec2 count, float dilate) else { if (size.x <= 0 || size.y <= 0) - size = 32; + size = ivec2(32, 32); data->count.i = data->isize.x > size.i ? data->isize.x / size.i : 1; data->count.j = data->isize.y > size.j ? data->isize.y / size.j : 1; data->size = size; diff --git a/src/worldentity.cpp b/src/worldentity.cpp index 34dd6686..41f5abdb 100644 --- a/src/worldentity.cpp +++ b/src/worldentity.cpp @@ -25,16 +25,16 @@ namespace lol WorldEntity::WorldEntity() { - position = 0; - rotation = 0; - velocity = 0; - bbox[0] = bbox[1] = 0; - - mousepos = 0; - mousebuttons = 0; - pressed = 0; - clicked = 0; - released = 0; + position = vec3(0); + rotation = vec3(0); + velocity = vec3(0); + bbox[0] = bbox[1] = vec3(0); + + mousepos = ivec2(0); + mousebuttons = ivec3(0); + pressed = ivec3(0); + clicked = ivec3(0); + released = ivec3(0); } WorldEntity::~WorldEntity()