-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfoBar.cpp
76 lines (61 loc) · 2.25 KB
/
InfoBar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "InfoBar.h"
InfoBar::InfoBar(float swidth, float yloc, float height,
std::string font_file)
: screen_width{ swidth }, y_offset{ yloc }, info_height{ height }
{
if (!font.loadFromFile(font_file))
{
std::cerr << "Could not load " << font_file << " font file." << std::endl;
}
// note: the string values of these text boxes is set in main.cpp
// the locations are also set in main.cpp via InfoBar::update_location();
clock_text.setFont(font);
clock_text.setCharacterSize(30);
clock_text.setFillColor(sf::Color(255,255,255));
bomb_text.setFont(font);
bomb_text.setCharacterSize(30);
bomb_text.setFillColor(sf::Color(255,255,255));
mistake_text.setFont(font);
mistake_text.setCharacterSize(30);
mistake_text.setFillColor(sf::Color(255,255,255));
}
void InfoBar::update_text(sf::Clock clock, const Board& m_board)
{
int mistakes_made = m_board.num_mistakes();
std::string m_text = "Mistakes: " + std::to_string(mistakes_made);
mistake_text.setString(m_text);
int bombs_unmarked = m_board.num_bombs() - m_board.num_marked() - mistakes_made;
std::string b_text = "Unmarked: " + std::to_string(bombs_unmarked);
bomb_text.setString(b_text);
sf::Time elapsed = clock.getElapsedTime();
int time = (int)elapsed.asSeconds();
clock_text.setString(std::to_string(time));
}
void InfoBar::update_location()
{
// x spacing first
float clock_width = clock_text.getGlobalBounds().width;
float bomb_width = bomb_text.getGlobalBounds().width;
float mistake_width = mistake_text.getGlobalBounds().width;
float spacing = (screen_width - clock_width - bomb_width - mistake_width) / 4;
// y spacing
float y_set = y_offset + (info_height - bomb_text.getGlobalBounds().height) / 2;
float clock_x = spacing;
clock_text.setPosition(sf::Vector2f(clock_x, y_set));
float bomb_x = 2 * spacing + clock_width;
bomb_text.setPosition(sf::Vector2f(bomb_x, y_set));
float mistake_x = 3 * spacing + clock_width + bomb_width;
mistake_text.setPosition(sf::Vector2f(mistake_x, y_set));
}
void InfoBar::update(sf::Clock clock, const Board& m_board,
sf::RenderTarget& target)
{
update_text(clock, m_board);
update_location();
}
void InfoBar::draw(sf::RenderTarget& target, sf::RenderStates) const
{
target.draw(clock_text);
target.draw(bomb_text);
target.draw(mistake_text);
}