' %%%%%%%%% LED Demo by Marc Gale (Xalthorn) %%%%%%%%%%%%%
' %%   Written for the 300 line demo competition set    %%
' %%   by Shockwave and held on the Yabasic Bulletin    %%
' %%   Boards (pub51.ezboard.com/byabasicprogramming)   %%
' %%                                                    %%
' %%        Download from www.ps2-yabasic.co.uk         %%
' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

' %%%%%%%%%%%%%%%%% INITIALISATION %%%%%%%%%%%%%%%%%%%%%%%
open window 640,512
lr=1 : REM ----------------------- Set up scroller colours

t$="Xalthorn Presents\n\n   His new Demo"
gosub fadedown : gosub fadeup

t$="A strictly 2D demo created specifically for\n\n"
t$=t$+"   Shockwave's 300 line demo competition"
gosub fadedown : gosub fadeup

dim x1(360),x2(360),y(360) : v=110
for a=0 to 360 : f=pi/180
 x1(a)=sin(a*2*f)*30+55 : x2(a)=585-sin(a*2*f)*30
 y(a)=cos(a*3*(pi/180))*30+280 : next a

read numcol : dim r(numcol),g(numcol),b(numcol)
for a=1 to numcol    : REM --- Read in the picture colours
 read r(a),g(a),b(a) : next a

read numpics : dim pics(40,20,numpics),xfx(800),xfy(800)
for a=1 to numpics   : REM --- Read in the actual pictures
 x=0 : y=0
 repeat              : REM ------- Keep reading until a -1
  read b,c           : REM ----------- b=colour c=how many
  if b>-1 then       : REM --------- Make sure we're valid
   for d=1 to c
    pics(x,y,a)=b
    x=x+1
    if x=40 then     : REM --- Have we gone over the edge?
     x=0 : y=y+1     : REM ------------------ If so, CR/LF
    fi
   next d
  fi
 until (b=-1)
next a : a=1

for x=0 to 39        : REM ----- Set up initial values for
 for y=0 to 19       : REM       the crossfade list
  xfx(a)=x : xfy(a)=y
  a=a+1
 next y
next x

for a=1 to 800       : REM ----- Mix the crossfade list up
 b=int(ran(800))+1   : c=int(ran(800))+1
 x=xfx(b)            : y=xfy(b)
 xfx(b)=xfx(c)       : xfy(b)=xfy(c)
 xfx(c)=x            : xfy(c)=y
next a

for a=0 to 1         : REM -------- set up the two screens
 setdrawbuf a : setdispbuf a

 for r=10 to 1 step -1
  restore logo : read re,gr,bl,ax,ay,avx,avy,dx,dy,dvx,dvy
  repeat
   for t=0 to 1 step 0.05 : REM ------------- Bezier lines
    bx=ax+avx : by=ay+avy
    cx=dx+dvx : cy=dy+dvy
    aa=t      : bb=1-t
    x=ax*bb^3+3*bx*bb^2*aa+(3*cx)*bb*aa^2+dx*aa^3
    y=ay*bb^3+3*by*bb^2*aa+(3*cy)*bb*aa^2+dy*aa^3
    rx=re/10  : gx=gr/10
    bx=bl/10  : setrgb 1,re-r*rx,gr-r*gx,bl-r*bx
    fill circle x,y,r    : REM --- Draw lines with circles
   next t

   read c      : REM --- Continue with the line or restart
   if c=1 then : REM -------------- Continue with this one
    ax=dx   : ay=dy
    avx=dvx : avy=dvy
    read dx,dy,dvx,dvy
   else        : REM --------------------- Start a new one
    read re,gr,bl,ax,ay,avx,avy,dx,dy,dvx,dvy
   fi
  until (dx=-1)
 next r
next a

for a=0 to 1    : REM ----------------- Setup both buffers
 setrgb 0,0,0,0 : setdrawbuf a
 clear fill rect 0,58,640,82: clear fill rect 0,98,640,122

 setrgb 1,100,100,200 : REM -------------- Draw the frames
 setrgb 2,50,50,100   : setrgb 3,50,50,100
 gtriangle 0,70 to 0,60 to 640,60
 gtriangle 0,70 to 0,80 to 640,80
 gtriangle 0,110 to 0,100 to 640,100
 gtriangle 0,110 to 0,120 to 640,120
 setrgb 3,100,100,200
 gtriangle 0,70 to 640,60 to 640,70
 gtriangle 0,70 to 640,80 to 640,70
 gtriangle 0,110 to 640,100 to 640,110
 gtriangle 0,110 to 640,120 to 640,110
 clear fill rect 58,38,582,142
 gtriangle 60,40 to 580,40 to 580,140
 gtriangle 60,40 to 60,140 to 580,140
 clear fill rect 108,168,532,392
 gtriangle 110,170 to 530,170 to 530,390
 gtriangle 110,170 to 110,390 to 530,390
 clear fill rect 70,50,570,130
 clear fill rect 120,180,520,380
next a

dim lt(255,8,8) : REM ------ Prepare the letter data store
repeat
 read lett : if lett=-1 goto alldone
 for a=1 to 8
  read row     : REM ------------------- Grab the row data
  for b=1 to 8 : REM ------------ Break it up into columns
   lt(lett,a,9-b)=and(row,2^b)/2^b
  next b
 next a
until (1=2)    : REM ---------- Keep going until we finish
label alldone  : REM ------------------ Aha! The loop exit

m$="                Here's another demo from Xalthorn, on"
m$=m$+"e without any 3D at all for a change. The demo obv"
m$=m$+"iously centres around the LED scroller, press the "
m$=m$+"buttons on the controller and see what happens. Th"
m$=m$+"e scroll data is created from standard character d"
m$=m$+"ata rather than specially prepared data to show pe"
m$=m$+"ople how it works. The dissolve picture also demon"
m$=m$+"strates some simple but effective code. Oh, and th"
m$=m$+"e text below is drawn with bezier curves. TTFN...."
mlen=len(m$) : dim me(mlen)
melen=mlen*8 : dim me1(melen),me2(melen),me3(melen)
dim me4(melen),me5(melen),me6(melen),me7(melen),me8(melen)

for a=1 to mlen         : REM --- Precalculate the message
 me(a)=asc(mid$(m$,a,1)) : next a

for a=1 to mlen : REM --- *Really* Precaculate the message
 l=me(a)        : REM -------------------- Grab the letter
 for b=1 to 8   : REM ---------------------- Set the LED's
  me1(aa)=50+lt(l,1,b)*155 : me2(aa)=50+lt(l,2,b)*155
  me3(aa)=50+lt(l,3,b)*155 : me4(aa)=50+lt(l,4,b)*155
  me5(aa)=50+lt(l,5,b)*155 : me6(aa)=50+lt(l,6,b)*155
  me7(aa)=50+lt(l,7,b)*155 : me8(aa)=50+lt(l,8,b)*155
  aa=aa+1
 next b
next a

p=1 : s=1
repeat
 joy=peek("port1")
 if and(joy,16384)>0 then : REM -------------------- Cross
  lr=0 : lg=0
  lb=1 : fi
 if and(joy,32768)>0 then : REM ------------------- Square
  lr=1 : lg=0
  lb=1 : fi
 if and(joy,8192)>0 then  : REM ------------------- Circle
  lr=1 : lg=0
  lb=0 : fi
 if and(joy,4096)>0 then  : REM ----------------- Triangle
  lr=0 : lg=1
  lb=0 : fi

 setdispbuf cb : REM -------------------- Double buffering
 cb=1-cb       : setdrawbuf cb
 delay=delay-1 : if delay>0 goto nopic

 REM ------------------- Draw the next part of the picture
 x=xfx(p)      : y=xfy(p)
 c=pics(x,y,s) : setrgb 1,r(c),g(c),b(c)
 xx=x*10+120   : yy=y*10+180
 fill rect xx,yy,xx+9,yy+9
 q=mod(q+1,2)  : if q=0 p=p+1
 if p=801 then
  p=1 : s=s+1
  delay=300 : if s>numpics s=1
 fi
 label nopic

 u=mod(u+1,360)    : v=mod(v+1,360)
 setrgb 1,48,48,98 : fill rect x1(u),y(u),x1(u)+5,y(u)+5
 fill rect x2(u),y(u),x2(u)+5,y(u)+5 : setrgb 1,98,98,200
 fill rect x1(v),y(v),x1(v)+5,y(v)+5
 fill rect x2(v),y(v),x2(v)+5,y(v)+5

 tmpos=mpos : REM ----------- Where are we in the message?
 for x=1 to 50
   xx=x*10+60
   r=me1(tmpos) : setrgb 1,r*lr,r*lg,r*lb
   fill rect xx,50,xx+8,58   : REM ------------ Draw row 1
   r=me2(tmpos) : setrgb 1,r*lr,r*lg,r*lb
   fill rect xx,60,xx+8,68   : REM ------------ Draw row 2
   r=me3(tmpos) : setrgb 1,r*lr,r*lg,r*lb
   fill rect xx,70,xx+8,78   : REM ------------ Draw row 3
   r=me4(tmpos) : setrgb 1,r*lr,r*lg,r*lb
   fill rect xx,80,xx+8,88   : REM ------------ Draw row 4
   r=me5(tmpos) : setrgb 1,r*lr,r*lg,r*lb
   fill rect xx,90,xx+8,98   : REM ------------ Draw row 5
   r=me6(tmpos) : setrgb 1,r*lr,r*lg,r*lb
   fill rect xx,100,xx+8,108 : REM ------------ Draw row 6
   r=me7(tmpos) : setrgb 1,r*lr,r*lg,r*lb
   fill rect xx,110,xx+8,118 : REM ------------ Draw row 7
   r=me8(tmpos) : setrgb 1,r*lr,r*lg,r*lb
   fill rect xx,120,xx+8,128 : REM ------------ Draw row 8
  tmpos=mod(tmpos+1,melen)
 next x
 mpos=mod(mpos+1,melen)
until (1=2)

label fadedown : REM ------------ Fade down to dark colour
 s=252      : e=0
 st=-4      : r=0.2
 g=0.2      : b=0.4
 gosub fade : wait 1
return

label fadeup : REM -------------- Fade up to bright colour
 s=0        : e=252
 st=4       : r=0.2
 g=0.2      : b=0.4
 gosub fade : wait 1
return

label fade : REM -------------------- Do the actual fading
 repeat
  setrgb 0,s*r,s*g,s*b : gosub flipscreen
  clear window         : setrgb 1,255*r,255*g,255*b
  text 320,256,t$,"cc" : s=s+st
 until (s=e)
return

label flipscreen       : REM ------------ Swap the buffers
 setdispbuf cb : cb=1-cb
 setdrawbuf cb : return

data 6,100,100,0,0,100,0,50,50,50,150,150,150,100,40,0,0
data 50,100
data 2,0,58,1,4,0,34,1,8,0,30,1,12,0,27,1,14,0,26,1,14,0
data 25,1,4,0,2,1,4,0,2,1,4,0,24,1,4,0,2,1,4,0,2,1,4,0,23
data 1,18,0,22,1,18,0,22,1,18,0,22,1,3,0,1,1,10,0,1,1,3,0
data 23,1,2,0,2,1,8,0,2,1,2,0,24,1,3,0,2,1,6,0,2,1,3,0,25
data 1,3,0,3,1,2,0,3,1,3,0,26,1,5,0,4,1,5,0,27,1,12,0,30,1
data 8,0,34,1,4,0,58,-1,-1,6,44,4,4,6,33,4,9,6,8,4,6,6,18
data 4,7,6,7,4,11,6,17,4,4,6,12,4,6,6,36,4,2,6,25,5,1,6,27
data 5,1,6,3,5,2,6,6,5,1,6,2,5,1,6,18,5,2,6,4,5,1,6,3,5,2
data 6,1,5,2,6,2,5,3,6,1,5,3,6,10,5,2,6,2,5,5,6,2,5,3,6,1
data 5,16,6,7,5,17,2,18,3,4,2,35,3,6,2,34,3,6,2,33,3,8,2
data 32,3,8,2,31,3,10,2,30,3,10,2,29,3,12,2,28,3,12,2,27,3
data 14,2,13,-1,-1

label logo
data 100,100,200,130,420,-20,-10,60,490,20,15,0,100,100
data 200,60,420,20,-15,130,490,-20,10,0,110,100,200,180
data 465,-40,-10,190,485,-20,30,0,110,100,200,180,460,10
data 10,195,495,-10,-10,0,120,100,200,225,420,0,10,225,495
data 0,-10,0,130,100,200,275,440,0,10,275,495,0,-10,0,130
data 100,200,260,460,10,0,300,460,-10,0,0,140,100,200,330
data 420,0,10,330,495,0,-10,0,140,100,200,330,495,10,-40
data 370,502,10,-40,0,150,100,200,430,482,-5,-20,400,482,5
data -20,0,150,100,200,430,482,-5,20,400,482,5,20,0,160
data 100,200,460,460,0,10,460,495,0,-10,1,490,470,-20,-10
data 0,170,100,200,520,460,0,10,520,495,0,-10,0,170,100
data 200,520,495,10,-40,560,502,10,-40,1,-1,-1,-1,-1
data 32,0,0,0,0,0,0,0,0,39,24,8,0,0,0,0,0,0,44,0,0,0,0,0,0
data 24,8,46,0,0,0,0,0,0,24,0,48,0,60,70,74,82,98,60,0,49
data 0,48,80,16,16,16,124,0,50,0,60,66,2,60,64,126,0,51,0
data 60,66,12,2,66,60,0,52,0,8,24,40,72,126,8,0,53,0,126
data 64,124,2,66,60,0,54,0,60,64,124,66,66,60,0,55,0,126,2
data 4,8,16,16,0,56,0,60,66,60,66,66,60,0,57,0,60,66,66,62
data 2,60,0,58,0,0,0,16,0,16,0,0,65,0,60,66,66,126,66,66,0
data 66,0,124,66,124,66,66,124,0,67,0,60,66,64,64,66,60,0
data 68,0,120,68,66,66,68,120,0,69,0,126,64,124,64,64,126
data 0,70,0,126,64,124,64,64,64,0,71,0,60,66,64,78,66,60,0
data 72,0,66,66,126,66,66,66,0,73,0,62,8,8,8,8,62,0,74,0,2
data 2,2,66,66,60,0,75,0,68,72,112,72,68,66,0,76,0,64,64
data 64,64,64,126,0,77,0,66,102,90,66,66,66,0,78,0,66,98
data 82,74,70,66,0,79,0,60,66,66,66,66,60,0,80,0,124,66,66
data 124,64,64,0,81,0,60,66,66,82,74,60,0,82,0,124,66,66
data 124,68,66,0,83,0,60,64,60,2,66,60,0,84,0,254,16,16,16
data 16,16,0,85,0,66,66,66,66,66,60,0,86,0,66,66,66,66,36
data 24,0,87,0,66,66,66,66,90,36,0,88,0,66,36,24,24,36,66
data 0,89,0,130,68,40,16,16,16,0,90,0,126,4,8,16,32,126,0
data 97,0,0,56,4,60,68,60,0,98,0,32,32,60,34,34,60,0,99,0
data 0,28,32,32,32,28,0,100,0,4,4,60,68,68,60,0,101,0,0,56
data 68,120,64,60,0,102,0,24,32,48,32,32,32,0,103,0,0,60
data 68,68,60,4,56,104,0,64,64,120,68,68,68,0,105,0,16,0
data 48,16,16,56,0,106,0,8,0,8,8,8,72,48,107,0,32,40,48,48
data 40,36,0,108,0,32,32,32,32,32,24,0,109,0,0,104,84,84
data 84,84,0,110,0,0,120,68,68,68,68,0,111,0,0,56,68,68,68
data 56,0,112,0,0,120,68,68,120,64,64,113,0,0,60,68,68,60
data 4,6,114,0,0,28,32,32,32,32,0,115,0,0,56,64,56,4,120,0
data 116,0,16,56,16,16,16,12,0,117,0,0,68,68,68,68,56,0
data 118,0,0,68,68,40,40,16,0,119,0,0,84,84,84,84,40,0,120
data 0,0,68,40,16,40,68,0,121,0,0,68,68,68,60,4,56,122,0,0
data 124,8,16,32,124,0,-1

