//*********************************************************************************
//                          SPARK Wrapper Basic Demo
//                  This demo shows how SPARK engine works 
//*********************************************************************************


// if you don't have dbpro objects in the scene make sure you
// allways set the backdrop on !
 
backdrop on
color backdrop 0
load image "media\explosion.bmp",1
move camera 0,-2.5

// Ok! First we need a Particle System.Particle systems will hold different particle
// groups together.Like a Fire System will hold smoke and flame groups.
// Here's a diagram of SPARK objects which shows how they are connected to each other
// 
//                            PARTICLE SYSTEM
//                                   |
//       -------------------------------------------------
//       |                     |                         |                   
//     Group1                Group2                   Group...n                
//       |                     |                         |
//       |-Emitters            |-Emitters                |-Emitters
//       |-Modifiers           |-Modifiers               |-Modifiers 
//       |-Interpolators       |-Interpolators           |-Interpolators
//       |-Renderer            |-Renderer                |-Renderer
//
// Now lets create an empty particle system first 

mySystem = sp create system (0,0,0,0)

// The first parameter is the prototypeFlag , if set to 1 the system will be created
// as a prototype.Prototypes are systems that only exist in memory.They cannot be
// rendered or updated , but exist as a valid particle systems.This is a very useful
// feature , like create an explosion prototype and each time you need an explosion
// in the scene , just make a copy of you're explosion prototype.
// 
// newExplosion = SP COPY SYSTEM (ExplosionPrototype , posx# , posy# , posz#)

// In this case we don't want to create a prototype so set this flag to 0
// (the particle system is renderable)
// The next 3 parameters are the position of the system in 3D space
// The command also returns the ID of the newly created system.Use this ID
// as a "handle" to manipulate the system later

// Ok we have our system now it's time to add some groups to it.
// because this is a basic demo we will add only 1 group to this system.

myGroup = sp add group (mySystem , 200 , 0.01 , 0,0,0)

// Now let's see the parameters. The 1st parameter is the systemID where the group
// will be added. The 2nd (integer number) parameter is the capacity of 
// this group (The maximum number of particles that the group can emit ,in this case 200)
// the 3rd (float) parameter is the radius of the group.The radius is the global 
// scale factor of the particles. (try changing this parameter to see what it does)
// and the last 3 parameters are the position of the group in 3D space

// Now set the lifetime of the particles in this group

sp set group lifetime myGroup , 3.5 , 4.5

// The first parameter is the ID of the group.The last two parameters are
// the minimum lifetime and maximum lifetime.When a particle is born in a group
// his lifetime will be a random number between min and max value.
// if both values are equal , this constant value will be used for each particle.
// In our case particles will have a random lifetime between 3.5 and 4.5 

// The next thing we have to do is adding a renderer to the group.Each group can have
// only one renderer.There are currently 3 type of renderers in SPARK
// the DX9 Quad renderer - Particles are rendered as DX9 quads.
// The DX9 Point Renderer - The particles are rendered point sprites (3d sprites)
// The line renderer - Particles are rendered as simple lines.(Very useful when
// simulating rain or sparks particles)
// Ok let's create a quad renderer to this group

myRenderer = sp set quad renderer (myGroup , 1 , 5,5,2,2,BLEND_ADD)

// the first parameter is the GroupID where the renderer will be created
// the second the texture image id (loaded with LOAD IMAGE) 
// the next two parameters are the size of the quads (x and y size) 
// and the next 2 values are something called "atlas dimensions" x and y
// it's a pretty cool feature of the SPARK engine ! 
// lets say ,you can have 4 different smoke images in one texture
// so if you have a 128x128 texture and you set the atlas dimensions to 2,2
// youre texture will be divided up in 64x64 pieces and with a random interpolator     
// you can easily randomize different texture on each particle.
// so you're particles will be completely different from each other   
// If you want to use the whole texture as one single image just set the atlas 
// dimensions to 1,1 
// the last parameter is the blend mode parameter (see the spark.dba file for details)


// Now we add few interpolators !
// With interpolators you can do extremely cool things ! Set particles to grow
// or make them rotate , change the atlas dimensions ...

// The first parameter is the ID of the group where the interpolator will be added
// the second parameter is the PARAM that you want to interpolate there are 5
// PARAM_SCALE the scale (or the size) factor of the particles

// PARAM_MASS interpolates the mass of the particles (use this only if a 
// group has a GRAVITY modifier otherwise it has no effect)

// PARAM_ROTATION_SPEED Particles can be rotated in SPARK ! This parameter controls
// the speed of their roataion (Use this only if a group has a Rotator modifier !
// otherwise it has no effect)

// PARAM_ANGLE Similar to PARAM_ROTATION_SPEED except it uses constant angles , and
// does not require a modifier

// and PARAM_ATALAS_DIMESNIONS which will interpolate the texture 
// coordinates of a particle (atlas dimensions set in the quad renderer)

// The following parameters are float numbers. if you call this command with :

// 4 float numbers - the interpolator will be a RANDOM INTERPOLATOR.The first 2 floats
// are the minimum and maximum born value , the last 2 is the 
// minimum and maximum death value.So a particle will born with a random value between
// min and max born and lineary change to a random number between min and max death

 
// 2 floats + typeFlag - it uses constant numbers for born and death values.
// if you set the typeflag to 0 the interpolator will be a RANDOM INITIALIZER 
// it will generate random numbers between born and death value.
// if set to 1 it will be a SIMPLE INTERPOLATOR and will lineary
// change the particle's PARAM from born to death value.

// 1 float - The PARAM will be constant for the particle (This type
// of interpolator is called : DEFAULT INITIALIZER) 


// Ok our first interpolator will be a RANDOM INTERPOLATOR , and we will interpolate
// the PARAM_SCALE. We will set our particles born size between 0.8 and 1.0
// and the death size between 5.0 and 12.0

scaleInterpolator = sp add interpolator (myGroup , PARAM_SCALE , 0.8 , 1.0 , 5 ,12)

// The second interpolator will be a RANDOM INITIALIZER,
// and we will interpolate the PARAM_ATALAS_DIMESNIONS.
// Because we previously set the atlas dimensions to 2,2
// that means we will have 2 * 2 = 4 textures in one image (at 0,64 - 64,128 and 64,64
// 64,128) so we set the interpolator float values to 0 and 4 and the typeFlag to 1
// so the interpolator will randomize textures between 0 and 4
// this ensures each particle will have different texture.  

textureInterpolator = sp add interpolator (myGroup , PARAM_ATLAS_DIMENSIONS , 0 , 4 ,1)

// The third interpolator will be a SIMPLE INTERPOLATOR (same as the previous one,
// only the typeFlag is 0) and we will interpolate
// the PARAM_ANGLE. This will make our particles born with angle 0.01
// and lineary changes to 1.2

angleInterpolator = sp add interpolator (myGroup ,PARAM_ANGLE, 0.01 , 2.2,0)

// Now add an RGBA Interpolator to the group ! with this interpolator we can easily
// change the color of each particle , or create cool fade-out effect
// setting the alpha from 255 to 0 . Each Group can have only one RGBA intepolator
// The color interpolators works exacly as the param interpolators except that they
// change the color parameter of the textures and uses DWORDs as color values instead
// of float numbers

rgbaInterpolator = sp add rgba interpolator (myGroup , 0xEA3000FF , 0xAD500000 , 0)

// We use a SIMPLE INTERPOLATOR which means
// a particle is born with color 0x5A1010FF and chages lineary to 0x10201000
// the last parameter is the typeFlag . if set to 0 the interpolator will be
// a SIMPLE INTERPOLATOR if set to 1 it will be a RANDOM INITIALIZER (random
// colors between 0x5A3010FF and 0x10101000)

// And finally we need an emitter.You can add unlimited emitters to a group.
// But we will add only one emitter since this is a basic demo.
// There's currently 5 type of Emitters available in SPARK

// Spheric Emitter - emits particles in a portion of a sphere in a given direction.
// Random Emitter - emits particles in random direction
// Noraml Emitter - An emitter that emits particles following a Zone normals.
// Static Emitter - An Emitter that emits particles with no initial velocity.
// Straight Emitter - An Emitter that emits in a given direction.

// All the emitters have a :

// Flow parameter - float value , that determines how many particles can be emited
// at once by this Emitter.The flow is in the unit : nb of particle per unit step. 
// A flow of -1 (or any negative number) indicates an infinite flow which 
// means all particles in the emitters's tank are generated instantly.


// Tank parameter - Sets the initial number of particles in this emitter's tank. 
// Each time the emitter is updated, the number of particles emitted
// is deduced from the emitter tank. When the tank reaches 0, the emitter 
// will not emit any longer until it is refilled.
// A number of -1 (or any negative number) means the emitter has an infinite tank 
// which will never be empty.
// Note that having both negative tank and flow is illegal !

// min and max Force - The force of the emitter varies for each launch of a Particle 
// between a minimum and a maximum. To have a fixed force for the Emitter, 
// just have min = max.

// All other parameters are Emitter specific !
// (like only spheric emitters have angleA and angleB parameter and
// only straight and spheric emitters have a direction parameter)


// Now we add a spheric emitter to our group. 
// We set the direction to 0,1,0 ensure our particles will be emited upwards
// we set the angleA to 0.3 and angleB to 0.6 (random angle between 0.3 and 0.6)
// set the flow to 20.0  
// Set the min and max force to 0.1 and 0.5 (random force for each particle between
// 0.1 and 0.5)
// and set the tank to -1 so our Emitter never becomes empty
// And finally we define a zone of this emitter.There are currently 
// 5 types of zones in SPARK :
// Point Zone - particles from this emitter are emited from a point defined by this zone
// Sphere Zone - particles are emited from a 3d sphere shape.
// Plane Zone - particles are emited from a plane defined by a normal
// Ring Zone - particles are emited from a 3d ring
// Box Zone - Particles are emited from a 3d box

// in this case we use a sphere zone.
myEmitter = sp add spheric emitter (myGroup , 0 , 1 , 0 , 0.3 , 0.6 , 20 , 0.1 , 0.5 ,-1 , sp sphere zone(0,0,0 ,0.05))

// So in this tutorial we learned the basics of the SPARK engine.
// There just one thing left ! Update our particle system 
// in the main loop with the SP UPDATE command



do
   
  
    sp update
    
    
loop




















