'*************************************************
' FBhge_tut06 - Creating menus
'*************************************************

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

Dim Shared As dword	 snd
Dim Shared As dword	 tex
Dim Shared As hgeQuad quad
Dim Shared As long	 gui
Dim Shared As long	 fnt
Dim Shared As long	 spr
Dim Shared As string  FPS

Function FrameFunc() As Integer 
	Dim As Single dt=hge_Timer_GetDelta()
	Static As Single t = 0 
	Dim As Single tx
	Dim As Single ty
	Dim As integer id
	Static As integer lastid =0

	
	If hge_Input_GetKeyState(HGEK_ESCAPE)then lastid=5: hge_GUI_Leave(gui)
	id=hge_GUI_Update(gui,dt)
	
	If id = -1 Then 
	   Select Case lastid
	   	Case  1
	   		hge_GUI_SetFocus(gui,1)
            hge_GUI_Enter(gui)
	   	Case  2
	   		hge_GUI_SetFocus(gui,1)
            hge_GUI_Enter(gui)
	   	Case  3
	   		hge_GUI_SetFocus(gui,1)
            hge_GUI_Enter(gui)
	   	Case  4
	   		hge_GUI_SetFocus(gui,1)
            hge_GUI_Enter(gui)
	   	Case  5
	          return 1
         End Select 
	ElseIf id Then  
		lastid=id
		hge_GUI_Leave(gui)
	
   End if

	t + = dt
	
	tx=50*cos(t/60)
	ty=50*sin(t/60)

	quad.v(0).tx=tx
	quad.v(0).ty=ty
	quad.v(1).tx=tx+800/64
   quad.v(1).ty=ty
	quad.v(2).tx=tx+800/64
	quad.v(2).ty=ty+600/64
	quad.v(3).tx=tx
	quad.v(3).ty=ty+600/64

   Return 0
End  Function
Function  RenderFunc()As  Integer
	hge_Gfx_BeginScene()
	hge_Gfx_Clear(0)
	hge_Gfx_RenderQuad(@quad)
	hge_GUI_Render(gui)
	FPS = Str(hge_Timer_GetFPS())
	hge_Font_SetColor(fnt,Rgb(255,255,255))
	hge_Font_Render(fnt,6, 6, HGETEXT_LEFT, "FPS: " + FPS)
	hge_Gfx_EndScene()
	Return 0
End  Function

	hge_Create(hge_VERSION)
	hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut06.log")
	hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
	hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
	hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 06 - Creating menus")
	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
	
	quad.tex = hge_Texture_Load("Res/bg.png")
	tex = hge_Texture_Load("Res/cursor.png")
	snd = hge_Effect_Load("Res/menu.wav",0)
	
	If (snd = 0) Or (tex = 0) Then
        MessageBox(NULL,"Can't load MENU.WAV or cursor.PNG","Error",MB_OK)
        hge_System_Shutdown()
        hge_Release()
        End 
	End If
	
   fnt = hge_Font("Res/font1.fnt",0)
   spr = hge_Sprite(tex, 0, 0, 32, 32)
	
	
	quad.blend=BLEND_ALPHABLEND or BLEND_COLORMUL or BLEND_NOZWRITE
	
	Dim i As Integer
	
	For  i = 0 To 3
	    quad.v(i).z=0.5
   	 quad.v(i).col=&hFFFFFFFF
	Next
	
	quad.v(0).x=0: quad.v(0).y=0 
	quad.v(1).x=800: quad.v(1).y=0 
	quad.v(2).x=800: quad.v(2).y=600 
	quad.v(3).x=0: quad.v(3).y=600 
	
	gui=hge_GUI()

	hge_GUI_AddCtrl(gui,hge_GUIMenuItem(1,fnt,snd,400,200,0  ,"Play"))
	hge_GUI_AddCtrl(gui,hge_GUIMenuItem(2,fnt,snd,400,240,0.1,"Options"))
	hge_GUI_AddCtrl(gui,hge_GUIMenuItem(3,fnt,snd,400,280,0.2,"Instructions"))
	hge_GUI_AddCtrl(gui,hge_GUIMenuItem(4,fnt,snd,400,320,0.3,"Credits"))
	hge_GUI_AddCtrl(gui,hge_GUIMenuItem(5,fnt,snd,400,360,0.4,"Exit"))

	hge_GUI_SetNavMode(gui,HGEGUI_UPDOWN or HGEGUI_CYCLED)
	hge_GUI_SetCursor(gui,spr)
	hge_GUI_SetFocus(gui,1)
	hge_GUI_Enter(gui)

	hge_System_Start()
	
	hge_Font_Delete(fnt)
	hge_Sprite_Delete(spr) 
	hge_GUI_Delete(gui)
	hge_Effect_Free(snd)
	hge_Texture_Free(tex)
	hge_Texture_Free(quad.tex)
End If

hge_System_Shutdown()
hge_Release()





