Bug 563: Reset backflipping when Tux spawns.
[supertux.git] / src / object / floating_image.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/floating_image.hpp"
18 #include "sprite/sprite.hpp"
19 #include "sprite/sprite_manager.hpp"
20 #include "supertux/globals.hpp"
21
22 FloatingImage::FloatingImage(const std::string& spritefile) :
23   sprite(),
24   layer(LAYER_FOREGROUND1 + 1), 
25   visible(false), 
26   anchor(ANCHOR_MIDDLE), 
27   pos(),
28   fading(0), 
29   fadetime(0)
30 {
31   sprite = sprite_manager->create(spritefile);
32 }
33
34 FloatingImage::~FloatingImage()
35 {
36 }
37
38 void
39 FloatingImage::update(float elapsed_time)
40 {
41   if(fading > 0) {
42     fading -= elapsed_time;
43     if(fading <= 0) {
44       fading = 0;
45       visible = true;
46     }
47   } else if(fading < 0) {
48     fading += elapsed_time;
49     if(fading >= 0) {
50       fading = 0;
51       visible = false;
52     }
53   }
54
55   //  (void) elapsed_time;
56 }
57
58 void
59 FloatingImage::set_action(const std::string& action)
60 {
61   sprite->set_action(action);
62 }
63
64 std::string
65 FloatingImage::get_action()
66 {
67   return sprite->get_action();
68 }
69
70 void
71 FloatingImage::fade_in(float fadetime)
72 {
73   this->fadetime = fadetime;
74   fading = fadetime;
75 }
76
77 void
78 FloatingImage::fade_out(float fadetime)
79 {
80   this->fadetime = fadetime;
81   fading = -fadetime;
82 }
83
84 void
85 FloatingImage::draw(DrawingContext& context)
86 {
87   context.push_transform();
88   context.set_translation(Vector(0, 0));
89
90   if(fading > 0) {
91     context.set_alpha((fadetime-fading) / fadetime);
92   } else if(fading < 0) {
93     context.set_alpha(-fading / fadetime);
94   } else if(!visible) {
95     context.pop_transform();
96     return;
97   }
98
99   Vector spos = pos + get_anchor_pos(Rectf(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
100                                      sprite->get_width(), sprite->get_height(), anchor);
101
102   sprite->draw(context, spos, layer);
103
104   context.pop_transform();
105 }
106
107 /* EOF */