My goal is make a loop, where function 'do_something()' is executed once every 5 seconds. I have code like this:
using namespace sf;
//(...)
Clock clock;
Time time;
//(...)
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
//some events...
}
time = clock.getElapsedTime();
if (time >= seconds(5.f))
{
do_something();
clock.restart();
}
}
This code execute my function in minimum possible time interval. I've tried every possible code options, that came into my mind, and all I achieved exept this, is application crash, or there is no reaction (statement never meet). This example looks so easy, that I don't know what can be wrong. Where I'm making mistake?
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Event.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window({800,600},"timedsfml");
sf::Time time;
sf::Clock clock;
while (window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if (event.type == sf::Event::Closed) window.close();
}
time = clock.getElapsedTime();
if(time >= sf::seconds(5.0f))
{
std::cout << "Five Seconds passed!\n";
clock.restart();
}
window.clear();
window.display();
}
}
I don't know what you are doing wrong, but this works for me(and looks almost exactly like your code)