Danilo's PBOSL Hi-res Timer as PB source include
Posted: Wed May 21, 2008 7:00 pm
I hope Danilo won't mind this, but srod did the same with the ToolBarPro library some months ago and he hasn't been assassinated afaik. Libs written in C have the drawback of only being usable in compiled form and at the rate the team is releasing new versions of PB, compiled userlibs have a shelf life of about 3 months.
I'm not sure why the timeBeginPeriod code is there, I can't see the need for it as TimeSetEvent covers all that ground but he's got it in there and this is a faithful translation so in it stays.
I'm not sure why the timeBeginPeriod code is there, I can't see the need for it as TimeSetEvent covers all that ground but he's got it in there and this is a faithful translation so in it stays.
Code: Select all
;===============================================================================
; Program: PBOSL_Timer Clone
; Author: Lloyd Gallant (netmaestro) based on work by Danilo Krahn
; Date: November 26, 2007
; Target Compiler: PureBasic 4.0 and later
; Target OS: Windows XP/Vista
; License: Free, unrestricted, no warranties expressed or implied
;
; This code was originally conceived and written in C by Danilo Krahn
; and released as part of the PureBasic Open Source Libraries collection.
; Its functions are reproduced here in PureBasic sourcecode for use
; as a PB Include file for those who prefer not to use compiled libraries in
; their projects. All functions in this library are fully compatible in name
; and functionality with those in the compiled PBOSL version.
;
; If you have programs that use the compiled PBOSL_Timer library and you would
; prefer a .pbi include alternative, you may safely include this file in your
; projects and all the hi-res timer functions will work as expected.
; (that's an opinion, not a warranty)
;
;===============================================================================
Global timer_res.TIMECAPS
Global Dim timer_handles(16)
Global Dim timer_procedures(16)
timeGetDevCaps_( timer_res, SizeOf(TIMECAPS) )
Procedure Timer_Init()
timeBeginPeriod_( timer_res\wPeriodMin )
For i=1 To 16
timer_handles(i) = 0
timer_procedures(i) = 0
Next
EndProcedure
Procedure GetMinTimerResolution()
ProcedureReturn timer_res\wPeriodMin
EndProcedure
Procedure GetMaxTimerResolution()
ProcedureReturn timer_res\wPeriodMax
EndProcedure
Procedure Timer_CallBack( timer_handle, msg, timer_number, dw1, dw2)
If timer_procedures(timer_number)
CallFunctionFast( timer_procedures(timer_number) )
EndIf
EndProcedure
Procedure StartTimer( timer_number, delay, *timer_proc_address )
If timer_number > 15
ProcedureReturn 0
EndIf
If timer_handles(timer_number)
timeKillEvent_( timer_handles(timer_number) )
EndIf
timer_procedures(timer_number) = *timer_proc_address
timer_handles(timer_number) = timeSetEvent_( delay, 0, @Timer_CallBack(), timer_number, #TIME_PERIODIC )
ProcedureReturn timer_handles(timer_number)
EndProcedure
Procedure EndTimer( timer_number )
If timer_number > 15
ProcedureReturn 0
EndIf
If timer_handles(timer_number)
timeKillEvent_( timer_handles(timer_number) )
ProcedureReturn 1
EndIf
ProcedureReturn 0
EndProcedure
Procedure Timer_End()
For i=1 To 16
If timer_handles(i)
timeKillEvent_(timer_handles(i))
EndIf
Next
timeEndPeriod_( timer_res\wPeriodMin )
EndProcedure