;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper - Michael Taupitz (2007)
;; PureBasic-Examples by Michael Taupitz (neotoma) & Marius Eckardt (Thalius) 
;; ----------------------------------------------------------------------------
;; Example 07: Particle System
;; This example creates a couple of simple particle systems To demonstrate
;; particle system effects
;; ----------------------------------------------------------------------------

;; ////////////////////////////////////////////////////////////////////////////
;; Include the Irr3D-Requester
XIncludeFile "Irr3DRequester.pb"

EnableExplicit

;; ////////////////////////////////////////////////////////////////////////////
;; Global variables

; irrlicht objects
Define *ParticleTexture.irr_texture
Define *TestNodeTexture.irr_texture
Define SmokeParticles.irr_model
Define SmokeEmitter.IRR_PARTICLE_EMITTER
Define *ParticlesNode.irr_node
Define *TestNode.irr_node
Define *camera.irr_camera

Define *em.l  ; pointer to the Emitter

; -----------------------------------------------------------------------------
; To Start the Irrlicht-Device we use the Irr3DRequester, to make it more comfortable
; for the Users, and also easier to test different resolutions and devices.
If Irr3DRequester()

  IrrSetTextureCreationFlag(#IRR_ETCF_CREATE_MIP_MAPS , #False)

  IrrSetTextureCreationFlag(#IRR_ETCF_OPTIMIZED_FOR_QUALITY,#True)

  ; send the window caption
  IrrSetWindowCaption( "Example 07: Particle Systems" )
  
  ; this adds a small test node to the scene, a test node is a simple cube, that
  ; you can move and texture like any other object however there is another
  ; reason for adding this node. the particle system needs another node rendered
  ; onto the display to allow it to display material effects. this may be an issue
  ; with the wrapper that needs investigation
  *TestNode = IrrAddCubeSceneNode(10)
  
  ; load a wall texture for our box
  *TestNodeTexture = IrrGetTexture( "media\wall.jpg" )
  
 IrrSetNodeMaterialType ( *TestNode, #IRR_EMT_TRANSPARENT_ADD_COLOR  ) ; uncomment me... ;)
  ; apply the texture to the box
  IrrSetNodeMaterialTexture( *TestNode, *TestNodeTexture, 0 )
  ; Switch Lighting off - as we have no lights in the scene anyway ..
  IrrSetNodeMaterialFlag( *TestNode, #IRR_EMF_LIGHTING, #IRR_OFF )
  
  ; -*
  ; Exercise: Torchlike...
  ; This shows how Material flags affect the "Effect" of a Material
  ; Set our BoxNode Material to ADDITIVE Blending. 
  ; This will make the Smoke overdraw our Box : this Results in a "burn-like" Effect 
  ; for Example suitable for a Torch ( using the appropriate Textures and
  ; Emitter Settings for our Particles ). 
  ; Have fun! ;) ...
  ;
  ; -*
  
  ; add a particle system to the irrlicht scene manager, a default emitter is not
  ; created as we will define out own. the particle system is blank at the moment
  *ParticlesNode = IrrAddParticleSystemToScene( #IRR_NO_EMITTER )
  
  ; when the particle system starts it creates objects similar to billboards,
  ; this command specifies how large each of those particles will be
  IrrSetParticleSize( *ParticlesNode, 12.0, 10.0 )
  
  ; here we define the particle emitter, a particle emitter is a mechanism that
  ; creates the particles over time and destroys them when their lifetime runs
  ; out. The parameters include: a box that defines the volume in which particles
  ; are created - the larger the box the more diffuse the particle cloud, a
  ; direction that determines where the particles drift off too, a range that
  ; defines hoe many particles will be generated each second, a colour range that
  ; determines a random tint that is applied to the particles, a range that
  ; determines the lifespan of the particles - the large the values the longer
  ; and larger (and slower) your particle cloud will be and finally an angle that 
  ; defines a cone around your direction that is the maximum deviation from the
  ; direction the particles can take - values here make your particles billow
  ; outward
  SmokeEmitter\min_box_x = -7
  SmokeEmitter\min_box_y = 0
  SmokeEmitter\min_box_z = -7
  SmokeEmitter\max_box_x = 7
  SmokeEmitter\max_box_y = 1
  SmokeEmitter\max_box_z = 7
  SmokeEmitter\direction_x = 0
  SmokeEmitter\direction_y = 0.06
  SmokeEmitter\direction_z = 0
  SmokeEmitter\min_paritlcles_per_second = 80
  SmokeEmitter\max_paritlcles_per_second = 100
  SmokeEmitter\min_start_color_red = 255
  SmokeEmitter\min_start_color_green = 255
  SmokeEmitter\min_start_color_blue = 255
  SmokeEmitter\max_start_color_red = 255
  SmokeEmitter\max_start_color_green = 255
  SmokeEmitter\max_start_color_blue = 255
  SmokeEmitter\min_lifetime = 800
  SmokeEmitter\max_lifetime = 2000
  SmokeEmitter\max_angle_degrees = 15
  
  ; finally we add this particle emitting object to the particle system
  *em.l = IrrAddParticleEmitter( *ParticlesNode, @SmokeEmitter )
  
  
  ; an affector is a mechanism that alters the particles over time, the fade out
  ; affector gradually fades the particles out so they are invisible when they
  ; are deleted this lets them smoothly vanish instead of poping out of existence
  IrrAddFadeOutParticleAffector( *ParticlesNode )
  
  ; another affector is the gravity affector, this adds a small amount of velocity
  ; to the particles each frame, although its called a gravity affector it can
  ; be used to push the particles in any direction so you can have drifting smoke
  ; bubbling fountains, etc ...
  IrrAddGravityParticleAffector ( *ParticlesNode, 0.05, 0.05, 0.0 )
  
  ; at the moment the particles are simple grey squares and before we apply a
  ; material to them we need to get the particle system as a scene node
  ;;ParticlesNode = SmokeParticles
  
  ; load a grey smoke like image for the particle
  *ParticleTexture = IrrGetTexture( "media\ParticleGrey.bmp" )
  
  ; apply the texture to the particles system, this texture will now be drawn
  ; across each particles surface
  IrrSetNodeMaterialTexture( *ParticlesNode, *ParticleTexture, 0 )
  
  ; out particle system is not affected by lighting so we make it self
  ; illuminating
  IrrSetNodeMaterialFlag( *ParticlesNode, #IRR_EMF_LIGHTING, #IRR_OFF )

  IrrSetNodeMaterialFlag( *ParticlesNode, #IRR_EMF_ZWRITE_ENABLE, #False )

;   
; 	ps->setMaterialFlag(video::EMF_LIGHTING, false);
; 	ps->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
; 	ps->setMaterialTexture(0, driver->getTexture("../../media/fire.bmp"));
; 	ps->setMaterialType(video::EMT_TRANSPARENT_VERTEX_ALPHA);  
  
  ; as the particle texture has black borders, each overlapping particle would
  ; draw this blackness ontop of one another as a sharp egde they would look like
  ; a lot of solid squares instead of the transparent gas effect we are trying
  ; to created. so here we change the material type of the node - instead of
  ; drawing the node solidly onto the screen it is added onto the colour
  ; underneath itself so black doesnt change the color and white would build up
  ; top of the existing color until it completly saturates the pixel
  IrrSetNodeMaterialType ( *ParticlesNode, #IRR_EMT_TRANSPARENT_VERTEX_ALPHA  )
  
  ; add a fixed position camera to the scene so we can view the particle system.
  ; the first three parameters are the cameras location, the second three
  ; parameters are where the camera is looking at
  *camera = IrrAddCamera( 100,40,0, 0,40,0 )
  
  ; -----------------------------------------------------------------------------
  ; while the irrlicht environment is still running
  While IrrRunning()
      ; clear the canvas to black to show the particles up better
      IrrBeginScene( 0, 0, 0 )
  
      ; draw the scene
      IrrDrawScene()
  
      ; end drawing the scene and display it
      IrrEndScene()
  Wend
  
  ; -----------------------------------------------------------------------------
  ; Stop the irrlicht engine and release resources
  IrrStop()
  
EndIf

End

; jaPBe Version=3.7.2.629
; Build=0
; FirstLine=23
; CursorPosition=44
; ExecutableFormat=Windows
; DontSaveDeclare
; IDE Options = PureBasic 4.10 Beta 3 (Windows - x86)
; CursorPosition = 76
; FirstLine = 54
; Folding = -