Code archives/Graphics/Pixel explosion effect

This code has been declared by its author to be Public Domain code.

Download source code

Pixel explosion effect by Mr Snidesmin2007
Just need to add code to save the frontbuffer...
Const gXd=256
Const gYd=256



Graphics gXd, gYd, 0, 2



Type bPix
	Field T	;Temperature (0-768)
	Field x#, y#
	Field decay%
	Field dX#, dY#
End Type


Dim bPix.bPix(gXd, gYd)




While Not KeyHit(1)
	If KeyHit(57) Then
		Cls
		x = gXd/2 
		y = gYd/2 
		bPix(x, y) = New bPix
		bPix(x, y)\x = x
		bPix(x, y)\y = y
		bPix(x, y)\T = 865
		bPix(x, y)\decay = (bPix(x, y)\T/100) ^ 2+8
	End If
	
	Update
Wend
End


Function Update()
	For p.bPix = Each bPix
		setcol p\T
		Plot p\x, p\y	
		
		p\x = p\x + p\dX
		p\y = p\y + p\dY

				
		For dx=-1 To 1
		For dy=-1 To 1
			x = p\x+dx	
			y = p\y+dy	
			
			If x>0 And x<=gXd And y>0 And y<=gYd Then
				If bPix(x, y) = Null And Rnd(10)>8 Then
					bPix(x, y) = New bPix
					bPix(x, y)\x = x
					bPix(x, y)\y = y
					bPix(x, y)\T = p\T-Rnd(20)
					spd# = Rnd(1.5)
					dir# = Rnd(360)
					bPix(x, y)\dX = spd*Cos(dir)
					bPix(x, y)\dY = spd*Sin(dir)
					
					
					If bPix(x, y)\T > 0 Then
						bPix(x, y)\decay = (bPix(x, y)\T/100) ^ 2+8
					Else
						Delete bPix(x, y)
					End If
				End If
			End If
		Next
		Next
		
			
		p\T = p\T - Rnd(p\decay) 
		If p\T <= 0 Then Delete p
	Next
End Function



Function SetCol(t%)
	If t >= 256*3 Then
		Color 255, 255, 255
	ElseIf t >= 256*2 Then
		Color 255, 255, t-256*2
	
	ElseIf t >= 256
		Color 255, t-256, 0
		
	ElseIf t > 0
		If t > 64 Then
			gb = (256-t)/3	
		Else
			gb = t
		End If
		
		Color t, gb, gb
		
	Else
		Color 0, 0, 0
	End If	
End Function

Comments

b322007
Cool! :D


klepto22007
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



Mr Snidesmin2007

I hope it is ok that I have made a Blitzmax conversion with some small additions.


sure... it's public domain after all...


mkg2007
Nice - I may use that! Thanks.


_PJ_2009
I know this is a biut of an old thread, but Ive just had a good luck at it. I'm curious to how writepixelfast may be worth adding


markcw2009
BlitzMax version:
I just get a black image saved although the initial image draws, am I missing something?

"Burn baby, burn!"


Code Archives Forum