;**
;* IrrlichtWrapper 1.4 - MainInclude
;* 
;* Last Modified : 25.03.2008 - Thalius

; Include Enumerations:
; IrrLicht
XIncludeFile #PB_Compiler_Home + "Includes\irrlicht_enums.pbi"
; IrrKlang
XIncludeFile #PB_Compiler_Home + "Includes\irrklang_enums.pbi"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Constant Definitions

; Flag definition
#IRR_OFF  = 0
#IRR_ON   = 1

; System Device definitions
#IRR_WINDOWED   = 0         ; Switch for Windowed Mode 
#IRR_FULLSCREEN = 1         ; Switch for Fullscreen Mode

#IRR_NO_SHADOWS = 0         ; Switch to not use Stencil Buffers 
#IRR_SHADOWS    = 1         ; Switch to not use Stencil Buffers ( Not supported on older Hardware )

#IRR_IGNORE_EVENTS  = 0     ; Ignore Events Switch - (Default)
#IRR_CAPTURE_EVENTS = 1     ; Process IrrlichtEvents , such as Custom Keyboard & Mouse Inputs

#IRR_LINEAR_FOG       = 0   ; Switch to use Linear Gradient Fog Algorythm ( DX 7/8 Hardware: TnL Unit / DX9/10: GPU internal TnL Shader Emulation )
#IRR_EXPONENTIAL_FOG  = 1   ; Switch to use Exponential Fog Algorythm ( DX 7/8 Hardware: TnL Unit / DX9/10: GPU internal TnL Shader Emulation )

; Filing system definitions
#IRR_USE_CASE     = 0       ; Filesystem Switch - Process Filenames Case Sensitive 
#IRR_IGNORE_CASE  = 1       ; Filesystem Switch - Ignore Filename's Case Sensitivity 
#IRR_USE_PATHS    = 0       ; Use Paths in Fileseek
#IRR_IGNORE_PATHS = 1       ; Ignore Paths in Fileseek ( Relative )

; 2D image definitions
#IRR_IGNORE_ALPHA = 0       ; Dont use Alpha Channel Information in 2D Images ( faster )
#IRR_USE_ALPHA    = 1       ; Use Alfa Channel Information for 2D Images

; Node definitions
#IRR_INVISIBLE    = 0       ; Switch Node Invisible ( Very useful to blend out far or not needed Objects in the Scene - theese dont get rendered and save CPU/GPU Power )
#IRR_VISIBLE      = 1       ; Switch Node Visible ( Default )

#IRR_ONE_SHOT     = 0       ; Single Run
#IRR_LOOP         = 1       ; Mainloop run til end Condition

; Particle definitions
#IRR_NO_EMITTER      = 0    ; Is not Emitting
#IRR_DEFAULT_EMITTER = 1    ; Is Emitting

; GUI Interface definitions
#IRR_GUI_NO_BORDER  = 0     ; Switch GUI Borders off ( Default )
#IRR_GUI_BORDER     = 1     ; Switch GUI Borders on
#IRR_GUI_NO_WRAP    = 0     ; No Linewrap in GUI
#IRR_GUI_WRAP       = 1     ; Linewrap GUI



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Type Definitions


; Irrlicht Keyboard And Mouse Event Structures

;@@structure IRR_MOUSE_EVENT
;@
;@@Description.de
;@		Structure um Maus-Events auszuwerten. 
;@
;@@Description
;@		Structure for Mouse events
Structure IRR_MOUSE_EVENT
    action.l
    wheel.f
    x.l
    y.l
EndStructure

;@@structure IRR_KEY_EVENT
;@
;@@Description.de
;@		Structure um Tasten-Events auszuwerten. 
;@
;@@Description
;@		Structure for Key events
Structure IRR_KEY_EVENT
    key.l
    direction.l
    flags.l
EndStructure


;@@structure IRR_GUI_EVENT
;@
;@@Description.de
;@		Structure um Gui-Events auszuwerten. 
;@
;@@Description
;@		Structure for Gui events
Structure IRR_GUI_EVENT
    Caller.l
    EventType.l
    itemID.l
EndStructure



; Particle emitter properties
Structure IRR_PARTICLE_EMITTER
    min_box_x.f                   ; The bounding box For the emitter
    min_box_y.f     
    min_box_z.f

    max_box_x.f                   ; The Maximum Dimension of the Bounding Box for the Emitter
    max_box_y.f
    max_box_z.f

    direction_x.f                 ; Emitter Direction
    direction_y.f
    direction_z.f

    min_paritlcles_per_second.l   ; Minimum Spawnrate in Particles/Second
    max_paritlcles_per_second.l   ; Maximum Spawnrate in Particles/Second

    min_start_color_red.l
    min_start_color_green.l
    min_start_color_blue.l

    max_start_color_red.l
    max_start_color_green.l
    max_start_color_blue.l
    
    min_lifetime.l                 ; Minimal lifetime of a particle, in milliseconds.
    max_lifetime.l                 ; Maximal lifetime of a particle, in milliseconds.
    
    max_angle_degrees.l            ; Maximal angle in degrees, 
                                   ; the emitting direction of the particle is allowed To differ from the orignial direction.
EndStructure

; a vertex used in creating custom Static mesh objects
Structure IRR_VERT
    x.f             ; The x position of the vertex
    y.f             ; The y position of the vertex
    z.f             ; The z position of the vertex
    normal_x.f      ; The x normal of the vertex
    normal_y.f      ; The y normal of the vertex
    normal_z.f      ; The z normal of the vertex
    vcolor.l        ; The 32bit ARGB color of the vertex
    texture_x.f     ; the x co-ordinate of the vertex on the texture (0 To 1)
    texture_y.f     ; the y co-ordinate of the vertex on the texture (0 To 1)
  EndStructure
  
  ; a vector consisting of 3 float values
  Structure IRR_VECTOR
    x.f
    y.f
    z.f
  EndStructure
  
  ; a vector consisting of 2 float values
  Structure IRR_VECTOR2DF
    x.f
    y.f
  EndStructure
  
  
; a structure to hold a Positin in Longs
Structure IRR_POS2D
    x.l;
    y.l;
EndStructure

; a structure to hold a Positin in Floats
Structure IRR_POS2DF
    x.f;
    y.f;
EndStructure

; a structure to hold a Dimension in Longs
Structure IRR_DIM2D
    width.l;
    height.l
EndStructure

; .............................................................................
; Irrlicht Object Types - these are used To represent objects in the Irrlicht
; engine And their purpose is To warn the programmer of type errors
; (thanks To v1ctor For the advice on this correction)

; Irrlicht Object Types
Structure irr_mesh            : EndStructure  ; Mesh
Structure irr_texture         : EndStructure  ; Texture
Structure irr_node            : EndStructure  ; Node
Structure irr_camera          : EndStructure  ; Camera
Structure irr_terrain         : EndStructure  ; Terrain
Structure irr_selector        : EndStructure  ; Selector
Structure irr_particle_system : EndStructure  ; Particle 
Structure irr_animator        : EndStructure  ; Animator
Structure irr_font            : EndStructure  ; 2D Font

; Irrlicht Model Object Definition
Structure irr_model
  StructureUnion 
    *node.irr_node                    ; As irr_node
    *camera.irr_camera                ; As irr_camera
    *terrain.irr_terrain              ; As irr_terrain
    *particles.irr_particle_system    ; As irr_particle_system
  EndStructureUnion    
EndStructure 

;----- Functions  ----------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;System Functions

; a structure for a GUI-Event
Structure GuiEventStruct
  *caller.l
  EventType.l
EndStructure 

; a structure for a Mouse-Event
Structure MouseEventStruct 
  x.l
  y.l
  wheel.f
  event.l
EndStructure

; a structure for a Key-Event
Structure KeyEventStruct 
  Char.l     ; Eventually becomes Type: String in the Future
  key.l
  PressedDown.l
  Shift.l
  Control.l  
EndStructure

; a structure for a Log-Event
Structure LogEventStruct
  text.l ; point auf Text !
  Level.l 
EndStructure 

; a structure for a User-Event
Structure UserEventStruct 
  UserData1.l
  UserData2.l
  UserData3.f 
EndStructure 
  
; a structure for Events in the EventReceiver
Structure SEvent
  EventType.l
  StructureUnion
    GuiEvent.GuiEventStruct
    MouseEvent.MouseEventStruct
    KeyEvent.KeyEventStruct
    LogEvent.LogEventStruct
    UserEvent.UserEventStruct
  EndStructureUnion
EndStructure
  
Structure IRR_KEYMAP
  action.l
  KeyCode.l
EndStructure 


Structure SD3D9VideoData
  *D3D9.l         ; IDirect3D9* D3D9;
  *D3D9Dev.l      ; IDirect3DDevice9* D3DDev9
  hwnd.l          ; s32 hwnd
EndStructure 

Structure SD3D8VideoData
  *D3D8.l         ; IDirect3D9* D3D9
  *D3D8Dev.l      ; IDirect3DDevice9* D3DDev9
  hwnd.l          ; s32 hwnd 
EndStructure 

Structure SOpenGLVideoData
  hdc.l           ; s32 hdc
  HRc.l           ; s32 HRc
  hwnd.l          ; s32 hwnd
EndStructure 

Structure SOpenGLLinuxVideoData
  ; XWindow Handle
  Window.l        ; u32 Window
EndStructure 

; D3D Structure for Direct Rendering Device Access
Structure SExposedVideoData
  StructureUnion 
    D3D9.SD3D9VideoData
    D3D8.SD3D8VideoData
    OpenGL.SOpenGLVideoData
    OpenGLLinux.SOpenGLLinuxVideoData
  EndStructureUnion
EndStructure 

; Physics Structure for Car Movement
Structure sPhysicsCar
	carBodyOffset.IRR_VECTOR      ; The car body's offset from the origin. For example, 
                                ; if you want the car body To be 5 units above the ground, this should be (0, 5.0f, 0).

	carBodySize.IRR_VECTOR        ; The car body is currently modelled As a box, With these dimensions. This will be changed soon!
  carMass.f                     ; The car's mass. Note that this is the mass of the car body, not the tires.

	frontAxleOffset.f             ; The front axle's offset from the origin. This will determine how far forwards the front wheels are.
	rearAxleOffset.f              ; The rear axle's offset from the origin. This should be a positive number. 
                                ; This will determine how far back the rear wheels are.

	axleWidth.f                   ; The axle width. The same value will be used For both front And back axles. 
                                ; This will determine how far apart the left And right wheels are.

	tireMass.f                    ; The mass of a tires.

	tireWidth.f                   ; The width of a tires.
	tireRadius.f                  ; The radius of a tire.

	
	*carBodyNode.l                ; The scene-node For the car body.
	
	*tireNode_FL.l                ; The scene node For the FRONT LEFT tire.
	
	*tireNode_FR.l                ; The scene node For the FRONT RIGHT tire.
	
	*tireNode_RL.l                ; The scene node For the REAR LEFT tire.
	
	*tireNode_RR.l                ; The scene node For the REAR RIGHT tire.

	
  tireSuspensionShock.f         ; The tire suspension shock - this is passed straight To Newton. 
                                ; From the Newton docs: 
                                ; "parametrized damping constant for a spring, mass, damper system. 
                                ; A Value of one corresponds To a critically damped system."
  
	tireSuspensionSpring.f        ; The tire suspension spring - this is passed straight To Newton. 
                                ; From the Newton docs: 
                                ; "parametrized spring constant for a spring, mass, damper system. 
                                ; A Value of one corresponds To a critically damped system."
  
	tireSuspensionLength.f        ; The tire suspension length - this is passed straight To Newton. 
                                ; From the Newton docs: 
                                ; "distance from the tire set position to the upper stop on the vehicle body frame. 
                                ; The total suspension length is twice that."

	maxSteerAngle.f               ; The max steering angle the car is able To make. 
                                ; A higher number means the car will be able To make tighter turns.
  
	maxTorque.f                   ; The max torque that the car can produce. A higher number means that the car gan go faster, 
                                ; but too high And it will start To skid And loose Control.
  
	maxBrakes.f                   ; The max brakes that the car has. A higher number means stronger brakes.
EndStructure


; Include Importfile
XIncludeFile #PB_Compiler_Home + "Includes\Imports\IrrlichtWrapper_Import.pbi"

; Simple handy thingies

; Hide / Unhide Mouse Simplified
Macro IrrHideMouse(): IrrDisplayMouse(0) : EndMacro
Macro IrrShowMouse(): IrrDisplayMouse(1) : EndMacro

; Return shifted ARGB ( Used for Texture and 2D Access )
Procedure.l IrrMakeARGB ( Alpha.b, Red.b, Green.b, Blue.b)
    ProcedureReturn (Alpha << 24) + (Red << 16) + (Green << 8) + Blue
EndProcedure

; Convert an PB Image into a Irrlicht 2D Texture.
Procedure.l IrrPBImage2Texture(ImageNo.l, name.s, hasAlpha.l = #True)
  Define *Image.l = 0
  Define Format.l = #IRR_ECF_A8R8G8B8  ; Standard
  Define ImageID.l
  Define ImageInfo.BITMAP
  Define ImWidth.l, ImHeight.l, ImBytes.l, ImLine.l
  Define *IImage.l, *MeshTexture.l
  
  ImageID = ImageID(ImageNo)
  If GetObject_(ImageID, SizeOf(BITMAP), @ImageInfo)
    ImWidth  = ImageInfo\bmWidth
    ImHeight = ImageInfo\bmHeight
    ImBytes  = ImageInfo\bmBitsPixel/8
    ImLine   = ImWidth*ImBytes
    *Image   = ImageInfo\bmBits
  EndIf
  
  ; find the right color Format
  Select ImageDepth(ImageNo)
    Case 1,8, 16
      Debug("Image needs minimal Depth of 24 Bit !!")
    Case 24
      If hasAlpha 
        Format = #IRR_ECF_A1R5G5B5
      Else
        Format = #IRR_ECF_R8G8B8
      EndIf
    Case 32
      If hasAlpha 
        Format = #IRR_ECF_A8R8G8B8
      Else
        Format = #IRR_ECF_R8G8B8
      EndIf
  EndSelect       
   
  ; Create an Image from Data
  *IImage.l = IrrCreateImageFromData(Format,ImWidth,ImHeight,*Image)
  *MeshTexture = IrrAddTextureFromImage(name,*IImage)
  IrrDropPointer(*IImage)
  
  ProcedureReturn  *MeshTexture
EndProcedure 

; IRRFace Target Node (Aligns Sourcenode to Look at Targetnode )
; Returns #True / #False (alignment / no alignment)
;
; Usage:
; If IrrFaceTargetNode( *source_node, *target_node )
;   Debug "Aligned"
; EndIf
;
Procedure IrrFaceTargetNode( *src.irr_node, *target.irr_node )
  Protected ndiff.IRR_VECTOR, targetPos.IRR_VECTOR, srcPos.IRR_VECTOR, degree.f
  
  ; Check Pointers
  If *target And *src
    ; Get Source Position
    IrrGetNodePosition(*src, @srcPos\x, @srcPos\y, @srcPos\z)
    ; Get Target Position
    IrrGetNodePosition(*target, @targetPos\x, @targetPos\y, @targetPos\z)
    ; Calculate Diff
    ndiff\x = targetPos\x - srcPos\x
    ndiff\y = targetPos\y - srcPos\y
    ndiff\z = targetPos\z - srcPos\z
    
    ; Check X
    If ndiff\x = 0.0
      ProcedureReturn #False
    Else
      ; Calculate Degree
      degree.f = (ATan(ndiff\z / ndiff\x) * (180.0 / #PI))
      
      ; Fix Delta
      If srcPos\x - targetPos\x > 0
        degree.f = 90 - degree.f
      Else
        degree.f = -90 - degree.f
      EndIf
      ; Rotate
      IrrSetNodeRotation(*src ,0.0, degree.f, 0.0)
      ProcedureReturn #True
    EndIf
  Else
    ProcedureReturn #False
  EndIf
  
EndProcedure


; IDE Options = PureBasic 4.30 (Windows - x86)
; CursorPosition = 230
; FirstLine = 225
; Folding = -