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

Author Topic: [SFML 2.1] float timer problem  (Read 4312 times)

0 Members and 1 Guest are viewing this topic.

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
[SFML 2.1] float timer problem
« 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;
}
 
« Last Edit: February 07, 2014, 07:17:47 pm by marcin107 »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: [SFML 2.1] float timer problem
« Reply #1 on: February 07, 2014, 07:19:02 pm »
Ever heard of <= operator?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: [SFML 2.1] float timer problem
« Reply #2 on: February 07, 2014, 07:22:28 pm »
But then i'll have to wait 1sec not 0.666667

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: [SFML 2.1] float timer problem
« Reply #3 on: February 07, 2014, 07:25:43 pm »
What kind of math is that .6666666666667 <= 0 ???
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: [SFML 2.1] float timer problem
« Reply #4 on: February 07, 2014, 07:29:32 pm »
okey, so how to make a cooldown for a half second, not for a 1 second?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: [SFML 2.1] float timer problem
« Reply #5 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
   }
}
« Last Edit: February 07, 2014, 07:45:21 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: [SFML 2.1] float timer problem
« Reply #6 on: February 07, 2014, 07:41:11 pm »
lol, it works. ^^
Thanks.