-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathText.cpp
More file actions
66 lines (56 loc) · 1.59 KB
/
Text.cpp
File metadata and controls
66 lines (56 loc) · 1.59 KB
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
#include "Text.h"
#include <cassert>
#include <iomanip>
#include <sstream>
namespace
{
sf::Font font;
bool fontIsInitialized = false;
}
void initializeHUDOverlay(const std::string& resourceDir)
{
const auto fontFile = resourceDir + "/fonts/emulogic.ttf";
if (!font.loadFromFile(fontFile))
{
throw std::runtime_error("Unable to load font from " + fontFile);
}
fontIsInitialized = true;
}
Text::Text(const std::string& content, const sf::Vector2f& position)
{
assert(fontIsInitialized);
mSfText.setFont(font);
mSfText.setString(content);
mSfText.setPosition(position);
mSfText.setFillColor(sf::Color::White);
mSfText.setCharacterSize(8);
}
void Text::updateString(const std::string& newString) {
mSfText.setString(newString);
}
void Text::draw(sf::RenderWindow& window) const
{
window.draw(mSfText);
}
void Text::updatePosition(float deltaX, float deltaY)
{
sf::Vector2f originalPosition = mSfText.getPosition();
mSfText.setPosition(originalPosition.x + deltaX, originalPosition.y + deltaY);
}
Points::Points(size_t numPoints, const sf::Vector2f& position) :
Text(formatPoints(numPoints), position),
mNumPoints(numPoints)
{
}
void Points::addPoints(size_t newPoints) {
mNumPoints += newPoints;
updateString(formatPoints(mNumPoints));
}
std::string Points::formatPoints(size_t numPoints)
{
auto stringValue = std::to_string(numPoints);
assert(stringValue.size() <= 6 && "Too many points to handle");
std::ostringstream str;
str << std::right << std::setw(6) << std::setfill('0') << stringValue;
return str.str();
}