Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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/main.hpp"
21
22 FloatingImage::FloatingImage(const std::string& spritefile) :
23   layer(LAYER_FOREGROUND1 + 1), 
24   visible(false), 
25   anchor(ANCHOR_MIDDLE), 
26   fading(0), 
27   fadetime(0)
28 {
29   sprite = sprite_manager->create(spritefile);
30 }
31
32 FloatingImage::~FloatingImage()
33 {
34 }
35
36 void
37 FloatingImage::update(float elapsed_time)
38 {
39   if(fading > 0) {
40     fading -= elapsed_time;
41     if(fading <= 0) {
42       fading = 0;
43       visible = true;
44     }
45   } else if(fading < 0) {
46     fading += elapsed_time;
47     if(fading >= 0) {
48       fading = 0;
49       visible = false;
50     }
51   }
52
53   //  (void) elapsed_time;
54 }
55
56 void
57 FloatingImage::set_action(const std::string& action)
58 {
59   sprite->set_action(action);
60 }
61
62 std::string
63 FloatingImage::get_action()
64 {
65   return sprite->get_action();
66 }
67
68 void
69 FloatingImage::fade_in(float fadetime)
70 {
71   this->fadetime = fadetime;
72   fading = fadetime;
73 }
74
75 void
76 FloatingImage::fade_out(float fadetime)
77 {
78   this->fadetime = fadetime;
79   fading = -fadetime;
80 }
81
82 void
83 FloatingImage::draw(DrawingContext& context)
84 {
85   context.push_transform();
86   context.set_translation(Vector(0, 0));
87
88   if(fading > 0) {
89     context.set_alpha((fadetime-fading) / fadetime);
90   } else if(fading < 0) {
91     context.set_alpha(-fading / fadetime);
92   } else if(!visible) {
93     context.pop_transform();
94     return;
95   }
96
97   Vector spos = pos + get_anchor_pos(Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
98                                      sprite->get_width(), sprite->get_height(), anchor);
99
100   sprite->draw(context, spos, layer);
101
102   context.pop_transform();
103 }
104
105 /* EOF */