REM ***********************************************
REM Title: Message Queue
REM Author: Phaelax
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
REM Message queue system
REM Coded by: Phaelax (phaelax@hotmail.com)
REM ---------------------------------------
set display mode 800,600,16
sync on
backdrop on
 
REM create array to hold messages
dim messageQueue$(0) as string
REM create array to hold the time the message was created
dim messageTimer(0) as integer
REM max number of messages allowed on screen at one time
GLOBAL _max_messages = 10
 
c = 0
 
do
 
   REM test for adding message to the queue
   if spacekey() and key = 0
      key = 1
      inc c
      addMessage("This is test number "+str$(c))
   endif
 
   if spacekey() = 0 then key = 0
 
 
   REM handle the queue
   handleMessages(0,400,4000)
 
   REM draw some line(show where text starts)
   ink rgb(255,255,255),0
   line 0,410,600,410
 
 
   sync
loop
 
 
 
REM Add a new message onto the queue
function addMessage(s$ as string)
   rem get current number of messages
   count = array count(messageQueue$(0))
   rem if queue is already full, delete oldest message
   rem to make room for the newer message
   if count = _max_messages
      removeMessage()
   endif
   rem add new message to queue
   add to queue messageQueue$(0)
   last = array count(messageQueue$(0))
   messageQueue$(last) = s$
   rem save the time the message was created
   add to queue messageTimer(0)
   messageTimer(last) = timer()
endfunction
 
 
REM Removes oldest message from the queue
function removeMessage()
   remove from queue messageQueue$(0)
   remove from queue messageTimer(0)
endfunction
 
 
REM Called within the main loop. Handles displaying and updating
REM the message queue
REM x, y is position of newest message, with older message displayed above
REM delay is how long in milliseconds to display a message
function handleMessages(x as integer, y as integer, delay as integer)
   rem number of messages
   count = array count(messageQueue$(0))
   rem
   offset = y - count*10
   ink rgb(255,0,0), 0
   rem display messages, newest on bottom, oldest at top
   for i = 1 to count
      text x,offset+i*10,messageQueue$(i)
   next i
 
   rem check how long message has been displayed
   rem remove message from queue if time is up
   for i = 1 to count
      if messageTimer(i) + delay < timer()
         removeMessage()
         dec count
         dec i
      endif
   next i
endfunction