; ParticleSort Example
; --------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,2,-6
CameraClsColor camera,8,12,24

floor=CreatePlane()
EntityColor floor,30,34,44

; Thick alpha-blended smoke - overlapping puffs reveal draw-order errors
emitter=CreateParticleEmitter(600)
PositionEntity emitter,0,1,0
ParticleEmitterShape emitter,2,1.2,1.2,1.2
ParticleEmissionRate emitter,80
ParticleLifetime emitter,2.5,3.5
ParticleVelocity emitter,0,0.6,0,0.3,0.2,0.3
ParticleColor emitter,200,200,210,0.9,120,120,130,0
ParticleSize emitter,0.3,0.6,0.2
EntityBlend emitter,2

; Start with back-to-front sorting on
ParticleSort emitter,True
sorted=True

While Not KeyDown(1)

    ; Space toggles back-to-front sorting for the transparent puffs
    If KeyHit(57)
        sorted=Not sorted
        ParticleSort emitter,sorted
    EndIf

    ; Arrow keys move the camera - orbit through the smoke to compare
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    ; UpdateWorld also advances the particle simulation
    UpdateWorld
    RenderWorld

    Text 0,0,"Arrow keys: move camera   Space: toggle sorting   Esc: exit"
    If sorted Then state$="True (puffs layer correctly)" Else state$="False (watch for popping overlaps)"
    Text 0,20,"ParticleSort emitter,"+state$

    Flip

Wend

End
