`%%%%%%%%%%%%%%%%%%%%
`   Analogue Clock
`     By OBese87
`%%%%%%%%%%%%%%%%%%%%
 
`set colours
white=rgb(255,255,255)
red=255*65536
yellow=red+255*256
 
`screen setup
hide mouse
sync on
 
`-------------------
` MAIN LOOP
`-------------------
DO
   `%% Control %%
   `adjust alarm
   l=leftkey() : r=rightkey()
   am=am+r-l
 
   `%% Calculate %%
   `bind alarm time
   if am>=60 then am=0 : ah=ah+1
   if am<0 then am=60 : ah=ah-1
   if ah<0 then ah=23
   if ah>=24 then ah=0
   `separate time into hours, minutes & seconds
   h=val(left$(get time$(),2))
   m=val(mid$(get time$(),4)+mid$(get time$(),5))
   s=val(right$(get time$(),2))
   if h*60+m = ah*60+am or h*60+m = ah*30+am then bgcol=red else bgcol=0
 
   `%% Display %%
   `draw frame
   ink white,0
   circle 320,240,200
   center text 320,240+@round(cos(0*30)*-220),"12"
   center text 320+@round(sin(3*30)*210),240,"3"
   center text 320,240+@round(cos(6*30)*-210),"6"
   center text 320+@round(sin(9*30)*210),240,"9"
   `draw time
   @dline( 320,240,320+@round(sin(h*30)*100),240+@round(cos(h*30)*-100) ) : `hours
   @dline( 320,240,320+@round(sin(m*6)*150),240+@round(cos(m*6)*-150) ) : `minutes
   ink red,0
   @dline( 320,240,320+@round(sin(s*6)*180),240+@round(cos(s*6)*-180) ) : `seconds
   ink yellow,0
   @dline( 320,240,320+@round(sin(ah*30+am*.5)*80),240+@round(cos(ah*30+am*.5)*-80) ) : `seconds
   sync
   cls bgcol
   print get time$()
   print ah ; ":" ; am
LOOP
 
`------------------
` FUNCTIONS
`------------------
`Round Float to Integer
FUNCTION @Round(n#)
   dec# = n# - int(n#)
   n = n# + dec#
ENDFUNCTION n
 
`DLine Function by OBese87
FUNCTION @dline(l,t,r,b)
   w = r-l : h = b-t
   if w >= 0 then xstep = 1 else xstep = -1
   if h >= 0 then ystep = 1 else ystep = -1
   w# = ABS(w) : h# = ABS(h)
   if w#=0 then w#=0.1
   if h#=0 then h#=0.1
   xfact# = w#/h#
   yfact# = h#/w#
 
   x = 0 : y = 0
   repeat
      `don't overshoot
      if abs(x+xstep) > abs(w) then xstep = 0
      if abs(y+ystep) > abs(h) then ystep = 0
 
      dot x+l,y+t
      if yfact# > xfact#
         inc y,ystep
         if ABS(x) < ABS(y*xfact#) then inc x,xstep
      else
         inc x,xstep
         if ABS(y) < ABS(x*yfact#) then inc y,ystep
      endif
   until xstep = 0 and ystep = 0
ENDFUNCTION