Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Timer based movement using perftimer()

Author
Message
Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 30th Mar 2008 06:06 Edited at: 30th Mar 2008 06:17
I figured out how to make DBPro's perftimer() function cpu independent, so I'm posting it here with my timer based movement module.

Functions
Tbm_init(fps) - initialize the timer based movement system (call with sync rate you would normally use)
Tbm_update() - put this at the top of your main loop, it updates the timer system
Tbm_cut() - If you need to pause the game call this function afterwards to ignore all the time passed since the last tbm_update.

Variables
Tbm.freq - This is the frequency of the precision timer.
Tbm.targetfps - The sync rate you called tbm_init() with
Tbm.realfps - The real sync rate the program is running at
Tbm.boot - The perftimer() value when tbm_init() was called
Tbm.pause - The number of perftimer() cycles spent on pause
Tbm.factor - The value you need to multiply all your movement speeds by to make your program timer based
Tbm.time - The number of perftimer() cycles since you called tbm_init() minus the time spent on pause
Tbm.now - Same as tbm.time, except in milliseconds so it's cpu independent.



Note: You must load Kernel32.dll and define a constant or variable called Kernel32 and set it to the dll number for this to work

Also note, Tbm.pause and Tbm.time sometimes get set off a couple hundred milliseconds when pausing/unpausing. I'm working on it but you probably won't notice anything.

Example:



So hopefully using perftimer() makes the calculations more accurate, what do you guys think? (Let me know if it doesn't work for some reason)

Sasuke
18
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 4th Apr 2008 00:02 Edited at: 4th Apr 2008 00:02
Not to sure what the delay bits about, but the return from Tbm.factor I think it too small, I had to increase the speed to above 50,000 just for the cube to spin a full rotation in under a minute. The delay kept freezing the input, so I had to remove it. The problem is Tbm.now reset to 0 after it's reached 2000 ms, because nextcheck becomes greater then 2000 ms you can't input anymore and the delay is missed. You would have to look at this cause I don't know much about perftimer, but I guess somewhere it loops. Other than that, it looks promising, if you can get it running right, this will be great. Hope thats the kind of response you where looking for.
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 5th Apr 2008 13:57 Edited at: 5th Apr 2008 13:58
It likely loops because the QueryPerformanceCounter()(perftimer uses this) requires you to pass the pointer to a variable using the __int64(double integer) data type, casting it to DWord will make it loop prematurely. Plus the code looks a bit long for what's really required, you only need to store the previous PerfTimer() value, subtract that from the current one and divide the result by the frequency, this will get the seconds passed since the last call, and you can 1/elapsedSeconds to get the FPS.

Login to post a reply

Server time is: 2024-11-25 02:50:21
Your offset time is: 2024-11-25 02:50:21