source: trunk/src/map.cpp @ 74

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

Support CSV .tmx files to get rid of that pathetic system() call.

  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1
2#include <cstdio>
3#include <cstring>
4#include <cstdlib>
5#include <malloc.h>
6#include <ctype.h>
7
8#include "map.h"
9#include "layer.h"
10
11Map::Map(char const *path) :
12    layers(0),
13    nlayers(0)
14{
15    char tmp[BUFSIZ];
16    uint32_t *data = NULL;
17    int width = 0, height = 0, level = 0, orientation = 0;
18    int firstgid = 0, ntiles = 0;
19
20    FILE *fp = fopen(path, "r");
21
22    if (!fp)
23        return;
24
25    while (!feof(fp))
26    {
27        char str[1024];
28        int i, j, k;
29        char a, b;
30
31        fgets(tmp, BUFSIZ, fp);
32
33        if (data)
34        {
35            /* We are in the process of reading layer data. Only stop
36             * when we have read the expected number of tiles. */
37            char const *parser = tmp;
38            while (ntiles < width * height)
39            {
40                data[ntiles++] = atoi(parser);
41                while (isdigit(*parser))
42                    parser++;
43                if (*parser == ',')
44                    parser++;
45                if (!isdigit(*parser))
46                    break;
47            }
48
49            if (ntiles == width * height)
50            {
51                layers[nlayers] = new Layer(width, height, level, data);
52                nlayers++;
53                data = NULL;
54            }
55        }
56        else if (sscanf(tmp, " <tileset firstgid=\"%i\"", &i) == 1)
57        {
58            /* This is a tileset description. Remember its firstgid value. */
59            firstgid = i;
60        }
61        else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1)
62        {
63            /* This is a tileset image file. Associate it with firstgid. */
64        }
65        else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" "
66                        "width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5)
67        {
68            /* This is a layer description. Prepare to read the data. */
69            layers = (Layer **)realloc(layers,
70                                       sizeof(Layer **) * (nlayers + 1));
71            orientation = toupper(a) == 'V' ? 1 : 0;
72            width = j;
73            height = k;
74            ntiles = 0;
75            data = (uint32_t *)malloc(width * height * sizeof(uint32_t));
76        }
77    }
78
79    fclose(fp);
80}
81
82Map::~Map()
83{
84    for (int i = 0; i < nlayers; i++)
85        delete layers[i];
86    free(layers);
87}
88
89void Map::Draw(Tileset *tileset)
90{
91    for (int i = 0; i < nlayers; i++)
92    {
93        int z = layers[i]->GetZ();
94
95        for (int y = 0; y < 32; y++)
96            for (int x = 0; x < 32; x++)
97                tileset->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z);
98    }
99}
100
Note: See TracBrowser for help on using the repository browser.