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