'*************************************************
' FBhge_tut03 - Using helper classes
'*************************************************

#Include  "inc/HgeType.bi"
#Include  "inc/HgeWrapper.bi"

   Dim Shared As long   fnt
	Dim Shared As long   spr1
	Dim Shared As long   spr2
	Dim Shared As long   par
	Dim Shared As long   tex
	Dim Shared As long   snd
	Dim Shared As hgeParticleSystemInfo  psi
	
   Dim shared as single  x=100.0f
   Dim shared as single  y=100.0f
   Dim shared as single  dx=0.0f 
   Dim shared as single  dy=0.0f
   const as single       speed=90
   const as single       friction=0.98
   Dim shared as integer i
	
	Dim Shared As string FPS

Sub Boom()    
   Dim As Integer pan = Cast(Integer,(x-400)/4)
   Dim As Integer pitch = (dx*dx + dy*dy) * 0.0005 + 0.2
   hge_Effect_PlayEx(snd,100,pan,pitch,0)
End Sub
Function  FrameFunc()As  Integer 
	
	Dim As Single dt=hge_Timer_GetDelta()

	''Process keys
	if (hge_Input_GetKeyState(HGEK_ESCAPE))then return 1
	if (hge_Input_GetKeyState(HGEK_LEFT))  Then dx-=speed*dt
	if (hge_Input_GetKeyState(HGEK_RIGHT)) Then dx+=speed*dt
	if (hge_Input_GetKeyState(HGEK_UP))    Then dy-=speed*dt
	if (hge_Input_GetKeyState(HGEK_DOWN))  Then dy+=speed*dt
	   
	'' Do some movement calculations and collision detection	
	dx*=friction: dy*=friction: x+=dx: y+=dy
	if(x>784)then x=784-(x-784):dx=-dx:boom()
	if(x<16) then x=16+16-x:dx=-dx:boom()
	if(y>584)then y=584-(y-584):dy=-dy:boom()
	if(y<16) then y=16+16-y:dy=-dy:boom() 
	
	  
  
   psi.nEmission=int(dx*dx+dy*dy)*2
   hge_PartSys_SetInfo(par,psi)
   hge_PartSys_MoveTo(par,x,y)
   hge_PartSys_Update(par,dt)

	return 0 
End  Function

Function  RenderFunc()As  Integer 
	hge_Gfx_BeginScene()
	hge_Gfx_Clear(0)
	hge_PartSys_Render(par)
	hge_Sprite_Render(spr1,x,y)
	FPS = str(hge_Timer_GetFPS())
	hge_Font_SetColor(fnt,rgb(255,255,255))
   hge_Font_Render(fnt,6, 6, HGETEXT_LEFT, "FPS: "+FPS)
   hge_Font_SetColor(fnt,rgb(90,90,90))
   hge_Font_Render(fnt,5, 5, HGETEXT_LEFT, "FPS: "+FPS)
   hge_Gfx_EndScene()
   return 0
End  Function

   hge_Create(hge_VERSION)
   hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut03.log")
	hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
	hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
	hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 03 - Using helper classes")
	hge_System_SetStateInt(HGE_FPS, 100)
	hge_System_SetStateBool(HGE_WINDOWED, 1)
	hge_System_SetStateInt(HGE_SCREENWIDTH, 800)
	hge_System_SetStateInt(HGE_SCREENHEIGHT, 600)
	hge_System_SetStateInt(HGE_SCREENBPP, 32)
	

	 If (hge_System_Initiate()) Then
        snd = hge_Effect_Load("Res/menu.wav",0)
        tex = hge_Texture_Load("Res/particles.png")
        
        If (snd = 0) Or (tex = 0) Then
            MessageBox(NULL,"Can't load MENU.WAV or PARTICLES.PNG","Error",MB_OK)
            hge_System_Shutdown()
            hge_Release()
        End If
        
      fnt = hge_Font("Res/font1.fnt",0)
        
      spr1 = hge_Sprite(tex, 96, 64, 32, 32)
	   hge_Sprite_SetColor(spr1,&hFFFFA000)
      hge_Sprite_SetHotSpot(spr1,16,16)
    	
      spr2=hge_Sprite(tex, 32, 32, 32, 32)
	   hge_Sprite_SetBlendMode(spr2,BLEND_COLORMUL or BLEND_ALPHAADD or BLEND_NOZWRITE)
	   hge_Sprite_SetHotSpot(spr2,16,16)
	   par = hge_PartSys("Res/trail.psi",spr2)
	   hge_PartSys_GetInfo(par,psi)
	   hge_PartSys_Fire(par)
	   
	   hge_System_Start()
	   hge_Font_Delete(fnt)
	   hge_Sprite_Delete(spr1) 
	   hge_Sprite_Delete(spr2) 
		hge_Texture_Free(tex)
		hge_Effect_Free(snd)
	End if

hge_System_Shutdown()
hge_Release()