Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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(const std::string& filename)
39 {
40 #ifdef DEBUG
41   Uint32 ticks = SDL_GetTicks();
42 #endif
43   load_tileset(filename);
44 #ifdef DEBUG
45   log_debug << "Tiles loaded in " << (SDL_GetTicks() - ticks) / 1000.0 << " seconds" << std::endl;
46 #endif
47 }
48
49 TileManager::~TileManager()
50 {
51   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
52     delete *i;
53 }
54
55 void TileManager::load_tileset(std::string filename)
56 {
57   // free old tiles
58   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
59     delete *i;
60   tiles.clear();
61
62   std::string::size_type t = filename.rfind('/');
63   if(t == std::string::npos) {
64     tiles_path = "";
65   } else {
66     tiles_path = filename.substr(0, t+1);
67   }
68  
69   lisp::Parser parser;
70   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
71
72   const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
73   if(!tiles_lisp)
74     throw std::runtime_error("file is not a supertux tiles file.");
75
76   lisp::ListIterator iter(tiles_lisp);
77   while(iter.next()) {
78     if(iter.item() == "tile") {
79       Tile* tile = new Tile();
80       tile->parse(*(iter.lisp()));
81       while(tile->id >= tiles.size()) {
82         tiles.push_back(0);
83       }
84       if(tiles[tile->id] != 0) {
85         log_warning << "Tile with ID " << tile->id << " redefined" << std::endl;
86       }
87       tiles[tile->id] = tile;
88     } else if(iter.item() == "tilegroup") {
89       TileGroup tilegroup;
90       const lisp::Lisp* tilegroup_lisp = iter.lisp();
91       tilegroup_lisp->get("name", tilegroup.name);
92       tilegroup_lisp->get_vector("tiles", tilegroup.tiles);
93       tilegroups.insert(tilegroup);
94     } else if (iter.item() == "tiles") {
95       // List of ids (use 0 if the tile should be ignored)
96       std::vector<unsigned int> ids;
97       // List of attributes of the tile
98       std::vector<unsigned int> attributes;
99       std::string image;
100
101       // width and height of the image in tile units, this is used for two
102       // purposes: 
103       //  a) so we don't have to load the image here to know its dimensions
104       //  b) so that the resulting 'tiles' entry is more robust,
105       //  ie. enlarging the image won't break the tile id mapping
106       // FIXME: height is actually not used, since width might be enough for
107       // all purposes, still feels somewhat more natural this way
108       unsigned int width  = 0;
109       unsigned int height = 0;
110
111       iter.lisp()->get_vector("ids",        ids);
112       iter.lisp()->get_vector("attributes", attributes);
113       iter.lisp()->get("image",      image);
114       iter.lisp()->get("width",      width);
115       iter.lisp()->get("height",     height);
116
117       if (ids.size() != attributes.size())
118         {
119           std::ostringstream err;
120           err << "Number of ids (" << ids.size() <<  ") and attributes (" << attributes.size()
121               << ") missmatch for image '" << image << "', but must be equal";
122           throw std::runtime_error(err.str());
123         }
124
125       for(std::vector<unsigned int>::size_type i = 0; i < ids.size() && i < width*height; ++i)
126         {
127           if (ids[i])
128             {
129               if(ids[i] >= tiles.size())
130                 tiles.resize(ids[i]+1, 0);
131
132               int x = 32*(i % width);
133               int y = 32*(i / width);
134               Tile* tile = new Tile(ids[i], attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32)));
135               tiles[ids[i]] = tile;
136             }
137         }
138       
139     } else if(iter.item() == "properties") {
140       // deprecated
141     } else {
142       log_warning << "Unknown symbol '" << iter.item() << "' tile defintion file" << std::endl;
143     }
144   }
145 }
146