'*************************************************
' FBhge_tut04 - Using render targets
'*************************************************

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

Dim Shared As Long   spr
Dim Shared As Long   spt
Dim Shared As Long   tar
Dim Shared As Long   fnt
Dim Shared As Long   par
Dim Shared As Long   tex
Dim Shared As Long   snd
Dim Shared As Long   target
Dim Shared As hgeParticleSystemInfo  psi
Dim Shared As string FPS
Dim Shared As Single dt

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

Function GfxRestoreFunc() As Integer
        If(tar And target) Then
        hge_Sprite_SetTexture(tar,hge_Target_GetTexture(target))
        End If
        Return false
End Function

Sub Boom()    
    Dim As Integer pan = Int((x-256)/2.56)
    Dim As Single pitch = pitch=(dx*dx+dy*dy)*0.0005+0.2
    hge_Effect_PlayEx(snd,100,pan,pitch,0)
End Sub

Function FrameFunc() As Integer 
        
        dt=hge_Timer_GetDelta()
    
        ''Process keys
        If (hge_Input_GetKeyState(HGEK_ESCAPE))Then Return true
        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>496)Then x=496-(x-496):dx=-dx:boom()
        If(x<16) Then x=16+16-x:dx=-dx:boom()
        If(y>496)Then y=496-(y-496):dy=-dy:boom()
        If(y<16) Then y=16+16-y:dy=-dy:boom()   
    
        psi.nEmission=Int(dx*dx+dy*dy)
        hge_PartSys_SetInfo(par,psi)
        hge_PartSys_MoveTo(par,x,y)
        hge_PartSys_Update(par,dt)
    
        Return false
End Function

Function  RenderFunc()As  Integer 
        hge_Gfx_BeginScene(target)
        hge_Gfx_Clear(0)
        hge_PartSys_Render(par)
        hge_Sprite_Render(spr,x,y)
        hge_Gfx_EndScene()
    
        FPS = str(hge_Timer_GetFPS())
        
        hge_Gfx_BeginScene()
        hge_Gfx_Clear(0)
        For i As Integer = 0 To 5
                hge_Sprite_SetColor(tar, &hFFFFFF Or (((5-i)*40+55) Shl 24))
                hge_Sprite_RenderEx(tar, i*100.0f, i*50.0f, i*M_PI/8, 1.0f-i*0.1f)
        Next
        hge_Font_Render(fnt,5, 5, HGETEXT_LEFT, "FPS: "+FPS+"(constant)")
        hge_Font_Render(fnt,5, 35, HGETEXT_LEFT, "dt: "+wstr(dt))
        hge_Gfx_EndScene()
    Return 0
End  Function

hge_Create(hge_VERSION)
hge_System_SetStateInt(hge_SHOWSPLASH, 0)
hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut04.log")
hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
hge_System_SetStateFunc(HGE_GFXRESTOREFUNC, @GfxRestoreFunc)
hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 04 - Using render targets")
hge_System_SetStateInt(HGE_FPS, 100)
hge_System_SetStateBool(HGE_WINDOWED,true)
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)
    
    spr = hge_Sprite(tex, 96, 64, 32, 32)
    hge_Sprite_SetColor(spr,&hFFFFA000)
    hge_Sprite_SetHotSpot(spr,16,16)
    
    spt=hge_Sprite(tex, 32, 32, 32, 32)
    hge_Sprite_SetBlendMode(spt,BLEND_COLORMUL Or BLEND_ALPHAADD Or BLEND_NOZWRITE)
    hge_Sprite_SetHotSpot(spt,16,16)
    par = hge_PartSys("Res/trail.psi",spt)
    hge_PartSys_GetInfo(par,psi)
    hge_PartSys_Fire(par)
    
    target=hge_Target_Create(512,512,false)
    tar=hge_Sprite(hge_Target_GetTexture(target),0,0,512,512)
	 hge_Sprite_SetBlendMode(tar,BLEND_COLORMUL Or BLEND_ALPHAADD Or BLEND_NOZWRITE)

    

    hge_System_Start()
        
    hge_Target_Free(target)
    hge_Texture_Free(tex)
    hge_Effect_Free(snd)
End If

hge_System_Shutdown()
hge_Release()