<MatzeB> -cleanup in resource management functions
[supertux.git] / src / tile.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
21 #include "tile.h"
22 #include "scene.h"
23 #include "assert.h"
24
25 TileManager* TileManager::instance_  = 0;
26 std::vector<TileGroup>* TileManager::tilegroups_  = 0;
27
28 Tile::Tile()
29 {
30 }
31
32 Tile::~Tile()
33 {
34   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
35       ++i) {
36     delete *i;
37   }
38   for(std::vector<Surface*>::iterator i = editor_images.begin();
39       i != editor_images.end(); ++i) {
40     delete *i;                                                                
41   }
42 }
43
44 //---------------------------------------------------------------------------
45
46 TileManager::TileManager()
47 {
48   std::string filename = datadir + "/images/tilesets/supertux.stgt";
49   load_tileset(filename);
50 }
51
52 TileManager::~TileManager()
53 {
54   for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
55     delete *i;                                                                  
56   }
57 }
58
59 void TileManager::load_tileset(std::string filename)
60 {
61   if(filename == current_tileset)
62     return;
63   
64   // free old tiles
65   for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
66     delete *i;
67   }
68   tiles.clear();
69  
70   lisp_object_t* root_obj = lisp_read_from_file(filename);
71
72   if (!root_obj)
73     st_abort("Couldn't load file", filename);
74
75   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") == 0)
76     {
77       lisp_object_t* cur = lisp_cdr(root_obj);
78       int tileset_id = 0;
79
80       while(!lisp_nil_p(cur))
81         {
82           lisp_object_t* element = lisp_car(cur);
83
84           if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
85             {
86               std::vector<std::string> editor_filenames;
87              
88               Tile* tile = new Tile;
89               tile->id      = -1;
90               tile->solid   = false;
91               tile->brick   = false;
92               tile->ice     = false;
93               tile->water   = false;
94               tile->fullbox = false;
95               tile->distro  = false;
96               tile->data    = 0;
97               tile->next_tile  = 0;
98               tile->anim_speed = 25;
99
100               LispReader reader(lisp_cdr(element));
101               assert(reader.read_int("id",  &tile->id));
102               reader.read_bool("solid",     &tile->solid);
103               reader.read_bool("brick",     &tile->brick);
104               reader.read_bool("ice",       &tile->ice);
105               reader.read_bool("water",     &tile->water);
106               reader.read_bool("fullbox",   &tile->fullbox);
107               reader.read_bool("distro",    &tile->distro);
108               reader.read_int("data",       &tile->data);
109               reader.read_int("anim-speed", &tile->anim_speed);
110               reader.read_int("next-tile",  &tile->next_tile);
111               reader.read_string_vector("images",  &tile->filenames);
112               reader.read_string_vector("editor-images", &editor_filenames);
113
114               for(std::vector<std::string>::iterator it = tile->
115                   filenames.begin();
116                   it != tile->filenames.end();
117                   ++it)
118                 {
119                   Surface* cur_image;
120                   tile->images.push_back(cur_image);
121                   tile->images[tile->images.size()-1] = new Surface(
122                                datadir +  "/images/tilesets/" + (*it),
123                                USE_ALPHA);
124                 }
125               for(std::vector<std::string>::iterator it = editor_filenames.begin();
126                   it != editor_filenames.end();
127                   ++it)
128                 {
129                   Surface* cur_image;
130                   tile->editor_images.push_back(cur_image);
131                   tile->editor_images[tile->editor_images.size()-1] = new Surface(
132                                datadir + "/images/tilesets/" + (*it),
133                                USE_ALPHA);
134                 }
135                 
136               if (tile->id + tileset_id >= int(tiles.size())
137                  )
138                 tiles.resize(tile->id + tileset_id+1);
139
140               tiles[tile->id + tileset_id] = tile;
141             }
142           else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
143             {
144               LispReader reader(lisp_cdr(element));
145               std::string filename;
146               reader.read_string("file",  &filename);
147               filename = datadir + "/images/tilesets/" + filename;
148               load_tileset(filename);
149             }
150           else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0)
151             {
152               TileGroup new_;
153               if(!tilegroups_)
154                 tilegroups_ = new std::vector<TileGroup>;
155               tilegroups_->push_back(new_);
156               LispReader reader(lisp_cdr(element));
157               tilegroups_->back().name;
158               reader.read_string("name",  &tilegroups_->back().name);
159               reader.read_int_vector("tiles", &tilegroups_->back().tiles);
160             }
161           else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
162             {
163               LispReader reader(lisp_cdr(element));
164               reader.read_int("id",  &tileset_id);
165               tileset_id *= 1000;
166             }
167           else
168             {
169               puts("Unhandled symbol");
170             }
171
172           cur = lisp_cdr(cur);
173         }
174     }
175   else
176     {
177       assert(0);
178     }
179
180   lisp_free(root_obj);
181   current_tileset = filename;
182 }
183
184 void
185 Tile::draw(float x, float y, unsigned int c, Uint8 alpha)
186 {
187   if (c != 0)
188     {
189       Tile* ptile = TileManager::instance()->get(c);
190       if(ptile)
191         {
192           if(ptile->images.size() > 1)
193             {
194               ptile->images[( ((global_frame_counter*25) / ptile->anim_speed) % (ptile->images.size()))]->draw(x,y, alpha);
195             }
196           else if (ptile->images.size() == 1)
197             {
198               ptile->images[0]->draw(x,y, alpha);
199             }
200           else
201             {
202               //printf("Tile not dravable %u\n", c);
203             }
204         }
205     }
206 }
207
208 // EOF //
209