6770f874ff360a04b56503aa8969d714e2a8fbdc
[supertux.git] / src / tile_manager.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21 #include <config.h>
22
23 #include <memory>
24 #include <stdexcept>
25 #include <sstream>
26 #include <iostream>
27 #include <assert.h>
28 #include <SDL.h>
29 #include "video/drawing_context.hpp"
30 #include "log.hpp"
31 #include "lisp/lisp.hpp"
32 #include "lisp/parser.hpp"
33 #include "lisp/list_iterator.hpp"
34 #include "tile.hpp"
35 #include "tile_manager.hpp"
36 #include "resources.hpp"
37
38 TileManager::TileManager()
39 {
40   tiles.resize(1, 0);
41   tiles[0] = new Tile();
42 }
43
44 TileManager::~TileManager()
45 {
46   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
47     delete *i;
48   tiles.clear();
49 }
50
51 void TileManager::load_tileset(std::string filename, unsigned int start, unsigned int end, int offset)
52 {
53
54 #ifdef DEBUG
55   Uint32 ticks = SDL_GetTicks();
56 #endif
57
58   std::string::size_type t = filename.rfind('/');
59   if(t == std::string::npos) {
60     tiles_path = "";
61   } else {
62     tiles_path = filename.substr(0, t+1);
63   }
64
65   lisp::Parser parser;
66   const lisp::Lisp* root = parser.parse(filename);
67
68   const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
69   if(!tiles_lisp)
70     throw std::runtime_error("file is not a supertux tiles file.");
71
72   lisp::ListIterator iter(tiles_lisp);
73   while(iter.next()) {
74     if(iter.item() == "tile") {
75       Tile* tile = new Tile();
76       tile->parse(*(iter.lisp()));
77
78       if ((tile->id < start) || (tile->id > end)) {
79         delete tile;
80         continue;
81       }
82       tile->id += offset;
83
84       if(tile->id >= tiles.size())
85         tiles.resize(tile->id+1, 0);
86
87       if(tiles[tile->id] != 0) {
88         log_warning << "Tile with ID " << tile->id << " redefined" << std::endl;
89         delete tile;
90       } else {
91         tiles[tile->id] = tile;
92       }
93     } else if(iter.item() == "tilegroup") {
94       TileGroup tilegroup;
95       const lisp::Lisp* tilegroup_lisp = iter.lisp();
96       tilegroup_lisp->get("name", tilegroup.name);
97       tilegroup_lisp->get_vector("tiles", tilegroup.tiles);
98       tilegroups.insert(tilegroup);
99     } else if (iter.item() == "tiles") {
100       // List of ids (use 0 if the tile should be ignored)
101       std::vector<unsigned int> ids;
102       // List of attributes of the tile
103       std::vector<unsigned int> attributes;
104       std::string image;
105
106       // width and height of the image in tile units, this is used for two
107       // purposes:
108       //  a) so we don't have to load the image here to know its dimensions
109       //  b) so that the resulting 'tiles' entry is more robust,
110       //  ie. enlarging the image won't break the tile id mapping
111       // FIXME: height is actually not used, since width might be enough for
112       // all purposes, still feels somewhat more natural this way
113       unsigned int width  = 0;
114       unsigned int height = 0;
115
116       iter.lisp()->get_vector("ids",        ids);
117       iter.lisp()->get_vector("attributes", attributes);
118       iter.lisp()->get("image",      image);
119       iter.lisp()->get("width",      width);
120       iter.lisp()->get("height",     height);
121
122       if (ids.size() != attributes.size())
123         {
124           std::ostringstream err;
125           err << "Number of ids (" << ids.size() <<  ") and attributes (" << attributes.size()
126               << ") missmatch for image '" << image << "', but must be equal";
127           throw std::runtime_error(err.str());
128         }
129
130       for(std::vector<unsigned int>::size_type i = 0; i < ids.size() && i < width*height; ++i)
131         {
132           if (ids[i])
133             {
134               if ((ids[i] < start) || (ids[i] > end)) {
135                 continue;
136               }
137               ids[i] += offset;
138               if(ids[i] >= tiles.size())
139                 tiles.resize(ids[i]+1, 0);
140
141               int x = 32*(i % width);
142               int y = 32*(i / width);
143               Tile* tile = new Tile(ids[i], attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32)));
144               if (tiles[ids[i]] == 0) {
145                 tiles[ids[i]] = tile;
146               } else {
147                 log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;
148                 delete tile;
149               }
150             }
151         }
152
153     } else if(iter.item() == "properties") {
154       // deprecated
155     } else {
156       log_warning << "Unknown symbol '" << iter.item() << "' tile defintion file" << std::endl;
157     }
158   }
159
160   if (0)
161     { // enable this if you want to see a list of free tiles
162       log_info << "Last Tile ID is " << tiles.size()-1 << std::endl;
163       int last = -1;
164       for(int i = 0; i < int(tiles.size()); ++i)
165         {
166           if (tiles[i] == 0 && last == -1)
167             {
168               last = i;
169             }
170           else if (tiles[i] && last != -1)
171             {
172               log_info << "Free Tile IDs (" << i - last << "): " << last << " - " << i-1 << std::endl;
173               last = -1;
174             }
175         }
176     }
177
178 #ifdef DEBUG
179   log_debug << "Tiles loaded in " << (SDL_GetTicks() - ticks) / 1000.0 << " seconds" << std::endl;
180 #endif
181
182 }