'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'        The Madstar demo by Shockwave (C) 2001.
'Greets to : Adam, Marc, Christopher, Fryer, Jacob, Mikael
'   And all the other forum users at WWW.Yabasic.co.uk
'   -================================================-
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'## Starfield
'## Variable definitions..
'##

nstar=30
zm=0
xm=1
ym=1
smallest=1
dim xxx(nstar)
dim yyy(nstar)
dim zzz(nstar)
dw=1

'##
'## Set Star positions..
'##
mm=0
for a=1 to nstar
  xxx(a)=-25+ran(50)
  yyy(a)=-25+ran(50)
  zzz(a)=mm
mm=mm+100/nstar
next a
open window 400,400

'## Number of faces in object:
polys=18
'## X and Y screen offset positions to centre object:
xff=330
yff=250

' ## X+Y rotation values!

rotx=0.005
roty=0.005
rtx=0
rty=0

' ## How big do you want it???
size=5

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'      Permanent and temporary storage definition!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

'## Define arrays to store data:

dim x(polys*3)
dim y(polys*3)
dim z(polys*3)

'## Define arrays to hold transformed data:

dim tx(polys*3)
dim ty(polys*3)
dim tz(polys*3)

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Read in the object data and place into storage arrays:
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
b=1
for a=1 to polys
   read x(b),y(b),x(b+1),y(b+1),x(b+2),y(b+2)
   b=b+3
next a

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'                       Main Loop!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

dw=0
label mloop
   setdispbuf dw
   dw= 1-dw
   setdrawbuf dw
   gosub cw
c=peek("port1")
if (and(c,32)>0) roty=roty+.01
if (and(c,64)>0) rotx=rotx-.01
if (and(c,128)>0) roty=roty-.01
if (and(c,16)>0) rotx=rotx+.01
if and(c,16384)>0 then 
rotx=0
roty=0
end if
if rotx>.05 rotx=.05
if rotx<-.05 rotx=-.05
if roty>.05 roty=.05
if roty<-.05 roty=-.05

     gosub dispstars
     gosub transform
     gosub rotate
     gosub draw
rectangle 10,10 to 630,502
setrgb 1,255,255,155
text 95,490,"PROGRAMMED BY *SHOCKWAVE* TRY YOUR JOYPAD!!!"
goto mloop


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'                     Main Loop Ends!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'           Rotate Object and apply perspective:
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

label rotate
'## New angles:
rtx=rtx+rotx
rty=rty+roty
'## For tidyness
crx=cos(rtx)
srx=sin(rtx)
cry=cos(rty)
sry=sin(rty)
'## spin `em!!
b=1
for p=1 to polys
for inner=1 to 3
 nwy= crx*ty(b) + srx*tz(b)
 nwz= crx*tz(b) - srx*ty(b)
 nwx= cry*tx(b) + sry*nwz
 nwz= cry*nwz - sry*tx(b)
'##  Perspective+offset
 nwz=nwz+700
 tx(b)=xff+nwx*1300 /nwz
 ty(b)=yff+nwy*1300 /nwz
 tz(b)=nwz
 b=b+1
next inner
next p
return


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'  Pull Object into temporary storage for manipulation!!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

label transform
b=1
for a=1 to polys
for inner=1 to 3
 tx(b)=x(b)*size
 ty(b)=y(b)*size
 tz(b)=z(b)*size
 b=b+1
next inner
next a
 
return

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' Draw the object held in the temporary array!!!!!!!!!!!!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

label draw

b=1
setrgb 1,105,140,225
for a=1 to polys
  fill triangle tx(b),ty(b) to tx(b+1),ty(b+1) to tx(b+2),ty(b+2)
 b=b+3
next a
return


'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
' 3D Object Description As Numerical Data...
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

label object
' data structure: x,y,z,x,y,z,x,y,z,r,g,b
' fanatix!!


data -39,-9,-25,-15,-24,-8
data -31,-15,-35,15,-28,15
data -38,0,-27,-4,-26,5
data -25,15,-20,-15,-21,18
data -21,-15,-15,15,-18,15
data -25,3,-18,-4,-16,4
data -15,14,-13,-14,-13,17
data -16,-14,-9,15,-5,18
data -11,-7,-8,-10,-6,19
data -3,-12,-5,15,-3,18
data -6,-13,5,15,2,16
data -6,3,-5,-2,4,4
data -2,-10,0,-6,15,-12
data 6,17,10,15,3,-13
data 12,15,20,15,15,-4
data 15,-4,17,-7,10,-8
data 15,-11,35,15,27,13
data 35,-15,29,-13,21,19

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'                    End of routine!!!
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label cw
setrgb 1,50*sin(mm/4),20*sin(mm/7),20*sin(mm/11)
setrgb 2,20*sin(mm/12),20*sin(mm/14),50*sin(mm/16)
setrgb 3,50*sin(mm/14),20*sin(mm/16),20*sin(mm/18)
gtriangle 0,0 to 640,512 to 0,512
gtriangle 0,0 to 640,512 to 640,0
return

label dispstars
mm=mm+.1
setrgb 1,100,100,100
sz=((tz(1)-700)/100)
sx=((tx(1)-330)/200)
sy=((ty(1)-240)/70)
a=smallest
for b=1 to nstar
setrgb 1,b,b*2,10+b*5
xx=xxx(a)*25
yy=yyy(a)*25
xx=xx/zzz(a)*25
yy=yy/zzz(a)*25
   xx=xx+320
   yy=yy+250
zz=10-zzz(a)/14
     fill rectangle xx,yy to xx+zz*5,yy+zz*5
zzz(a)=zzz(a)+sz
xxx(a)=xxx(a)+sx
yyy(a)=yyy(a)+sy
if yyy(a)<-25 yyy(a)=yyy(a)+50
if yyy(a)>25 yyy(a)=yyy(a)-50
if xxx(a)<-25 xxx(a)=xxx(a)+50
if xxx(a)>25 xxx(a)=xxx(a)-50
if zzz(a)<1 then
zzz(a)=zzz(a)+100 
smallest=a
end if
if zzz(a)>100 then
zzz(a)=zzz(a)-100
smallest=a
end if
a=a-1
if a<1 a=nstar
next b
return


