SFML community forums

Help => General => Topic started by: marianexyx on April 04, 2015, 10:50:21 pm

Title: Time in statement
Post by: marianexyx on April 04, 2015, 10:50:21 pm
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?
Title: Re: Time in statement
Post by: victorlevasseur on April 04, 2015, 10:54:10 pm
Where are the events processed ?
Title: Re: Time in statement
Post by: marianexyx on April 04, 2015, 10:58:42 pm
Where are the events processed ?

eddited in my example code
Title: Re: Time in statement
Post by: SpeCter on April 05, 2015, 12:39:40 am
#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)
Title: Re: Time in statement
Post by: marianexyx on April 05, 2015, 02:18:46 am
Hmmm... then I don't know what I did wrong. I'm gonna make some more tests and checks, and see what else is wrong.

EDIT: Well, my time loop was made good, but problem was in function in that loop. Thought it can't be wrong, because it worked somewhere else, and this time statement was something new for me, so I was sure the problem is in it. Sorry for problem guys  :-[