Reputation: 1811
Here is my code:
...
sf::Clock clock;
float time = 0.f;
while(window.isOpen())
{
time = clock.getElapsed().asSeconds();
cout << (int)time << endl;
}
...
This is the output I am getting:
0
0
0
0
1
1
1
1
As you can see from the above output, each second is getting logged multiple times, and I just want it to be displayed once like below:
0
1
2
3
Code from link in comment:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "SFML works!");
sf::Clock clock;
float dt = 0.f;
int lastTime = 0;
int currentTime = 0;
while (window.isOpen())
{
dt = clock.getElapsedTime().asSeconds();
lastTime = (int)dt;
currentTime = lastTime;
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.display();
if(currentTime!=lastTime)
cout << (int)dt << std::endl;
}
return 0;
}
Upvotes: 0
Views: 734
Reputation: 910
You are inside a while, you have a cout inside it! so it will print all the time, and convert each time(milliseconds) to seconds!
for this purpose you need to use a timer or an easier method, save the last time and +it with 1000 milliseconds and check it inside your while condition.
as a pseudo code:
int time = 0;
int last_time = 0;
while(window.isOpen())
{
if(time > last_time){
cout << time << endl;
}
last_time = time;
time = clock.getElapsed().asSeconds();
}
Upvotes: 2