source: trunk/src/map.cpp @ 1046

Last change on this file since 1046 was 1046, checked in by sam, 12 years ago

core: split vector operations into linear and non-linear so that we can
reuse the linear operations in quaternions. Also mark some constructors
explicit to better spot coding errors.

  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdio>
16#include <cstring>
17#include <cstdlib>
18#include <cmath>
19#include <ctype.h>
20
21#include "core.h"
22
23using namespace std;
24
25namespace lol
26{
27
28/*
29 * Map implementation class
30 */
31
32class MapData
33{
34    friend class Map;
35
36    static int const MAX_TILESETS = 128;
37
38private:
39    TileSet *tilesets[MAX_TILESETS];
40    int ntilers;
41
42    Layer **layers;
43    int nlayers;
44
45    int width, height;
46};
47
48/*
49 * Public Map class
50 */
51
52Map::Map(char const *path)
53  : data(new MapData())
54{
55    data->ntilers = 0;
56    data->layers = NULL;
57    data->nlayers = 0;
58    data->width = 0;
59    data->height = 0;
60
61    char tmp[BUFSIZ];
62    int gids[MapData::MAX_TILESETS];
63    uint32_t *tiles = NULL;
64    int level = 0, orientation = 0, ntiles = 0;
65
66    FILE *fp = fopen(path, "r");
67
68    if (!fp)
69        return;
70
71    while (!feof(fp))
72    {
73        char str[1024];
74        int i, j, k;
75        char a, b;
76
77        /* Read a line, then decide what to do with it. */
78        fgets(tmp, BUFSIZ, fp);
79
80        if (tiles && !strchr(tmp, '<'))
81        {
82            /* We are in the process of reading layer data. Only stop
83             * when we have read the expected number of tiles. */
84            char const *parser = tmp;
85            while (ntiles < data->width * data->height)
86            {
87                uint32_t code = 0;
88                int id = atoi(parser);
89                if (id)
90                {
91                    for (int n = 0; n < data->ntilers; n++)
92                    {
93                        if (id < gids[n])
94                            continue;
95                        if (n == data->ntilers - 1
96                             || id < gids[n + 1])
97                        {
98                            static int error = 1;
99                            if (error && !(error = 0))
100                                Log::Error("tilesets no longer work this way\n");
101                            //code = (data->tilesets[n] << 16) | (id - gids[n]);
102                            break;
103                        }
104                    }
105                }
106
107                int x = ntiles % data->width;
108                int y = data->height - 1 - (ntiles / data->width);
109                tiles[y * data->width + x] = code;
110                ntiles++;
111
112                while (isdigit(*parser))
113                    parser++;
114                if (*parser == ',')
115                    parser++;
116                if (!isdigit(*parser))
117                    break;
118            }
119
120            if (ntiles == data->width * data->height)
121            {
122                Layer *l = new Layer(data->width, data->height,
123                                     level, orientation, tiles);
124                data->layers[data->nlayers] = l;
125                data->nlayers++;
126                tiles = NULL;
127                //Log::Debug("new layer %ix%i\n", data->width, data->height);
128            }
129        }
130        else if (sscanf(tmp, " <tileset firstgid=\"%i\"", &i) == 1)
131        {
132            /* This is a tileset description. Remember its first gid value. */
133            gids[data->ntilers] = i;
134        }
135        else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1)
136        {
137            /* This is a tileset image file. Associate it with firstgid. */
138            data->tilesets[data->ntilers] = Tiler::Register(str, ivec2(32),
139                                                            ivec2(0), sqrtf(2));
140            data->ntilers++;
141            //Log::Debug("new tiler %s\n", str);
142        }
143        else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" "
144                        "width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5)
145        {
146            /* This is a layer description. Prepare to read the data. */
147            data->layers = (Layer **)realloc(data->layers,
148                                       sizeof(Layer **) * (data->nlayers + 1));
149            orientation = toupper(a) == 'V' ? 1 : 0;
150            level = i * 32;
151            data->width = j;
152            data->height = k;
153            tiles = (uint32_t *)malloc(j * k * sizeof(uint32_t));
154            ntiles = 0;
155        }
156    }
157
158    fclose(fp);
159}
160
161Map::~Map()
162{
163    for (int i = 0; i < data->ntilers; i++)
164        Tiler::Deregister(data->tilesets[i]);
165    for (int i = 0; i < data->nlayers; i++)
166        delete data->layers[i];
167    free(data->layers);
168    delete data;
169}
170
171void Map::Render(int x, int y, int z)
172{
173    for (int i = 0; i < data->nlayers; i++)
174        data->layers[i]->Render(x, y, z);
175}
176
177int Map::GetWidth()
178{
179    return data->width * 32;
180}
181
182int Map::GetHeight()
183{
184    return data->height * 32;
185}
186
187} /* namespace lol */
188
Note: See TracBrowser for help on using the repository browser.