TGC Codebase Backup



Alarm Timers by IanM

2nd Jan 2004 18:32
Summary

Provides up to 64 'alarm' timers (they go off when you specify)



Description

Allows up to 64 alarm timers (simple code change to increase this)

Use CreateAlarm() to create a new alarm (you can set the alarm to repeat too)
Use Alarm() to test the status of the alarm. This function will return a 1 when the alarm has gone off
Use DeleteAlarm() to deactivate an alarm. A non-respawning alarm automatically deletes itself when it has gone off.

This code will print Hello once per second

sync on
sync rate 0

CreateAlarm(1, 1000, 1)

do
if Alarm(1) then print "Hello"
sync
loop



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` alarm.dba

` Simple time management routines

remstart
AVAILABLE FUNCTIONS*********************************************************************

CreateAlarm()
   Creates and initialises an alarm

Alarm()
   Tests the alarm to see if it has 'gone off'

DeleteAlarm()
   Deletes the specified alarm

****************************************************************************************
remend

#constant MAX_ALARM  64

type alarm_t
   Active as integer    ` Set to 1 if the timer is active
   Respawn as integer   ` Set to 0 if no respawn, or the timer increment if there is a respawn
   Time as integer      ` Contains the time of the next alarm
endtype

dim Store_Alarm(MAX_ALARM) as alarm_t
global AlarmCreated as integer

function CreateAlarm(AlarmNo as integer, TimeIncrement as integer, Respawn as integer)
   if AlarmCreated = 0
      undim Store_Alarm()
      global dim Store_Alarm(MAX_ALARM) as alarm_t
      AlarmCreated = 1
   endif

   if AlarmNo < 1 or AlarmNo > MAX_ALARM then exitfunction 0
   if Store_Alarm( AlarmNo ).Active <> 0 then exitfunction 0

   Store_Alarm( AlarmNo ).Active = 1
   Store_Alarm( AlarmNo ).Time = timer() + TimeIncrement
   if Respawn = 0
      Store_Alarm( AlarmNo ).Respawn = 0
   else
      Store_Alarm( AlarmNo ).Respawn = TimeIncrement
   endif
endfunction 1

function Alarm(AlarmNo as integer)
   if AlarmNo < 1 or AlarmNo > MAX_ALARM then exitfunction 0
   if Store_Alarm( AlarmNo ).Active = 0 then exitfunction 0

   if Store_Alarm( AlarmNo ).Time > timer() then exitfunction 0

   if Store_Alarm( AlarmNo ).Respawn = 0
      Store_Alarm( AlarmNo ).Active = 0
   else
      Store_Alarm( AlarmNo ).Time = Store_Alarm( AlarmNo).Time + Store_Alarm( AlarmNo ).Respawn
   endif
endfunction 1

function DeleteAlarm(AlarmNo as integer)
   Store_Alarm( AlarmNo ).Active = 0
endfunction