Jump to content
  • Advertisement
Sign in to follow this  
andyb716

Game Programming All in One Timer Question.

This topic is 5449 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

There is a timer class in the source code for Game Programming All in one. It's in vs 6. I put the code in my c++ .NET class and the GetDelta() function returns 0. Is there a difference between vs 6 and .NET?
void XTimer::Update(void){

	LARGE_INTEGER kTempTimer;
	time_t iTempTimeDate;

	QueryPerformanceCounter(&kTempTimer);
	m_iDelta.QuadPart = kTempTimer.QuadPart - m_iLastQuery.QuadPart;

	m_iLastQuery.QuadPart = kTempTimer.QuadPart;

	time(&iTempTimeDate);
	m_pkTime = localtime(&iTempTimeDate);

}

float XTimer::GetDelta(void){
	return (float)(m_iDelta.QuadPart) / (float)(m_iFrequency.QuadPart);
}

Share this post


Link to post
Share on other sites
Advertisement
It may look like it returns 0, but it's not actually 0. It's just so small, you can only look at its value if your debugging it or write a function to see its value. I'd suggestion multiplying it by a thousand(to turn it from nano to micro seconds), and then have float = that value, and print to screen to see if its working.

__int64s are very weird, and so is the query performance crap. I got my fixed_time engine running by guessing, intuition, and logic.

Make sure you call Update just once, before you begin calling GetDelta. Heres some simple code to show the Performance* functions in action.


__int64 g_i64Ticks;
__int64 g_i64Frequency;
float g_Time

main()
{
// Must know frequency of computer for accurate Performance Queries.
QueryPerformanceFrequency((LARGE_INTEGER*) &g_i64Frequency)

while(bGame == true)
{
QueryPerformanceCounter((LARGE_INTEGER*) &g_i64Frequency);

g_Time = (float)1000*((__int64)g_i64Ticks/(__int64)g_i64Frequency)

// ... rest of game code ... //
}
}

Share this post


Link to post
Share on other sites
Its seems like that only increments by one a second. The code I had was telling me how many FPS I was getting. But what I really want to do I create a timeout function and after so many milliseconds call the function. So I need to find out how to get milliseconds.

Share this post


Link to post
Share on other sites
Wow only one poster out of 119. I guess you guys are all beginners like I am. I guess I'll have to do some more searching on google.

Share this post


Link to post
Share on other sites
I don't see anything wrong with those two functions. The problem is likely to be how they are being used. For example, if you call XTimer::Update twice in a row, then XTimer::GetDelta might return 0 (depending on the resolution of the timer).

Oh, and I have noticed that my version of MSVC seems to have a problem converting __int64 to float. See if using double instead of float makes a difference.

Also, maybe you forgot to initialize m_iFrequency.

Share this post


Link to post
Share on other sites
I suggest you use doubles instead of floats, since you'll probably need the extra resolution if you're not doing a full 'real'(ie, physics, AI, animation etc) game update between calls.

Floats are only 32 bits, so there is 0 chance that they can properly represent the range of 64 bit integers, so the two numbers would have to be different by at least 2^32 for the difference to show up at all, and even then it is unlikely to work well since floats aren't linear. Doubles, being 64 bits(on 32-bit x86 anyways), are more likely to be able to represent them to a high enough degree of accuracy. Really, you'd want the 80 bit 'long double' if it is available in your compiler.

Share this post


Link to post
Share on other sites
I used doubles and I had to times it by 1000 * 1000 to get a hundredth of one. My question is why does it work in the books code and not in mine. I inited the frequency. I even updated the time when it did. I don't understand.

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!