SFML community forums

Help => System => Topic started by: marcin107 on February 07, 2014, 07:15:05 pm

Title: [SFML 2.1] float timer problem
Post by: marcin107 on February 07, 2014, 07:15:05 pm
Hi guys, a few days before i started programming in SFML and i'm stucked in one moment, exactly in making cooldowns. I wanted to make a basic attack depending on attackspeedplayer variable, but the program clock doesn't want to update more often than 1 sec, so when i run this program i'll get something like this:
0,66666667
-0,3333333
-1,3333333
but the if will be skipped.
Is there any other way to make it properly?

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>

using namespace std;

int main()
{
    float attackspeedplayer = 1.5;
    float attackspeedtotal = 1 / attackspeedplayer;
   
    sf::RenderWindow window( sf::VideoMode( 200, 200 ), "SFML works!" );
    sf::CircleShape shape( 100.f );
    shape.setFillColor( sf::Color::Green );
   
    sf::Clock clock;
    sf::Time elapsed;
    while( window.isOpen() )
    {
        elapsed = clock.getElapsedTime();
        cout << attackspeedtotal - elapsed.asMilliseconds() / 1000 << endl;
        if( attackspeedtotal - elapsed.asMilliseconds() / 1000 == 0 )
        {
            cout << "CD minal!";
        }
        sf::Event event;
        while( window.pollEvent( event ) )
        {
            if( event.type == sf::Event::Closed )
                 window.close();
           
        }
       
        window.clear();
        window.display();
    }
   
    return 0;
}
 
Title: Re: [SFML 2.1] float timer problem
Post by: zsbzsb on February 07, 2014, 07:19:02 pm
Ever heard of <= operator?
Title: Re: [SFML 2.1] float timer problem
Post by: marcin107 on February 07, 2014, 07:22:28 pm
But then i'll have to wait 1sec not 0.666667
Title: Re: [SFML 2.1] float timer problem
Post by: zsbzsb on February 07, 2014, 07:25:43 pm
What kind of math is that .6666666666667 <= 0 ???
Title: Re: [SFML 2.1] float timer problem
Post by: marcin107 on February 07, 2014, 07:29:32 pm
okey, so how to make a cooldown for a half second, not for a 1 second?
Title: Re: [SFML 2.1] float timer problem
Post by: zsbzsb on February 07, 2014, 07:33:13 pm
sf::Time cooldown;
cooldown = sf::seconds(0.5f);
sf::Clock frameclock;
sf::Time elapsedtime;
while (window.isOpen())
{
   elapsedtime += frameclock.restart();
   if (elapsedtime >= cooldown)
   {
      elapsedtime = sf::Time::Zero;
      // cooldown finished
   }
}
Title: Re: [SFML 2.1] float timer problem
Post by: marcin107 on February 07, 2014, 07:41:11 pm
lol, it works. ^^
Thanks.