From c73ee58065f8d1cc92160e52f3fde77aed0d4ed7 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 17 Jan 2011 13:22:07 +0000 Subject: [PATCH] The Tile size can now be specified upon TileSet load. Add a sample with the Monsterz tiles. --- src/Makefile.am | 2 +- src/debugboard.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++ src/debugboard.h | 34 +++++++++++++++++++++ src/debugsprite.cpp | 2 +- src/map.cpp | 2 +- src/test-map.cpp | 9 ++++++ src/tiler.cpp | 4 +-- src/tiler.h | 2 +- src/tileset.cpp | 29 +++++++++++------- src/tileset.h | 2 +- 10 files changed, 142 insertions(+), 18 deletions(-) create mode 100644 src/debugboard.cpp create mode 100644 src/debugboard.h diff --git a/src/Makefile.am b/src/Makefile.am index 9730b84f..2259c4d2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,7 @@ libcommon_a_SOURCES = \ profiler.cpp profiler.h input.h input.cpp world.cpp world.h \ debugfps.cpp debugfps.h debugsprite.cpp debugsprite.h \ debugrecord.cpp debugrecord.h debugstats.cpp debugstats.h \ - debugsphere.cpp debugsphere.h + debugsphere.cpp debugsphere.h debugboard.cpp debugboard.h libcommon_a_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` test_map_SOURCES = test-map.cpp sdlinput.cpp sdlinput.h diff --git a/src/debugboard.cpp b/src/debugboard.cpp new file mode 100644 index 00000000..3f6d0799 --- /dev/null +++ b/src/debugboard.cpp @@ -0,0 +1,74 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010-2011 Sam Hocevar +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include + +#include "core.h" +#include "debugboard.h" + +/* + * DebugBoard implementation class + */ + +class DebugBoardData +{ + friend class DebugBoard; + +private: + Game *game; + int tiler; + float x, y, z; +}; + +/* + * Public DebugBoard class + */ + +DebugBoard::DebugBoard(Game *game) +{ + data = new DebugBoardData(); + data->game = game; + Ticker::Ref(game); + data->tiler = Tiler::Register("monsterz/tiles.png", 48); + data->x = 32; + data->y = 0; + data->z = 112; +} + +void DebugBoard::TickGame(float deltams) +{ + Entity::TickGame(deltams); +} + +void DebugBoard::TickDraw(float deltams) +{ + Entity::TickDraw(deltams); + + int x = data->x; + int y = data->y; + int z = data->z; + + for (int j = 0; j < 8; j++) + for (int i = 0; i < 8; i++) + { + int id = 28 + ((i + 3) * (j + 1) % 10) * 3 + ((i ^ (j + 2)) % 5); + id += (id % 5) / 4; + data->game->GetScene()->AddTile((data->tiler << 16) | id, + x + i * 48 - 16, y + j * 48, z, 1); + } +} + +DebugBoard::~DebugBoard() +{ + Ticker::Unref(data->game); + Tiler::Deregister(data->tiler); + delete data; +} + diff --git a/src/debugboard.h b/src/debugboard.h new file mode 100644 index 00000000..fd18d177 --- /dev/null +++ b/src/debugboard.h @@ -0,0 +1,34 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010-2011 Sam Hocevar +// + +// +// The DebugBoard class +// -------------------- +// + +#if !defined __DH_DEBUGBOARD_H__ +#define __DH_DEBUGBOARD_H__ + +#include "entity.h" +#include "game.h" + +class DebugBoardData; + +class DebugBoard : public Entity +{ +public: + DebugBoard(Game *game); + virtual ~DebugBoard(); + +protected: + virtual void TickGame(float deltams); + virtual void TickDraw(float deltams); + +private: + DebugBoardData *data; +}; + +#endif // __DH_DEBUGBOARD_H__ + diff --git a/src/debugsprite.cpp b/src/debugsprite.cpp index d48a04dd..66222a7d 100644 --- a/src/debugsprite.cpp +++ b/src/debugsprite.cpp @@ -36,7 +36,7 @@ DebugSprite::DebugSprite(Game *game) data = new DebugSpriteData(); data->game = game; Ticker::Ref(game); - data->tiler = Tiler::Register("art/test/character-dress.png"); + data->tiler = Tiler::Register("art/test/character-dress.png", 32); data->x = 320; data->y = 206; data->z = 0; diff --git a/src/map.cpp b/src/map.cpp index 96502fed..8f21d6af 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -121,7 +121,7 @@ Map::Map(char const *path) else if (sscanf(tmp, " tilers[data->ntilers] = Tiler::Register(str); + data->tilers[data->ntilers] = Tiler::Register(str, 32); data->ntilers++; //fprintf(stderr, "new tiler %s\n", str); } diff --git a/src/test-map.cpp b/src/test-map.cpp index 100288fb..5787c70b 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -9,12 +9,16 @@ #include #include +#if defined _WIN32 +# include +#endif #include #include "core.h" #include "sdlinput.h" #include "debugfps.h" +#include "debugboard.h" #include "debugsprite.h" #include "debugsphere.h" #include "debugrecord.h" @@ -49,12 +53,17 @@ int main(int argc, char **argv) Video::Setup(video->w, video->h); /* Create a game */ +#if defined _WIN32 + _chdir(".."); /* Temporary Win32 hack */ +#endif Game *game = new Game("maps/testmap.tmx"); + game->SetMouse(160, 96); /* Register an input driver and some debug stuff */ new SdlInput(); new DebugFps(); new DebugSprite(game); + new DebugBoard(game); new DebugSphere(); //new DebugRecord("lolengine.ogg"); new DebugStats("stats.txt"); diff --git a/src/tiler.cpp b/src/tiler.cpp index 0c681d86..3b730cf6 100644 --- a/src/tiler.cpp +++ b/src/tiler.cpp @@ -28,13 +28,13 @@ static TilerData * const data = &tilerdata; * Public Tiler class */ -int Tiler::Register(char const *path) +int Tiler::Register(char const *path, int size) { int id = data->tilesets.MakeSlot(path); if (!data->tilesets.GetEntity(id)) { - TileSet *tileset = new TileSet(path); + TileSet *tileset = new TileSet(path, size); data->tilesets.SetEntity(id, tileset); } diff --git a/src/tiler.h b/src/tiler.h index cee15769..a638a566 100644 --- a/src/tiler.h +++ b/src/tiler.h @@ -17,7 +17,7 @@ class Tiler { public: - static int Register(char const *path); + static int Register(char const *path, int size); static void Deregister(int id); static void BlitTile(uint32_t code, int x, int y, int z, int o); diff --git a/src/tileset.cpp b/src/tileset.cpp index 964c4d71..a296aea0 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -37,7 +37,7 @@ class TileSetData private: char *name; int *tiles; - int nw, nh, ntiles; + int size, nw, nh, ntiles; float tx, ty; SDL_Surface *img; @@ -48,7 +48,7 @@ private: * Public TileSet class */ -TileSet::TileSet(char const *path) +TileSet::TileSet(char const *path, int size) { data = new TileSetData(); data->name = strdup(path); @@ -66,11 +66,15 @@ TileSet::TileSet(char const *path) exit(1); } - data->nw = data->img->w / 32; - data->nh = data->img->h / 32; + if (size <= 0) + size = 32; + + data->size = size; + data->nw = data->img->w / size; + data->nh = data->img->h / size; data->ntiles = data->nw * data->nh; - data->tx = 32.0f / data->img->w; - data->ty = 32.0f / data->img->h; + data->tx = (float)size / data->img->w; + data->ty = (float)size / data->img->h; drawgroup = DRAWGROUP_BEFORE; } @@ -120,20 +124,23 @@ void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o) float ty = data->ty * ((id & 0xffff) / data->nw); float sqrt2 = sqrtf(2.0f); - int off = o ? 32 : 0; + int off = o ? data->size : 0; + int dx = data->size; + int dy = data->size * 38 / 32; /* Magic... fix this one day */ + int dy2 = data->size * 70 / 32; if (!data->img) { glBindTexture(GL_TEXTURE_2D, data->texture); glBegin(GL_QUADS); glTexCoord2f(tx, ty); - glVertex3f(x, sqrt2 * (y - 38 - off), sqrt2 * (z + off)); + glVertex3f(x, sqrt2 * (y - dy - off), sqrt2 * (z + off)); glTexCoord2f(tx + data->tx, ty); - glVertex3f(x + 32, sqrt2 * (y - 38 - off), sqrt2 * (z + off)); + glVertex3f(x + dx, sqrt2 * (y - dy - off), sqrt2 * (z + off)); glTexCoord2f(tx + data->tx, ty + data->ty); - glVertex3f(x + 32, sqrt2 * (y - 70), sqrt2 * z); + glVertex3f(x + dx, sqrt2 * (y - dy2), sqrt2 * z); glTexCoord2f(tx, ty + data->ty); - glVertex3f(x, sqrt2 * (y - 70), sqrt2 * z); + glVertex3f(x, sqrt2 * (y - dy2), sqrt2 * z); glEnd(); } } diff --git a/src/tileset.h b/src/tileset.h index cdc91328..84f96406 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -23,7 +23,7 @@ class TileSetData; class TileSet : public Entity { public: - TileSet(char const *path); + TileSet(char const *path, int size); virtual ~TileSet(); protected: