%include ":l:moose:mouse.inc"
%include ":l:src:util.imp"
%include ":l:level1:graphinc.imp"

%begin
%integer c,s,t,k,ns,sym,setting,inc
%constinteger no seg=210
%halfintegerarray cm image(0:511)

%routine Set Up
%integer i
%integer zero = 0
  %on 0 %start
    Print String ("Software requires a graphics system")
    Newlines (2)
    %return
  %finish
  Offset (0,0)
  enable(16_FF)
  Colour (White)
%end

%routine Mix Colour (%byte Col, %integer Red, Green, Blue)
   red=31 %if red>31; green=31 %if green>31; blue=31 %if blue>31
!   write(col, 3); write(red, 3); write(green, 3); write(blue, 3); newline
   cm image(Col)=Red+Green<<5+Blue<<10
   cm image(Col+256)=cm image(col)
%end

%routine makemap
%integer r, g, b, c

r=0; g=0; b=19; c=1

%routine build(%integer ct, dr, dg, db)
   %integer i

   %for i=0,1,ct-1 %cycle
      mix colour(c, r, g, b); c=c+1
      r=r+dr; g=g+dg; b=b+db
   %repeat
%end

mix colour(0,0,0,19)
build(19,0,1,0)
build(19,0,0,-1)
build(19,1,0,0)
build(19,0,-1,0)
build(19,0,0,1)
build(19,-1,0,0)
build(13,1,1,1)
build(19,0,1,0)
build(19,0,0,-1)
build(19,1,0,0)
build(19,0,-1,0)
build(19,0,0,1)
build(19,-1,0,0)
build(13,-1,-1,-1)
mix colour(255,0,0,19)
%end

%routine wait(%integer msec)
   msec = realtime + msec
   %while realtime <= msec %cycle
   %repeat
%end

%bytearray a(0:512*512-1)
%integer i,j
makemap
setup
update colour map(cm image(0))

k=0
%cycle
   printline("Space stops and starts, newline allows speed change")
   printline("Negative speeds rotate map backwards. zero stops")
   prompt("Animation speed (try 45):"); read(s)
   %if s>0 %then inc=1 %else s=-s %and inc=-1
   %cycle; %repeatuntil testsymbol#10
   %stop %if s=0
   %cycle
      update colour map(cm image(k))
      k=(k+inc)&255
      wait(s)
      c=testsymbol
      %if c=' ' %start
         %cycle; %repeatuntil testsymbol=' '
      %elseif c=13
         %exit
      %finish
   %repeat
%repeat
%endofprogram

