Change anim_fps to fps in snail (fixes sprite; frames could use some work maybe)...
[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   fps(1)
30 {
31 }
32
33 Tile::Tile(const TileSet& new_tileset, const std::vector<ImageSpec>& imagespecs_, 
34            uint32_t attributes, uint32_t data, float fps) :
35   tileset(new_tileset),
36   imagespecs(imagespecs_),
37   images(),
38   attributes(attributes), 
39   data(data), 
40   fps(fps)
41 {
42   correct_attributes();
43 }
44
45 Tile::~Tile()
46 {
47 }
48
49 void
50 Tile::load_images()
51 {
52   if(images.size() == 0 && imagespecs.size() != 0)
53   {
54     assert(images.size() == 0);
55     for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i != imagespecs.end(); ++i) 
56     {
57       const ImageSpec& spec = *i;
58
59       SurfacePtr surface;
60       if(spec.rect.get_width() <= 0) 
61       {
62         surface = Surface::create(spec.file);
63       }
64       else 
65       {
66         surface = Surface::create(spec.file,
67                                   Rect((int) spec.rect.p1.x,
68                                        (int) spec.rect.p1.y,
69                                        Size((int) spec.rect.get_width(),
70                                             (int) spec.rect.get_height())));
71       }
72       images.push_back(surface);
73     }
74   }
75 }
76
77 void
78 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
79 {
80   if(images.size() > 1) {
81     size_t frame = size_t(game_time * fps) % images.size();
82     context.draw_surface(images[frame], pos, z_pos);
83   } else if (images.size() == 1) {
84     context.draw_surface(images[0], pos, z_pos);
85   }
86 }
87
88 void
89 Tile::correct_attributes()
90 {
91   //Fix little oddities in attributes (not many, currently...)
92   if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
93     attributes |= SOLID;
94     //But still be vocal about it
95     log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;
96   }
97 }
98
99 void
100 Tile::print_debug(int id) const
101 {
102   log_debug << " Tile: id " << id << ", data " << getData() << ", attributes " << getAttributes() << ":" << std::endl;
103   for(std::vector<Tile::ImageSpec>::const_iterator im = imagespecs.begin(); im != imagespecs.end(); ++im) 
104   {
105     log_debug << "  Imagespec: file " << im->file << "; rect " << im->rect << std::endl;
106   }
107 }
108
109 /* EOF */