Alpha Mask loading for Sprite Control

Blitz3D Forums/Blitz3D Programming/Alpha Mask loading for Sprite Control

Added JFK's 8-bit alpha image loading code to Sprite Control, if anyone wants to use it. This is only relevant for people using Sprite Control.

Add the following code into the end of "sprite control.bb" - don't overwrite anything. Syntax Error - feel free to add this to Sprite Control if you want, but credit goes to JFK. http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=1286 Although one look at my fast, ugly hack of your code and you'll probably shoot me. ;)

Usage:
LoadAlphaImage3D(file$,mask$,[flags],[parent])

After loading, simply use the normal DrawImage3D command - there's no slowdown as the alpha transparency has been added into the texture data. :)

Note: the alpha-mask image must be the same size as the sprite image - and an 8-bit grey-scale mask. Just choose 8-bit in your PNG saving options.

; Load an image into a texture and attach it to a quad sprite
; Loads an 8-bit grey-scale alpha mask image to mask the sprite
Function LoadAlphaImage3D(file$,alpha$,texflags=2,par=-1)
	texflags=2 ;it won't work if you change this >_<
	If par=-1 par=spritepivot
	Local tmpimage=LoadImage(file$)
	Local alphaimage=LoadImage(alpha$)
	sprite=ImageToSpriteAlpha(tmpimage,alphaimage,texflags)
	FreeImage tmpalpha
	FreeImage alphaimage
	Return sprite
End Function

; Convert an existing 2D image to quad sprite
; Alpha modified version
Function ImageToSpriteAlpha(img,alpha,texflags=5,numframesX=1,numframesY=1,par=-1)
	If par=1 par=spritepivot
	Local iw=ImageWidth(img) , ih=ImageHeight(img)
	Local tw=2 Shl (Len(Int(Bin(iw-1)))-1)
	Local th=2 Shl (Len(Int(Bin(ih-1)))-1)
	Local tex=CreateTexture(tw,th,texflags)
	Local ib=ImageBuffer(img) : LockBuffer ib
	Local tb=TextureBuffer(tex) : LockBuffer tb
	Local x,y
	For x=0 To iw-1
		For y=0 To ih-1
			rgbc=ReadPixelFast(x,y,ib) And $00ffffff
			If rgbc=((r Shl 16)+(g Shl 8)+b)
				WritePixelFast x,y,($00000000),tb
			Else
				WritePixelFast x,y,(rgbc Or $ff000000),tb
			EndIf
		Next
	Next
	UnlockBuffer ib : UnlockBuffer tb
	Local alphatex=CreateTexture(tw,th,texflags)
	ib=ImageBuffer(alpha):LockBuffer ib
	tb=TextureBuffer(alphatex):LockBuffer tb
	For x=0 To iw-1
		For y=0 To ih-1
			rgbc=ReadPixelFast(x,y,ib) And $00ffffff
			If rgbc=((r Shl 16)+(g Shl 8)+b)
				WritePixelFast x,y,($00000000),tb
			Else
				WritePixelFast x,y,(rgbc Or $ff000000),tb
			EndIf
		Next
	Next
	UnlockBuffer ib
	UnlockBuffer tb
	ScaleTexture alphatex,Float(tw)/Float(iw/numframesX),Float(th)/Float(ih/numframesY)
	ScaleTexture tex,Float(tw)/Float(iw/numframesX),Float(th)/Float(ih/numframesY)
	Local sprite=CreateImage3D(iw,ih,par)
	SetBuffer TextureBuffer(tex):LockBuffer TextureBuffer(tex)
	LockBuffer TextureBuffer(alphatex)
	For j = 0 To ih-1
		For i = 0 To iw-1
			argb=ReadPixelFast(i,j,TextureBuffer(alphatex)) And $ffffff
			r=(argb Shr 16)And $FF
			g=(argb Shr 8)And $FF
			b=argb And $FF
			grey=((r+g+b)/3)
			If grey > 255 Then grey=255
			rgb=(ReadPixelFast(i,j,TextureBuffer(tex))) And $FFFFFF
			WritePixelFast i,j,rgb Or (grey Shl 24),TextureBuffer(tex)
			Next
		Next
	UnlockBuffer TextureBuffer(alphatex)
	UnlockBuffer TextureBuffer(tex)
	EntityTexture sprite,tex
	EntityFX sprite,16+1 : EntityOrder sprite,-100
	ScaleEntity sprite,Float(iw/numframesX)/2,Float(ih/numframesY)/2,1
	Return sprite
End Function


Example:


If anyone's interested, I'll extend it to work for animated images too. >_< Think of this as a replacement for MaskImage3D where you want variable alpha transparency without memory intensive image files.

+BlackD

wait a minute, I just got to... where's that ammo box... :P

Seriously - nice to see ppl are using my stuff. And thanks for sharing, very useful library BTW.

that's so cool! =)

Ooops.. should've posted this in the Userlibs forum. I'll streamline the code, add a LoadAnimAlphaImage3D() command, and put in support for full colour (24-bit, 16-bit, etc) masks, then dump it in the Userlib's forum - so anyone thinking of using this, check there tomorrow (or the day after ;)). Mmm.. and I'll add support for the mask and the image to be in the same file, with the toggle replacing [flags] as flags must be set to 2 anyway.

+BlackD