Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Time in statement  (Read 1788 times)

0 Members and 1 Guest are viewing this topic.

marianexyx

  • Newbie
  • *
  • Posts: 11
    • View Profile
Time in statement
« 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?
« Last Edit: April 04, 2015, 10:58:24 pm by marianexyx »

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Time in statement
« Reply #1 on: April 04, 2015, 10:54:10 pm »
Where are the events processed ?

marianexyx

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Time in statement
« Reply #2 on: April 04, 2015, 10:58:42 pm »
Where are the events processed ?

eddited in my example code

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: Time in statement
« Reply #3 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)

marianexyx

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Time in statement
« Reply #4 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  :-[
« Last Edit: April 05, 2015, 12:33:39 pm by marianexyx »

 

anything