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