Page 4 of 4
Posted: Thu Jun 09, 2005 12:24 am
by blueznl
in pseudo code (the 0.9 is arbitrary)
Code: Select all
flipbuffers
get hpc_thisframe
if hpc_thisframe > hpc_nextframe
inc safety
elseif hpc_thisframe < hpc_nextframe
dec safety
endif
ticks = hpc_thisframe - hpc_previousframe
hpc_nextframe = hpc_thisframe + ticks
hpc_previousframe = hpc_thisframe
<< graphics code >>
get hpc_now
remainder = hpc_nextframe - hpc_now
m = (remainder - safety) / 1000 * 0.9 * hpfrequency
delay(m)
<< you might delay a little more using delay(0) until hpc_nextframe is reached >>
repeat from top
even regular gettickcount_ may suffice, though its accuracy lacks
Posted: Thu Jun 09, 2005 5:34 am
by Hatonastick
This is what I'm using to keep a consistent speed between different architecture machines. Mind you this is for a console game - I'm not sure that it's that useful for the rest of you. BTW this came from a VB website.
Code: Select all
; This should probably not be 1 for most people out there, but was
; needed for my program because of the macro scripting language
; handling delays in 1 millisecond increments
#TRM_TICKINTERVAL = 1
starttime.l
nextframe.l
; Try to get our timer to run in approx 1 millisecond increments.
; This doesn't seem to work so well on Win XP for some reason.
; Win 2k however responds to it extremely well.
timeBeginPeriod_(1)
; Our main program loop
Repeat
; To do with our brake system
starttime = timeGetTime_()
nextframe = #TRM_TICKINTERVAL + starttime
; Your main code
; The brake system
While timeGetTime_() < nextframe
If timeGetTime_() + 5 < nextframe
Delay(nextframe - (timeGetTime_() + 5))
EndIf
Wend
key$ = Inkey()
Until key$ <> ""
timeEndPeriod_(1)
Posted: Thu Jun 09, 2005 4:30 pm
by blueznl
yeah that would do for most applications, but i don't like the accuracy

which is why i had a look at the high performance counter
next step would be to make the code 'adaptive' ie. it should be smart enough to adjust the safety margin itself
Posted: Fri Jun 10, 2005 5:03 am
by Hatonastick
Yes I agree with both of your points.

When my brain starts working again I might have a go at it.
Posted: Wed Jun 15, 2005 11:39 pm
by Johan_Haegg
One thing i have found during my work with PureBasic and OpenGL (while writing 64kb intros) is that the ordinary way to use delay does not work.
One way is to use StartProgramCounter-CurrentCounter to get the time elapsed. This is however sometimes a pain, so i started using SetTimer_() to get a fixed frequency and then do all the moving in the procedure.
This is probably not needed when using a standard PB window however.
It works, but you dont get to show off your mad skills

Posted: Thu Jun 16, 2005 2:11 am
by Rescator
Just make sure to use a timer callback procedure, if not you end up having to handle WM_TIMER messages, and once again you get limited to system msg queue speeds. (See Windows PSDK for more info)
I assume that a SetTimer actually has millisecond accuracy if a callback is used?
And thus not limited to the 20ms latency or higher that a Delay(1) etc. tend to have?