# SDL_GetTicks() Problems

This topic is 3652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by YdosWe 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 on other sites
Quote:
 Original post by Evil SteveIs 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 on other sites
Thanks everybody for all the help. We managed to solve the problem using our own timer class that's counting frames.