2D Pixel exp thingi.

BlitzPlus Forums/BlitzPlus Programming/2D Pixel exp thingi.

Paul "Taiphoz"(Posted 2003) [#1]
http://www.teamrebellion.co.uk/pixelmorph.zip

This is a crap little gfx pixel type image thing ive done. I took and image cut it into blocks then scattered em, then I make em re form the image and then blow up again.

nofink special. what im wondering is if there is any way of making this faster. and would you mind showing me how ?

;
;	Pixel Morph Example.
;		By Yavin
;
;
Const gfx_width=640
Const gfx_height=480
Const gfx_bit=16
Const gfx_window=0

Graphics gfx_width,gfx_height,gfx_bit,gfx_window
SetBuffer BackBuffer()
Cls

Type pixel

	Field frame
	Field image
	
	Field x#
	Field y#

	Field dx#
	Field dy#
	
	Field r
	Field g
	Field b
	Field dest_x#
	Field dest_y#
End Type

Global pixel_width%
Global pixel_height%

Const bit_size=5
Global pixelmap=LoadImage("yav.bmp")

Global pw=ImageWidth(pixelmap)
Global ph=ImageHeight(pixelmap)
FreeImage pixelmap

pixelmap=LoadAnimImage("yav.bmp",bit_size,bit_size,0,(pw/bit_size)*(ph/bit_size)):MaskImage pixelmap,255,0,255


Global pixel.pixel = New pixel : Delete pixel


loadpixelmap()
Global counter

While Not KeyHit(1)

	Cls
		updatepixelmap()
		Drawpixelmap(250,250,True,pixelmap)
		Text 1,1,"Working"
		counter=counter+1
		
	Flip
	If counter=1 Then WaitKey()
	If counter=100 Then WaitKey()

Wend

FreeImage pixelmap

For pixel.pixel = Each pixel
	Delete pixel
Next


Function updatepixelmap()
	For pixel.pixel = Each pixel
			pixel\x=pixel\x+pixel\dx
			pixel\y=pixel\y+pixel\dy
	Next
End Function

Function drawpixelmap(x=0,y=0,Midhand=False,image)

	If Midhand=True
		
		Local midx,midy,width,height
		midx=pw/2
		midy=ph/2
		x=x-midx
		y=y-midy
	End If

	For pixel.pixel = Each pixel
		DrawImage pixelmap,pixel\x*bit_size,pixel\y*bit_size,pixel\frame
		;DebugLog pixel\frame
	Next

End Function

Function loadpixelmap()
	;Stop
	SeedRnd MilliSecs()
	Local row,col,fr
	fr=0
	row=0
	col=0
	For looper=1 To (pw/bit_size)*(ph/bit_size)

		fr=fr+1
		
		If row=>pw/bit_size
			col=col+1
			row=0
		End If	
		
		pixel.pixel=New pixel
			pixel\x=Rnd(-(gfx_width/2),(gfx_width/2));gfx_width/bit_size)
			pixel\y=Rnd(-(gfx_height/2),(gfx_height/2));gfx_height/bit_size)

			pixel\frame=fr-1
											
			pixel\dest_x=row
			pixel\dest_y=col
			
			pixel\dx= (pixel\dest_x-pixel\x)/100;
			pixel\dy= (pixel\dest_y-pixel\y)/100;
			
		row=row+1					

	Next
	
End Function



Bremer(Posted 2003) [#2]
Nice effect, I don't really much you can do to improve on it, because improvements are usually found in the main code the only thing I spotted was this:

midx=pw/2
midy=ph/2

which can be changed to:

midx=pw shr 1
midy=ph shr 1

bit shifting is faster than using / and *

another thing is (and this won't get you faster code) is that this:

For pixel.pixel = Each pixel
Delete pixel
Next

can be done like this:

delete each pixel

that will do the same thing and is easier.


Clyde(Posted 2003) [#3]
Nice work there Yavin!!

Morphing Pixels boy does that sound awesome!!

Cool code too and welldone - fantastic idea!


Paul "Taiphoz"(Posted 2003) [#4]
Thanks Mikey.

Although still not as good as some of your lots demos. You know you guys could help the community a lot by providing some code or even a tut on how you do them.


Clyde(Posted 2003) [#5]
You should Pay us a visit mate!

Cheers & All the best!! :)


BlitzSupport(Posted 2003) [#6]
You might be able to get something out of this stuff:

http://www.hi-toro.com/blitz/explosions.zip