Browse Source

tileset: replace the Tiler ID system with real TileSet objects. The only

thing that is still broken is the Layer <--> Map interface.
legacy
Sam Hocevar sam 13 years ago
parent
commit
6266c289ee
9 changed files with 47 additions and 110 deletions
  1. +4
    -4
      src/emitter.cpp
  2. +2
    -1
      src/emitter.h
  3. +5
    -5
      src/font.cpp
  4. +3
    -0
      src/layer.cpp
  5. +7
    -6
      src/map.cpp
  6. +11
    -8
      src/scene.cpp
  7. +3
    -1
      src/scene.h
  8. +8
    -77
      src/tiler.cpp
  9. +4
    -8
      src/tiler.h

+ 4
- 4
src/emitter.cpp View File

@@ -30,7 +30,7 @@ class EmitterData
static const int MAX_PARTICLES = 1000;

private:
int tiler;
TileSet *tileset;
vec3 gravity;
int particles[MAX_PARTICLES];
vec3 positions[MAX_PARTICLES];
@@ -42,10 +42,10 @@ private:
* Public Emitter class
*/

Emitter::Emitter(int tiler, vec3 gravity)
Emitter::Emitter(TileSet *tileset, vec3 gravity)
: data(new EmitterData())
{
data->tiler = tiler;
data->tileset = tileset;
data->gravity = gravity;
data->nparticles = 0;
}
@@ -73,7 +73,7 @@ void Emitter::TickDraw(float deltams)
Entity::TickDraw(deltams);

for (int i = 0; i < data->nparticles; i++)
Scene::GetDefault()->AddTile((data->tiler << 16) | data->particles[i],
Scene::GetDefault()->AddTile(data->tileset, data->particles[i],
data->positions[i].x,
data->positions[i].y,
data->positions[i].z, 0);


+ 2
- 1
src/emitter.h View File

@@ -17,6 +17,7 @@
#define __LOL_EMITTER_H__

#include "entity.h"
#include "tileset.h"

namespace lol
{
@@ -26,7 +27,7 @@ class EmitterData;
class Emitter : public Entity
{
public:
Emitter(int tiler, vec3 gravity);
Emitter(TileSet *tileset, vec3 gravity);
virtual ~Emitter();

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


+ 5
- 5
src/font.cpp View File

@@ -32,7 +32,7 @@ class FontData

private:
char *name;
int tiler;
TileSet *tileset;
vec2i size;
};

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

data->tiler = Tiler::Register(path, 0, 16, 1.0f);
data->size = Tiler::GetSize(data->tiler, 0);
data->tileset = Tiler::Register(path, 0, 16, 1.0f);
data->size = data->tileset->GetSize(0);

drawgroup = DRAWGROUP_BEFORE;
}

Font::~Font()
{
Tiler::Deregister(data->tiler);
Tiler::Deregister(data->tileset);
free(data->name);
delete data;
}
@@ -78,7 +78,7 @@ void Font::Print(vec3i pos, char const *str)
uint32_t ch = (uint8_t)*str++;

if (ch != ' ')
scene->AddTile((data->tiler << 16) | (ch & 255),
scene->AddTile(data->tileset, ch & 255,
pos.x, pos.y, pos.z, 0);

pos.x += data->size.x;


+ 3
- 0
src/layer.cpp View File

@@ -47,6 +47,8 @@ Layer::~Layer()

void Layer::Render(int x, int y, int z)
{
Log::Error("FIXME: Layer::Render no longer works\n");
#if 0
Scene *scene = Scene::GetDefault();
for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++)
@@ -54,6 +56,7 @@ void Layer::Render(int x, int y, int z)
scene->AddTile(data[j * width + i],
x + i * 32, y + j * 32 - altitude,
altitude + z, orientation);
#endif
}

int Layer::GetZ()


+ 7
- 6
src/map.cpp View File

@@ -33,10 +33,10 @@ class MapData
{
friend class Map;

static int const MAX_TILERS = 128;
static int const MAX_TILESETS = 128;

private:
int tilers[MAX_TILERS];
TileSet *tilesets[MAX_TILESETS];
int ntilers;

Layer **layers;
@@ -59,7 +59,7 @@ Map::Map(char const *path)
data->height = 0;

char tmp[BUFSIZ];
int gids[MapData::MAX_TILERS];
int gids[MapData::MAX_TILESETS];
uint32_t *tiles = NULL;
int level = 0, orientation = 0, ntiles = 0;

@@ -95,7 +95,8 @@ Map::Map(char const *path)
if (n == data->ntilers - 1
|| id < gids[n + 1])
{
code = (data->tilers[n] << 16) | (id - gids[n]);
Log::Error("tilesets no longer work this way");
//code = (data->tilesets[n] << 16) | (id - gids[n]);
break;
}
}
@@ -132,7 +133,7 @@ Map::Map(char const *path)
else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1)
{
/* This is a tileset image file. Associate it with firstgid. */
data->tilers[data->ntilers] = Tiler::Register(str, 32, 0,
data->tilesets[data->ntilers] = Tiler::Register(str, 32, 0,
sqrtf(2));
data->ntilers++;
//Log::Debug("new tiler %s\n", str);
@@ -158,7 +159,7 @@ Map::Map(char const *path)
Map::~Map()
{
for (int i = 0; i < data->ntilers; i++)
Tiler::Deregister(data->tilers[i]);
Tiler::Deregister(data->tilesets[i]);
for (int i = 0; i < data->nlayers; i++)
delete data->layers[i];
free(data->layers);


+ 11
- 8
src/scene.cpp View File

@@ -28,8 +28,9 @@ namespace lol

struct Tile
{
uint32_t prio, code;
int x, y, z, o;
TileSet *tileset;
uint32_t prio;
int id, x, y, z, o;
};

static Shader *stdshader = NULL;
@@ -113,14 +114,15 @@ void Scene::Reset()
SceneData::scene = NULL;
}

void Scene::AddTile(uint32_t code, int x, int y, int z, int o)
void Scene::AddTile(TileSet *tileset, int id, int x, int y, int z, int o)
{
if ((data->ntiles % 1024) == 0)
data->tiles = (Tile *)realloc(data->tiles,
(data->ntiles + 1024) * sizeof(Tile));
/* FIXME: this sorting only works for a 45-degree camera */
data->tiles[data->ntiles].prio = -y - 2 * 32 * z + (o ? 0 : 32);
data->tiles[data->ntiles].code = code;
data->tiles[data->ntiles].tileset = tileset;
data->tiles[data->ntiles].id = id;
data->tiles[data->ntiles].x = x;
data->tiles[data->ntiles].y = y;
data->tiles[data->ntiles].z = z;
@@ -273,7 +275,7 @@ void Scene::Render() // XXX: rename to Blit()

/* Count how many quads will be needed */
for (n = i + 1; n < data->ntiles; n++)
if (data->tiles[i].code >> 16 != data->tiles[n].code >> 16)
if (data->tiles[i].tileset != data->tiles[n].tileset)
break;

/* Create a vertex array object */
@@ -282,15 +284,16 @@ void Scene::Render() // XXX: rename to Blit()

for (int j = i; j < n; j++)
{
Tiler::BlitTile(data->tiles[j].code, data->tiles[j].x,
data->tiles[j].y, data->tiles[j].z, data->tiles[j].o,
data->tiles[i].tileset->BlitTile(data->tiles[j].id,
data->tiles[j].x, data->tiles[j].y,
data->tiles[j].z, data->tiles[j].o,
vertex + 18 * (j - i), texture + 12 * (j - i));
}

stdshader->Bind();

/* Bind texture */
Tiler::Bind(data->tiles[i].code);
data->tiles[i].tileset->Bind();

/* Bind vertex, color and texture coordinate buffers */
#if defined HAVE_GL_2X


+ 3
- 1
src/scene.h View File

@@ -18,6 +18,8 @@

#include <stdint.h>

#include "tileset.h"

namespace lol
{

@@ -32,7 +34,7 @@ public:
static Scene *GetDefault();
static void Reset();

void AddTile(uint32_t code, int x, int y, int z, int o);
void AddTile(TileSet *tileset, int id, int x, int y, int z, int o);
void Render();

private:


+ 8
- 77
src/tiler.cpp View File

@@ -27,16 +27,10 @@ static class TilerData

public:
TilerData()
#if !LOL_RELEASE
: lasterror(-1)
#endif
{ }

private:
Dict tilesets;
#if !LOL_RELEASE
int lasterror;
#endif
}
tilerdata;

@@ -46,87 +40,24 @@ static TilerData * const data = &tilerdata;
* Public Tiler class
*/

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

if (!data->tilesets.GetEntity(id))
{
TileSet *tileset = new TileSet(path, size, count, dilate);
data->tilesets.SetEntity(id, tileset);
#if !LOL_RELEASE
if (id == data->lasterror)
data->lasterror = -1;
#endif
}

return id + 1; /* ID 0 is for the empty tileset */
}

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

vec2i Tiler::GetCount(int id)
{
TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1);
#if !LOL_RELEASE
if (!tileset)
{
Log::Error("getting count for null tiler #%i\n", id);
return 0;
}
#endif
return tileset->GetCount();
}

vec2i Tiler::GetSize(int id, int tileid)
{
TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id - 1);
#if !LOL_RELEASE
if (!tileset)
{
Log::Error("getting size for null tiler #%i\n", id);
return 0;
tileset = new TileSet(path, size, count, dilate);
data->tilesets.SetEntity(id, tileset);
}
#endif
return tileset->GetSize(tileid);
}

void Tiler::Bind(uint32_t code)
{
int id = (code >> 16) - 1; /* ID 0 is for the empty tileset */

TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id);
#if !LOL_RELEASE
if (!tileset)
{
if (id != data->lasterror)
Log::Error("binding null tiler #%i\n", id);
data->lasterror = id;
return;
}
#endif
tileset->Bind();
return tileset;
}

void Tiler::BlitTile(uint32_t code, int x, int y, int z, int o,
float *vertex, float *texture)
void Tiler::Deregister(TileSet *tileset)
{
int id = (code >> 16) - 1; /* ID 0 is for the empty tileset */

TileSet *tileset = (TileSet *)data->tilesets.GetEntity(id);
#if !LOL_RELEASE
if (!tileset)
{
if (id != data->lasterror)
Log::Error("blitting to null tiler #%i\n", id);
data->lasterror = id;
return;
}
#endif
tileset->BlitTile(code & 0xffff, x, y, z, o, vertex, texture);
data->tilesets.RemoveSlot(tileset);
}

} /* namespace lol */


+ 4
- 8
src/tiler.h View File

@@ -19,21 +19,17 @@

#include <stdint.h>

#include "tileset.h"

namespace lol
{

class Tiler
{
public:
static int Register(char const *path, vec2i size, vec2i count,
static TileSet *Register(char const *path, vec2i size, vec2i count,
float dilate);
static void Deregister(int id);

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

} /* namespace lol */


Loading…
Cancel
Save