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
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
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.
; 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)
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
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?