Sign in to follow this  
Ydos

SDL_GetTicks() Problems

Recommended Posts

Ydos    122
We are currently working on an adventure game and were using SDL_GetTicks() to determine the time each line of subtitles is going to be shown. The problem is that the time that the line shows differ from time to time. Sometimes it's to long, sometimes to short. The method were using is as follows:
starttime = SDL_GetTicks();

while (quit==false){

  //Show the text here

  if( (SDL_GetTicks() - starttime) >= 3000){
      //Remove text
  }

}


That should mean that the sub is going to be shown for 3 secs. For some reason that doesn't work. It's never three seconds and it's varying from time to time. Does anyone have an idea of how we could fix the problem? In that case we would be more than grateful.

Share this post


Link to post
Share on other sites
Gage64    1235
Inside the if statement, are you updating the starttime variable accordingly?

Something like:

starttime = SDL_GetTicks();

while (quit==false){

//Show the text here

if( (SDL_GetTicks() - starttime) >= 3000){
starttime = SDL_GetTicks();
//Remove text
}

}


Also, you should create a timer class for this sort of thing. It will make your code less error-prone.

Share this post


Link to post
Share on other sites
Ydos    122
Yeah, we're updating the start time everytime, only forgot to write it in our example.

We actually have a timer class, only right now it works exactly as if we're just doing SDL_GetTicks(). Got any tips on how we can improve it, if thats the reason for the error?

Share this post


Link to post
Share on other sites
rip-off    10979
I have used that technique and have never had problems with it. There is of course a chance that you have some obscure memory corruption bug that writes into the Timer instances memory and corrupts it somehow. However, barring the above I cannot see any problems with the technique as posted.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Is it actually 3 seconds you're measuring? I wouldn't be surprused if SDL_GetTicks() uses GetTickCount() on Windows, which is only accurate to about 25ms. But it should be unnoticible if you're waiting 3000ms...

Share this post


Link to post
Share on other sites
Gage64    1235
Quote:
Original post by Ydos
We actually have a timer class, only right now it works exactly as if we're just doing SDL_GetTicks(). Got any tips on how we can improve it, if thats the reason for the error?


I meant something like this class (you might have to change it a bit to suit your needs). Note that that class acts as a counting timer that can report when a certain amount of time has passed, Not as a general timer that reports the current time.

Share this post


Link to post
Share on other sites
MJP    19790
Quote:
Original post by Evil Steve
Is it actually 3 seconds you're measuring? I wouldn't be surprused if SDL_GetTicks() uses GetTickCount() on Windows, which is only accurate to about 25ms. But it should be unnoticible if you're waiting 3000ms...


*Checks SDL source*

Looks like it uses QueryPerformanceCounter if it's available, so it shouldn't be a problem...


Uint32 SDL_GetTicks(void)
{
DWORD now, ticks;
#ifndef USE_GETTICKCOUNT
LARGE_INTEGER hires_now;
#endif

#ifdef USE_GETTICKCOUNT
now = GetTickCount();
#else
if (hires_timer_available)
{
QueryPerformanceCounter(&hires_now);

hires_now.QuadPart -= hires_start_ticks.QuadPart;
hires_now.QuadPart *= 1000;
hires_now.QuadPart /= hires_ticks_per_second.QuadPart;

return (DWORD)hires_now.QuadPart;
}
else
{
now = timeGetTime();
}
#endif

if ( now < start ) {
ticks = (TIME_WRAP_VALUE-start) + now;
} else {
ticks = (now - start);
}
return(ticks);
}


Share this post


Link to post
Share on other sites
Ydos    122
Thanks everybody for all the help. We managed to solve the problem using our own timer class that's counting frames.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this