SetTimer accuracy

Share your advanced PureBasic knowledge/code with the community.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

SetTimer accuracy

Post by wilbert »

Code updated For 5.20+

Does anyone know a way to set an accurate timed callback ?
My testing code below, shows that setTimer isn't accurate and won't work for high speeds. :?

Code: Select all

Global startTimer, myTimer, frames, fps

fps = 60

hWND = OpenWindow(0, 0, 0, 340, 70, "SetTimer test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)



TextGadget(0, 20, 16, 140, 24, "Animation speed : " + Str(fps) + " fps")
TrackBarGadget(1, 20, 40, 300, 25, 10, 200)
SetGadgetState(1, fps)

Procedure TimerProc(hwnd,uMsg,idEvent,dwTime)
  frames + 1
  SetGadgetText(0,StrF(frames/((dwTime - startTimer)/1000)) + " FPS / " + Str(fps) + " FPS")
EndProcedure   

myTimer = SetTimer_(0, 0, 1000 / fps, @TimerProc() )
frames = 0
startTimer = GetTickCount_()

Repeat
  EventID = WaitWindowEvent()   
  If EventID = #PB_Event_Gadget
    Select EventGadget()
      Case 1
        fps = GetGadgetState(1)
        KillTimer_(0, myTimer)
        myTimer = SetTimer_(0, 0, Int(1000 / fps), @TimerProc() )
        frames = 0
        startTimer = GetTickCount_()
    EndSelect
  EndIf
Until EventID = #PB_Event_CloseWindow

KillTimer_(0, myTimer)

End
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

Wilbert:
You can try the high resolution timer procedures StartTimer() and
EndTimer(), part of Danilo's PureTools.
Post Reply