| 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), |
| 26 | text(text_), |
| 27 | timer() |
| 28 | { |
| 29 | timer.start(.1f); |
| 30 | position.x -= static_cast<float>(text.size()) * 8.0f; |
| 31 | } |
| 32 | |
| 33 | FloatingText::FloatingText(const Vector& pos, int score) : |
| 34 | position(pos), |
| 35 | text(), |
| 36 | timer() |
| 37 | { |
| 38 | timer.start(.1f); |
| 39 | |
| 40 | // turn int into a string |
| 41 | char str[10]; |
| 42 | snprintf(str, 10, "%d" , score); |
| 43 | text = str; |
| 44 | |
| 45 | position.x -= static_cast<float>(text.size()) * 8.0f; |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | FloatingText::update(float dt_sec) |
| 50 | { |
| 51 | position.y -= 1.4f * dt_sec; |
| 52 | |
| 53 | if (timer.check()) |
| 54 | remove_me(); |
| 55 | } |
| 56 | |
| 57 | const float FADING_TIME = .350f; |
| 58 | |
| 59 | void |
| 60 | FloatingText::draw(DrawingContext& context) |
| 61 | { |
| 62 | // make an alpha animation when disappearing |
| 63 | float alpha; |
| 64 | if (timer.get_timeleft() < FADING_TIME) |
| 65 | alpha = timer.get_timeleft() * 255.0f / FADING_TIME; |
| 66 | else |
| 67 | alpha = 255.0f; |
| 68 | |
| 69 | context.push_transform(); |
| 70 | context.set_alpha(alpha); |
| 71 | |
| 72 | context.color().draw_text(Resources::normal_font, text, position, ALIGN_LEFT, LAYER_OBJECTS+1, FloatingText::text_color); |
| 73 | |
| 74 | context.pop_transform(); |
| 75 | } |
| 76 | |
| 77 | /* EOF */ |
| 78 | |