"Timer" Commands?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hitman.

There's a HiResTimer library in the PB drawer, but I can't find the related documents or command reference, do anyone knows how to use it......

Thanks very much.

Well, everyone needs to do something for living.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

The HighResTimer library is in "UserLibraries",
so its not supported by Frederic.

I will release it with official HelpFile later,
but this should help you:

Code: Select all

[b]StartTimer( #Timer, MilliSec, @proc() )[/b]
      #Timer  = Timer-Number (0-15)
     MilliSec = Milliseconds
      @proc() = your Procedure


Starts a Timer. The procedure @proc() is called
every MilliSec.
The Timer-Procedure cant have any Parameters
and runs in its own thread (so make needed
variables SHARED in the procedure).


[b]EndTimer( #Timer )[/b]
      #Timer  = Timer-Number (0-15)


Ends the Timer.


[b]x = GetMinTimerResolution()[/b]


Get the minimal timer resolution.
The minimal Timer resolution is different
on Win9x and WinNT/2k.
On NT based Systems (NT/2k, dunno about XP)
it should be 1 millisecond. On Win9x based
system this value can be different.


[b]x = GetMaxTimerResolution()[/b]


Get the maximal Timer Resolution.
The max resolution is different on misc.
Windows systems.
Values up to 1 Minute (60.000ms) should be
OK on all systems.

Example 1:

Code: Select all

;--------------------
;--------------------
Procedure mytimer1()
Shared hoehe
If hoehe = 1000: hoehe = 100: EndIf
   beep_(hoehe,100)
hoehe+100
EndProcedure


Procedure mytimer2()
   beep_(1800,5)
EndProcedure


Procedure mytimer3()
   beep_(1800,100)
EndProcedure


Procedure mytimer4()
   beep_(800,100)
EndProcedure


hWnd = OpenWindow(0, (GetSystemMetrics_(#SM_CXSCREEN)-200)/2,(GetSystemMetrics_(#SM_CYSCREEN)-277)/2, 400, 360, #PB_Window_SystemMenu|#PB_Window_SizeGadget, "High Resolution Timer by Danilo")


StartTimer(0, 1000, @mytimer1())
StartTimer(1,  200, @mytimer2())
StartTimer(2, 2000, @mytimer3())
StartTimer(3,  600, @mytimer4())


  Repeat
         Select WaitWindowEvent()
              ; IF LeftMouseButton pressed...
              Case #WM_LBUTTONDOWN
                   EndTimer(1): EndTimer(2): EndTimer(3)
              ; pressed CloseButton or ALT+F4 ??
              Case #PB_EventCloseWindow
                   End
         EndSelect
  ForEver
;--------------------
;--------------------


Example 2:

Code: Select all

;--------------------
;--------------------
Global myText$
Global hWnd.l
Global TextLength.l
#ScrollSpeed_in_Milliseconds = 70


myText$ = "     This is a little Scroller-Test with the Timer by Danilo     "
TextLength = Len(myText$)


Procedure Scroller()
;Shared Scroller_a
Shared Scroller_Position
Shared Scroller_Direction

If Scroller_Position > 1


Procedure Timer()
Shared y, size, flag, color
  y+#ScreenHeight/200 : If y > #ScreenHeight : y=0 : EndIf
  color = (size<<3)-1
  If size < 32 And flag = 0
    size+1
  Else
    flag = 1 : size-1 : If size = 0 : flag = 0 : EndIf
  EndIf
EndProcedure


;DirectX init
If InitSprite() = 0 Or InitKeyboard() = 0
   MessageRequester("ERROR","Can't initialize DirectX 7 or later", 0):End
EndIf


If OpenScreen(#ScreenWidth,#ScreenHeight,#ScreenBits,"Timer Test") = 0
   MessageRequester("ERROR","Can´t open DirectX screen",0):End
EndIf


; Start the Timer-Procedure
StartTimer(0,15,@Timer())


  ; Wait for Escape-Key
  Repeat
     ExamineKeyboard()
     FlipBuffers()
     If IsScreenActive()
        ClearScreen(0,0,0)
        StartDrawing(ScreenOutput())
           FrontColor(color,color,0)
           Box(x-size/2,y,size,size) 
        StopDrawing()
     Else
        Delay(200)
     EndIf
  Until KeyboardPushed(#PB_Key_Escape)


; End Program
EndTimer(0)
Delay(20)
End
;--------------------
;--------------------


cya,
...Danilo
(registered PureBasic user)

Edited by - Danilo on 01 July 2002 01:01:15
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hitman.

Thanks, this example is good.

Well, everyone needs to do something for living.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

Danilo,

One question please: What happens if I call StartTimer() on a timer that's already started? I would be calling StartTimer() with identical parameters - the same timer number, milliseconds, and procedure. Will this cause any problems? I know that it doesn't make a lot of sense to do this, but otherwise I have to put in additional logic to determine when to end and restart the timer.

Thanks,
Eric
The HighResTimer library is in "UserLibraries",
so its not supported by Frederic.

I will release it with official HelpFile later,
but this should help you:



cya,
...Danilo
(registered PureBasic user)

Edited by - Danilo on 01 July 2002 01:01:15
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

If you call with the same timer-number again,
the old timer is closed and the new timer initialized.

Code: Select all

StartTimer( 1, 1000, @mytest() )  ; start a timer with speed 1000ms
StartTimer( 1,  500, @mytest() )  ; now the same timer (virtual) with more speed
StartTimer( 1,  500, @cool()   )  ; still 500ms, but another timer-procedure
So you can use the same Timer number again and again -
the old timer is closed automatically.

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

Danilo,

Perfect! Thanks for your quick reply.

Eric
If you call with the same timer-number again,
the old timer is closed and the new timer initialized.

Code: Select all

StartTimer( 1, 1000, @mytest() )  ; start a timer with speed 1000ms
StartTimer( 1,  500, @mytest() )  ; now the same timer (virtual) with more speed
StartTimer( 1,  500, @cool()   )  ; still 500ms, but another timer-procedure
So you can use the same Timer number again and again -
the old timer is closed automatically.

cya,
...Danilo

(registered PureBasic user)
Post Reply