[bb] alarm.bb by kochOn [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:38

Previous topic - Next topic

BlitzBot

Title : alarm.bb
Author : kochOn
Posted : 1+ years ago

Description : This is a library for all Blitz languages except Blitz Max.

With it you could create multiple alarms and wait for them to be triggered.
There are many functions to help you through the process of making, setting and getting alarm states but also pausing and resuming single alarm or all at a time.

I' ve realized it cause I need to trigger events according to the Sprite Candy Effects timers, but I think it could be useful for most of you.


Code :
Code (blitzbasic) Select
;::::::::::::::::::::::::::::::
;:::::::::: alarm.bb ::::::::::
;::::::::::::::::::::::::::::::
;
; A simple alarm library to
; easily deal with multiple
; timers
;
; by Nicolas ATEK aka kochOn
; 01/03/2007
;
; www.kochonet.com

Const ALARM_TRIGGERED = 0
Const ALARM_ACTIVED   = 1
Const ALARM_PAUSED    = 2
Const ALARM_SECONDS   = 3
Const ALARM_MILLISECS = 4
Const ALARM_INIT      = 5

Type TAlarm
Field set%, time%, active%, overflow%
Field paused%, stime%
End Type

; CreateAlarm%(time% = 0)
;
; time: period in millisecs before alarm starts 'ringing'
;  Setting time parameter empty, lesser or equal To 0 will desactive the alarm
;
; Return a handle of the newly created alarm

Function CreateAlarm%(time% = 0)
Local alarm.TAlarm

alarm = New TAlarm

If time <= 0 Then
alarmset    = 0
alarm ime   = 0
alarmactive = False
Else
alarmset    = time
alarm ime   = MilliSecs() + time
alarmactive = True
EndIf

alarmoverflow = 0
alarmpaused = False
alarmstime  = 0

Return Handle(alarm)
End Function

; SetAlarm(halarm%, time% = 0, overflow% = False)
;
; halarm  : handle of an existing alarm
; time    : period in millisecs before alarm starts 'ringing'
;  Setting time parameter empty, lesser or equal to 0 will desactive the alarm
; overflow: True to keep an accurate alarm when you perpetually set or reset it in a loop  

Function SetAlarm%(halarm%, time% = 0, overflow% = False)
Local alarm.TAlarm

alarm = Object.TAlarm(halarm)
If alarm = Null Then Return -1

If overflow = False Or alarmpaused = True Then alarmoverflow = 0
If time <= 0 Then
alarmset    = 0
alarm ime   = 0
alarmactive = False
Else
alarmset    = time
alarm ime   = MilliSecs() + time + alarmoverflow
alarmactive = True
EndIf
End Function

; ResetAlarm(halarm%, overflow% = False)
;  Reset the alarm to its last time initialized with CreateAlarm or SetAlarm
;
; halarm  : handle of an existing alarm
; overflow: True to keep an accurate alarm when you perpetually set or reset it in a loop  

Function ResetAlarm%(halarm%, overflow% = False)
Local alarm.TAlarm

alarm = Object.TAlarm(halarm)
If alarm = Null Then Return -1

If overflow = False Or alarmpaused = True Then alarmoverflow = 0
If alarmset > 0 Then
alarm ime   = MilliSecs() + alarmset + alarmoverflow
alarmactive = True
EndIf
End Function

; DesactiveAlarm%(halarm%)
;
; halarm: handle of an existing alarm

Function DesactiveAlarm(halarm%)
Local alarm.TAlarm

alarm = Object.TAlarm(halarm)
If alarm = Null Then Return -1

alarmactive = False
alarmpaused = False
alarmstime  = 0
alarm ime   = 0
End Function

; PauseAlarm%(halarm%)
;
; halarm: handle of an existing alarm

Function PauseAlarm%(halarm%)
Local alarm.TAlarm

alarm = Object.TAlarm(halarm)
If alarm = Null Then Return -1

If alarmpaused = True Or alarmactive = False Then Return

alarmpaused = True
alarmstime  = alarm ime - MilliSecs()
End Function

; ResumeAlarm%(halarm%)
;
; halarm: handle of an existing alarm

Function ResumeAlarm%(halarm%)
Local alarm.TAlarm

alarm = Object.TAlarm(halarm)
If alarm = Null Then Return -1

If alarmpaused = False Or alarmactive = False Then Return

alarmpaused = False
alarm ime = MilliSecs() + alarmstime
End Function

; PauseAlarms()

Function PauseAlarms()
Local alarm.TAlarm

For alarm = Each TAlarm
If alarmactive = True Then
If alarmpaused = False Then
alarmpaused = True
alarmstime  = alarm ime - MilliSecs()
EndIf
EndIf
Next
End Function

; ResumeAlarms()

Function ResumeAlarms()
Local alarm.TAlarm

For alarm = Each TAlarm
If alarmactive = True Then
If alarmpaused = True Then
alarmpaused = False
alarm ime = MilliSecs() + alarmstime
EndIf
EndIf
Next
End Function

; GetAlarm%(halarm%, state% = 0)
;
; halarm: handle of an existing alarm
; state : use one of 0:ALARM_TRIGGERED, 1:ALARM_ACTIVED, 2:ALARM_PAUSED, 3:ALARM_SECONDS, 4:ALARM_MILLISECS, 5:ALARM_INIT
;  Default is 0 so you just need to use GetAlarm(halarm) without state parameter to know if an alamr is triggered
;
; Return the value according to the selected state or -1 if the alarm does not exist

Function GetAlarm%(halarm%, state% = 0)
Local alarm.TAlarm

alarm = Object.TAlarm(halarm)
If alarm = Null Then Return -1

Select state
Case ALARM_TRIGGERED:
If alarmactive = True Then
If alarmpaused = False Then
If MilliSecs() >= alarm ime Then
alarmoverflow = alarm ime - MilliSecs()
alarm ime   = 0
alarmactive = False
Return True
EndIf
Else
Return False
EndIf
EndIf
Return False

Case ALARM_ACTIVED:
Return alarmactive

Case ALARM_PAUSED:
Return alarmpaused

Case ALARM_SECONDS:
If alarmactive = True Then
If alarmpaused = True Then
Return alarmstime / 1000
Else
Return (alarm ime - MilliSecs()) / 1000
EndIf
Else
Return 0
EndIf

Case ALARM_MILLISECS:
If alarmactive = True Then
If alarmpaused = True Then
Return alarmstime
Else
Return (alarm ime - MilliSecs())
EndIf
Else
Return 0
EndIf

Case ALARM_INIT:
Return alarmset

End Select
End Function

; DestroyAlarm(halarm%)
;  Destroy the specified alarm
;
; halarm: handle of an existing alarm

Function DestroyAlarm(halarm%)
Local alarm.TAlarm

alarm = Object.TAlarm(halarm)
If alarm = Null Then Return -1

Delete alarm
End Function

; DestroyAlarms()
;  Destroy all previously created alarms

Function DestroyAlarms()
Local alarm.TAlarm

For alarm = Each TAlarm
Delete alarm
Next
End Function

;:::::::::: a little demo ::::::::::

; Graphics 640, 480, 32, 2
; SetBuffer BackBuffer()
;
; ; Create 3 alarms with different values (test with other values)
; Global alarm1 = CreateAlarm(500)
; Global alarm2 = CreateAlarm(1000)
; Global alarm3 = CreateAlarm(1500)
;
; ; for alarms triggered message to be posted an amount of time
; Global alarm1_triggered = CreateAlarm()
; Global alarm2_triggered = CreateAlarm()
; Global alarm3_triggered = CreateAlarm()
;
; While Not KeyDown(1)
; Cls
;
; Color 255, 255, 255
; Text 160, 200, "alarm1:" + GetAlarm(alarm1, ALARM_INIT) + " mil.", True, True
; Text 160, 240, GetAlarm(alarm1, ALARM_MILLISECS), True, True    
; Text 320, 200, "alarm2:" + GetAlarm(alarm2, ALARM_INIT) + " mil.", True, True
; Text 320, 240, GetAlarm(alarm2, ALARM_MILLISECS), True, True    
; Text 480, 200, "alarm3:" + GetAlarm(alarm3, ALARM_INIT) + " mil.", True, True
; Text 480, 240, GetAlarm(alarm3, ALARM_MILLISECS), True, True    
;
; ; If alarms are triggered, reset them to their initial values
; ; and set alarms_triggered for informations posting
; ; ResetAlarm use the overflow parameter to keep accuracy (try False and compare)
; If GetAlarm(alarm1) Then SetAlarm(alarm1_triggered, 250) : ResetAlarm(alarm1, True)
; If GetAlarm(alarm2) Then SetAlarm(alarm2_triggered, 250) : ResetAlarm(alarm2, True)
; If GetAlarm(alarm3) Then SetAlarm(alarm3_triggered, 250) : ResetAlarm(alarm3, True)
;
; Color 255, 0, 0
; ; Check if alarms used to post triggered message are active
; ; and so post informations
; If GetAlarm(alarm1_triggered, ALARM_ACTIVED) Then Text 160, 280, "Triggered", True, True
; If GetAlarm(alarm2_triggered, ALARM_ACTIVED) Then Text 320, 280, "Triggered", True, True
; If GetAlarm(alarm3_triggered, ALARM_ACTIVED) Then Text 480, 280, "Triggered", True, True
;
; ; Necessary to know when alarms will be triggered and so will become inactive
; GetAlarm(alarm1_triggered)  
; GetAlarm(alarm2_triggered)
; GetAlarm(alarm3_triggered)
;
; Color 255, 255, 0
; If GetAlarm(alarm1, ALARM_PAUSED) = False Then
; Text 320, 80, "PRESS SPACE TO PAUSE ALARMS", True, True
; Else
; Text 320, 80, "PRESS SPACE TO RESUME ALARMS", True, True
; EndIf
;
; If KeyHit(57) Then
; If GetAlarm(alarm1, ALARM_PAUSED) Then
; ResumeAlarms()
; Else
; PauseAlarms()
; EndIf
; EndIf
;
; Flip
; Wend


Comments :


Naughty Alien(Posted 1+ years ago)

 ..handy..I'm gonna use this..thanks!