'*************************************************
' FBhge_tut05 - Using distortion mesh
'*************************************************

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

Dim Shared As Long   tex 'htexture
Dim Shared As Long   dis 'hgeDistortionMesh
Dim Shared As Long   fnt 'hgeFont

Const nRows=16
Const nCols=16
Const As Single cellw=512.0/(nCols-1)
Const As Single cellh=512.0/(nRows-1)

Const As Single meshx=144
Const As Single meshy=44

Dim Shared As String FPS


Function FrameFunc() As Integer 
        
        Dim As Single dt=hge_Timer_GetDelta()
        Static As Single t = 0
        Static As Integer trans = 0
    
        Dim As Integer i, j, col
        Dim As Single r, a, dx, dy
        t += dt
    
        ' Process keys
        Select Case hge_Input_GetKey
               Case HGEK_ESCAPE
                    Return true
            
               Case HGEK_SPACE
                    trans += 1
                     If trans > 2 Then trans=0
            hge_DistortionMesh_Clear(dis, &hFF000000, 0)
       end Select
    
        ' Calculate new displacements and coloring for one of the three effects
        Select Case trans
                Case 0
            For i = 1 To nRows - 2
                For j = 1 To nCols - 2
                    hge_DistortionMesh_SetDisplacement(dis, j,i,Cos(t*10+(i+j)/2)*5,Sin(t*10+(i+j)/2)*5,HGEDISP_NODE)
                Next
            Next
            
        Case 1
            For i = 0 To nRows - 1
                For j = 0 To nCols - 1
                    hge_DistortionMesh_SetDisplacement(dis, j,i,Cos(t*5+j/2)*15,0,HGEDISP_NODE)
                    col=Int((Cos(t*5+(i+j)/2)+1)*35)
                    hge_DistortionMesh_SetColor(dis, j,i,&hFF Shl 24 Or col Shl 16 Or col Shl 8 Or col)
                Next
            Next
            
        Case 2
            For i = 0 To nRows - 1
                For j = 0 To nCols - 1
                    r=Sqr(((j-nCols/2)*(j-nCols/2)) + (i-nRows/2)*(i-nRows/2))
                    a=r*Cos(t*2)*0.1f
                    dx=Sin(a)*(i*cellh-256)+Cos(a)*(j*cellw-256)
                    dy=Cos(a)*(i*cellh-256)-Sin(a)*(j*cellw-256)
                    hge_DistortionMesh_SetDisplacement(dis, j,i,dx,dy,HGEDISP_CENTER)
                    col=Int((Cos(r+t*4)+1)*40)
                    hge_DistortionMesh_SetColor(dis, j,i, &hFF Shl 24 Or col Shl 16 Or (col/2) Shl 8)
                Next
            Next
            
    End Select
    
        Return false
End  Function

Function  RenderFunc()As  Integer 
        hge_Gfx_BeginScene()
        hge_Gfx_Clear(0)
        hge_DistortionMesh_Render(dis, meshx, meshy)
    
        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_Render(fnt,6, 58, HGETEXT_LEFT, "Use your")
        hge_Font_Render(fnt,6, 84, HGETEXT_LEFT, "SPACE")
        hge_Gfx_EndScene()
    Return 0
End  Function

hge_Create(hge_VERSION)
hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut05.log")
hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 05 - Using distortion mesh")
hge_System_SetStateBool(HGE_WINDOWED, true)
hge_System_SetStateInt(HGE_SCREENWIDTH, 800)
hge_System_SetStateInt(HGE_SCREENHEIGHT, 600)
hge_System_SetStateInt(HGE_SCREENBPP, 32)
hge_System_SetStateInt(HGE_USESOUND, false)


If (hge_System_Initiate()) Then
    tex = hge_Texture_Load("Res/texture.jpg")
    
    If tex = 0 Then
        MessageBox(NULL,"Can't load TEXTURE.JPG","Error",MB_OK)
        hge_System_Shutdown()
        hge_Release()
    End If
    fnt = hge_Font("Res/font1.fnt",0)
    
    dis=hge_DistortionMesh(nCols, nRows)
    hge_DistortionMesh_SetTexture(dis, tex)
    hge_DistortionMesh_SetTextureRect(dis,0,0,512,512)
    hge_DistortionMesh_SetBlendMode(dis, BLEND_COLORADD Or BLEND_ALPHABLEND Or BLEND_ZWRITE)
    hge_DistortionMesh_Clear(dis, &hFF000000, 0)
    
    
    hge_System_Start()
       
    hge_Texture_Free(tex)
End If

hge_System_Shutdown()
hge_Release()