-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverlay.cpp
More file actions
60 lines (48 loc) · 1.74 KB
/
Overlay.cpp
File metadata and controls
60 lines (48 loc) · 1.74 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
#include "Overlay.h"
#include <stdexcept>
Overlay::Overlay(const sf::Vector2u& winSize,
const std::string& text,
const std::string& fontPath,
unsigned int charSize,
const std::string& audioPath,
sf::Color maskColor)
: mMask {sf::Vector2f(static_cast<float>(winSize.x),
static_cast<float>(winSize.y))},
mFont {},
mText {mFont, text, charSize},
mBuf {}
{
if (!mFont.openFromFile(fontPath))
throw std::runtime_error("Cannot open font: " + fontPath);
mText.setFont(mFont);
mText.setFillColor(sf::Color::White);
const auto bounds = mText.getLocalBounds();
mText.setOrigin({bounds.size.x * 0.5f,
bounds.size.y * 0.5f});
mText.setPosition({winSize.x * 0.5f,
winSize.y * 0.5f});
mMask.setFillColor(maskColor);
mBuf.loadFromFile(audioPath);
}
void Overlay::play(sf::RenderWindow& window,
float seconds,
const std::function<void()>& drawBelow)
{
if (!window.isOpen()) return;
drawBelow();
window.draw(mMask);
window.draw(mText);
window.display();
sf::Sound sound(mBuf);
sound.play();
sf::Clock timer;
while (timer.getElapsedTime().asSeconds() < seconds && window.isOpen())
{
while (const auto ev = window.pollEvent())
if (ev->is<sf::Event::Closed>()) {
window.close();
return;
}
sf::sleep(sf::milliseconds(10));
}
}