GetColor, Plot -> Read/WritePixelFast

Blitz3D Forums/Blitz3D Beginners Area/GetColor, Plot -> Read/WritePixelFast

Steen(Posted 2009) [#1]
I made a little program that puts a couple of lights on a black screen and blends them together. It works like it's supposed to when I use GetColor and Plot but I would like it to be faster by using ReadPixelFast and WritePixelFast instead. I've searched around and tried using them but I can't really get it to work.

Here's the code(I use 1-10 lights usually):


Any takers on making a quick conversion to Read/WritePixelFast? :)


Ross C(Posted 2009) [#2]
I wouldn't recommend using WritePixelFast unless your reading/writing a decent amount of pixel. The time it takes to lock a buffer and unlock it again (which you have to do with read/writepixelfast), outweighs the speed gained.

ReadPixel and WritePixel should be faster than plot and get colour though :o)

You will need 3 Globals in your code, in order to get the results for the readcolour operation:

Global Got_Red
Global Got_Green
Global Got_Blue


Just read these globals after you call the Get_Color_Fast() function

Function Get_Color_Fast(x,y)

	
	RGB1=ReadPixel(x,y,FrontBuffer())
	Got_Red=(RGB1 And $FF0000)Shr 16;separate out the red
	Got_Green=(RGB1 And $FF00) Shr 8;green
	Got_Blue=RGB1 And $FF;and blue parts of the color
	;a=(RGB1 And $FF000000)Shr 24 <<< enable this is you want the alpha value to be read also.
	
End Function

Function Plot_Fast(x,y,r,g,b,a=0)

	newrgb= (a Shl 24) Or (r Shl 16) Or (g Shl 8) Or b; combine the ARGB back into a number
	WritePixel x,y,argb,BackBuffer() ; You will most likely want to write to the back buffer, to be
									 ; Flipped. If not, change to appropriate buffer.
									
End Function


If you really want to use WritePixelFast, lock the frontbuffer() or backbuffer() (whatever one your accessing) at the start of your function, and unlock at the end, NOT after each read/write operation. And obviously function the commands in the functions i provided to WritePixelFast and WritePixelFast :o)

Hope that helps.


Steen(Posted 2009) [#3]
Thanks! That made things go faster after I fixed your newrgb/argb typo in the Plot_Fast function. ;)


Ross C(Posted 2009) [#4]
Typo? If so, i need to change that in my other stuff ;o) Could you please point it out.


Steen(Posted 2009) [#5]
newrgb= (a Shl 24) Or (r Shl 16) Or (g Shl 8) Or b
WritePixel x,y,argb,BackBuffer()


You put the color in newrgb but uses argb to WritePixel. I didn't understand why everything went black at first. :P


Ross C(Posted 2009) [#6]
Ah sorry, i ripped that part out of a function :) Apologies!


jfk EO-11110(Posted 2009) [#7]
I'd suggest to write the values to a 2-dimensional array (eg dim a(640,480))
and then writepixelfast them later, all together.