src/object/floating_text.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "object/floating_text.hpp"
00018 
00019 #include <stdio.h>
00020 
00021 #include "supertux/resources.hpp"
00022 #include "video/drawing_context.hpp"
00023 
00024 FloatingText::FloatingText(const Vector& pos, const std::string& text_) :
00025   position(pos), 
00026   text(text_),
00027   timer()
00028 {
00029   timer.start(.1f);
00030   position.x -= text.size() * 8;
00031 }
00032 
00033 FloatingText::FloatingText(const Vector& pos, int score) :
00034   position(pos),
00035   text(),
00036   timer()
00037 {
00038   timer.start(.1f);
00039 
00040   // turn int into a string
00041   char str[10];
00042   snprintf(str, 10, "%d", score);
00043   text = str;
00044 
00045   position.x -= text.size() * 8;
00046 }
00047 
00048 void
00049 FloatingText::update(float elapsed_time)
00050 {
00051   position.y -= 1.4 * elapsed_time;
00052 
00053   if(timer.check())
00054     remove_me();
00055 }
00056 
00057 #define FADING_TIME .350
00058 
00059 void
00060 FloatingText::draw(DrawingContext& context)
00061 {
00062   // make an alpha animation when disappearing
00063   int alpha;
00064   if(timer.get_timeleft() < FADING_TIME)
00065     alpha = int(timer.get_timeleft() * 255 / FADING_TIME);
00066   else
00067     alpha = 255;
00068 
00069   context.push_transform();
00070   context.set_alpha(alpha);
00071 
00072   context.draw_text(Resources::normal_font, text, position, ALIGN_LEFT, LAYER_OBJECTS+1, FloatingText::text_color);
00073 
00074   context.pop_transform();
00075 }
00076 
00077 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1