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

Author Topic: How to do a count-down timer  (Read 11127 times)

0 Members and 3 Guests are viewing this topic.

Cccake

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to do a count-down timer
« on: November 13, 2010, 08:37:09 pm »
Hi, I'm new to SFML and I've been trying to make a timer using the clock that's built into SFML with a while loop, I also tried using a for loop and Sleep(), but neither seem to work, when I looked at it in debug mode it seems that the main loop keeps running while the timer loop is executing, and I don't see why that should be happening. I've been trying to get this working for a while now and I just can't think straight, is there a simple way to do this?
I'm making a Pong game and just trying to add a pause/count-down between someone scoring and the ball respawning in the middle, I'd appreciate any help.

Svenstaro

  • Full Member
  • ***
  • Posts: 222
    • View Profile
How to do a count-down timer
« Reply #1 on: November 13, 2010, 09:30:58 pm »
In your main loop, get the frame time each loop and subtract it from your countdown. When countdown becomes < 0.f, do something.

Canadadry

  • Hero Member
  • *****
  • Posts: 1081
    • View Profile
How to do a count-down timer
« Reply #2 on: November 13, 2010, 10:21:31 pm »
For the pause feature there an clock class in the wiki. And for the timer you just have to check if your time clock is superior of the time you want

Cccake

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to do a count-down timer
« Reply #3 on: November 14, 2010, 12:45:03 am »
Thanks for the replies, but I've already tried it like that, maybe I should have been a little more clear sorry, what I mean is that when I start the count down loop the main loop continues running in the background so that even while the countdown is taking place the game is running, what I want to do is count down and then start the game.

Svenstaro

  • Full Member
  • ***
  • Posts: 222
    • View Profile
How to do a count-down timer
« Reply #4 on: November 14, 2010, 01:04:33 am »
Like I said:

Somewhere:

Code: [Select]
float count_le_down = 10.f // set countdown to 10 seconds

Code: [Select]
while(they_see_me_looping) {
    count_le_down -= YourPunyRenderWindow.GetFrameTime();
    if(count_le_down <= 0.f) {
        std::cout << "lol, the count down counted down!" << std::endl;
    }
   
    // your main game stuff here
}

Cccake

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to do a count-down timer
« Reply #5 on: November 14, 2010, 01:43:28 am »
I must be doing something badly wrong because here's what I have:
Code: [Select]

while(!(CountDown<=0.f))
{
CountDown-=PongW.GetFrameTime();
PlayerScored.SetText(""+(int)CountDown);
PongW.Draw(PlayerScored);
}

and it doesn't seem to do anything...I mean if I step through it in debug mode it does count down, but it seems to do it so fast that it seems like nothing actually happens, also the text doesn't display, so what am I doing wrong? I feel really stupid now :P

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
How to do a count-down timer
« Reply #6 on: November 14, 2010, 04:50:07 am »
Quote from: "Cccake"

   PlayerScored.SetText(""+(int)CountDown);


This does not do what you think.

http://cplusplus.com/articles/numb_to_text/

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
How to do a count-down timer
« Reply #7 on: November 14, 2010, 09:37:03 am »
You need to call Display()

Disch+1
SFML / OS X developer

Cccake

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to do a count-down timer
« Reply #8 on: November 14, 2010, 03:13:53 pm »
Quote from: "Disch"
Quote from: "Cccake"

   PlayerScored.SetText(""+(int)CountDown);


This does not do what you think.

http://cplusplus.com/articles/numb_to_text/


Ahh, thanks this is good to know! This and calling Display() fixed it, thanks guys!

 

anything