Routine for making a sprite dissolve?

BlitzPlus Forums/BlitzPlus Programming/Routine for making a sprite dissolve?

First set up a function to go through your image and count how many non black pixels you have. Then assign them each to an array. Then travel through that array 10 at a time fading them out to a predeterminined palette (stored in an array). The travel is determined by a prime number greater than the amount of pixels (you can look these up on the internet) various primes look different and you can make the formula more random (forgotten how at the moment) but you can look it up. The example is quick and tries to show it but I think you need to extend the palette to at least 32 and have them fade slowly to get a decent effect. Hope it helps

eg

[/code]
Graphics 320,240,32,2
sprite=CreateImage(16,16)
SetBuffer ImageBuffer(sprite)
Rect 2,5,11,5,1
Rect 5,2,5,11,1
count=0
arraycount=0

Dim cp(10);the amount of pixels we want picked at a time


SetBuffer ImageBuffer(sprite)
LockBuffer ImageBuffer(sprite)
For nx=0 To 15
For ny=0 To 15
temp=ReadPixelFast(nx,ny)
If temp=$FF000000;which is black FF is the alpha value
Else
count=count+1
EndIf
Next
Next
UnlockBuffer

;once you know the number
;arrays used for speed
Dim fadex(count)
Dim fadey(count)



SetBuffer ImageBuffer(sprite)
LockBuffer ImageBuffer(sprite)

For ny=0 To 15
For nx=0 To 15
temp=ReadPixelFast(nx,ny)
If temp=$ff000000
Else
fadex(arraycount)=nx
fadey(arraycount)=ny
arraycount=arraycount+1
If arraycount=4
;Stop
EndIf
EndIf
Next
Next
UnlockBuffer

;setupfast fade palette
;you may want to change to rand(100-(n*8),255-(n*8))
;so later colours are darker
Dim palette(16)
For n=1 To 15
r=Rand(100,255)
g=Rand(100,255)
b=Rand(100,255)
palette(n)=r Shl 16 + g Shl 8 + b
Next
palette(16)=$ff000001;last colour black

;now To test
pixel=Rand (84); random starting pixel must call this before function starts

For time=0 To 600
If time>120 ;wait 2 seconds then disolve
If totalcount<84
SetBuffer ImageBuffer (sprite)
LockBuffer ImageBuffer (sprite)
;now here the random bit what you need is a prime
;number greater than the number of pixels you're going
;to cycle through in this case 89 (you can look these
;up on the internet it works better with non uniform shapes
;85 pixels in this image

If palcount=0 ;this makes sure all pixels are faded out
;before selecting the next 10 pixels
For n=0 To 9
cp(n)=pixel
pixel=pixel+103
While pixel>84;greater than pixel number
pixel=pixel-85
Wend
Next
palcount=1
Else
For n=0 To 9
WritePixelFast(fadex(cp(n)),fadey(cp(n)),palette(palcount))
Next
palcount=palcount+1
If palcount=17
totalcount=totalcount+10
palcount=0
EndIf
EndIf
UnlockBuffer
EndIf
EndIf


SetBuffer BackBuffer()
Text 0,0,count
DrawImage sprite,100,100
Flip

Next
[/code]