Pārlūkot izejas kodu

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.
legacy
Sam Hocevar sam pirms 13 gadiem
vecāks
revīzija
01ebcec3b8
10 mainītis faili ar 80 papildinājumiem un 60 dzēšanām
  1. +1
    -1
      src/debug/record.cpp
  2. +1
    -1
      src/font.cpp
  3. +1
    -1
      src/image/codec/dummy-image.cpp
  4. +4
    -4
      src/input.cpp
  5. +2
    -2
      src/map.cpp
  6. +16
    -1
      src/matrix.cpp
  7. +43
    -38
      src/matrix.h
  8. +1
    -1
      src/text.cpp
  9. +1
    -1
      src/tileset.cpp
  10. +10
    -10
      src/worldentity.cpp

+ 1
- 1
src/debug/record.cpp Parādīt failu

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


+ 1
- 1
src/font.cpp Parādīt failu

@@ -46,7 +46,7 @@ Font::Font(char const *path)
data->name = (char *)malloc(7 + strlen(path) + 1);
sprintf(data->name, "<font> %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;


+ 1
- 1
src/image/codec/dummy-image.cpp Parādīt failu

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


+ 4
- 4
src/input.cpp Parādīt failu

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



+ 2
- 2
src/map.cpp Parādīt failu

@@ -135,8 +135,8 @@ Map::Map(char const *path)
else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1)
{
/* This is a tileset image file. Associate it with firstgid. */
data->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);
}


+ 16
- 1
src/matrix.cpp Parādīt failu

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


+ 43
- 38
src/matrix.h Parādīt failu

@@ -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<typename U> \
inline operator Vec##elems<U>() const \
{ \
Vec##elems<U> ret; \
for (int n = 0; n < elems; n++) \
ret[n] = static_cast<U>((*this)[n]); \
return ret; \
} \
\
inline Vec##elems<T> operator -() const \
{ \
Vec##elems<T> ret; \
@@ -116,28 +99,47 @@ namespace lol
} \
\
template<typename U> \
friend U dot(Vec##elems<U>, Vec##elems<U>); \
template<typename U> \
friend Vec##elems<U> normalize(Vec##elems<U>); \
\
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<typename U> \
inline operator Vec##elems<U>() const \
{ \
Vec##elems<U> ret; \
for (int n = 0; n < elems; n++) \
ret[n] = static_cast<U>((*this)[n]); \
return ret; \
} \
\
template<typename U> \
friend U dot(Vec##elems<U>, Vec##elems<U>);

#define SWIZZLE2(e1, e2) \
inline Vec2<T> e1##e2() const \
{ \
return Vec2<T>(e1, e2); \
return Vec2<T>(this->e1, this->e2); \
}

#define SWIZZLE3(e1, e2, e3) \
inline Vec3<T> e1##e2##e3() const \
{ \
return Vec3<T>(e1, e2, e3); \
return Vec3<T>(this->e1, this->e2, this->e3); \
}

#define SWIZZLE4(e1, e2, e3, e4) \
inline Vec4<T> e1##e2##e3##e4() const \
{ \
return Vec4<T>(e1, e2, e3, e4); \
return Vec4<T>(this->e1, this->e2, this->e3, this->e4); \
}

#define SWIZZLE22(e1) \
@@ -194,10 +196,11 @@ template <typename T> struct Vec4;
template <typename T> 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<uint64_t> u64vec2;
template <typename T> 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<T> _xy, T _z) { x = _xy.x; y = _xy.y; z = _z; }
inline Vec3(T _x, Vec2<T> _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<uint64_t> u64vec3;
template <typename T> 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<T> _xy, T _z, T _w) { x = _xy.x; y = _xy.y; z = _z; w = _w; }
inline Vec4(T _x, Vec2<T> _yz, T _w) { x = _x; y = _yz.x; z = _yz.y; w = _w; }
inline Vec4(T _x, T _y, Vec2<T> _zw) { x = _x; y = _y; z = _zw.x; w = _zw.y; }
inline Vec4(Vec2<T> _xy, Vec2<T> _zw) { x = _xy.x; y = _xy.y; z = _zw.x; w = _zw.y; }
inline Vec4(Vec3<T> _xyz, T _w) { x = _xyz.x; y = _xyz.y; z = _xyz.z; w = _w; }
inline Vec4(T _x, Vec3<T> _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<T> _xy, T _z, T _w) : x(_xy.x), y(_xy.y), z(_z), w(_w) { }
inline Vec4(T _x, Vec2<T> _yz, T _w) : x(_x), y(_yz.x), z(_yz.y), w(_w) { }
inline Vec4(T _x, T _y, Vec2<T> _zw) : x(_x), y(_y), z(_zw.x), w(_zw.y) { }
inline Vec4(Vec2<T> _xy, Vec2<T> _zw) : x(_xy.x), y(_xy.y), z(_zw.x), w(_zw.y) { }
inline Vec4(Vec3<T> _xyz, T _w) : x(_xyz.x), y(_xyz.y), z(_xyz.z), w(_w) { }
inline Vec4(T _x, Vec3<T> _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 <typename T> 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++)


+ 1
- 1
src/text.cpp Parādīt failu

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


+ 1
- 1
src/tileset.cpp Parādīt failu

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


+ 10
- 10
src/worldentity.cpp Parādīt failu

@@ -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()


Notiek ielāde…
Atcelt
Saglabāt