computer speed independent programming

Share your advanced PureBasic knowledge/code with the community.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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
( 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... )
Hatonastick
Enthusiast
Enthusiast
Posts: 149
Joined: Wed Apr 27, 2005 11:50 am
Location: Adelaide, Australia
Contact:

Post 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)
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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
( 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... )
Hatonastick
Enthusiast
Enthusiast
Posts: 149
Joined: Wed Apr 27, 2005 11:50 am
Location: Adelaide, Australia
Contact:

Post by Hatonastick »

Yes I agree with both of your points. :) When my brain starts working again I might have a go at it.
Johan_Haegg
User
User
Posts: 60
Joined: Wed Apr 30, 2003 2:25 pm
Location: Västerås
Contact:

Post 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 ;)
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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?
Post Reply