#include "multiput.bas"
#include "fbgfx.bi"

const PI = 3.1415 / 180
const BUB = 300

dim as any ptr BUF,BUFFA,FUNDOAA,FUNDOB
dim as any ptr BUBBLE,PEXE(6,5)
dim as fb.image ptr TMP
dim as integer FPS,NPX,MAXX,MAXY,BUBCNT
dim as integer COUNT,SAX,SAY,FP,SYNC=1
dim as double BBT,TMR,SPD,PP,MAX
dim as string TECLA

FPS = 64:MAX = BUB:NPX = 20:BUBCNT = BUB
MAXX = 640:MAXY = 480

dim as integer AGA(BUB), AGB(BUB)
dim as integer PAN(NPX), PTY(NPX), PPR(NPX)
dim as double PPX(NPX), PPY(NPX), AGS(BUB),PSP(NPX)
dim as double AGX(BUB), AGY(BUB)

randomize timer

for COUNT = 0 to NPX
  PPX(COUNT) = rnd * MAXX
  PPY(COUNT) = rnd * MAXY
  PAN(COUNT) = rnd * 360
  PSP(COUNT) = .5 + rnd * 2
  PTY(COUNT) = cint(rnd * 5)
  PPR(COUNT) = cint(rnd * 5)
next COUNT

for COUNT = 0 to BUB
  AGY(COUNT) = rnd * 380
  AGX(COUNT) = 330 + rnd * 20
  AGS(COUNT) = .5 + rnd * 2
  AGA(COUNT) = cint(rnd * 360)
  AGB(COUNT) = 1 + cint(rnd * 10)
next COUNT

screenres 640,480,8

#macro CreateBitmapImage(FILENAME,IWID,IHEI,DESTINY)
DESTINY = ImageCreate(IWID,IHEI)
bload "scr/"+FILENAME+".bmp",DESTINY
#endmacro

CreateBitmapImage("fundoaa",640,480,FUNDOAA)
CreateBitmapImage("fundob",640,334,FUNDOB)
CreateBitmapImage("bubble",4,4,BUBBLE)

for COUNT = 0 to 5
  CreateBitmapImage( "peixe1"+chr$(COUNT+48),52,56,PEXE(1,5-COUNT))
  CreateBitmapImage( "peixe2"+chr$(COUNT+48),41,64,PEXE(2,5-COUNT))
  CreateBitmapImage( "peixe3"+chr$(COUNT+48),28,47,PEXE(3,5-COUNT))
  CreateBitmapImage( "peixe4"+chr$(COUNT+48),28,54,PEXE(4,5-COUNT))
  CreateBitmapImage( "peixe5"+chr$(COUNT+48),38,77,PEXE(5,5-COUNT))
  CreateBitmapImage( "peixe6"+chr$(COUNT+48),29,55,PEXE(6,5-COUNT))
next COUNT



BBT=timer
do
	
  TMR = timer
  screenlock	
	put(0,0),FUNDOAA,pset
	
  'SPD = (60 / FPS)
  
	for COUNT = 0 to NPX
		if PPR(COUNT) < 2 then			      
      TMP = PEXE(PTY(COUNT)+1,PPR(COUNT))
      SAX = TMP->width shr 1:SAY = TMP->height shr 1
			multiput(0,PPX(COUNT)+SAX,PPY(COUNT)+SAX, TMP,1,1,PAN(COUNT),1)
    end if
  next 
  
	for COUNT = 0 to BUB
    if AGX(COUNT) then
      PP = sin(PI * AGA(COUNT))
      put(AGX(COUNT) + (PP * 10), AGY(COUNT)), BUBBLE,trans
      AGY(COUNT) -= AGS(COUNT) * SPD
      AGA(COUNT) += AGB(COUNT) * SPD
      if cint(rnd * (50/SPD)) = 1 then AGS(COUNT) = .5 + rnd * 2
      if AGA(COUNT) > 360 then AGA(COUNT) = AGA(COUNT) - 360    
      if AGY(COUNT) < -10 then
        AGX(COUNT) = 0        
        BUBCNT -= 1
      end if
    else
      if COUNT <= MAX then
        BUBCNT += 1
        AGY(COUNT) = 380
        AGX(COUNT) = 330 + rnd * 20
        AGS(COUNT) = (.5 + rnd * 2) * (rnd*(MAX/BUBCNT))
        AGA(COUNT) = cint(rnd * 360)
        AGB(COUNT) = 1 + cint(rnd * 10)
      end if
    end if
    
  next COUNT
  
	for COUNT = 0 to NPX
		if PPR(COUNT) = 2 or PPR(COUNT) = 3 then
			TMP = PEXE(PTY(COUNT)+1,PPR(COUNT))
      SAX = TMP->width shr 1:SAY = TMP->height shr 1
			multiput(0,PPX(COUNT)+SAX,PPY(COUNT)+SAX, TMP,1,1,PAN(COUNT),1)            
    end if
  next COUNT
  
	put(0,150),FUNDOB,trans
  
	for COUNT = 0 to NPX
		if PPR(COUNT) > 3 then
			TMP = PEXE(PTY(COUNT)+1,PPR(COUNT))
      SAX = TMP->width shr 1:SAY = TMP->height shr 1
			multiput(0,PPX(COUNT)+SAX,PPY(COUNT)+SAX, TMP,1,1,PAN(COUNT),1)            
    end if
		PPX(COUNT) += ((sin(PI * PAN(COUNT)) * PSP(COUNT)) * SPD)
		PPY(COUNT) -= ((cos(PI * PAN(COUNT)) * PSP(COUNT)) * SPD)
		if PPX(COUNT) > 740 then
			PPX(COUNT) = -100: PAN(COUNT) = rnd * 360
			PTY(COUNT) = cint(rnd * 5)
			PPR(COUNT) = cint(rnd * 5)
    end if
		if PPX(COUNT) < -100 then
			PPX(COUNT) = 740: PAN(COUNT) = rnd * 360
			PTY(COUNT) = cint(rnd * 5)
			PPR(COUNT) = cint(rnd * 5)
    end if
		if PPY(COUNT) > 580 then
			PPY(COUNT) = -100: PAN(COUNT) = rnd * 360
			PTY(COUNT) = cint(rnd * 5)
			PPR(COUNT) = cint(rnd * 5)
    end if
		if PPY(COUNT) < -100 then
			PPY(COUNT) = 580: PAN(COUNT) = rnd * 360
			PTY(COUNT) = cint(rnd * 5)
			PPR(COUNT) = cint(rnd * 5)
    end if
  next COUNT
  
  draw string (0,0),"Fps: " & FPS
	if SYNC = 0 then draw string (0,10), "No sync"
  if SYNC = 1 then draw string (0,10), "Sleep"
  if SYNC = 2 then draw string (0,10), "Vsync"
  if SYNC = 3 then draw string (0,10), "Sleep+Vsync"
  
  screenunlock
  
  if SYNC and 1 then sleep 1
  if SYNC and 2 then screensync
  
  TMR = timer-TMR
  SPD = (TMR/(1/60))
  FP += 1
  if MAX > 50 then
    MAX -= SPD*10
    if MAX < 50 then MAX = 50
  end if
  
  if timer-BBT >= 1 then    
    if int(rnd*5) = 3 then MAX = 300
    FPS = FP:FP = 0
    BBT = timer
  end if
  
  TECLA = inkey$
  if TECLA = chr$(13) then SYNC = (SYNC+1) and 3    
  
loop until TECLA = chr$(27)
end


