Console logging is now identical in all builds; warning and error show the console...
[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 bool Tile::draw_editor_images = false;
24
25 Tile::Tile(const TileSet& new_tileset) :
26   tileset(new_tileset), 
27   imagespecs(),
28   images(),
29   editor_imagespecs(),
30   editor_images(),
31   attributes(0), 
32   data(0), 
33   fps(1)
34 {
35 }
36
37 Tile::Tile(const TileSet& new_tileset, const std::vector<ImageSpec>& imagespecs_, const std::vector<ImageSpec>& editor_imagespecs_, 
38            uint32_t attributes, uint32_t data, float fps) :
39   tileset(new_tileset),
40   imagespecs(imagespecs_),
41   images(),
42   editor_imagespecs(editor_imagespecs_),
43   editor_images(),
44   attributes(attributes), 
45   data(data), 
46   fps(fps)
47 {
48   correct_attributes();
49 }
50
51 Tile::~Tile()
52 {
53 }
54
55 void
56 Tile::load_images()
57 {
58   if(images.size() == 0 && imagespecs.size() != 0)
59   {
60     assert(images.size() == 0);
61     for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i != imagespecs.end(); ++i) 
62     {
63       const ImageSpec& spec = *i;
64
65       SurfacePtr surface;
66       if(spec.rect.get_width() <= 0) 
67       {
68         surface = Surface::create(spec.file);
69       }
70       else 
71       {
72         surface = Surface::create(spec.file,
73                                   Rect((int) spec.rect.p1.x,
74                                        (int) spec.rect.p1.y,
75                                        Size((int) spec.rect.get_width(),
76                                             (int) spec.rect.get_height())));
77       }
78       images.push_back(surface);
79     }
80   }
81
82   if(editor_images.size() == 0 && editor_imagespecs.size() != 0)
83   {
84     assert(editor_images.size() == 0);
85     for(std::vector<ImageSpec>::iterator i = editor_imagespecs.begin(); i != editor_imagespecs.end(); ++i) 
86     {
87       const ImageSpec& spec = *i;
88
89       SurfacePtr surface;
90       if(spec.rect.get_width() <= 0) 
91       {
92         surface = Surface::create(spec.file);
93       }
94       else 
95       {
96         surface = Surface::create(spec.file,
97                                   Rect((int) spec.rect.p1.x,
98                                        (int) spec.rect.p1.y,
99                                        Size((int) spec.rect.get_width(),
100                                             (int) spec.rect.get_height())));
101       }
102       editor_images.push_back(surface);
103     }
104   }
105 }
106
107 void
108 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
109 {
110   if(draw_editor_images) {
111     if(editor_images.size() > 1) {
112       size_t frame = size_t(game_time * fps) % editor_images.size();
113       context.draw_surface(editor_images[frame], pos, z_pos);
114       return;
115     } else if (editor_images.size() == 1) {
116       context.draw_surface(editor_images[0], pos, z_pos);
117       return;
118     }
119   }
120
121   if(images.size() > 1) {
122     size_t frame = size_t(game_time * fps) % images.size();
123     context.draw_surface(images[frame], pos, z_pos);
124   } else if (images.size() == 1) {
125     context.draw_surface(images[0], pos, z_pos);
126   }
127 }
128
129 void
130 Tile::correct_attributes()
131 {
132   //Fix little oddities in attributes (not many, currently...)
133   if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
134     attributes |= SOLID;
135     //But still be vocal about it
136     log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;
137   }
138 }
139
140 void
141 Tile::print_debug(int id) const
142 {
143   log_debug << " Tile: id " << id << ", data " << getData() << ", attributes " << getAttributes() << ":" << std::endl;
144   for(std::vector<Tile::ImageSpec>::const_iterator im = editor_imagespecs.begin(); im != editor_imagespecs.end(); ++im) 
145     log_debug << "  Editor Imagespec: file " << im->file << "; rect " << im->rect << std::endl;
146   for(std::vector<Tile::ImageSpec>::const_iterator im = imagespecs.begin(); im != imagespecs.end(); ++im) 
147     log_debug << "  Imagespec:        file " << im->file << "; rect " << im->rect << std::endl;
148 }
149
150 /* EOF */