Jump to content
  • Advertisement
Sign in to follow this  
Tamior

Timer problem

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, everybody! :) I have a following question. Why when I use glutGet(GLUT_ELAPSED_TIME) in the main loop of my game it returns one and the same value for some time and then jumps through 16 milliseconds and returns that new value? And my game runs at around 2000 fps. I want to implement fps independent animation but this problem won't allow that because this function for some time tells my program that no time has passed.

Share this post


Link to post
Share on other sites
Advertisement
I tried to use this QueryPerformanceTimer but it saves its value to some LARGE_INTEGER type which I can't transpose to int or long int and this type has all its operations locked, so I can't subtract one value of time from another.

Share this post


Link to post
Share on other sites
LARGE_INTEGER is a struct which has a couple of members, Microsoft's documentation defines it like that:


typedef union _LARGE_INTEGER {
struct {
DWORD LowPart;
LONG HighPart;
};
struct {
DWORD LowPart;
LONG HighPart;
} u;
LONGLONG QuadPart;
} LARGE_INTEGER, *PLARGE_INTEGER;



So if you want to use the numbers of a LARGE_INTEGER, either use the 64 bits value directly with QuadPart, or use both 32 bits with LowPart and HighPart.

Share this post


Link to post
Share on other sites
Something like the following would do very well:

//Initiate Timer Function::nehe.gamedev.net/
void TimerInit(void)
{
memset(&timer, 0, sizeof(timer));
if (!QueryPerformanceFrequency((LARGE_INTEGER *) &timer.frequency))
{
timer.performance_timer = FALSE;
timer.mm_timer_start = timeGetTime();
timer.resolution = 1.0f/1000.0f;
timer.frequency = 1000;
timer.mm_timer_elapsed = timer.mm_timer_start;
}
else
{
QueryPerformanceCounter((LARGE_INTEGER *) &timer.performance_timer_start);
timer.performance_timer = TRUE;
timer.resolution = (float)(((double)1.0f)/((double)timer.frequency));
timer.performance_timer_elapsed = timer.performance_timer_start;
}
}

//Very Precise Get Time Function::nehe.gamedev.net/
float TimerGetTime()
{
__int64 time;
if(timer.performance_timer)
{
QueryPerformanceCounter((LARGE_INTEGER *) &time);
return((float)(time-timer.performance_timer_start)*timer.resolution)*1000.0f;
}
else
{
return((float)(timeGetTime()-timer.mm_timer_start)*timer.resolution)*1000.0f;
}
}

Share this post


Link to post
Share on other sites
Thank you guys. It was very helpful. But still is this glutGet so retarded? I am just trying to make my game platform independent and it seems it's not that simple. Eh...

Share this post


Link to post
Share on other sites
Quote:
Original post by Tamior
Thank you guys. It was very helpful. But still is this glutGet so retarded? I am just trying to make my game platform independent and it seems it's not that simple. Eh...


You can simply write your own time wrapper.


#ifdef _WINDOWS_ // or what is your defined symbol
use QueryPerformanceTimer()
#else
use glut timer
#endif


Share this post


Link to post
Share on other sites
QueryPerformanceTimer is unrealiable and should be not used in an app that u want other ppl to use
have u looked at sdl its platform independant + is better maintained than glut which hasnt been updated this century, the timer seems to be accurate to 1ms, if youre getting over 1000fps u can always average time over a few frames (which u will do anyways for a fps timer as u dont want the numbers jumping around)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!