rem #####################################################
rem #     Program : Swirl
rem # 
rem #      Author : Marc Gale
rem #
rem # Description : Just a little graphical demo
rem #
rem #####################################################

gosub initialise
gosub main_loop
exit

rem #####################################################
rem # Procedure name : main_loop
rem # 
rem #        Details : The main housekeeping routine.
rem #                  This is basically the heart of
rem #                  the program, calling routines 
rem #                  to do the various tasks required
rem #                  by the program.
rem #
rem #####################################################
label main_loop
 aos=0
 repeat
  gosub flip_screen
  clear window

  gosub draw
  gosub cycle

  gosub process_input
 until (and(joypad,16384)>0)
return

rem #####################################################
rem # Procedure name : process_input
rem # 
rem #        Details : This routine deals with the input
rem # 
rem #####################################################
label process_input
 joypad=peek("port1")

 if and(joypad,32)>0 and aos<12 then
  aos=aos+1
 fi

 if and(joypad,128)>0 and aos>-12 then
  aos=aos-1
 fi

return

rem #####################################################
rem # Procedure name : draw
rem # 
rem #        Details : This routine draws the display
rem # 
rem #####################################################
label draw
 ao=360
 for radius=0 to 420 step 20
  radius2=radius+20
  brightness1=radius/2
  brightness2=radius2/2
  red1=brightness1*redval
  green1=brightness1*greenval
  blue1=brightness1*blueval
  red2=brightness2*redval
  green2=brightness2*greenval
  blue2=brightness2*blueval
  setrgb 1,red1,green1,blue1
  setrgb 3,red2,green2,blue2
  for a=1 to numpoints
   angle1=points(a)+ao
   if a=numpoints then
    angle2=points(1)+ao
   else
    angle2=points(a+1)+ao
   fi
   setrgb 2,red1,green1,blue1
   gtriangle cx+cosines(angle1)*radius,cy+sines(angle1)*radius to cx+cosines(angle2)*radius,cy+sines(angle2)*radius to cx+cosines(angle2+aos+1)*radius2,cy+sines(angle2+aos+1)*radius2
   setrgb 2,red2,green2,blue2
   gtriangle cx+cosines(angle1)*radius,cy+sines(angle1)*radius to cx+cosines(angle1+aos+1)*radius2,cy+sines(angle1+aos+1)*radius2 to cx+cosines(angle2+aos+1)*radius2,cy+sines(angle2+aos+1)*radius2
  points(a)=points(a)+1
  if points(a)>360 then points(a)=points(a)-360 fi
  if points(a)<0 then points(a)=points(a)+360 fi
  next a
  ao=ao+aos
 next radius
return

rem #####################################################
rem # Procedure name : flip_screen
rem # 
rem #        Details : This is the screen buffer handling
rem #                  routine.  Call this routine just
rem #                  before you start drawing your 
rem #                  screen.  Oh, you might want to 
rem #                  clear the window just after you've
rem #                  called this routine.
rem # 
rem #####################################################
label flip_screen
 setdispbuf curbuf 
 curbuf=1-curbuf
 setdrawbuf curbuf
return

rem #####################################################
rem # Procedure name : cycle
rem # 
rem #        Details : This routine cycles the colours
rem # 
rem #####################################################
label cycle
 cycle_delay=cycle_delay-1
 if cycle_delay=0 then
  cycle_delay=cycle_reset
  gosub change_colour
 fi

 if redval<newred then
  redval=redval+0.01
 fi

 if redval>newred then
  redval=redval-0.01
 fi

 if greenval<newgreen then
  greenval=greenval+0.01
 fi

 if greenval>newgreen then
  greenval=greenval-0.01
 fi

 if blueval<newblue then
  blueval=blueval+0.01
 fi

 if blueval>newblue then
  blueval=blueval-0.01
 fi
return

rem #####################################################
rem # Procedure name : change_colour
rem # 
rem #        Details : This routine changes the target
rem #                  colour
rem # 
rem #####################################################
label change_colour
 newred=int(ran(100))/100
 newgreen=int(ran(100))/100
 newblue=int(ran(100))/100
return

rem #####################################################
rem # Procedure name : initialise
rem # 
rem #        Details : This routine sets up all of the
rem #                  global variables and arrays.  I've
rem #                  placed it at the end of the code
rem #                  because it's out of the way and 
rem #                  can easily be reached by pressing
rem #                  the CTRL and END keys together.
rem # 
rem #####################################################
label initialise
 sw=640
 sh=512
 cx=sw/2
 cy=sh/2
 curbuf=0
 open window sw,sh

 redval=1
 greenval=0
 blueval=0

 newred=0
 newgreen=0
 newblue=1

 cycle_reset=200
 cycle_delay=cycle_reset

 dim cosines(1080)
 dim sines(1080)

 for angle=0 to 1080
  cosines(angle)=cos(angle*(pi/180))
  sines(angle)=sin(angle*(pi/180))
 next angle

 numpoints=10
 dim points(numpoints)
 for a=1 to numpoints
  points(a)=(360/numpoints)*a
 next a

return







