
-----------------------------------
copthesaint
Wed Feb 02, 2011 9:46 pm

Particle Emitter Try
-----------------------------------
So I tried to make this particle emitter thing in my spare time, heres how it works.
First thing you have to do is create a new pointer to this class, backImage class.

class backImage
    export imageWidth, imageHeight, loadImage, imageColor
    var sizeX, sizeY : int
    var colorScale : flexible array 0 .. -1 of int
    fcn imageWidth : int
        result sizeX
    end imageWidth
    fcn imageHeight : int
        result sizeY
    end imageHeight
    proc loadImage (pictureName : string)
        var winTemp, winPre : int
        winPre := Window.GetActive
        var picTemp : int := Pic.FileNew (pictureName)
        sizeX := Pic.Width (picTemp)
        sizeY := Pic.Height (picTemp)
        winTemp := Window.Open ("position:top;right,offscreenonly,nobuttonbar,graphics:" + intstr (sizeX) + ";" + intstr (sizeY))
        Window.SetActive (winTemp)
        Pic.Draw (picTemp, 0, 0, picCopy)
        for x : 0 .. sizeX
            for y : 0 .. sizeY
                new colorScale, upper (colorScale) + 1
                colorScale (upper (colorScale)) := whatdotcolor (x, y)
            end for
        end for
        Pic.Free (picTemp)
        Window.Close (winTemp)
        Window.SetActive (winPre)
    end loadImage
    fcn imageColor (x : int, y : int) : int
        result colorScale (((x mod (sizeX + 1)) * sizeY) + (y mod (sizeY + 1)))
    end imageColor
end backImage
For example you have an image 4x4 pixels, a checkerboard patern. It will save this image in a flexible array so its colors can be used later for an emitter
http://i56.tinypic.com/i5yq9c.jpg
Now that we have an image to use, I made a particle emitter to draw particles based on the color of the image at the specified pixel.
Create a new pointer to ParticleEmitter.
class ParticleEmitter
    inherit backImage
    export DrawParticle, ChangeVelocity, MovePosition, ChangeParticleLifeTime, ChangeParticleCount, ResetEmitter
    var dynIndex, indexSize, indexMax, pLifeTime : int := -1
    var posX, posY, pVel, pVelAngle, pVelX, pVelY : real
    var particleDraw : flexible array 0 .. -1 of boolean
    var particleX, particleY, particleVel, particleVelAngle, particleVelX, particleVelY, particleLife, particleBirthTime : flexible array 0 .. -1 of real
    proc MovePosition (x : real, y : real)
        posY := y
        posX := x
    end MovePosition
    proc ChangeVelocity (x : real, y : real, t : int)
        case t of
            label 0 :
                pVelX := x
                pVelY := y
                pVel := 0
                pVelAngle := 0
            label 1 :
                pVelX := 0
                pVelY := 0
                pVel := x
                pVelAngle := y
            label :
                Error.Halt ("There is no such type.")
        end case
    end ChangeVelocity
    proc ChangeParticleLifeTime (x : int)
        pLifeTime := x
    end ChangeParticleLifeTime
    proc ChangeParticleCount (x : int)
        indexMax := x
    end ChangeParticleCount
    proc DrawParticle (image : pointer to backImage)
        for i : 0 .. indexSize
            particleX (i) := particleX (i) + particleVelX (i) + particleVel (i) * cosd (particleVelAngle (i) + 90)
            particleY (i) := particleY (i) + particleVelY (i) + particleVel (i) * sind (particleVelAngle (i) + 90)
            drawdot (round (particleX (i)), round (particleY (i)), image -> imageColor (round (particleX (i)), round (particleY (i))))
            if Time.ElapsedCPU > particleLife (i) + particleBirthTime (i) then
                if indexSize > upper (particleDraw) then
                    Error.Halt ("Array subscript is out of range. Value sent was: " + intstr (indexSize) + " which is greater then max of: " + intstr (upper (particleDraw)))
                end if
                particleDraw (indexSize) := false
                particleX (i) := particleX (indexSize)
                particleY (i) := particleY (indexSize)
                particleVel (i) := particleVel (indexSize)
                particleVelAngle (i) := particleVelAngle (indexSize)
                particleVelX (i) := particleVelX (indexSize)
                particleVelY (i) := particleVelY (indexSize)
                particleLife (i) := particleLife (indexSize)
                particleBirthTime (i) := particleBirthTime (indexSize)
                particleX (indexSize) := 0
                particleY (indexSize) := 0
                particleVel (indexSize) := 0
                particleVelAngle (indexSize) := 0
                particleVelX (indexSize) := 0
                particleVelY (indexSize) := 0
                particleLife (indexSize) := 0
                indexSize := indexSize - 1

            end if
        end for

        if indexSize < indexMax then
            dynIndex := indexSize + 1
            if upper (particleDraw)  imageWidth)
            particleDraw (dynIndex) := true
            particleVel (dynIndex) := pVel
            particleVelAngle (dynIndex) := pVelAngle
            particleVelX (dynIndex) := pVelX
            particleVelY (dynIndex) := pVelY
            particleX (dynIndex) := posX + randTrans * cosd (particleVelAngle (dynIndex))
            particleY (dynIndex) := posY + randTrans * sind (particleVelAngle (dynIndex))
            particleLife (dynIndex) := pLifeTime
            particleBirthTime (dynIndex) := Time.ElapsedCPU
            indexSize := indexSize + 1
        end if
    end DrawParticle

    proc ResetEmitter
        for i : 0 .. indexSize
            particleDraw (i) := false
            particleX (i) := 0
            particleY (i) := 0
            particleVel (i) := 0
            particleVelAngle (i) := 0
            particleVelX (i) := 0
            particleVelY (i) := 0
            particleLife (i) := 0
        end for
        indexSize := -1
    end ResetEmitter
end ParticleEmitter
So what if for example you want the color from say point 5,9? well the image in a sort is 'stacked' so the color of the particle really.
http://i56.tinypic.com/1zzsunl.jpg
The particles will change color depending on the image in what ever direction the particles are going.

The below is a download example with all parts of the files together.
