%const %integer Frame Addr = 16_E00000
@Frame Addr %integerarray Frame(0:32767)
@16_E20002 %short Origin Reg
@16_E20001 %byte Colour Reg
@16_E20000 %byte Enable Reg
%control 0
%begin
%include ":l:b:inc:region.imp"
%include ":l:moose:mouse.inc"
%include ":l:ndemo:level1:graphinc.imp"
%include ":l:ndemo:graeme:random.inc"
%include ":l:src:util.imp"

%constantinteger no of maps=13
%bytearray bitmap(1:no of maps,1:64*64)
%integer i,reference,size
%integer x,y,who,speed,calc

%routine wait(%integer msecs)
%integer start

start= realtime
%cycle;%repeatuntil realtime-start >= msecs

%end


%option "-low"
%routine disp(%integer x,y,%bytename line)

%label ok

  *move.l d0,d2
  *lsl.l #7,d1
  *andi.l #16_FFFFFFF0,d0
  *lsr.l #3,d0
  *add.l d0,d1
  *lea   frame,a1
  *lea   0(a1,d1.l),a1
  *move.l a1,a3
  *adda.l #128,a3
  *lea   colourreg,a2
  *andi.l #15,d2
  *eor.l  #15,d2
  *clr.l  d1
  *bset   d2,d1
  *moveq  #63,d3
z:*moveq  #63,d0
a:*move.b (a0)+,(a2)
  *move.w d1,(a1)
  *ror.w #1,d1
  *btst  #15,d1
  *beq   ok
  *lea    2(a1),a1
ok:
  *dbra   d0,a
  *move.l a3,a1
  *adda.l #128,a3
  *bset   d2,d1
  *dbra   d3,z
%end

clear
colour map(i)=i<<10!i<<5!i %for i=0,1,31
access file("ndemo:Graeme:gallery.dat",0,reference,size)
readregion(reference,0,no of maps*64*64,bitmap(1,1))
de access file(reference)
!connect file (":l:graeme:gallery.dat",0,addr(bitmap(1,1)),size)

x=0;y=0;speed=0
calc=0
initialise random
printstring("Speed:")
%cycle
      x=random int(0,623)
      y=random int(0,447)
      who=random int(1,no of maps) %untilnot 7<=who<=8
      disp(x,y,bitmap(who,1))
      i=testsymbol
      newline %and%stopif i=25
      %if i # -1 %start
         %if i=10 %start
            newline
            printstring("Speed:")
            speed=calc
            calc=0
            %continue
         %finish
         %continueif i<'0' %or i>'9'
         printsymbol(i)
         calc=calc*10+i-'0'
         %continue
      %finish
      speed=speed&20000
      wait(speed)
%repeat
%endofprogram
