Alpha color

Blitz3D Forums/Blitz3D Beginners Area/Alpha color

Walter(Posted 2004) [#1]
With BlitzPlus when I display sprites (DrawImage or DrawBlock) there's no paramater for the alpha color (translucent). How can I display several sprites with different translucidity degrees for each of them?


JazzieB(Posted 2004) [#2]
You can't in Blitz Plus or the 2D command set of Blitz3D. All you can do is set a mask colour, as I'm sure you already know.

If you want varying degrees of alpha then you need to write your own functions that utilise the Read/WritePixelFast commands, which take the colour of the background, the colour of the foreground (your sprite) and then calculate the new colour to be written to the screen. As you can imagine, this is fairly slow and shouldn't be used for large images or many smaller sprites.

If you had Blitz3D you could use 3D sprites, which can have alpha applied to them. You could code your own routines or make use of one of the many 3D sprite libraries that are knocking about that let you use normal 2D co-ordinates.

For examples of the former search these forums or take a look in the code archives, as this question has been asked many times before.


Sunteam Software(Posted 2004) [#3]
I don't use BlitzPlus so I don't know how compatible it is with the ole 2D command set but I wrote an alpha include a while ago which might help. You can find it here -> http://www.blitzbasic.com/codearcs/codearcs.php?code=343

There are also many others who have contributed similar code.


Walter(Posted 2004) [#4]
I just made a read/write pixel to simulate a translucent sprite drawing but it is incrediblely slow! And if I add Lockbuffer/r/w fastpixel nothing is displayed.
Why? If it worked, would it be really faster?..

----------------------
Graphics(800,600,0,2)
SetBuffer BackBuffer()

bck=LoadImage("picture800x600.jpg",2)
img=LoadImage("sprite200x180.jpg",2)

bck_buf=ImageBuffer(bck)
img_buf=ImageBuffer(img)

While Not KeyHit(1)
x=MouseX()
y=MouseY()
DrawBlock(bck,0,0)

For j=0 To 179
For i=0 To 199
a=ReadPixel(x+i,y+j,bck_buf)
b=ReadPixel(i,j,img_buf)
WritePixel(x+i,y+j,a+b)
Next
Next
Flip()
Wend

FreeImage(bck)
FreeImage(img)


Walter(Posted 2004) [#5]
LockBuffer works now. It's because I had put it before the drawblock... Now with the fast pixels, yes it's fast! :)


Walter(Posted 2004) [#6]
Well, I just coded my version of translucent drawimage and it works fast enought for some sprites. I really would like this function is integrated in the next update of Blitz+.

Graphics(800,600,0,2)
SetBuffer BackBuffer()

Const LG_IMG=200,HT_IMG=180 ;Size of the sprite

bck=LoadImage("picture800x600.jpg")
img=LoadImage("sprite200x180.jpg")

bck_buf=ImageBuffer(bck)
img_buf=ImageBuffer(img)

LockBuffer(bck_buf)
LockBuffer(img_buf)

While Not KeyHit(1)
	x=MouseX()
	y=MouseY()
	
	DrawBlock(bck,0,0)
	LockBuffer()
	
	For j=0 To HT_IMG-1
		For i=0 To LG_IMG-1
			offx=x+i
			offy=y+j
			
			bck_pix=ReadPixelFast(offx,offy,bck_buf)
			bck_r=bck_pix And $00FF0000
			bck_g=bck_pix And $0000FF00
			bck_b=bck_pix And $000000FF
					
			img_pix=ReadPixelFast(i,j,img_buf)
			img_r=img_pix And $00FF0000
			img_g=img_pix And $0000FF00
			img_b=img_pix And $000000FF
			
			tr=(((bck_r Shr 16)+(img_r Shr 16)) Shr 1) Shl 16
			tg=(((bck_g Shr 8)+(img_g Shr 8)) Shr 1) Shl 8
			tb=(bck_b+img_b) Shr 1
					
			WritePixelFast(offx,offy,tr+tg+tb)	
		Next
	Next

	UnlockBuffer()
	Flip()
Wend

FreeImage(bck)
FreeImage(img)



WolRon(Posted 2004) [#7]
There are faster methods than using ReadPixel or ReadPixelFast. Always try to avoid reading Video RAM.


Walter(Posted 2004) [#8]
I'm made a new version using bank pointer and Peek/poke with integers, it's really faster!!


WolRon(Posted 2004) [#9]
I think what you did is already in the code archives...