'-- Program: Stars 
'-- Author: Paul Overy 
'
'-- This program uses large values, which are scaled down to fit   
'-- the screen replacing the need to carry out floating point calculations.    
'
'-- Unlike the Popcorn, this demo does not make full use of all the  
'-- Powerbob commands (ie. Plsr), it can still be sped up.       
'  
'Bank 14 - Hold large logo.
'Bank 15 - Holds gfx balls.  
'
Hide 
'
'-- Number of stars/balls
Repeat : Input "Number of balls max(64) ";N : Until N>0 and N<65
'
Dim SPEED(N),G(N),R(N),T(N),MX(N),MY(N)
Dim TEMP_MX(N),TEMP_MY(N)
Dim X(N),Y(N),C(N),OX(N),OY(N),S_RAN(64),TIME(64)
'
'--------------------------------------- 
Load "PowerBobsV1.0:DEMOS/Bob_Stars.Abk"
'--------------------------------------- 
'
'-- This will cause the main program to intialise itself.    
For K=0 To N : C(K)=1 : Next K
'
Proc _GET_BOBS[N]
'
Break Off 
NUL=Execall(-132) : Rem Turn off multitasking
'
Repeat 
   '
   '-- Star reset position counter.   
   Pdec Varptr(C(0)),0 To N
   '
   '-- Star size toggle counter.
   Pdec Varptr(T(0)),0 To N
   '
   ' -- Calculate all new X-axis star positions.  
   Psum Varptr(X(0)),Varptr(MX(0)),0 To N
   '
   ' -- Calculate all new y-axis star positions.  
   Psum Varptr(Y(0)),Varptr(MY(0)),0 To N
   '
   For K=0 To N
      '
      '-- Reset star to centre 
      If C(K)=0
         MX(K)=Rnd(2048)-1024 : Rem     new angle  
         MY(K)=Rnd(2048)-1024 : Rem     "   "  
         X(K)=81920+MX(K)*16 : Rem      new X-axis starting point   
         Y(K)=65535+MY(K)*16 : Rem       "  Y  "      "       " 
         SPEED(K)=Rnd(1024)+1 : Rem     New Speed.   
         C(K)=Rnd(20)+25 : Rem          Time star stay on screen.     
         R(K)=C(K)/16 : Rem             store fade in reset value.     
         T(K)=R(K) : Rem                Reset fade in counter.    
         G(K)=16 : Rem                  Start with a small star.  
      End If 
      '
      TEMP_MX(K)=MX(K)/SPEED(K) : TEMP_MY(K)=MY(K)/SPEED(K)
      '
      '-- Increase the size of a star.   
      If T(K)=-1 Then Dec G(K) : T(K)=R(K)
      '
      '-- Use large numbers & divide them down instead of using floats.
      OX(K)=X(K)/512 : OY(K)=Y(K)/512
      '
   Next K
   '
   '-- Speed up stars.  
   Psum Varptr(MX(0)),Varptr(TEMP_MX(0)),1 To N
   Psum Varptr(MY(0)),Varptr(TEMP_MY(0)),1 To N
   '
   ' -- Place all bobs.   
   Pbob Varptr(OX(0)),Varptr(OY(0)),Varptr(G(0)),1 To N
   '
   ' -- Draw all bobs, and show physical screen.    
   Pbob Update : Screen Swap 
   '
   '--  Faster machines need to wait for a complete screen refresh. 
   '--  Otherwise there would be no point using a buffered screen.
   Wait Vbl 
   '
Until Not Btst(6,$BFE001) : Rem -- Test left mouse 
'
NUL=Execall(-138) : Rem Turn on Multitasking 
Break On 
'  
Procedure _GET_BOBS[_BOBS]
   '
   Unpack 15 To 0 : Rem -- Get graphics from bank  
   '
   N=1 : Rem -- Bob image counter 
   For X=0 To 304 Step 16
      Get Bob N,X,Y+64 To X+16,Y+79 : Inc N
   Next X
   '
   '-- Set up screen buffering to stop flicker  
   '
   Cls 0
   Unpack 14 To 0 : Rem      -- Get logo screen  
   Double Buffer : Rem       -- Screen buffering to stop flicker    
   Autoback 0 : Rem          -- Turn off Amos automatic screen buffer system 
   Bob Update Off : Rem      -- (not really needed)    
   Sprite Update Off : Rem   -- (not really needed)     
   Make Mask : Rem           -- Must do this, or no masks will be created.  
   '                         -- Unlike Amos bobs, for speed this is not 
   '                         -- automatic when bobs are first drawn.
   '
   Reserve Pbobs _BOBS : Rem -- The total amount of bobs being used 
   Pbob Dbuf True : Rem      -- Double buffering to be used on Pbobs   
   '
   For K=1 To _BOBS
      Pbob Height K,15
   Next K
   '
End Proc
