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

   Dim Shared As long     fnt
   
   Type ph
   	  spr     As Long
   	  body    As Long 
   End Type
   
   Dim Shared As ph       phObj()
   Dim shared As Long     NumPhObj
   Dim shared As Long     Car 
   Dim shared As Long     Wheel1
   Dim shared As Long     Wheel2
   Dim shared As Long     Axel1
   Dim shared As Long     Axel2
   
	Dim Shared As long     spr1
	Dim Shared As long     spr2
	Dim Shared As long     tex1
	Dim Shared As long     tex2
	Dim Shared As long     tex3
	Dim Shared As long     tex4
	Dim Shared As long     sprcur
	Dim Shared As long	  gui
	Dim Shared As Single   mx,my

	Dim Shared As long     phWorld
	
	Declare function CreatePhObjBox(ByVal As Long,ByVal As Single,ByVal As Single,ByVal As Single,ByVal As single,ByVal  As Single,ByVal As Single=0,ByVal As Single=0,ByVal As Single=0.1)as Long
	Declare function CreatePhObjCircle(ByVal As Long,ByVal As Single,ByVal As Single,ByVal As single,ByVal As Single,ByVal As Single=0,ByVal As Single=0,ByVal As Single=0.1)as Long
	Declare Function rnd_range (first As Double, last As Double) As Double
	Declare Sub UpdatePhObj()
	
	Dim Shared As string FPS

Function  FrameFunc()As  Integer 
	
	Dim As Single dt=hge_Timer_GetDelta()
	Dim As integer id
   Ph_WorldStep(phWorld,dt)
   
	
	hge_Input_GetMousePos(mx,my)
	
	if (hge_Input_GetKeyState(HGEK_ESCAPE))then return 1
	
	if (hge_Input_GetKeyState(HGEK_LEFT))Then
    	 Ph_SetJointMotorSpeed Axel1,-1000
       Ph_SetJointMotorForce Axel1,2000000    
   ElseIf(hge_Input_GetKeyState(HGEK_RIGHT))Then
    	 Ph_SetJointMotorSpeed Axel1,1000
       Ph_SetJointMotorForce Axel1,2000000
   Else    
       Ph_SetJointMotorForce Axel1,0
		 Ph_SetJointMotorSpeed Axel1,0		
	EndIf
	
	if (hge_Input_GetKeyState(HGEK_DOWN))Then	
		Ph_SetAngularVelocity Wheel1,0	   
	EndIf	
	
	

	if hge_Input_IsMouseOver() Then
	   if (hge_Input_KeyDown(HGEK_LBUTTON))Then CreatePhObjBox(tex2,32,32,mx,my,0,1,0.5,2)
	   if (hge_Input_KeyDown(HGEK_RBUTTON))Then CreatePhObjCircle(tex4,32,mx,my,0,1,0.5,2)
	End If
	
	id=hge_GUI_Update(gui,dt)  
	return 0 
	
End  Function

Function  RenderFunc()As  Integer 
	hge_Gfx_BeginScene()
	hge_Gfx_Clear(0)
	UpdatePhObj()
	hge_GUI_Render(gui)
	FPS = str(hge_Timer_GetFPS())
	hge_Font_SetColor(fnt,rgb(255,255,255))
   hge_Font_Render(fnt,40, 40, HGETEXT_LEFT, "FPS: "+FPS+"(const)")
   hge_Font_Render(fnt,40, 60, HGETEXT_LEFT, "phObj: " +str(NumPhObj))
   hge_Font_Render(fnt,40, 80, HGETEXT_LEFT, "CreatephObj: Mouse LBUTTON or RBUTTON")
   hge_Font_Render(fnt,40, 100,HGETEXT_LEFT, "Car: Key <-LEFT,DOWN,RIGHT->")
   hge_Font_Render(fnt,40, 120,HGETEXT_LEFT, "CurPosX: "+Str(mx)+" CurPosY: "+Str(my))

   hge_Gfx_EndScene()
   return 0
End  Function

   hge_Create(hge_VERSION)
   hge_System_SetStateString(HGE_LOGFILE, "FBhge_tut03.log")
	hge_System_SetStateFunc(HGE_FRAMEFUNC, @FrameFunc)
	hge_System_SetStateFunc(HGE_RENDERFUNC, @RenderFunc)
	hge_System_SetStateString(HGE_TITLE, "FreeBasic-HGE Tutorial 09 - Physics")
	hge_System_SetStateInt(HGE_FPS, 100)
	hge_System_SetStateBool(HGE_WINDOWED, 1)
	hge_System_SetStateInt(HGE_SCREENWIDTH, 1024)
	hge_System_SetStateInt(HGE_SCREENHEIGHT, 768)
	hge_System_SetStateInt(HGE_SCREENBPP, 32)
	
	 If (hge_System_Initiate()) Then
      fnt = hge_Font("Res/font2.fnt",0)
      
      tex1 = hge_Texture_Load("Res/bg2.png")
      tex2 = hge_Texture_Load("Res/bg.png")
      tex3 = hge_Texture_Load("Res/cursor.png")
      tex4 = hge_Texture_Load("Res/particles.png")
       
      phWorld=Ph_CreateWorld(0,0,1024,768,0,100,1)
      
    	CreatePhObjBox(tex1,160,8,445,730,-0.2)
	   CreatePhObjBox(tex1,160,8,600,730,0.2)
      CreatePhObjBox(tex1,1024,32,512,752,0,0,0.3,1)
      CreatePhObjBox(tex1,1024,32,512,16,0)
	   CreatePhObjBox(tex1,32,704,16,384,0,)
	   CreatePhObjBox(tex1,32,704,1008,384,0)
	   CreatePhObjBox(tex1,160,32,230,500,0)
	   CreatePhObjBox(tex1,160,32,800,500,0)
	   CreatePhObjBox(tex1,32,360,320,300,-1.1)
	   CreatePhObjBox(tex1,32,360,710,300, 1.1)
	   
	   Car = CreatePhObjBox(tex2,120,16,100, 682,0, 1, 0.3, 1)
      Wheel1 = CreatePhObjCircle(tex4,32,70,690,0, 1, 0.3, 2500)
      Wheel2 = CreatePhObjCircle(tex4,32,130,690,0, 1, 0.3, 2500)
      Axel1  = Ph_CreateRevoluteJoint(phWorld, Car, Wheel1, 70,690)
      Axel2  = Ph_CreateRevoluteJoint(phWorld, Car, Wheel2, 130,690)
      Ph_SetJointMotorSpeed Axel1,1000
      
      Dim As Long section1 = CreatePhObjBox(tex2,8,40,512,55,0, 0, 0, 0)
      Dim As Long section2 = CreatePhObjBox(tex2,8,48,512,100,0, 1, 0.1, 0.1)
      Dim As Long section3 = CreatePhObjBox(tex2,8,48,512,150,0, 1, 0.1, 0.1)
      Dim As Long section4 = CreatePhObjBox(tex2,8,48,512,200,0, 1, 0.1, 0.1)
      Dim As Long section5 = CreatePhObjBox(tex2,8,48,512,250,0, 1, 0.1, 0.1)
      Dim As Long section6 = CreatePhObjBox(tex2,8,48,512,300,0, 1, 0.1, 0.1)
      Dim As Long section7 = CreatePhObjBox(tex2,8,48,512,350,0, 1, 0.1, 0.1)
      Ph_CreateRevoluteJoint(phWorld,section1,section2,512,75)
      Ph_CreateRevoluteJoint(phWorld,section2,section3,512,125)
      Ph_CreateRevoluteJoint(phWorld,section3,section4,512,175)
      Ph_CreateRevoluteJoint(phWorld,section4,section5,512,225)
      Ph_CreateRevoluteJoint(phWorld,section5,section6,512,275)
      Ph_CreateRevoluteJoint(phWorld,section6,section7,512,325)
      
      Dim As Long section8  = CreatePhObjBox(tex2,16,16,320,500,0, 0, 0, 0)
      Dim As Long section9 = CreatePhObjBox(tex2,16,16,710,500,0, 0, 0, 0)
      Dim As Long section10 = CreatePhObjBox(tex2,8,48,330,530,0, 1, 0.1, 0.01)
      Dim As Long section11 = CreatePhObjBox(tex2,8,48,700,530,0, 1, 0.1, 0.01)
      Dim As Long section12 = CreatePhObjBox(tex2,48,8,356,556,0, 1, 0.1, 0.01)
      Dim As Long section13 = CreatePhObjBox(tex2,48,8,408,556,0, 1, 0.1, 0.01)
      Dim As Long section14 = CreatePhObjBox(tex2,48,8,460,556,0, 1, 0.1, 0.01)
      Dim As Long section15 = CreatePhObjBox(tex2,48,8,512,556,0, 1, 0.1, 0.01)
      Dim As Long section16 = CreatePhObjBox(tex2,48,8,564,556,0, 1, 0.1, 0.01)
      Dim As Long section17 = CreatePhObjBox(tex2,48,8,616,556,0, 1, 0.1, 0.01)
      Dim As Long section18 = CreatePhObjBox(tex2,48,8,668,556,0, 1, 0.1, 0.01)
     
      Ph_CreateRevoluteJoint(phWorld,section8,section10,328,508)
      Ph_CreateRevoluteJoint(phWorld,section9,section11,700,508)
      Ph_CreateRevoluteJoint(phWorld,section10,section12,328,556)
      Ph_CreateRevoluteJoint(phWorld,section12,section13,380,556)
      Ph_CreateRevoluteJoint(phWorld,section13,section14,432,556)
      Ph_CreateRevoluteJoint(phWorld,section14,section15,484,556)
      Ph_CreateRevoluteJoint(phWorld,section15,section16,536,556)
      Ph_CreateRevoluteJoint(phWorld,section16,section17,588,556)
      Ph_CreateRevoluteJoint(phWorld,section17,section18,640,556)
      Ph_CreateRevoluteJoint(phWorld,section18,section11,700,556)
     
      
     
	   
	   sprcur=hge_Sprite(tex3, 0, 0, 32, 32)
	   gui=hge_GUI()
	   hge_GUI_SetCursor(gui,sprcur)
	   
	   hge_System_Start()
	   
	   hge_Font_Delete(fnt)
	   Dim i As integer
	   For i = 0 To NumPhObj-1
	   	hge_Sprite_Delete(phObj(i).spr)
	   Next
	   hge_Sprite_Delete(sprcur) 
	   hge_GUI_Delete(gui) 
		hge_Texture_Free(tex1)
		hge_Texture_Free(tex2)
		hge_Texture_Free(tex3)
		hge_Texture_Free(tex4)
	 End If
	 
Ph_DeleteWorld(phWorld)
hge_System_Shutdown()
hge_Release()

Function CreatePhObjBox(ByVal tex As Long,ByVal ScaleX As Single,ByVal ScaleY As Single,ByVal PosX As Single,ByVal PosY As single,ByVal Angle As Single,ByVal Density As Single=0,ByVal Restitution As Single=0,ByVal Friction As Single=0)As Long
	   
	   ReDim Preserve phObj(NumPhObj)
	   phObj(NumPhObj).spr = hge_Sprite(tex,0,0,ScaleX,ScaleY)
      hge_Sprite_SetHotSpot(phObj(NumPhObj).spr,ScaleX*0.5,ScaleY*0.5)      
      phObj(NumPhObj).body = Ph_CreateBoxBody(phWorld,ScaleX,ScaleY,Density,Restitution,Friction)      
      Ph_SetPosBody(phObj(NumPhObj).body,PosX,PosY)
      Ph_SetAngleBody(phObj(NumPhObj).body,Angle)
      Function = phObj(NumPhObj).body 
    	NumPhObj = NumPhObj + 1
    	
End Function
function CreatePhObjCircle(ByVal tex As Long,ByVal Radius As Single,ByVal PosX As Single,ByVal PosY As single,ByVal Angle As Single,ByVal Density As Single=0,ByVal Restitution As Single=0,ByVal Friction As Single=0)as Long 
	    
	   ReDim Preserve phObj(NumPhObj)
	   phObj(NumPhObj).spr = hge_Sprite(tex,96,64,Radius,Radius)
	   hge_Sprite_SetColor(phObj(NumPhObj).spr,rgb(rnd_range(1,255),rnd_range(1,255),rnd_range(1,255)))
      hge_Sprite_SetHotSpot(phObj(NumPhObj).spr,Radius*0.5,Radius*0.5)      
      phObj(NumPhObj).body = Ph_CreateCircleBody(phWorld,Radius,Density,Restitution,Friction)
      Ph_SetPosBody(phObj(NumPhObj).body,PosX,PosY)
      Ph_SetAngleBody(phObj(NumPhObj).body,Angle)
      Function = phObj(NumPhObj).body 
    	NumPhObj = NumPhObj + 1
    	                        	
End Function
Sub UpdatePhObj()
	 Dim i As integer
	 For i = 0 To NumPhObj-1
	    hge_Sprite_RenderEx(phObj(i).spr,Ph_GetBodyX(phObj(i).body),Ph_GetBodyY(phObj(i).body),Ph_GetBodyAngle(phObj(i).body))
    Next
End Sub
Function rnd_range (first As Double, last As Double) As Double
    Function = Rnd * (last - first) + first
End Function
