--
--	mrevents.occ -- sdlraster events tester
--	Copyright (C) 2007  University of Kent
--
--	This program is free software; you can redistribute it and/or
--	modify it under the terms of the GNU General Public License as
--	published by the Free Software Foundation, either version 2 of the
--	License, or (at your option) any later version.
--
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--	General Public License for more details.
--
--	You should have received a copy of the GNU General Public License
--	along with this program.  If not, see
--	<http://www.gnu.org/licenses/>.
--

#INCLUDE "course.module"
#INCLUDE "sdlraster.module"

PROC dump.events (CHAN RASTER.EVENT in?, CHAN BYTE out!)
  INITIAL BOOL running IS TRUE:
  WHILE running
    RASTER.EVENT ev:
    SEQ
      in ? ev
      out.string ("Got event: type=", 0, out!)
      out.int (ev[type], 0, out!)
      out.string (" id=", 0, out!)
      out.int (ev[id], 0, out!)
      out.string (" mod=", 0, out!)
      out.int (ev[mod], 0, out!)
      out.string (" x,y=", 0, out!)
      out.int (INT ev[x], 0, out!)
      out ! ','
      out.int (INT ev[y], 0, out!)
      out ! '*n'
      out ! FLUSH
      running := ev[type] <> RASTER.ET.SHUTDOWN
:

PROC draw.rubbish (CHAN RASTER in?, out!)
  RASTER r:
  INITIAL INT seed IS 25091981:
  SEQ
    in ? r
    WHILE (SIZE r) <> 0
      INT c:
      SEQ
        c, seed := random (#1000000, seed)
        raster.clear (r, c)
        SEQ i = 0 FOR 1000
          INT y, x:
          SEQ
            y, seed := random (SIZE r, seed)
            x, seed := random (SIZE r[0], seed)
            r[y][x], seed := random (#1000000, seed)
        out ! r
        in ? r
        TIMER tim:
        INT t:
        SEQ
          tim ? t
          tim ? AFTER t PLUS 500000
    out ! r
:

PROC mrevents (CHAN BYTE out!)
  CHAN RASTER c0, c1:
  CHAN RASTER.EVENT events:
  PAR
    raster.display ("Raster events test", 640, 480, 1, c0?, c1!, events!)
    dump.events (events?, out!)
    draw.rubbish (c1?, c0!)
:
