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