From 4537534dcff19f75b0781193c91c1a7e2d7fa97b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 6 Jul 2010 21:37:47 +0000 Subject: [PATCH] Support CSV .tmx files to get rid of that pathetic system() call. --- src/Makefile | 2 +- src/layer.cpp | 8 ++-- src/layer.h | 7 ++-- src/map.cpp | 77 ++++++++++++++++------------------ src/map.h | 4 +- src/test-map.cpp | 12 +++--- src/{tiler.cpp => tileset.cpp} | 22 +++++----- src/{tiler.h => tileset.h} | 10 ++--- src/video.cpp | 2 +- 9 files changed, 69 insertions(+), 75 deletions(-) rename src/{tiler.cpp => tileset.cpp} (92%) rename src/{tiler.h => tileset.h} (69%) diff --git a/src/Makefile b/src/Makefile index 5e620730..1e05beac 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,5 +1,5 @@ -SRC = test-map.cpp video.cpp tiler.cpp layer.cpp map.cpp +SRC = test-map.cpp video.cpp tileset.cpp layer.cpp map.cpp all: test-map diff --git a/src/layer.cpp b/src/layer.cpp index b74d01eb..77783b77 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -1,12 +1,12 @@ #include "layer.h" -Layer::Layer(int w, int h, int z, char const *base64) +Layer::Layer(int w, int h, int in_z, uint32_t *in_data) { width = w; height = h; - level = z; - data = new unsigned int[w * h]; + z = in_z; + data = in_data; #if 0 fread(data, sizeof(unsigned int), width * height, fp); @@ -26,7 +26,7 @@ Layer::~Layer() int Layer::GetZ() { - return level; + return z; } unsigned int Layer::GetTile(int x, int y) diff --git a/src/layer.h b/src/layer.h index 10f9ae76..0fbd6ca1 100644 --- a/src/layer.h +++ b/src/layer.h @@ -7,19 +7,20 @@ #define __DH_LAYER_H__ #include +#include class Layer { public: - Layer(int w, int h, int z, char const *base64); + Layer(int w, int h, int z, uint32_t *data); ~Layer(); int GetZ(); unsigned int GetTile(int x, int y); private: - int width, height, level; - unsigned int *data; + int width, height, z; + uint32_t *data; }; #endif // __DH_LAYER_H__ diff --git a/src/map.cpp b/src/map.cpp index 9dc89228..6a247cbe 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -1,6 +1,9 @@ #include +#include +#include #include +#include #include "map.h" #include "layer.h" @@ -10,7 +13,9 @@ Map::Map(char const *path) : nlayers(0) { char tmp[BUFSIZ]; - int firstgid = 0, width = 0, height = 0, level = 0, data = 0; + uint32_t *data = NULL; + int width = 0, height = 0, level = 0, orientation = 0; + int firstgid = 0, ntiles = 0; FILE *fp = fopen(path, "r"); @@ -27,63 +32,51 @@ Map::Map(char const *path) : if (data) { - if (--data == 0) + /* We are in the process of reading layer data. Only stop + * when we have read the expected number of tiles. */ + char const *parser = tmp; + while (ntiles < width * height) { - layers[nlayers] = new Layer(width, height, level, tmp); + data[ntiles++] = atoi(parser); + while (isdigit(*parser)) + parser++; + if (*parser == ',') + parser++; + if (!isdigit(*parser)) + break; + } + + if (ntiles == width * height) + { + layers[nlayers] = new Layer(width, height, level, data); nlayers++; + data = NULL; } } else if (sscanf(tmp, " AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z); + tileset->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z); } } diff --git a/src/map.h b/src/map.h index 54955919..3abd2d89 100644 --- a/src/map.h +++ b/src/map.h @@ -9,7 +9,7 @@ #include #include "layer.h" -#include "tiler.h" +#include "tileset.h" class Map { @@ -17,7 +17,7 @@ public: Map(char const *path); ~Map(); - void Draw(Tiler *tiler); + void Draw(Tileset *tileset); private: Layer **layers; diff --git a/src/test-map.cpp b/src/test-map.cpp index 6ab49a8d..b20c459f 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -6,20 +6,20 @@ #include #include "video.h" -#include "tiler.h" +#include "tileset.h" #include "map.h" int main(int argc, char **argv) { Video *video = new Video("Deus Hax", 640, 480); - Tiler *tiler = new Tiler(); + Tileset *tileset = new Tileset(); Map *map = new Map("maps/testmap-grass.tmx"); for (int done = 0; !done; ) { video->Clear(); - map->Draw(tiler); + map->Draw(tileset); /* Test stuff */ int playerx, playery; @@ -27,9 +27,9 @@ int main(int argc, char **argv) playerx = playerx * (640 - 32) / 640; playery = playery * (480 - 32) / 480; - tiler->AddTile(50, playerx, playery, 1); + tileset->AddTile(50, playerx, playery, 1); - tiler->Render(); + tileset->Render(); video->Refresh(33.33333f); /* This could go in a separate function */ @@ -49,7 +49,7 @@ int main(int argc, char **argv) } delete map; - delete tiler; + delete tileset; delete video; return EXIT_SUCCESS; diff --git a/src/tiler.cpp b/src/tileset.cpp similarity index 92% rename from src/tiler.cpp rename to src/tileset.cpp index 208e4d46..f85fc738 100644 --- a/src/tiler.cpp +++ b/src/tileset.cpp @@ -16,15 +16,15 @@ #include -#include "tiler.h" +#include "tileset.h" /* - * Tiler implementation class + * Tileset implementation class */ -class TilerData +class TilesetData { - friend class Tiler; + friend class Tileset; private: static int Compare(void const *p1, void const *p2) @@ -43,12 +43,12 @@ private: }; /* - * Public Tiler class + * Public Tileset class */ -Tiler::Tiler() +Tileset::Tileset() { - data = new TilerData(); + data = new TilesetData(); data->tiles = NULL; data->ntiles = 0; @@ -74,13 +74,13 @@ Tiler::Tiler() glGenBuffers(3, data->buflist); } -Tiler::~Tiler() +Tileset::~Tileset() { free(data->tiles); delete data; } -void Tiler::AddTile(int n, int x, int y, int z) +void Tileset::AddTile(int n, int x, int y, int z) { if ((data->ntiles % 1024) == 0) { @@ -96,10 +96,10 @@ void Tiler::AddTile(int n, int x, int y, int z) data->ntiles++; } -void Tiler::Render() +void Tileset::Render() { /* Sort tiles */ - qsort(data->tiles, data->ntiles, 4 * sizeof(int), TilerData::Compare); + qsort(data->tiles, data->ntiles, 4 * sizeof(int), TilesetData::Compare); /* Texture coord buffer */ float uvs[8 * data->ntiles]; diff --git a/src/tiler.h b/src/tileset.h similarity index 69% rename from src/tiler.h rename to src/tileset.h index e001ba46..2a9cd369 100644 --- a/src/tiler.h +++ b/src/tileset.h @@ -6,20 +6,20 @@ #if !defined __DH_TILER_H__ #define __DH_TILER_H__ -class TilerData; +class TilesetData; -class Tiler +class Tileset { public: - Tiler(); - ~Tiler(); + Tileset(); + ~Tileset(); void AddTile(int n, int x, int y, int z); void Render(); private: - TilerData *data; + TilesetData *data; }; #endif // __DH_TILER_H__ diff --git a/src/video.cpp b/src/video.cpp index f248d12a..40b68cf6 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -54,7 +54,7 @@ Video::Video(char const *title, int width, int height) SDL_WM_SetCaption(title, NULL); SDL_ShowCursor(0); - SDL_WM_GrabInput(SDL_GRAB_ON); + //SDL_WM_GrabInput(SDL_GRAB_ON); /* Initialise OpenGL */ glViewport(0, 0, data->video->w, data->video->h);