source: trunk/src/map.cpp @ 758

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

ps3: start a minimal PS3 port using PSGL, but not CG yet.

  • Property svn:keywords set to Id
File size: 4.7 KB
RevLine 
[100]1//
[221]2// Lol Engine
[100]3//
[221]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//
[38]10
[100]11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
[38]15#include <cstdio>
[74]16#include <cstring>
17#include <cstdlib>
[222]18#include <cmath>
[74]19#include <ctype.h>
[38]20
[150]21#include "core.h"
[38]22
[758]23using namespace std;
24
[686]25namespace lol
26{
27
[75]28/*
29 * Map implementation class
30 */
31
32class MapData
[38]33{
[75]34    friend class Map;
35
[126]36    static int const MAX_TILERS = 128;
37
[75]38private:
39    int tilers[MAX_TILERS];
40    int ntilers;
[126]41
[75]42    Layer **layers;
43    int nlayers;
[126]44
45    int width, height;
[75]46};
47
48/*
49 * Public Map class
50 */
51
52Map::Map(char const *path)
[259]53  : data(new MapData())
[75]54{
55    data->ntilers = 0;
56    data->layers = NULL;
57    data->nlayers = 0;
[126]58    data->width = 0;
59    data->height = 0;
[75]60
[70]61    char tmp[BUFSIZ];
[126]62    int gids[MapData::MAX_TILERS];
[75]63    uint32_t *tiles = NULL;
[126]64    int level = 0, orientation = 0, ntiles = 0;
[70]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
[75]77        /* Read a line, then decide what to do with it. */
[70]78        fgets(tmp, BUFSIZ, fp);
79
[79]80        if (tiles && !strchr(tmp, '<'))
[70]81        {
[74]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;
[126]85            while (ntiles < data->width * data->height)
[70]86            {
[75]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                        {
[76]98                            code = (data->tilers[n] << 16) | (id - gids[n]);
[75]99                            break;
100                        }
101                    }
102                }
103
[186]104                int x = ntiles % data->width;
105                int y = data->height - 1 - (ntiles / data->width);
106                tiles[y * data->width + x] = code;
107                ntiles++;
108
[74]109                while (isdigit(*parser))
110                    parser++;
111                if (*parser == ',')
112                    parser++;
113                if (!isdigit(*parser))
114                    break;
115            }
116
[126]117            if (ntiles == data->width * data->height)
[74]118            {
[133]119                Layer *l = new Layer(data->width, data->height,
120                                     level, orientation, tiles);
121                data->layers[data->nlayers] = l;
[75]122                data->nlayers++;
123                tiles = NULL;
[734]124                //Log::Debug("new layer %ix%i\n", data->width, data->height);
[70]125            }
126        }
127        else if (sscanf(tmp, " <tileset firstgid=\"%i\"", &i) == 1)
128        {
[75]129            /* This is a tileset description. Remember its first gid value. */
130            gids[data->ntilers] = i;
[70]131        }
132        else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1)
133        {
[74]134            /* This is a tileset image file. Associate it with firstgid. */
[633]135            data->tilers[data->ntilers] = Tiler::Register(str, 32, 0,
[222]136                                                          sqrtf(2));
[75]137            data->ntilers++;
[734]138            //Log::Debug("new tiler %s\n", str);
[70]139        }
[74]140        else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" "
141                        "width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5)
[70]142        {
[74]143            /* This is a layer description. Prepare to read the data. */
[75]144            data->layers = (Layer **)realloc(data->layers,
145                                       sizeof(Layer **) * (data->nlayers + 1));
[74]146            orientation = toupper(a) == 'V' ? 1 : 0;
[141]147            level = i * 32;
[126]148            data->width = j;
149            data->height = k;
150            tiles = (uint32_t *)malloc(j * k * sizeof(uint32_t));
[74]151            ntiles = 0;
[70]152        }
153    }
154
155    fclose(fp);
[38]156}
157
158Map::~Map()
159{
[75]160    for (int i = 0; i < data->ntilers; i++)
161        Tiler::Deregister(data->tilers[i]);
162    for (int i = 0; i < data->nlayers; i++)
163        delete data->layers[i];
164    free(data->layers);
165    delete data;
[38]166}
167
[289]168void Map::Render(int x, int y, int z)
[38]169{
[75]170    for (int i = 0; i < data->nlayers; i++)
[289]171        data->layers[i]->Render(x, y, z);
[38]172}
173
[126]174int Map::GetWidth()
175{
176    return data->width * 32;
177}
178
179int Map::GetHeight()
180{
181    return data->height * 32;
182}
183
[686]184} /* namespace lol */
185
Note: See TracBrowser for help on using the repository browser.