Created
May 6, 2019 18:11
-
-
Save MarioLiebisch/5c26083d96e56da45390675751dc478c to your computer and use it in GitHub Desktop.
Quick and simple timer example using SFML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Graphics.hpp> | |
#include <sstream> | |
#include <iomanip> | |
int main(int argc, char **argv) { | |
sf::RenderWindow window(sf::VideoMode(300, 100), "Timer Test", sf::Style::Default); | |
window.setVerticalSyncEnabled(true); | |
sf::Font font; | |
font.loadFromFile("Arial.ttf"); | |
sf::Text text("00:00:00", font, 64); | |
sf::Clock clock; | |
std::stringstream ss; | |
while (window.isOpen()) { | |
sf::Event event; | |
while (window.pollEvent(event)) { | |
switch (event.type) { | |
case sf::Event::Closed: | |
window.close(); | |
break; | |
} | |
} | |
long time = clock.getElapsedTime().asMilliseconds(); | |
const int milliseconds = time % 1000; | |
time /= 1000; | |
const int seconds = time % 60; | |
time /= 60; | |
const int minutes = time % 60; | |
ss.str(""); | |
ss << std::setw(2) << std::setfill('0') << minutes << ':' << std::setw(2) << seconds << '.' << std::setw(3) << milliseconds; | |
text.setString(ss.str()); | |
window.clear(sf::Color(64, 127, 192)); | |
window.draw(text); | |
window.display(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment