'*************************************************
' FBhge_tut02 - - Using input, sound and rendering
'*************************************************

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

Declare Function FrameFunc() As Integer
Declare Function RenderFunc() As Integer
Declare Sub Boom()
Declare Sub Main()

Dim Shared As Long   fnt
Dim Shared As Long   tex1
Dim Shared As Long   tex2
Dim Shared As Long   spr1
Dim Shared As Long   spr2
Dim Shared As String FPS

Dim Shared quad As hgeQuad
Dim Shared snd As dword
Dim Shared As Single x,y
Dim Shared As Single dx,dy
Const speed As Single = 90
Const friction As Single = 0.98

Main()

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

Function FrameFunc() As Integer
        Dim dt As Single
        dt = hge_Timer_GetDelta()
    
        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
    
        dx*=friction
        dy*=friction
        x+=dx
        y+=dy
    
        If (x>784) Then
           x=784-(x-784)
           dx=-dx
           boom()
        End If
        If (x<16) Then
           x=16+16-x
           dx=-dx
           boom()
        End If
        If (y>584) Then
           y=584-(y-584)
           dy=-dy
           boom()
        End If
        If (y<16) Then
           y=16+16-y
           dy=-dy
           boom()
        End If
    
        quad.v(0).x=x-16
        quad.v(0).y=y-16
        quad.v(1).x=x+16
        quad.v(1).y=y-16
        quad.v(2).x=x+16
        quad.v(2).y=y+16
        quad.v(3).x=x-16
        quad.v(3).y=y+16
    
        Return 0
End Function

Function RenderFunc() As Integer
        hge_Gfx_BeginScene()
        hge_Gfx_Clear(0)
        hge_Gfx_RenderQuad(@quad)
        hge_Gfx_EndScene()
        Return 0
End Function

Sub Main()
    Dim i As Integer
    
    hge_Create(hge_VERSION)
    hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut02.log")
    hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
    hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
    hge_System_SetStateString(HGE_TITLE, "FreeBasic HGE Tutorial 02 - Using input, sound and rendering")
    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)
        quad.tex = hge_Texture_Load("Res/particles.png")
        
        If (snd = 0) Or (quad.tex = 0) Then
            MessageBox(NULL,"Can't load MENU.WAV or PARTICLES.PNG","Error",MB_OK)
            hge_System_Shutdown()
            hge_Release()
            Return
        End If
        
        quad.blend = BLEND_ALPHAADD Or BLEND_COLORMUL Or BLEND_ZWRITE
        
        For i = 0 To 3
            quad.v(i).z = 0.5
            quad.v(i).col = &HFFFFA000
        Next
        
        quad.v(0).tx=96.0/128.0
        quad.v(0).ty=64.0/128.0
        quad.v(1).tx=128.0/128.0
        quad.v(1).ty=64.0/128.0
        quad.v(2).tx=128.0/128.0
        quad.v(2).ty=96.0/128.0
        quad.v(3).tx=96.0/128.0
        quad.v(3).ty=96.0/128.0
        
        hge_System_Start()
        
        hge_Texture_Free(quad.tex)
        hge_Effect_Free(snd)
    Else
        MessageBox(NULL, "hge_System_GetErrorMessage", "Error", MB_OK)

    End If
    
    hge_System_Shutdown()
    hge_Release()
End Sub