From 6a20cd36450a158f62595d91cd8f72e4c0b70e0a Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 3 Jul 2010 22:27:40 +0000 Subject: [PATCH] Dynamically allocate layers when loading maps. --- src/Makefile | 6 ++-- src/layer.cpp | 28 +++++++++++++++++ src/layer.h | 21 +++++++++++++ src/map.cpp | 51 +++++++++++++++++++++++++++++++ src/map.h | 19 ++++++++++++ src/test-map.cpp | 78 +++++++++++------------------------------------- src/tiler.h | 5 ++++ 7 files changed, 144 insertions(+), 64 deletions(-) create mode 100644 src/layer.cpp create mode 100644 src/layer.h create mode 100644 src/map.cpp create mode 100644 src/map.h diff --git a/src/Makefile b/src/Makefile index 5bbf388e..5e620730 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,13 +1,13 @@ -SRC = test-map.cpp video.cpp tiler.cpp +SRC = test-map.cpp video.cpp tiler.cpp layer.cpp map.cpp all: test-map test-map: $(SRC:%.cpp=%.o) - g++ -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image` + g++ -g -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image` %.o: %.cpp - g++ -Wall -O3 -c $^ -o $@ `pkg-config --cflags sdl gl SDL_image` + g++ -g -Wall -O3 -c $^ -o $@ `pkg-config --cflags sdl gl SDL_image` clean: rm -f *.o test-map diff --git a/src/layer.cpp b/src/layer.cpp new file mode 100644 index 00000000..351e5d5e --- /dev/null +++ b/src/layer.cpp @@ -0,0 +1,28 @@ + +#include "layer.h" + +Layer::Layer(int w, int h, FILE *fp) +{ + width = w; + height = h; + data = new unsigned int[w * h]; + + fread(data, sizeof(unsigned int), width * height, fp); + for (int n = 0; n < width * height; n++) + { + unsigned int i = data[n]; + // XXX: endianness swapping might be necessary here + data[n] = i ? i - 1 : 0; + } +} + +Layer::~Layer() +{ + delete data; +} + +unsigned int Layer::GetTile(int x, int y) +{ + return data[y * width + x]; +} + diff --git a/src/layer.h b/src/layer.h new file mode 100644 index 00000000..c2805acc --- /dev/null +++ b/src/layer.h @@ -0,0 +1,21 @@ + +#if !defined __LAYER_H__ +#define __LAYER_H__ + +#include + +class Layer +{ +public: + Layer(int w, int h, FILE *fp); + ~Layer(); + + unsigned int GetTile(int x, int y); + +//private: + int width, height; + unsigned int *data; +}; + +#endif // __LAYER_H__ + diff --git a/src/map.cpp b/src/map.cpp new file mode 100644 index 00000000..868f8efb --- /dev/null +++ b/src/map.cpp @@ -0,0 +1,51 @@ + +#include +#include + +#include "map.h" +#include "layer.h" + +Map::Map(char const *path) : + layers(0), + nlayers(0) +{ + char tmp[1024]; + + sprintf(tmp, "grep '\\(^ [^< ]\\|layer name\\)' %s | while read i; do echo \"$i\"; read i; echo -n \"$i\" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | gunzip; done", path); + + FILE *fp = popen(tmp, "r"); + + while (fp && !feof(fp)) + { + char name[1024]; + int width, height; + + fscanf(fp, "AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, i); +} + diff --git a/src/map.h b/src/map.h new file mode 100644 index 00000000..e6fede5d --- /dev/null +++ b/src/map.h @@ -0,0 +1,19 @@ + +#include + +#include "layer.h" +#include "tiler.h" + +class Map +{ +public: + Map(char const *path); + ~Map(); + + void Draw(Tiler *tiler); + +private: + Layer **layers; + int nlayers; +}; + diff --git a/src/test-map.cpp b/src/test-map.cpp index a836e9af..4df7c140 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -7,74 +7,29 @@ #include "video.h" #include "tiler.h" +#include "map.h" -/* Global objects */ -Video *video; -Tiler *tiler; - -/* Storage for map layers */ -int *layers[128]; -int width = 32, height = 32; -int nlayers = 0; - -void LoadMap(void) -{ - FILE *fp = popen("grep '^ [^< ]' maps/testmap.tmx | while read i; do echo -n \"$i\" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | gunzip; done", "r"); - while (fp && !feof(fp)) - { - layers[nlayers] = (int *)malloc(width * height * sizeof(int)); - fread(layers[nlayers], sizeof(int), width * height, fp); - if (feof(fp)) - { - free(layers[nlayers]); - layers[nlayers] = 0; - fclose(fp); - break; - } - for (int n = 0; n < width * height; n++) - { - unsigned int i = layers[nlayers][n]; - //i = (i >> 24) | ((i >> 8) & 0xff00) | ((i << 8) & 0xff0000) | (i << 24); - layers[nlayers][n] = i ? i - 1 : 0; - } - nlayers++; - } -} - -/* The main drawing function. */ -void DrawScene() +int main(int argc, char **argv) { - video->Clear(); - - for (int i = 0; i < nlayers; i++) - for (int y = 0; y < height; y++) - for (int x = 0; x < width; x++) - tiler->AddTile(layers[i][y * width + x], x * 32, y * 32, i); + Video *video = new Video("Deus Hax", 640, 480); + Tiler *tiler = new Tiler(); + Map *map = new Map("maps/testmap-coll.tmx"); - /* Test stuff */ - int playerx, playery; - SDL_GetMouseState(&playerx, &playery); - tiler->AddTile(50, playerx, playery, 1); - tiler->AddTile(50, playerx + 64, playery + 32, 3); - - tiler->Render(); - video->Refresh(33.33333f); -} + for (int done = 0; !done; ) + { + video->Clear(); -int main(int argc, char **argv) -{ - video = new Video("Deus Hax", 640, 480); - tiler = new Tiler(); + map->Draw(tiler); - int done; + /* Test stuff */ + int playerx, playery; + SDL_GetMouseState(&playerx, &playery); - /* Loop, drawing and checking events */ - LoadMap(); + tiler->AddTile(50, playerx, playery, 1); + tiler->AddTile(50, playerx + 64, playery + 32, 3); - done = 0; - while (!done) - { - DrawScene(); + tiler->Render(); + video->Refresh(33.33333f); /* This could go in a separate function */ SDL_Event event; @@ -92,6 +47,7 @@ int main(int argc, char **argv) } } + delete map; delete tiler; delete video; diff --git a/src/tiler.h b/src/tiler.h index fae95892..1acf53fd 100644 --- a/src/tiler.h +++ b/src/tiler.h @@ -4,6 +4,9 @@ * The tile manager */ +#if !defined __TILER_H__ +#define __TILER_H__ + class TilerData; class Tiler @@ -20,3 +23,5 @@ private: TilerData *data; }; +#endif // __TILER_H__ +