For some reason when using a modulus operator with sf::Time it doesn't seem to work properly for me
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(100,100,32), "Clock test");
window.setVerticalSyncEnabled(true);
sf::Clock myClock;
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed) window.close();
}
std::cout << myClock.getElapsedTime().asMilliseconds() << std::endl; //miliseconds keep piling up
if(myClock.getElapsedTime().asMilliseconds() % 1000 == 0) //but this doesn't work (It prints "Tick" when elapsed time is 0 and that's that)
{
std::cout << "Tick" << std::endl;
}
window.clear(sf::Color::Black);
window.display();
}
return 0;
}
I'm using SFML 2.1 in Visual Studio 2012
I hoped I could use a single clock for multiple events
You just have to write it slightly differently:
sf::Time lastEvent1;
sf::Time lastEvent2;
...
sf::Time now = clock.getElapsedTime();
if (now - lastEvent1 > sf::milliseconds(300))
{
// trigger event 1...
lastEvent1 = now;
}
if (now - lastEvent2 > sf::seconds(5))
{
// trigger event 2...
lastEvent2 = now;
}
This way you can have any number of events with different periods, all using the same absolute time base.
I hoped I could use a single clock for multiple events
You just have to write it slightly differently:
sf::Time lastEvent1;
sf::Time lastEvent2;
...
sf::Time now = clock.getElapsedTime();
if (now - lastEvent1 > sf::milliseconds(300))
{
// trigger event 1...
lastEvent1 = now;
}
if (now - lastEvent2 > sf::seconds(5))
{
// trigger event 2...
lastEvent2 = now;
}
This way you can have any number of events with different periods, all using the same absolute time base.
Sweet! Certainly less messy than what I had in mind