
;XIncludeFile #PB_Compiler_Home + "Includes\IrrlichtWrapper_main.pbi"
XIncludeFile #PB_Compiler_Home + "Includes\IrrlichtWrapper_Include.pbi"

EnableExplicit

Macro mc_setVector(__v__,__x__,__y__,__z__)
  __v__\x = __x__ : __v__\y = __y__ : __v__\z = __z__ 
EndMacro


CompilerIf #PB_Compiler_Debugger
Macro mc_DebugIrrEvent(__event__)
  Select(__event__\EventType)
    Case #IRR_EET_KEY_INPUT_EVENT
      Debug("IrrlichtWrapper- Key-Event")
      Debug("Key="+Str(__event__\KeyEvent\key))
      Debug("PressedDown="+Bin(__event__\KeyEvent\PressedDown))
    Case #IRR_EET_GUI_EVENT
      Debug("IrrlichtWrapper- GUI-Event")
    Case #IRR_EET_MOUSE_INPUT_EVENT
      Debug("IrrlichtWrapper- Mouse-Event")
    Default:
      Debug("IrrlichtWrapper- Unknown-Event ="+Str(__event__\EventType))    
  EndSelect
EndMacro    
CompilerElse
Macro mc_DebugIrrEvent(__event__) : EndMacro
CompilerEndIf


        
    
  

Global Dim m_keys.l(#IRR_KEY_KEY_CODES_COUNT)
Global *p_car.PhysicsCar = #Null;
Define m_car.PhysicsCar
Define i.l,dummy.l
Define *light.l, *mesh.l, *node.l, *camera.l
Define *eventRec.l

; Event-Callback... 
ProcedureCDLL.l eventCallback(*event.SEvent)
  mc_DebugIrrEvent(*event)
;    Shared m_keys();
;    Shared *p_car.l;
  ;Gui-Events
  ;Key-Events
  Define dummy.l,i.l
  If *event\EventType = #IRR_EET_KEY_INPUT_EVENT    
    If *event\KeyEvent\key = #IRR_KEY_KEY_W
      Debug("Key-W")
;       Debug("*car = "+Hex(PeekL(*p_car)))
;       Debug("IrrPhysicsCarSetThrottlePercent = "+Hex(PeekL(@IrrPhysicsCarSetThrottlePercent())))
;       i.l = IrrTestMe()
;       Debug ("IrrTestMe="+Str(i))
;       Debug("IrrGetVersion="+PeekS(IrrGetVersion(),-1,#PB_Ascii))
      
      If m_keys(#IRR_KEY_KEY_W) = #False
        m_keys(#IRR_KEY_KEY_W) = #True;
        IrrPhysicsCarSetThrottlePercent(*p_car,100.0)
      ElseIf *event\KeyEvent\PressedDown & $1 = #False
        m_keys(#IRR_KEY_KEY_W) = #False;
      EndIf
    EndIf 
        
    If *event\KeyEvent\key = #IRR_KEY_KEY_S
      Debug("Key-S")
      If m_keys(#IRR_KEY_KEY_S) = #False
        m_keys(#IRR_KEY_KEY_S) = #True;
        IrrPhysicsCarSetThrottlePercent(*p_car,-100.0)
      ElseIf *event\KeyEvent\PressedDown & $1= #False
        m_keys(#IRR_KEY_KEY_S) = #False;
      EndIf 
    EndIf 
    If  *event\KeyEvent\key = #IRR_KEY_KEY_A
      If m_keys(#IRR_KEY_KEY_A) = #False
        m_keys(#IRR_KEY_KEY_A) = #True;
        IrrPhysicsCarSetSteeringPercent(*p_car,-100.0)
      ElseIf *event\KeyEvent\PressedDown & $1= #False
        m_keys(#IRR_KEY_KEY_A) = #False;
      EndIf 
    EndIf 
    If  *event\KeyEvent\key = #IRR_KEY_KEY_D
      If m_keys(#IRR_KEY_KEY_D) = #False
        m_keys(#IRR_KEY_KEY_D) = #True;
        IrrPhysicsCarSetSteeringPercent(*p_car,100.0)
      ElseIf *event\KeyEvent\PressedDown & $1 = #False
        m_keys(#IRR_KEY_KEY_D) = #False;
      EndIf 
    EndIf 
    
;		If(!m_keys[KEY_KEY_A] && !m_keys[KEY_KEY_D])
    
    If m_keys(#IRR_KEY_KEY_A)<>#True And m_keys(#IRR_KEY_KEY_D)<>#True
      IrrPhysicsCarSetSteeringPercent(*p_car,0.0)
    EndIf 
    If m_keys(#IRR_KEY_KEY_W)<>#True And m_keys(#IRR_KEY_KEY_S)<>#True
      IrrPhysicsCarSetThrottlePercent(*p_car,0.0)
    EndIf 
                  
  EndIf

;   If *event\EventType = #IRR_EET_MOUSE_INPUT_EVENT
;     mouse_X.l = *event\MouseEvent\X
;     mouse_Y.l = *event\MouseEvent\X
;     mouse_wheel.f = *event\MouseEvent\Wheel
;     
;   EndIf
    
  ProcedureReturn #False
EndProcedure 

; Procedure betest()
;     If m_keys(#IRR_KEY_KEY_W) = #True
;         IrrPhysicsCarSetThrottlePercent(*p_car,100.0)
;     EndIf 
;         
;     If m_keys(#IRR_KEY_KEY_S) = #True
;         IrrPhysicsCarSetThrottlePercent(*p_car,-100.0)
;     EndIf 
; EndProcedure



For i=0 To #IRR_KEY_KEY_CODES_COUNT-1: m_keys(i)=#False:Next


 
;InitIrrlichtWrapperDll()
IrrStartEx(#IRR_EDT_DIRECT3D9,640,480,#False, #False, #True,32,#True,#False,#False,0)
IrrSetWindowCaption( "IrrPhysicsExample 03: A Car" )

*light.l = IrrAddLight(0,1000.0,0, 1,1,1, 100)



*camera = IrrAddFPSCamera()
IrrSetNodePosition(*camera, 0,30.0,-30.0) 
 
 IrrPhysicsInit() 
 
 
  ; 	// create our custom event receiver, pass a pointer To the car
*eventRec = IrrCreateEventReceiver(@eventCallback())
 IrrSetEventReceiver(*eventRec)

 
; 	// create the car Data
; 	SPhysicsCar carData;
; 
mc_setVector(m_car\carBodyOffset,0,0,0)
mc_setVector(m_car\carBodySize,1.2,0.5,0.2)
m_car\carMass = 400.0;
m_car\frontAxleOffset = 0.6;
m_car\rearAxleOffset = 0.6;
m_car\axleWidth = 0.8;
m_car\tireMass = 20.0;
m_car\tireRadius = 0.3;
m_car\tireWidth = 0.2;
m_car\maxSteerAngle = 0.6;
m_car\maxTorque = 1000.0;
m_car\maxBrakes = 50.0;
; 
m_car\tireSuspensionLength = 0.2;
m_car\tireSuspensionSpring = (m_car\tireMass * 1.0 * 9.8) / m_car\tireSuspensionLength; 
m_car\tireSuspensionShock = Sqr(m_car\tireSuspensionSpring) * 1.0;
; 
m_car\carBodyNode = IrrAddCubeSceneNode(1.0)
IrrSetNodeScale(m_car\carBodyNode,m_car\carBodySize\x,m_car\carBodySize\y,m_car\carBodySize\z);

; 
m_car\tireNode_FL = IrrAddSphereSceneNode(1.0,16);
IrrSetNodeScale(m_car\tireNode_FL,m_car\tireRadius,m_car\tireRadius,m_car\tireWidth);
m_car\tireNode_FR = IrrAddSphereSceneNode(1.0,16);;
IrrSetNodeScale(m_car\tireNode_FR,m_car\tireRadius,m_car\tireRadius,m_car\tireWidth);
m_car\tireNode_RL = IrrAddSphereSceneNode(1.0,16);
IrrSetNodeScale(m_car\tireNode_RL,m_car\tireRadius,m_car\tireRadius,m_car\tireWidth);
m_car\tireNode_RR = IrrAddSphereSceneNode(1.0,16);;
IrrSetNodeScale(m_car\tireNode_RR,m_car\tireRadius,m_car\tireRadius,m_car\tireWidth);
*p_car = IrrPhysicsAddCar(@m_car)


Debug("*car = "+Hex(PeekL(*p_car)))
Debug("IrrPhysicsCarSetThrottlePercent = "+Hex(PeekL(@IrrPhysicsCarSetThrottlePercent()))) 
 *mesh.l = IrrGetMesh("../media/blockland2.obj")
 ;*mesh.l = IrrGetMesh("./blockland2.obj")
 *node.l = IrrAddMeshToScene(*mesh)


IrrPhysicsAddStaticMesh(*mesh,*node, 0.1,0.1,0.1)
IrrSetNodeScale(*node,0.1,0.1,0.1)
; 
 
IrrPhysicsSetEntityPos(*p_car,0,20,0)
;  
;  



  While IrrRunning()
      IrrBeginScene( 0,0,0 )   
      IrrDrawScene()
      IrrEndScene()
      IrrPhysicsUpdate()
;      betest()
  Wend
 
  IrrPhysicsClose()
  IrrStop()
 End
;  FreeIrrlichtWrapperDll()
  
  

; IDE Options = PureBasic v4.02 (Windows - x86)
; CursorPosition = 187
; FirstLine = 155
; Folding = -
; Executable = T:\projects\pb4\fun\ipvk\IrrlichtWrapperNewtonExample3.exe
; jaPBe Version=3.7.2.629
; Build=0
; Language=0x0000 Language Neutral
; FirstLine=162
; CursorPosition=189
; ExecutableFormat=Console
; CPU=1
; DontSaveDeclare
; EOF