Moved Tile parsing code into TileSetParser
[supertux.git] / src / supertux / tile.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "supertux/tile.hpp"
19
20 #include "supertux/tile_set.hpp"
21 #include "video/drawing_context.hpp"
22
23 Tile::Tile(const TileSet& new_tileset) :
24   tileset(new_tileset), 
25   imagespecs(),
26   images(),
27   attributes(0), 
28   data(0), 
29   anim_fps(1)
30 {
31 }
32
33 Tile::Tile(const TileSet& new_tileset, std::vector<std::string> images, Rect rect, 
34            uint32_t attributes, uint32_t data, float animfps) :
35   tileset(new_tileset),
36   imagespecs(),
37   images(),
38   attributes(attributes), 
39   data(data), 
40   anim_fps(animfps)
41 {
42   for(std::vector<std::string>::iterator i = images.begin(); i != images.end(); ++i) {
43     imagespecs.push_back(ImageSpec(*i, rect));
44   }
45   correct_attributes();
46 }
47
48 Tile::~Tile()
49 {
50   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
51       ++i) {
52     delete *i;
53   }
54 }
55
56 void
57 Tile::load_images()
58 {
59   if(images.size() == 0 && imagespecs.size() != 0)
60   {
61     const std::string& tiles_path = tileset.tiles_path;
62
63     assert(images.size() == 0);
64     for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
65           imagespecs.end(); ++i) {
66       const ImageSpec& spec = *i;
67       Surface* surface;
68       std::string file = tiles_path + spec.file;
69       if(spec.rect.get_width() <= 0) {
70         surface = new Surface(file);
71       } else {
72         surface = new Surface(file,
73                               (int) spec.rect.p1.x,
74                               (int) spec.rect.p1.y,
75                               (int) spec.rect.get_width(),
76                               (int) spec.rect.get_height());
77       }
78       images.push_back(surface);
79     }
80   }
81 }
82
83 void
84 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
85 {
86   if(images.size() > 1) {
87     size_t frame = size_t(game_time * anim_fps) % images.size();
88     context.draw_surface(images[frame], pos, z_pos);
89   } else if (images.size() == 1) {
90     context.draw_surface(images[0], pos, z_pos);
91   }
92 }
93
94 void
95 Tile::correct_attributes()
96 {
97   //Fix little oddities in attributes (not many, currently...)
98   if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
99     attributes |= SOLID;
100     //But still be vocal about it
101     log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;
102   }
103 }
104
105 void
106 Tile::print_debug(int id) const
107 {
108   log_debug << " Tile: id " << id << ", data " << getData() << ", attributes " << getAttributes() << ":" << std::endl;
109   for(std::vector<Tile::ImageSpec>::const_iterator im = imagespecs.begin(); im != imagespecs.end(); ++im) 
110   {
111     log_debug << "  Imagespec: file " << im->file << "; rect " << im->rect << std::endl;
112   }
113 }
114
115 /* EOF */