Browse Source

Get rid of float3, float4, int3 etc. in favour of GLSL types.

legacy
Sam Hocevar sam 14 years ago
parent
commit
6405ef32ea
20 changed files with 101 additions and 110 deletions
  1. +2
    -2
      src/debugfps.cpp
  2. +5
    -5
      src/emitter.cpp
  3. +2
    -2
      src/emitter.h
  4. +3
    -3
      src/font.cpp
  5. +2
    -2
      src/font.h
  6. +9
    -9
      src/input.cpp
  7. +4
    -4
      src/input.h
  8. +16
    -16
      src/matrix.cpp
  9. +0
    -10
      src/matrix.h
  10. +6
    -6
      src/scene.cpp
  11. +1
    -1
      src/sdlinput.cpp
  12. +4
    -4
      src/text.cpp
  13. +1
    -1
      src/text.h
  14. +3
    -3
      src/tiler.cpp
  15. +4
    -3
      src/tiler.h
  16. +5
    -5
      src/tileset.cpp
  17. +3
    -3
      src/tileset.h
  18. +7
    -7
      src/video.cpp
  19. +8
    -8
      src/worldentity.h
  20. +16
    -16
      test/matrix.cpp

+ 2
- 2
src/debugfps.cpp View File

@@ -40,12 +40,12 @@ DebugFps::DebugFps(int x, int y)
for (int i = 0; i < 5; i ++)
{
data->lines[i] = new Text(NULL, "gfx/font/ascii.png");
data->lines[i]->SetPos(int3(x, y + (i ? 8 : 0) + 16 * i, 0));
data->lines[i]->SetPos(vec3i(x, y + (i ? 8 : 0) + 16 * i, 0));
Ticker::Ref(data->lines[i]);
}
#else
data->lines[0] = new Text(NULL, "gfx/font/ascii.png");
data->lines[0]->SetPos(int3(x, y, 100));
data->lines[0]->SetPos(vec3i(x, y, 100));
Ticker::Ref(data->lines[0]);
#endif
}


+ 5
- 5
src/emitter.cpp View File

@@ -29,10 +29,10 @@ class EmitterData

private:
int tiler;
float3 gravity;
vec3 gravity;
int particles[MAX_PARTICLES];
float3 positions[MAX_PARTICLES];
float3 velocities[MAX_PARTICLES];
vec3 positions[MAX_PARTICLES];
vec3 velocities[MAX_PARTICLES];
int nparticles;
};

@@ -40,7 +40,7 @@ private:
* Public Emitter class
*/

Emitter::Emitter(int tiler, float3 gravity)
Emitter::Emitter(int tiler, vec3 gravity)
: data(new EmitterData())
{
data->tiler = tiler;
@@ -77,7 +77,7 @@ void Emitter::TickDraw(float deltams)
data->positions[i].z, 0);
}

void Emitter::AddParticle(int id, float3 pos, float3 vel)
void Emitter::AddParticle(int id, vec3 pos, vec3 vel)
{
if (data->nparticles >= EmitterData::MAX_PARTICLES)
return;


+ 2
- 2
src/emitter.h View File

@@ -23,10 +23,10 @@ class EmitterData;
class Emitter : public Entity
{
public:
Emitter(int tiler, float3 gravity);
Emitter(int tiler, vec3 gravity);
virtual ~Emitter();

void AddParticle(int id, float3 pos, float3 vel);
void AddParticle(int id, vec3 pos, vec3 vel);

protected:
virtual void TickGame(float deltams);


+ 3
- 3
src/font.cpp View File

@@ -28,7 +28,7 @@ class FontData
private:
char *name;
int tiler;
int2 size;
vec2i size;
};

/*
@@ -64,7 +64,7 @@ char const *Font::GetName()
return data->name;
}

void Font::Print(int3 pos, char const *str)
void Font::Print(vec3i pos, char const *str)
{
Scene *scene = Scene::GetDefault();

@@ -80,7 +80,7 @@ void Font::Print(int3 pos, char const *str)
}
}

int2 Font::GetSize() const
vec2i Font::GetSize() const
{
return data->size;
}


+ 2
- 2
src/font.h View File

@@ -33,8 +33,8 @@ protected:

public:
/* New methods */
void Print(int3 pos, char const *str);
int2 GetSize() const;
void Print(vec3i pos, char const *str);
vec2i GetSize() const;

private:
FontData *data;


+ 9
- 9
src/input.cpp View File

@@ -37,8 +37,8 @@ public:
{ }

private:
int2 mouse;
int3 buttons;
vec2i mouse;
vec3i buttons;

static int const MAX_ENTITIES = 100;
WorldEntity *entities[MAX_ENTITIES];
@@ -53,10 +53,10 @@ static InputData * const data = &inputdata;
* Public Input class
*/

float2 Input::GetAxis(int axis)
vec2 Input::GetAxis(int axis)
{
float invsqrt2 = sqrtf(0.5f);
float2 f;
vec2 f;

/* Simulate a joystick using the keyboard. This SDL call is free. */
Uint8 *keystate = SDL_GetKeyState(NULL);
@@ -70,12 +70,12 @@ float2 Input::GetAxis(int axis)
return f;
}

int2 Input::GetMousePos()
vec2i Input::GetMousePos()
{
return data->mouse;
}

int3 Input::GetMouseButtons()
vec3i Input::GetMouseButtons()
{
return data->buttons;
}
@@ -101,7 +101,7 @@ void Input::UntrackMouse(WorldEntity *e)
}
}

void Input::SetMousePos(int2 coord)
void Input::SetMousePos(vec2i coord)
{
data->mouse = coord;

@@ -123,7 +123,7 @@ void Input::SetMousePos(int2 coord)
{
if (data->entities[n] == best)
{
data->entities[n]->mousepos = (int2)((int3)coord - best->bbox[0]);
data->entities[n]->mousepos = (vec2i)((vec3i)coord - best->bbox[0]);
if (best != data->lastfocus)
data->entities[n]->pressed = data->buttons;
else
@@ -131,7 +131,7 @@ void Input::SetMousePos(int2 coord)
}
else
{
data->entities[n]->mousepos = int2(-1);
data->entities[n]->mousepos = vec2i(-1);
/* FIXME */
data->entities[n]->released = 0;
data->entities[n]->pressed = 0;


+ 4
- 4
src/input.h View File

@@ -24,16 +24,16 @@ class Input
{
public:
/* These methods are general queries */
static float2 GetAxis(int axis);
static int2 GetMousePos();
static int3 GetMouseButtons();
static vec2 GetAxis(int axis);
static vec2i GetMousePos();
static vec3i GetMouseButtons();

/* Entities can subscribe to events */
static void TrackMouse(WorldEntity *e);
static void UntrackMouse(WorldEntity *e);

/* These methods are called by the underlying input listeners */
static void SetMousePos(int2 coord);
static void SetMousePos(vec2i coord);
static void SetMouseButton(int index);
static void UnsetMouseButton(int index);
};


+ 16
- 16
src/matrix.cpp View File

@@ -27,7 +27,7 @@ static inline float det3(float a, float b, float c,
+ c * (d * h - g * e);
}

static inline float cofact3(float4x4 const &mat, int i, int j)
static inline float cofact3(mat4 const &mat, int i, int j)
{
return det3(mat[(i + 1) & 3][(j + 1) & 3],
mat[(i + 2) & 3][(j + 1) & 3],
@@ -40,7 +40,7 @@ static inline float cofact3(float4x4 const &mat, int i, int j)
mat[(i + 3) & 3][(j + 3) & 3]) * (((i + j) & 1) ? -1.0f : 1.0f);
}

template<> float float4x4::det() const
template<> float mat4::det() const
{
float ret = 0;
for (int n = 0; n < 4; n++)
@@ -48,9 +48,9 @@ template<> float float4x4::det() const
return ret;
}

template<> float4x4 float4x4::invert() const
template<> mat4 mat4::invert() const
{
float4x4 ret;
mat4 ret;
float d = det();
if (d)
{
@@ -62,14 +62,14 @@ template<> float4x4 float4x4::invert() const
return ret;
}

template<> float4x4 float4x4::ortho(float left, float right, float bottom,
float top, float near, float far)
template<> mat4 mat4::ortho(float left, float right, float bottom,
float top, float near, float far)
{
float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;

float4x4 ret(0.0f);
mat4 ret(0.0f);
ret[0][0] = 2.0f * invrl;
ret[1][1] = 2.0f * invtb;
ret[2][2] = -2.0f * invfn;
@@ -80,14 +80,14 @@ template<> float4x4 float4x4::ortho(float left, float right, float bottom,
return ret;
}

template<> float4x4 float4x4::frustum(float left, float right, float bottom,
float top, float near, float far)
template<> mat4 mat4::frustum(float left, float right, float bottom,
float top, float near, float far)
{
float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;

float4x4 ret(0.0f);
mat4 ret(0.0f);
ret[0][0] = 2.0f * near * invrl;
ret[1][1] = 2.0f * near * invtb;
ret[2][0] = (right + left) * invrl;
@@ -98,8 +98,8 @@ template<> float4x4 float4x4::frustum(float left, float right, float bottom,
return ret;
}

template<> float4x4 float4x4::perspective(float theta, float width,
float height, float near, float far)
template<> mat4 mat4::perspective(float theta, float width,
float height, float near, float far)
{
float t1 = tanf(theta / 2.0f);
float t2 = t1 * height / width;
@@ -107,16 +107,16 @@ template<> float4x4 float4x4::perspective(float theta, float width,
return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
}

template<> float4x4 float4x4::translate(float x, float y, float z)
template<> mat4 mat4::translate(float x, float y, float z)
{
float4x4 ret(1.0f);
mat4 ret(1.0f);
ret[3][0] = x;
ret[3][1] = y;
ret[3][2] = z;
return ret;
}

template<> float4x4 float4x4::rotate(float theta, float x, float y, float z)
template<> mat4 mat4::rotate(float theta, float x, float y, float z)
{
float st = sinf(theta);
float ct = cosf(theta);
@@ -131,7 +131,7 @@ template<> float4x4 float4x4::rotate(float theta, float x, float y, float z)
float mty = (1.0f - ct) * y;
float mtz = (1.0f - ct) * z;

float4x4 ret(1.0f);
mat4 ret(1.0f);

ret[0][0] = x * mtx + ct;
ret[0][1] = x * mty + st * z;


+ 0
- 10
src/matrix.h View File

@@ -278,15 +278,5 @@ template <typename T> struct Mat4
typedef Mat4<float> mat4;
typedef Mat4<int> mat4i;

/* Aliases for deprecated stuff */
typedef Vec2<float> float2;
typedef Vec2<int> int2;
typedef Vec3<float> float3;
typedef Vec3<int> int3;
typedef Vec4<float> float4;
typedef Vec4<int> int4;
typedef Mat4<float> float4x4;
typedef Mat4<int> int4x4;

#endif // __DH_MATRIX_H__


+ 6
- 6
src/scene.cpp View File

@@ -38,7 +38,7 @@ struct Tile
#if LOL_EXPERIMENTAL
extern Shader *stdshader;
#endif
extern float4x4 projection_matrix, view_matrix, model_matrix;
extern mat4 projection_matrix, view_matrix, model_matrix;

/*
* Scene implementation class
@@ -136,15 +136,15 @@ void Scene::Render() // XXX: rename to Blit()
qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);

// XXX: debug stuff
model_matrix = float4x4::translate(320.0f, 240.0f, 0.0f);
model_matrix *= float4x4::rotate(-data->angle, 1.0f, 0.0f, 0.0f);
model_matrix = mat4::translate(320.0f, 240.0f, 0.0f);
model_matrix *= mat4::rotate(-data->angle, 1.0f, 0.0f, 0.0f);
#if 0
static float f = 0.0f;
f += 0.01f;
model_matrix *= float4x4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f);
model_matrix *= float4x4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f);
model_matrix *= mat4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f);
model_matrix *= mat4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f);
#endif
model_matrix *= float4x4::translate(-320.0f, -240.0f, 0.0f);
model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
// XXX: end of debug stuff

#if LOL_EXPERIMENTAL


+ 1
- 1
src/sdlinput.cpp View File

@@ -48,7 +48,7 @@ void SdlInput::TickGame(float deltams)
Entity::TickGame(deltams);

/* Handle mouse input */
int2 mouse;
vec2i mouse;
if (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
{
SDL_GetMouseState(&mouse.x, &mouse.y);


+ 4
- 4
src/text.cpp View File

@@ -29,7 +29,7 @@ class TextData
private:
int font, align, length;
char *text;
int3 pos;
vec3i pos;
};

/*
@@ -42,7 +42,7 @@ Text::Text(char const *text, char const *font)
data->font = Forge::Register(font);
data->text = text ? strdup(text) : NULL;
data->length = text ? strlen(text) : 0;
data->pos = int3(0, 0, 0);
data->pos = vec3i(0, 0, 0);

drawgroup = DRAWGROUP_HUD;
}
@@ -65,7 +65,7 @@ void Text::SetInt(int val)
data->length = strlen(text);
}

void Text::SetPos(int3 pos)
void Text::SetPos(vec3i pos)
{
data->pos = pos;
}
@@ -82,7 +82,7 @@ void Text::TickDraw(float deltams)
if (data->text)
{
Font *font = Forge::GetFont(data->font);
int3 delta = 0;
vec3i delta = 0;
if (data->align == ALIGN_RIGHT)
delta.x -= data->length * font->GetSize().x;
else if (data->align == ALIGN_CENTER)


+ 1
- 1
src/text.h View File

@@ -28,7 +28,7 @@ public:

void SetText(char const *text);
void SetInt(int val);
void SetPos(int3 pos);
void SetPos(vec3i pos);
void SetAlign(int align);

enum


+ 3
- 3
src/tiler.cpp View File

@@ -45,7 +45,7 @@ static TilerData * const data = &tilerdata;
* Public Tiler class
*/

int Tiler::Register(char const *path, int2 size, int2 count, float dilate)
int Tiler::Register(char const *path, vec2i size, vec2i count, float dilate)
{
int id = data->tilesets.MakeSlot(path);

@@ -67,7 +67,7 @@ void Tiler::Deregister(int id)
data->tilesets.RemoveSlot(id - 1); /* ID 0 is for the empty tileset */
}

int2 Tiler::GetSize(int id)
vec2i Tiler::GetSize(int id)
{
TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1);
#if !LOL_RELEASE
@@ -80,7 +80,7 @@ int2 Tiler::GetSize(int id)
return tileset->GetSize();
}

int2 Tiler::GetCount(int id)
vec2i Tiler::GetCount(int id)
{
TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1);
#if !LOL_RELEASE


+ 4
- 3
src/tiler.h View File

@@ -22,11 +22,12 @@
class Tiler
{
public:
static int Register(char const *path, int2 size, int2 count, float dilate);
static int Register(char const *path, vec2i size, vec2i count,
float dilate);
static void Deregister(int id);

static int2 GetSize(int id);
static int2 GetCount(int id);
static vec2i GetSize(int id);
static vec2i GetCount(int id);
static void Bind(uint32_t code);
static void BlitTile(uint32_t code, int x, int y, int z, int o,
float *vertex, float *texture);


+ 5
- 5
src/tileset.cpp View File

@@ -42,7 +42,7 @@ class TileSetData
private:
char *name, *path;
int *tiles, ntiles;
int2 size, count;
vec2i size, count;
float dilate, tx, ty;

SDL_Surface *img;
@@ -53,7 +53,7 @@ private:
* Public TileSet class
*/

TileSet::TileSet(char const *path, int2 size, int2 count, float dilate)
TileSet::TileSet(char const *path, vec2i size, vec2i count, float dilate)
: data(new TileSetData())
{
data->name = (char *)malloc(10 + strlen(path) + 1);
@@ -80,7 +80,7 @@ TileSet::TileSet(char const *path, int2 size, int2 count, float dilate)
if (count.i > 0 && count.j > 0)
{
data->count = count;
data->size = int2(data->img->w, data->img->h) / count;
data->size = vec2i(data->img->w, data->img->h) / count;
}
else
{
@@ -157,12 +157,12 @@ char const *TileSet::GetName()
return data->name;
}

int2 TileSet::GetSize() const
vec2i TileSet::GetSize() const
{
return data->size;
}

int2 TileSet::GetCount() const
vec2i TileSet::GetCount() const
{
return data->count;
}


+ 3
- 3
src/tileset.h View File

@@ -28,7 +28,7 @@ class TileSetData;
class TileSet : public Entity
{
public:
TileSet(char const *path, int2 size, int2 count, float dilate);
TileSet(char const *path, vec2i size, vec2i count, float dilate);
virtual ~TileSet();

protected:
@@ -38,8 +38,8 @@ protected:

public:
/* New methods */
int2 GetSize() const;
int2 GetCount() const;
vec2i GetSize() const;
vec2i GetCount() const;
void Bind();
void BlitTile(uint32_t id, int x, int y, int z, int o,
float *vertex, float *texture);


+ 7
- 7
src/video.cpp View File

@@ -31,7 +31,7 @@
#if LOL_EXPERIMENTAL
Shader *stdshader;
#endif
float4x4 projection_matrix, view_matrix, model_matrix;
mat4 projection_matrix, view_matrix, model_matrix;

#if LOL_EXPERIMENTAL
static char const *vertexshader =
@@ -92,7 +92,7 @@ void Video::SetFov(float theta)
{
#undef near /* Fuck Microsoft */
#undef far /* Fuck Microsoft again */
float4x4 proj;
mat4 proj;

float width = GetWidth();
float height = GetHeight();
@@ -103,7 +103,7 @@ void Video::SetFov(float theta)
if (theta < 1e-4f)
{
/* The easy way: purely orthogonal projection. */
proj = float4x4::ortho(0, width, 0, height, near, far);
proj = mat4::ortho(0, width, 0, height, near, far);
}
else
{
@@ -123,14 +123,14 @@ void Video::SetFov(float theta)
near = 1.0f;
}

proj = float4x4::frustum(-near * t1, near * t1,
-near * t2, near * t2, near, far)
* float4x4::translate(-0.5f * width, -0.5f * height, -dist);
proj = mat4::frustum(-near * t1, near * t1,
-near * t2, near * t2, near, far)
* mat4::translate(-0.5f * width, -0.5f * height, -dist);
}

#if LOL_EXPERIMENTAL
projection_matrix = proj;
view_matrix = float4x4(1.0f);
view_matrix = mat4(1.0f);
#else
glMatrixMode(GL_PROJECTION);
glLoadIdentity();


+ 8
- 8
src/worldentity.h View File

@@ -21,14 +21,14 @@
class WorldEntity : public Entity
{
public:
float3 position;
float3 rotation;
float3 velocity;
float3 bbox[2];
int2 mousepos;
int3 mousebuttons;
int3 pressed, clicked, released;
vec3 position;
vec3 rotation;
vec3 velocity;
vec3 bbox[2];
vec2i mousepos;
vec3i mousebuttons;
vec3i pressed, clicked, released;

protected:
WorldEntity();


+ 16
- 16
test/matrix.cpp View File

@@ -32,15 +32,15 @@ public:

void setUp()
{
identity = float4x4(1.0f);
triangular = float4x4(float4(1.0f, 0.0f, 0.0f, 0.0f),
float4(7.0f, 2.0f, 0.0f, 0.0f),
float4(1.0f, 5.0f, 3.0f, 0.0f),
float4(8.0f, 9.0f, 2.0f, 4.0f));
invertible = float4x4(float4( 1.0f, 1.0f, 2.0f, -1.0f),
float4(-2.0f, -1.0f, -2.0f, 2.0f),
float4( 4.0f, 2.0f, 5.0f, -4.0f),
float4( 5.0f, -3.0f, -7.0f, -6.0f));
identity = mat4(1.0f);
triangular = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f),
vec4(7.0f, 2.0f, 0.0f, 0.0f),
vec4(1.0f, 5.0f, 3.0f, 0.0f),
vec4(8.0f, 9.0f, 2.0f, 4.0f));
invertible = mat4(vec4( 1.0f, 1.0f, 2.0f, -1.0f),
vec4(-2.0f, -1.0f, -2.0f, 2.0f),
vec4( 4.0f, 2.0f, 5.0f, -4.0f),
vec4( 5.0f, -3.0f, -7.0f, -6.0f));
}

void tearDown() {}
@@ -55,9 +55,9 @@ public:

void test_mat_mul()
{
float4x4 m0 = identity;
float4x4 m1 = identity;
float4x4 m2 = m0 * m1;
mat4 m0 = identity;
mat4 m1 = identity;
mat4 m2 = m0 * m1;

CPPUNIT_ASSERT(m2[0][0] == 1.0f);
CPPUNIT_ASSERT(m2[1][0] == 0.0f);
@@ -82,10 +82,10 @@ public:

void test_mat_inv()
{
float4x4 m0 = invertible;
float4x4 m1 = m0.invert();
mat4 m0 = invertible;
mat4 m1 = m0.invert();

float4x4 m2 = m0 * m1;
mat4 m2 = m0 * m1;

CPPUNIT_ASSERT(m2[0][0] == 1.0f);
CPPUNIT_ASSERT(m2[1][0] == 0.0f);
@@ -109,7 +109,7 @@ public:
}

private:
float4x4 triangular, identity, invertible;
mat4 triangular, identity, invertible;
};

CPPUNIT_TEST_SUITE_REGISTRATION(MatrixTest);


Loading…
Cancel
Save