Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / floating_text.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_text.hpp"
18
19 #include <stdio.h>
20
21 #include "supertux/resources.hpp"
22 #include "video/drawing_context.hpp"
23
24 FloatingText::FloatingText(const Vector& pos, const std::string& text_)
25   : position(pos), text(text_)
26 {
27   timer.start(.1f);
28   position.x -= text.size() * 8;
29 }
30
31 FloatingText::FloatingText(const Vector& pos, int score)
32   : position(pos)
33 {
34   timer.start(.1f);
35
36   // turn int into a string
37   char str[10];
38   snprintf(str, 10, "%d", score);
39   text = str;
40
41   position.x -= text.size() * 8;
42 }
43
44 void
45 FloatingText::update(float elapsed_time)
46 {
47   position.y -= 1.4 * elapsed_time;
48
49   if(timer.check())
50     remove_me();
51 }
52
53 #define FADING_TIME .350
54
55 void
56 FloatingText::draw(DrawingContext& context)
57 {
58   // make an alpha animation when disappearing
59   int alpha;
60   if(timer.get_timeleft() < FADING_TIME)
61     alpha = int(timer.get_timeleft() * 255 / FADING_TIME);
62   else
63     alpha = 255;
64
65   context.push_transform();
66   context.set_alpha(alpha);
67
68   context.draw_text(normal_font, text, position, ALIGN_LEFT, LAYER_OBJECTS+1, FloatingText::text_color);
69
70   context.pop_transform();
71 }
72
73 /* EOF */