Graphical timer in SDL

Hello,

I need to implement a graphical timer which is using
SDL rendering for output of numbers on the screen
(using .ttf font),I am wondering does anyone have
one of these ready made for use in games? :slight_smile: some code
would be appreciated,if not some good idea at least on how to
do this.

dekyco

In the other words, I am confused about this:

how can I convert miliseconds from SDL_GetTick() to seconds
in order to decrement some (int) variable.This variable is to
be used in countdown timer.

dekyco

dekyco wrote:

In the other words, I am confused about this:

how can I convert miliseconds from SDL_GetTick() to seconds
in order to decrement some (int) variable.This variable is to
be used in countdown timer.

You can’t divide immediately if you end up rounding to ints, so
sticking to milliseconds might be better. What does your code look
like?

I often stick to a (start_time - end_time) kind of scheme. Seconds
is a pretty large and coarse unit for most SDL apps. What are the
precision or latency requirements?–
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia

Could be very usefull for race game or puzzle parts. I’m very
interested too.On 06/04/2010, at 13:15, KHMan wrote:

dekyco wrote:

In the other words, I am confused about this:
how can I convert miliseconds from SDL_GetTick() to seconds
in order to decrement some (int) variable.This variable is to
be used in countdown timer.

You can’t divide immediately if you end up rounding to ints, so
sticking to milliseconds might be better. What does your code look
like?

I often stick to a (start_time - end_time) kind of scheme. Seconds
is a pretty large and coarse unit for most SDL apps. What are the
precision or latency requirements?

–
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

So basically what you want is a coutdown timer on-screen?

I’ve got one already implemented, but it’s in Delphi. Here’s the basic idea, though:

Create a countdown timer object. It should contain a number of milliseconds it needs to count down, a timestamp for the last time it was updated, logic to compute a visual representation (convert “130,000 miliseconds” to “2:10”), code to output the visual representation to the screen, an Active flag and an Update method.

When you call Update, if the Active flag is set, get a new timestamp with SDL_GetTicks(), compare it against the last timestamp, subtract the difference from the “time remaining”, replace last timestamp with new timestamp, and recalculate the display value, updating the screen if you’ve passed to a different second. Once you have this working, all you have to do is put a call to Update in your game’s main frame loop.________________________________
From: xvlaja@beotel.net (dekyco)
To: sdl at lists.libsdl.org
Sent: Tue, April 6, 2010 3:59:34 AM
Subject: Re: [SDL] Graphical timer in SDL

In the other words, I am confused about this:

how can I convert miliseconds from SDL_GetTick() to seconds
in order to decrement some (int) variable.This variable is to
be used in countdown timer.

dekyco

I have this code. It’s not perfect, it’s only a start.
Works in my Mac.

If you optimize the code please send me the code. I will use a
graphical timer too.On 06/04/2010, at 14:13, Mason Wheeler wrote:

So basically what you want is a coutdown timer on-screen?

I’ve got one already implemented, but it’s in Delphi. Here’s the
basic idea, though:

Create a countdown timer object. It should contain a number of
milliseconds it needs to count down, a timestamp for the last time
it was updated, logic to compute a visual representation (convert
"130,000 miliseconds" to “2:10”), code to output the visual
representation to the screen, an Active flag and an Update method.

When you call Update, if the Active flag is set, get a new timestamp
with SDL_GetTicks(), compare it against the last timestamp, subtract
the difference from the “time remaining”, replace last timestamp
with new timestamp, and recalculate the display value, updating the
screen if you’ve passed to a different second. Once you have this
working, all you have to do is put a call to Update in your game’s
main frame loop.

From: dekyco
To: sdl at lists.libsdl.org
Sent: Tue, April 6, 2010 3:59:34 AM
Subject: Re: [SDL] Graphical timer in SDL

In the other words, I am confused about this:

how can I convert miliseconds from SDL_GetTick() to seconds
in order to decrement some (int) variable.This variable is to
be used in countdown timer.

dekyco


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

-------------- next part --------------
A non-text attachment was scrubbed…
Name: timer.cpp
Type: application/octet-stream
Size: 8486 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20100406/ee34b1ec/attachment.obj

So basically what you want is a coutdown timer on-screen?

Yes that is correct.

I will be back on SDL_GetTicks() and milliseconds but I wanted to hear from you
people what do you think about this alternative approach without SDL_GetTicks()
using things from the “time.h” header,it is probably too complicated and I am not
sure about that CLOCKS_PER_SECOND constant (mine is 500000).
“The CLOCKS_PER_SEC macro tells you the rate, in Hertz, at which the free-running
counter is incremented. For example, if CLOCKS_PER_SEC is 1000 then each change
in the value returned by clock() represents 1mS.”

Code:

#include <time.h>

from this header:
"/* Time used by the program so far (user time + system time).
The result / CLOCKS_PER_SECOND is program time in seconds. */
extern clock_t clock (void) __THROW;"

Now my code:

Level class members of clock_t type:

Code:

clock_t m_Start, m_Stop;

In the constructor of the Level class:

Code:

m_Start = clock();

In the Level::logic():

Code:

if (!m_TimerStop)
{
m_Stop = clock () / 500000 ; // clock() / CLOCKS_PER_SECOND (?)
sprintf(m_TimerStr, “%d” , 15 - m_Stop ); // 15 seconds countdown
m_Timer->text(m_TimerStr); // m_Timer graphical object showing time
}

if ( atoi(m_TimerStr) == 0 ) //countdown completed
{
m_TimerStop = true;
m_GameOver = true;
}

In the Level::render():

Code:

if ( m_GameOver )
{
//from SDL_Gfx
stringRGBA(screen, 25, 20, “Game Over!”, 255, 0 ,255, 255);
}

this works but does it make sense using clock() like this?
Especially not sure about that CLOCKS_PER_SECOND thing. Value of 500000 gives
approx. 1 second on my system (linux) but what about other processors
or windows port? Precision is not crucial here, just a raw timer that gives
something like a second.

dekyco

Just to close this thread, if someone is still interested.
I have found Timer class at Lazyfoo SDL tutorial and made my
graphical TimerText class and used them like this:

Code:

In the Level constructor:

m_TimerText = new TimerText()

m_Timer = new Timer();
m_Timer->Start();
m_Then = m_Timer->GetTicks(); // it is just SDL_GetTicks() packaged in the class

In the Level::logic():

Code:

if (!m_TimerStop)
{
    m_Now = m_Timer->get_ticks();
    sprintf( m_SecStr, "%d", 10 - ((m_Now - m_Then) / 1000));
    m_TimerText->text(m_SecStr);
}

if ( atoi(m_SecStr) == 0 )
{
    m_TimerStop = true;
    m_GameOver = true;
    m_TimerText->text("Game Over!");
}

In the Level::render():

Code:

m_TimerText->Show();

dekyco