source: trunk/src/map.cpp @ 150

Last change on this file since 150 was 150, checked in by sam, 11 years ago

Replace mouse scrolling with a joystick simulated by the e/s/d/f keys.

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