gosub initialise
gosub mainloop
exit

rem ##########################################
label mainloop
 repeat
  gosub flip_screen
  clear window
  gosub draw_stars
  gosub display_scroller
  gosub rotchange
  tt=tt+1
  if tt=tdelay then
   drawm=mod(drawm+1,5)
   tt=0
  fi
 until (1=2)
return

rem ##########################################
label display_scroller
 setrgb 1,255,255,255
 mx=mx-2
 if mx<=-330 then 
  mx=mx+10
  mpos=mpos+1
  if mpos>len(m$) then
   mpos=0
  fi
 fi
 text mx,250,mid$(m$,mpos,65)
return

rem ##########################################
label rotchange
 xr=mod(xr+xrr,360)
 yr=mod(yr+yrr,360)
 xrot=angg(xr)
 yrot=angg(yr)
return

rem ##########################################
label draw_stars
 focus=2000
 setrgb 1,0,0,255
 x=0:y=0:r=200:st=10
 gosub drawball

 for a=1 to numstars step 5
  new curve
  for b=0 to 4
  star=a+b
  xx=starx(star)
  y=stary(star)
  z=starz(star)

  rem x rotation
  yy=y*cosines(xrot)+z*sines(xrot)
  zz=z*cosines(xrot)-y*sines(xrot)

  rem y rotation
  x=xx*cosines(yrot)-zz*sines(yrot)
  z=xx*sines(yrot)+zz*cosines(yrot)

  rem perspective transformation
  x=x/((z/focus)+1)
  yy=yy/((z/focus)+1)

  if z<0 then
   brightness=-z
   sz=brightness/20
   setrgb 1,0,brightness,brightness

   if drawm=0 fill rectangle x,yy to x+sz,yy+sz
   if drawm=1 line to x,yy
   if drawm=2 rect x,yy to x+sz,yy+sz
   if drawm=3 triangle x,yy to x-sz,yy+sz to x+sz,yy+sz
   if drawm=4 then
    line x-sz,yy to x+sz,yy
    line x,yy-sz to x,yy+sz
   fi

  fi
  next b
 next a
return

rem ##########################################
label drawball
 ang=0
 repeat
  xo1=cosines(ang)*r:yo1=sines(ang)*r
  xo2=cosines(ang+st)*r:yo2=sines(ang+st)*r
  gtriangle x,y to x+xo1,y+yo1 to x+xo2,y+yo2
  gtriangle x,y to x-xo1,y+yo1 to x-xo2,y+yo2
  gtriangle x,y to x-xo1,y-yo1 to x-xo2,y-yo2
  gtriangle x,y to x+xo1,y-yo1 to x+xo2,y-yo2
  ang=ang+st
 until (ang>90)
return

rem ##########################################
label flip_screen
 setdispbuf curbuf 
 curbuf=1-curbuf
 setdrawbuf curbuf
return

rem ##########################################
label setup_shapes

 dim shapes(numshapes,numstars,3)

 for a=1 to numstars step 5
 llong=int(ran(360))
 llat=int(ran(360))
  for b=0 to 4
  long=mod(llong+int(ran(12)-6)*3,360)
  lat=mod(llat+int(ran(12)-6)*3,360)
  shapes(1,a+b,1)=int(sin(long*(pi/180))*sin(lat*(pi/180))*200)
  shapes(1,a+b,2)=int(sin(long*(pi/180))*cos(lat*(pi/180))*200)
  shapes(1,a+b,3)=int(cos(long*(pi/180))*200)
  next b
 next a
return

rem ##########################################
label setup_message
 m$=""
 for a=1 to 65:m$=m$+" ":next a
 m$=m$+"Hello, Xalthorn here with another odd looking "
 m$=m$+"demo.  I've called it the odd ball demo.  It was "
 m$=m$+"something that developed from an evenings "
 m$=m$+"messing around with old code. The final effect "
 m$=m$+"looks a bit like things flying around the "
 m$=m$+"atmosphere around a blue planet (well I think it "
 m$=m$+"does).  Anyway, I hope you enjoy watching the "
 m$=m$+"demo.  You might want to have some fun and add "
 m$=m$+"your own drawing effects to the loop."
return

rem ##########################################
label initialise
 numstars=70
 numshapes=1

 gosub setup_message

 dim starx(numstars)
 dim stary(numstars)
 dim starz(numstars)

 dim cosines(360),sines(360),angg(360)
 for angle=0 to 360
  cosines(angle)=cos(angle*(pi/180))
  sines(angle)=sin(angle*(pi/180))
  angg(angle)=180+cosines(angle)*170
 next angle

 gosub setup_shapes

 for a=1 to numstars
  starx(a)=shapes(1,a,1)
  stary(a)=shapes(1,a,2)
  starz(a)=shapes(1,a,3)
 next a

 xr=10:xrr=1
 yr=80:yrr=1
 setrgb 2,0,0,0
 setrgb 3,0,0,0

 mx=-300
 drawm=0
 tt=0:tdelay=500
 open window 640,512
 window origin "cc"
return

