#include <ctime>
#include <limits>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <string>
template <typename T>
T random(T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
{
static bool seeded = false;
if (!(seeded)) {
srand((unsigned)time(0));
seeded = true;
}
return T(((rand() % max) - min) + 1);
}
std::string replace_all(std::string x, const std::string& s1, std::string y)
{
std::string s2 = s1;
size_t pos = 0;
while ((pos = s2.find(x)) != std::string::npos)
s2.replace(pos, x.size(), y);
return s2;
}
int main()
{
const static std::string lyrics[] = {
"Hap-py- birth-day- to- me,-",
"Hap-py- birth-day- to- me.-",
"Hap-py- birth-day- to- Chris-name,-",
"Hap-py- birth-day- to- me.-",
};
const static size_t nlines = 4;
size_t i = 0, line = 0;
sf::RenderWindow window(sf::VideoMode(640, 480),
"Happy birthday to ME :)");
sf::Font font;
sf::Clock clock;
sf::String normal;
sf::String hlight;
font.LoadFromFile("font.ttf");
normal.SetFont(font);
hlight.SetFont(font);
normal.SetColor(sf::Color(255, 255, 255));
hlight.SetColor(sf::Color(255, 255, 0));
clock.Reset();
while (window.IsOpened()) {
sf::Event event;
while (window.GetEvent(event)) {
switch (event.Type) {
case sf::Event::Closed:
window.Close();
break;
default:
break;
}
}
window.Clear(sf::Color(random<char>(), random<char>(), random<char>()));
if (i < lyrics[line].size()) {
normal.SetText(replace_all("-", lyrics[line], ""));
size_t pos = lyrics[line].find("-", i + 1);
if (pos != std::string::npos) {
i = pos + 1;
hlight.SetText(replace_all("-", lyrics[line].substr(0, i), ""));
}
window.Draw(normal);
window.Draw(hlight);
} else {
i = 0;
line = ((line < (nlines - 1)) ? line + 1 : 0);
}
window.Display();
while (clock.GetElapsedTime() < 0.275f)
;
clock.Reset();
}
return 0;
}