This looks very good :)
I hope it is ok that I have made a Blitzmax conversion with some small additions. Every frame is saved as a seperate PNG file and after rendering it could also be saved as an animstrip.
Type TPixelExplosion
Field Image:TImage
Field Width:Int = 256
Field Height:Int = 256
Field Map:TExPix[,]
Field Pixels:TList = New TList
Field Frame:Int = 0
Function Create:TPixelExplosion(W:Int = 256,H:Int = 256)
Local E:TPixelExplosion = New TPixelExplosion
E.Width = W
E.Height = H
E.Init()
Return E
End Function
Method Init()
Image = CreateImage(Width,Height)
Pixels.Addlast(TExPix.Create(Width/2,Height/2,760,760/100.0^2+8))
Map = New TExPix[Width,Height]
Map[Width/2,Height/2] = TExPix(Pixels.Last())
End Method
Method Update()
Local PixMap:TPixmap = LockImage(Image)
ClearPixels(PixMap)
For Local p:TExPix = EachIn Pixels
Local C:Int = Setcol(P.T)
WritePixel PixMap,P.X,P.Y,C
P.X = P.X + P.DX
P.Y = P.Y + P.DY
For Local dx:Int = -1 To 1
For Local dy:Int = -1 To 1
Local X:Float
Local Y:Float
X = P.X+DX
Y = P.Y+DY
If X>0 And x<Width-1 And y>0 And Y<Height-1 Then
If Map[x,y] = Null And Rnd(10)>8 Then
Map[X,Y] = TExPix.Create(X,Y,P.T-Rnd(20),0)
Local spd:Float = Rnd(1.5)
Local dir:Float = Rnd(360)
Map[X,Y].dx = spd*Cos(dir)
Map[X,Y].dy = spd*Sin(dir)
Pixels.Addlast(Map[X,Y])
If Map[X,Y].T > 0 Then
Map[X,Y].decay = (Map[x, y].T/100.0) ^ 2+8
Else
Pixels.Remove(Map[X,Y])
'Map[X,Y] = Null
EndIf
EndIf
EndIf
Next
Next
P.T = P.T - Rnd(p.decay)
If p.T <= 0 Or (P.X<0 Or x>Width-1 Or y<0 Or Y>Height-1) Then
Pixels.Remove(P)
P = Null
EndIf
Next
UnlockImage(Image)
End Method
Method Draw(X:Int,Y:Int)
DrawImage(Image,X,Y)
End Method
Method Save(X:Int,Y:Int)
If Pixels.Count() > 1 Then
SavePixmapPNG(MaskPixmap(GrabPixmap(X,Y,Width,Height),0,0,0),"Frame"+Frame+".png")
Frame:+1
Else
'SaveAnimImage()
EndIf
End Method
Method SaveAnimImage()
Local Pix:TPixmap = CreatePixmap(Width*Frame,Height,PF_RGBA8888)
Print (Width*Height) + "Frames : " + Frame
'DebugStop()
For Local F:Int = 0 To Frame-1
Local PixMap:TPixmap = LoadPixmap("Frame"+f+".png")
For Local X:Int = 0 To Width-1
For Local Y:Int = 0 To Height-1
Local C:Int = ReadPixel(Pixmap,X,Y)
WritePixel(Pix,X+(F*Width),Y,C)
'Print "X:"+X+"Y:"+Y+"C:"+Hex(C)
Next
Next
Next
SavePixmapPNG(Pix,"AnimExpl.png")
Print "Ready ..."
End Method
End Type
Type TExPix
Field T:Int
Field X:Float
Field Y:Float
Field DX:Float
Field DY:Float
Field Decay:Float
Function Create:TExPix(x:Float,y:Float,Temp:Int,decay:Float)
Local P:TExPix = New TExPix
P.X = X
P.Y = Y
P.T = Temp
P.Decay = decay
Return P
End Function
End Type
Function SetCol:Int(t:Float)
If t >= 256*3.0 Then
SetColor 255, 255, 255
ElseIf t >= 256*2 Then
SetColor 255, 255, t-256*2
ElseIf t >= 256
SetColor 255, t-256, 0
ElseIf t > 0
If t > 64 Then
gb = (256-t)/3.0
Else
gb = t
End If
SetColor t, gb, gb
Else
SetColor 0, 0, 0
End If
Local R:Int
Local G:Int
Local B:Int
GetColor(R,G,B)
SetColor 255,255,255
Return IntColor(R,G,B)
End Function
Function IntColor(R,G,B,A=255)
'returns argb value from red, green, blue.
Return A Shl 24 | R Shl 16 | G Shl 8 | B Shl 0
End Function
Graphics 800,600,0,-1
Local Ex:TPixelExplosion = TPixelExplosion.Create(128,128)
Local anim:Byte = False
Local Img:TImage '= LoadAnimImage("AnimExpl.png",ex.width,ex.height,0,ex.frame)
Local fr:Float = 0
While Not KeyHit(KEY_ESCAPE)
Cls
Ex.Update()
Ex.Draw(0,0)
Ex.Save(0,0)
If KeyHit(KEY_SPACE) Then
Ex.SaveAnimImage()
img = LoadAnimImage("AnimExpl.png",ex.width,ex.height,0,ex.frame)
anim = True
End If
If anim = True Then
DrawImage img,0+ex.width,0,fr
fr:+.1
If fr>ex.frame-1 Then fr = 0
EndIf
Flip
Wend
|