1// SuperTux
2// Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
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 "supertux/player_status_hud.hpp"
18
19#include <sstream>
20
21#include "supertux/game_object.hpp"
22#include "supertux/player_status.hpp"
23#include "supertux/resources.hpp"
24#include "video/drawing_context.hpp"
25#include "video/surface.hpp"
26
27static const int DISPLAYED_COINS_UNSET = -1;
28
29PlayerStatusHUD::PlayerStatusHUD(PlayerStatus& player_status) :
30 m_player_status(player_status),
31 displayed_coins(DISPLAYED_COINS_UNSET),
32 displayed_coins_frame(0),
33 coin_surface(Surface::from_file("images/engine/hud/coins-0.png"))
34{
35}
36
37void
38PlayerStatusHUD::reset()
39{
40 displayed_coins = DISPLAYED_COINS_UNSET;
41}
42
43void
44PlayerStatusHUD::update(float dt_sec)
45{
46}
47
48void
49PlayerStatusHUD::draw(DrawingContext& context)
50{
51 int player_id = 0;
52
53 if ((displayed_coins == DISPLAYED_COINS_UNSET) ||
54 (std::abs(displayed_coins - m_player_status.coins) > 100)) {
55 displayed_coins = m_player_status.coins;
56 displayed_coins_frame = 0;
57 }
58 if (++displayed_coins_frame > 2) {
59 displayed_coins_frame = 0;
60 if (displayed_coins < m_player_status.coins) displayed_coins++;
61 if (displayed_coins > m_player_status.coins) displayed_coins--;
62 }
63 displayed_coins = std::min(std::max(displayed_coins, 0), m_player_status.get_max_coins());
64
65 std::ostringstream ss;
66 ss << displayed_coins;
67 std::string coins_text = ss.str();
68
69 context.push_transform();
70 context.set_translation(Vector(0, 0));
71
72 if (coin_surface)
73 {
74 context.color().draw_surface(coin_surface,
75 Vector(static_cast<float>(context.get_width()) - BORDER_X - static_cast<float>(coin_surface->get_width()) - Resources::fixed_font->get_text_width(coins_text),
76 BORDER_Y + 1.0f + (Resources::fixed_font->get_text_height(coins_text) + 5) * static_cast<float>(player_id)),
77 LAYER_HUD);
78 }
79
80 context.color().draw_text(Resources::fixed_font,
81 coins_text,
82 Vector(static_cast<float>(context.get_width()) - BORDER_X - Resources::fixed_font->get_text_width(coins_text),
83 BORDER_Y + (Resources::fixed_font->get_text_height(coins_text) + 5.0f) * static_cast<float>(player_id)),
84 ALIGN_LEFT,
85 LAYER_HUD,
86 PlayerStatusHUD::text_color);
87
88 context.pop_transform();
89}
90
91/* EOF */
92