'*************************************************
' FBhge_tut07 - Thousand of Hares
'*************************************************

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

#undef Color
#define SCREEN_WIDTH  800
#define SCREEN_HEIGHT 600

#define MIN_OBJECTS        100
#define MAX_OBJECTS 2000

Type sprObject
        As Single x,y
        As Single dx,dy
        As Single scale,rot
        As Single dscale,drot
        As DWORD Color
End Type

Dim Shared As sprObject pObjects(MAX_OBJECTS)
Dim Shared As Integer nObjects
Dim Shared As Integer nBlend

Dim Shared As Long   tex, bgtex
Dim Shared As Long   spr, bgspr
Dim Shared As Long   fnt

Sub SetBlend(blend As Integer)
        Static As Integer sprBlend(5) = _
    {_
                BLEND_COLORMUL Or BLEND_ALPHABLEND Or BLEND_NOZWRITE,_
                BLEND_COLORADD Or BLEND_ALPHABLEND Or BLEND_NOZWRITE,_
                BLEND_COLORMUL Or BLEND_ALPHABLEND Or BLEND_NOZWRITE,_
                BLEND_COLORMUL Or BLEND_ALPHAADD   Or BLEND_NOZWRITE,_
                BLEND_COLORMUL Or BLEND_ALPHABLEND Or BLEND_NOZWRITE _
        }
    
        Static As DWORD fntColor(5)= {&hFFFFFFFF, &hFF000000, &hFFFFFFFF, &hFF000000, &hFFFFFFFF}

        Static As DWORD sprColors(5, 5) = _
        {_
                { &hFFFFFFFF, &hFFFFE080, &hFF80A0FF, &hFFA0FF80, &hFFFF80A0 }, _
                { &hFF000000, &hFF303000, &hFF000060, &hFF006000, &hFF600000 }, _
                { &h80FFFFFF, &h80FFE080, &h8080A0FF, &h80A0FF80, &h80FF80A0 }, _
                { &h80FFFFFF, &h80FFE080, &h8080A0FF, &h80A0FF80, &h80FF80A0 }, _
                { &h40202020, &h40302010, &h40102030, &h40203010, &h40102030 } _
        }

        If blend > 4 Then blend=0
        nBlend=blend

        hge_Sprite_SetBlendMode(spr, sprBlend(blend))
    hge_Font_SetColor(fnt, fntColor(blend))
        For i As Integer = 0 To MAX_OBJECTS - 1
        pObjects(i).color=sprColors(blend, hge_Random_Int(0,4))
    Next
End Sub





Dim Shared As String FPS


Function FrameFunc() As Integer 
        
        Dim As Single dt=hge_Timer_GetDelta()
    Dim As Integer i
    
        ' Process keys
    
        Select Case hge_Input_GetKey
                Case HGEK_UP
            If nObjects < MAX_OBJECTS Then nObjects += 100
            
                Case HGEK_DOWN
            If nObjects > MIN_OBJECTS Then nObjects -= 100
                
        Case HGEK_SPACE
            nblend += 1
            SetBlend(nBlend)
            
                Case HGEK_ESCAPE
                        Return true
    End Select
    
        ' Update the scene
        
        For i = 0 To nObjects - 1
                pObjects(i).x+=pObjects(i).dx*dt
                If (pObjects(i).x>SCREEN_WIDTH Or pObjects(i).x<0) Then pObjects(i).dx=-pObjects(i).dx
        
                pObjects(i).y+=pObjects(i).dy*dt
                If (pObjects(i).y>SCREEN_HEIGHT Or pObjects(i).y<0) Then pObjects(i).dy=-pObjects(i).dy
        
                pObjects(i).scale+=pObjects(i).dscale*dt
                If (pObjects(i).scale>2 Or pObjects(i).scale<0.5) Then pObjects(i).dscale=-pObjects(i).dscale
                
        pObjects(i).rot+=pObjects(i).drot*dt
        Next

        Return false
End Function

Function RenderFunc() As Integer

        Dim As Integer i
        
        ' Render the scene
        
        hge_Gfx_BeginScene()
    hge_Sprite_Render(bgspr, 0, 0)
        
        For i = 0 To nObjects - 1
        hge_Sprite_SetColor(spr, pObjects(i).color)
        hge_Sprite_RenderEx(spr, pObjects(i).x, pObjects(i).y, pObjects(i).rot, pObjects(i).scale)
        Next
    
    
        FPS = Str(hge_Timer_GetFPS())
    hge_Font_SetColor(fnt,Rgb(255,255,255))
    hge_Font_Render(fnt, 7, 7, HGETEXT_LEFT, "UP and DOWN to adjust number of hares: " + Str(nObjects))
    hge_Font_Render(fnt, 7, 7 + 12, HGETEXT_LEFT, "SPACE to change blending mode: " + Str(nBlend))
    hge_Font_Render(fnt, 7, 7 + 24, HGETEXT_LEFT, "FPS: " + FPS)
    
    
    
    hge_Gfx_EndScene()
    Return false
End Function

hge_Create(hge_VERSION)
hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut07.log")
hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 07 - Thousand of Hares")
hge_System_SetStateInt(HGE_USESOUND, false)
hge_System_SetStateBool(HGE_WINDOWED, 1)
hge_System_SetStateInt(HGE_SCREENWIDTH, SCREEN_WIDTH)
hge_System_SetStateInt(HGE_SCREENHEIGHT, SCREEN_HEIGHT)
hge_System_SetStateInt(HGE_SCREENBPP, 32)


If (hge_System_Initiate()) Then
    bgtex = hge_Texture_Load("Res/bg2.png")
    tex = hge_Texture_Load("Res/zazaka.png")
    
    If (bgtex = 0) Or (tex = 0) Then
        MessageBox(NULL,"Can't load BG2.PNG or ZAZAKA.PNG","Error",MB_OK)
        hge_System_Shutdown()
        hge_Release()
    End If
    
    fnt = hge_Font("Res/font2.fnt", 0)
  
    spr = hge_Sprite(tex, 0, 0, 64, 64)
    hge_Sprite_SetHotSpot(spr, 16, 16)


    bgspr = hge_Sprite(bgtex, 0, 0, 800, 600)
    hge_Sprite_SetBlendMode(bgspr, BLEND_COLORADD Or BLEND_ALPHABLEND Or BLEND_NOZWRITE)
    hge_Sprite_SetColor(bgspr, &hFF000000, 0)
    hge_Sprite_SetColor(bgspr, &hFF000000, 1)
    hge_Sprite_SetColor(bgspr, &hFF000040, 2)
    hge_Sprite_SetColor(bgspr, &hFF000040, 3)

    ' Initialize objects list

    nObjects=1000

    For i As Integer = 0 To MAX_OBJECTS - 1
        pObjects(i).x=hge_Random_Float(0,SCREEN_WIDTH)
        pObjects(i).y=hge_Random_Float(0,SCREEN_HEIGHT)
        pObjects(i).dx=hge_Random_Float(-200,200)
        pObjects(i).dy=hge_Random_Float(-200,200)
        pObjects(i).scale=hge_Random_Float(0.5f,2.0f)
        pObjects(i).dscale=hge_Random_Float(-1.0f,1.0f)
        pObjects(i).rot=hge_Random_Float(0,M_PI*2)
        pObjects(i).drot=hge_Random_Float(-1.0f,1.0f)
    Next
    
    SetBlend(0)
    hge_System_Start()
        
    hge_Texture_Free(bgtex)
    hge_Texture_Free(tex)
End If

hge_System_Shutdown()
hge_Release()