source: trunk/src/map.cpp @ 38

Last change on this file since 38 was 38, checked in by sam, 10 years ago

Dynamically allocate layers when loading maps.

  • Property svn:keywords set to Id
File size: 1.2 KB
Line 
1
2#include <cstdio>
3#include <malloc.h>
4
5#include "map.h"
6#include "layer.h"
7
8Map::Map(char const *path) :
9    layers(0),
10    nlayers(0)
11{
12    char tmp[1024];
13
14    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);
15
16    FILE *fp = popen(tmp, "r");
17
18    while (fp && !feof(fp))
19    {
20        char name[1024];
21        int width, height;
22
23        fscanf(fp, "<layer name=\"%[^\"]\" ", name);
24        if (feof(fp))
25            break;
26        fscanf(fp, "width=\"%u\" ", &width);
27        fscanf(fp, "height=\"%u\" ", &height);
28        fgets(tmp, 1024, fp); // Ignore rest of line
29        layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1));
30        layers[nlayers] = new Layer(width, height, fp);
31        nlayers++;
32    }
33
34    pclose(fp);
35}
36
37Map::~Map()
38{
39    for (int i = 0; i < nlayers; i++)
40        delete layers[i];
41    free(layers);
42}
43
44void Map::Draw(Tiler *tiler)
45{
46    for (int i = 0; i < nlayers; i++)
47        for (int y = 0; y < 32; y++)
48            for (int x = 0; x < 32; x++)
49                tiler->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, i);
50}
51
Note: See TracBrowser for help on using the repository browser.