| @@ -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 | all: test-map | ||||
| @@ -1,12 +1,12 @@ | |||||
| #include "layer.h" | #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; | width = w; | ||||
| height = h; | height = h; | ||||
| level = z; | |||||
| data = new unsigned int[w * h]; | |||||
| z = in_z; | |||||
| data = in_data; | |||||
| #if 0 | #if 0 | ||||
| fread(data, sizeof(unsigned int), width * height, fp); | fread(data, sizeof(unsigned int), width * height, fp); | ||||
| @@ -26,7 +26,7 @@ Layer::~Layer() | |||||
| int Layer::GetZ() | int Layer::GetZ() | ||||
| { | { | ||||
| return level; | |||||
| return z; | |||||
| } | } | ||||
| unsigned int Layer::GetTile(int x, int y) | unsigned int Layer::GetTile(int x, int y) | ||||
| @@ -7,19 +7,20 @@ | |||||
| #define __DH_LAYER_H__ | #define __DH_LAYER_H__ | ||||
| #include <cstdio> | #include <cstdio> | ||||
| #include <stdint.h> | |||||
| class Layer | class Layer | ||||
| { | { | ||||
| public: | public: | ||||
| Layer(int w, int h, int z, char const *base64); | |||||
| Layer(int w, int h, int z, uint32_t *data); | |||||
| ~Layer(); | ~Layer(); | ||||
| int GetZ(); | int GetZ(); | ||||
| unsigned int GetTile(int x, int y); | unsigned int GetTile(int x, int y); | ||||
| private: | private: | ||||
| int width, height, level; | |||||
| unsigned int *data; | |||||
| int width, height, z; | |||||
| uint32_t *data; | |||||
| }; | }; | ||||
| #endif // __DH_LAYER_H__ | #endif // __DH_LAYER_H__ | ||||
| @@ -1,6 +1,9 @@ | |||||
| #include <cstdio> | #include <cstdio> | ||||
| #include <cstring> | |||||
| #include <cstdlib> | |||||
| #include <malloc.h> | #include <malloc.h> | ||||
| #include <ctype.h> | |||||
| #include "map.h" | #include "map.h" | ||||
| #include "layer.h" | #include "layer.h" | ||||
| @@ -10,7 +13,9 @@ Map::Map(char const *path) : | |||||
| nlayers(0) | nlayers(0) | ||||
| { | { | ||||
| char tmp[BUFSIZ]; | 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"); | FILE *fp = fopen(path, "r"); | ||||
| @@ -27,63 +32,51 @@ Map::Map(char const *path) : | |||||
| if (data) | 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++; | nlayers++; | ||||
| data = NULL; | |||||
| } | } | ||||
| } | } | ||||
| else if (sscanf(tmp, " <tileset firstgid=\"%i\"", &i) == 1) | else if (sscanf(tmp, " <tileset firstgid=\"%i\"", &i) == 1) | ||||
| { | { | ||||
| /* This is a tileset description. Remember its firstgid value. */ | |||||
| firstgid = i; | firstgid = i; | ||||
| fprintf(stderr, "found tileset, firstgid %i\n", firstgid); | |||||
| } | } | ||||
| else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1) | else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1) | ||||
| { | { | ||||
| fprintf(stderr, "image %s\n", str); | |||||
| /* This is a tileset image file. Associate it with firstgid. */ | |||||
| } | } | ||||
| else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" width=\"%i\" height=\"%i\"", | |||||
| &a, &i, &b, &j, &k) == 5) | |||||
| else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" " | |||||
| "width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5) | |||||
| { | { | ||||
| fprintf(stderr, "%s layer, level %i, sublevel %c, %ix%i\n", | |||||
| a == 'H' ? "horizontal" : "vertical", i, b, j, k); | |||||
| layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1)); | |||||
| /* This is a layer description. Prepare to read the data. */ | |||||
| layers = (Layer **)realloc(layers, | |||||
| sizeof(Layer **) * (nlayers + 1)); | |||||
| orientation = toupper(a) == 'V' ? 1 : 0; | |||||
| width = j; | width = j; | ||||
| height = k; | height = k; | ||||
| data = 2; | |||||
| } | |||||
| else | |||||
| { | |||||
| fprintf(stderr, "."); | |||||
| ntiles = 0; | |||||
| data = (uint32_t *)malloc(width * height * sizeof(uint32_t)); | |||||
| } | } | ||||
| } | } | ||||
| fclose(fp); | fclose(fp); | ||||
| /* | |||||
| 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)) | |||||
| { | |||||
| int width, height; | |||||
| fscanf(fp, "<layer name=\"%[^\"]\" ", name); | |||||
| if (feof(fp)) | |||||
| break; | |||||
| fscanf(fp, "width=\"%u\" ", &width); | |||||
| fscanf(fp, "height=\"%u\" ", &height); | |||||
| fgets(tmp, 1024, fp); // Ignore rest of line | |||||
| layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1)); | |||||
| layers[nlayers] = new Layer(name, width, height, fp); | |||||
| nlayers++; | |||||
| } | |||||
| pclose(fp); | |||||
| */ | |||||
| } | } | ||||
| Map::~Map() | Map::~Map() | ||||
| @@ -93,7 +86,7 @@ Map::~Map() | |||||
| free(layers); | free(layers); | ||||
| } | } | ||||
| void Map::Draw(Tiler *tiler) | |||||
| void Map::Draw(Tileset *tileset) | |||||
| { | { | ||||
| for (int i = 0; i < nlayers; i++) | for (int i = 0; i < nlayers; i++) | ||||
| { | { | ||||
| @@ -101,7 +94,7 @@ void Map::Draw(Tiler *tiler) | |||||
| for (int y = 0; y < 32; y++) | for (int y = 0; y < 32; y++) | ||||
| for (int x = 0; x < 32; x++) | for (int x = 0; x < 32; x++) | ||||
| tiler->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z); | |||||
| tileset->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z); | |||||
| } | } | ||||
| } | } | ||||
| @@ -9,7 +9,7 @@ | |||||
| #include <cstdio> | #include <cstdio> | ||||
| #include "layer.h" | #include "layer.h" | ||||
| #include "tiler.h" | |||||
| #include "tileset.h" | |||||
| class Map | class Map | ||||
| { | { | ||||
| @@ -17,7 +17,7 @@ public: | |||||
| Map(char const *path); | Map(char const *path); | ||||
| ~Map(); | ~Map(); | ||||
| void Draw(Tiler *tiler); | |||||
| void Draw(Tileset *tileset); | |||||
| private: | private: | ||||
| Layer **layers; | Layer **layers; | ||||
| @@ -6,20 +6,20 @@ | |||||
| #include <math.h> | #include <math.h> | ||||
| #include "video.h" | #include "video.h" | ||||
| #include "tiler.h" | |||||
| #include "tileset.h" | |||||
| #include "map.h" | #include "map.h" | ||||
| int main(int argc, char **argv) | int main(int argc, char **argv) | ||||
| { | { | ||||
| Video *video = new Video("Deus Hax", 640, 480); | Video *video = new Video("Deus Hax", 640, 480); | ||||
| Tiler *tiler = new Tiler(); | |||||
| Tileset *tileset = new Tileset(); | |||||
| Map *map = new Map("maps/testmap-grass.tmx"); | Map *map = new Map("maps/testmap-grass.tmx"); | ||||
| for (int done = 0; !done; ) | for (int done = 0; !done; ) | ||||
| { | { | ||||
| video->Clear(); | video->Clear(); | ||||
| map->Draw(tiler); | |||||
| map->Draw(tileset); | |||||
| /* Test stuff */ | /* Test stuff */ | ||||
| int playerx, playery; | int playerx, playery; | ||||
| @@ -27,9 +27,9 @@ int main(int argc, char **argv) | |||||
| playerx = playerx * (640 - 32) / 640; | playerx = playerx * (640 - 32) / 640; | ||||
| playery = playery * (480 - 32) / 480; | 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); | video->Refresh(33.33333f); | ||||
| /* This could go in a separate function */ | /* This could go in a separate function */ | ||||
| @@ -49,7 +49,7 @@ int main(int argc, char **argv) | |||||
| } | } | ||||
| delete map; | delete map; | ||||
| delete tiler; | |||||
| delete tileset; | |||||
| delete video; | delete video; | ||||
| return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||||
| @@ -16,15 +16,15 @@ | |||||
| #include <malloc.h> | #include <malloc.h> | ||||
| #include "tiler.h" | |||||
| #include "tileset.h" | |||||
| /* | /* | ||||
| * Tiler implementation class | |||||
| * Tileset implementation class | |||||
| */ | */ | ||||
| class TilerData | |||||
| class TilesetData | |||||
| { | { | ||||
| friend class Tiler; | |||||
| friend class Tileset; | |||||
| private: | private: | ||||
| static int Compare(void const *p1, void const *p2) | 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->tiles = NULL; | ||||
| data->ntiles = 0; | data->ntiles = 0; | ||||
| @@ -74,13 +74,13 @@ Tiler::Tiler() | |||||
| glGenBuffers(3, data->buflist); | glGenBuffers(3, data->buflist); | ||||
| } | } | ||||
| Tiler::~Tiler() | |||||
| Tileset::~Tileset() | |||||
| { | { | ||||
| free(data->tiles); | free(data->tiles); | ||||
| delete data; | 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) | if ((data->ntiles % 1024) == 0) | ||||
| { | { | ||||
| @@ -96,10 +96,10 @@ void Tiler::AddTile(int n, int x, int y, int z) | |||||
| data->ntiles++; | data->ntiles++; | ||||
| } | } | ||||
| void Tiler::Render() | |||||
| void Tileset::Render() | |||||
| { | { | ||||
| /* Sort tiles */ | /* 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 */ | /* Texture coord buffer */ | ||||
| float uvs[8 * data->ntiles]; | float uvs[8 * data->ntiles]; | ||||
| @@ -6,20 +6,20 @@ | |||||
| #if !defined __DH_TILER_H__ | #if !defined __DH_TILER_H__ | ||||
| #define __DH_TILER_H__ | #define __DH_TILER_H__ | ||||
| class TilerData; | |||||
| class TilesetData; | |||||
| class Tiler | |||||
| class Tileset | |||||
| { | { | ||||
| public: | public: | ||||
| Tiler(); | |||||
| ~Tiler(); | |||||
| Tileset(); | |||||
| ~Tileset(); | |||||
| void AddTile(int n, int x, int y, int z); | void AddTile(int n, int x, int y, int z); | ||||
| void Render(); | void Render(); | ||||
| private: | private: | ||||
| TilerData *data; | |||||
| TilesetData *data; | |||||
| }; | }; | ||||
| #endif // __DH_TILER_H__ | #endif // __DH_TILER_H__ | ||||
| @@ -54,7 +54,7 @@ Video::Video(char const *title, int width, int height) | |||||
| SDL_WM_SetCaption(title, NULL); | SDL_WM_SetCaption(title, NULL); | ||||
| SDL_ShowCursor(0); | SDL_ShowCursor(0); | ||||
| SDL_WM_GrabInput(SDL_GRAB_ON); | |||||
| //SDL_WM_GrabInput(SDL_GRAB_ON); | |||||
| /* Initialise OpenGL */ | /* Initialise OpenGL */ | ||||
| glViewport(0, 0, data->video->w, data->video->h); | glViewport(0, 0, data->video->w, data->video->h); | ||||