Browse Source

Breaking the map/layer code apart. Needs more work.

legacy
Sam Hocevar sam 14 years ago
parent
commit
76b9dfc1be
3 changed files with 60 additions and 18 deletions
  1. +5
    -15
      src/layer.cpp
  2. +2
    -2
      src/layer.h
  3. +53
    -1
      src/map.cpp

+ 5
- 15
src/layer.cpp View File

@@ -1,25 +1,14 @@


#include "layer.h" #include "layer.h"


Layer::Layer(char const *name, int w, int h, FILE *fp)
Layer::Layer(int w, int h, int z, char const *base64)
{ {
width = w; width = w;
height = h; height = h;
level = z;
data = new unsigned int[w * h]; data = new unsigned int[w * h];


if (sscanf(name, "Ground %d", &z) == 1)
;
else if (sscanf(name, "Ground Decal %d", &z) == 1)
;
else if (sscanf(name, "Object %d", &z) == 1)
z++;
else if (sscanf(name, "Wall %d", &z) == 1)
z++;
else if (sscanf(name, "Wall Decal %d", &z) == 1)
z++;
else
z = -1;

#if 0
fread(data, sizeof(unsigned int), width * height, fp); fread(data, sizeof(unsigned int), width * height, fp);
for (int n = 0; n < width * height; n++) for (int n = 0; n < width * height; n++)
{ {
@@ -27,6 +16,7 @@ Layer::Layer(char const *name, int w, int h, FILE *fp)
// XXX: endianness swapping might be necessary here // XXX: endianness swapping might be necessary here
data[n] = i ? i - 1 : 0; data[n] = i ? i - 1 : 0;
} }
#endif
} }


Layer::~Layer() Layer::~Layer()
@@ -36,7 +26,7 @@ Layer::~Layer()


int Layer::GetZ() int Layer::GetZ()
{ {
return z;
return level;
} }


unsigned int Layer::GetTile(int x, int y) unsigned int Layer::GetTile(int x, int y)


+ 2
- 2
src/layer.h View File

@@ -11,14 +11,14 @@
class Layer class Layer
{ {
public: public:
Layer(char const *name, int w, int h, FILE *fp);
Layer(int w, int h, int z, char const *base64);
~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, z;
int width, height, level;
unsigned int *data; unsigned int *data;
}; };




+ 53
- 1
src/map.cpp View File

@@ -9,6 +9,58 @@ Map::Map(char const *path) :
layers(0), layers(0),
nlayers(0) nlayers(0)
{ {
char tmp[BUFSIZ];
int firstgid = 0, width = 0, height = 0, level = 0, data = 0;

FILE *fp = fopen(path, "r");

if (!fp)
return;

while (!feof(fp))
{
char str[1024];
int i, j, k;
char a, b;

fgets(tmp, BUFSIZ, fp);

if (data)
{
if (--data == 0)
{
layers[nlayers] = new Layer(width, height, level, tmp);
nlayers++;
}
}
else if (sscanf(tmp, " <tileset firstgid=\"%i\"", &i) == 1)
{
firstgid = i;
fprintf(stderr, "found tileset, firstgid %i\n", firstgid);
}
else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1)
{
fprintf(stderr, "image %s\n", str);
}
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));
width = j;
height = k;
data = 2;
}
else
{
fprintf(stderr, ".");
}
}

fclose(fp);

/*
char tmp[1024]; 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); 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);
@@ -17,7 +69,6 @@ Map::Map(char const *path) :


while (fp && !feof(fp)) while (fp && !feof(fp))
{ {
char name[1024];
int width, height; int width, height;


fscanf(fp, "<layer name=\"%[^\"]\" ", name); fscanf(fp, "<layer name=\"%[^\"]\" ", name);
@@ -32,6 +83,7 @@ Map::Map(char const *path) :
} }


pclose(fp); pclose(fp);
*/
} }


Map::~Map() Map::~Map()


Loading…
Cancel
Save