How 2 mask brush

Blitz3D Forums/Blitz3D Programming/How 2 mask brush

Using the Load_Pixie function as a basis to load pixel-perfect sprites, I can't keep my alpha transparency when I use a rectangle .3ds mesh. How can I set the mask colour for a brush, or pass the alpha channel of the source .png froma texture? The code is this.
Function TG_Load_Pixie(file$,width=0,height=0)
; load squared texture
	texture=LoadTexture(file,4)

	If Not width
		width=TextureWidth(texture)
	EndIf
	If Not height
		height=TextureHeight(texture)
	EndIf

; change these for viewports
	viewwidth=GraphicsWidth()
	viewheight=GraphicsHeight()
; find existing pixiespace parented to camera
	magic=0
	n=CountChildren(TG_camera\camera)
	For i=1 To n
		If EntityName(GetChild(TG_camera\camera,i))="pixiespace" 
			magic=GetChild(GetChild(TG_camera\camera,i),1)
		EndIf
	Next
	If magic=0
		magic=CreatePivot(TG_camera\camera)
		NameEntity(magic,"pixiespace")
		aspect#=Float(viewheight)/viewwidth
		PositionEntity magic,-1,aspect,1 
		scale#=2.0/viewwidth 
		ScaleEntity magic,scale,-scale,-scale 
		magic=CreatePivot(magic)
		PositionEntity magic,-.5,-.5,0
	EndIf
; create sprite from texture as child of magic overlay	
	sprite=CopyMesh(TG_sprite)
	EntityParent sprite,magic		;cludge for blitz bug in createsprite(parent)
	brush=CreateBrush()
	BrushFX brush,1
	BrushTexture brush,texture
	PaintEntity sprite,brush
	FreeBrush brush
	scale#=0.2/viewwidth 
	ScaleMesh sprite,width*scale,height*scale,1
	Return sprite
End Function


Check out the function from my code archive entries on this.

Hi Ross,

Do you mean this...
Function texture_mask_colour(texture,r1,g1,b1,tolerance=0)

	
	LockBuffer TextureBuffer(texture)

	For loop=0 To TextureWidth(texture)
		For loop1=0 To TextureHeight(texture)
			RGB1=ReadPixelFast(loop,loop1,TextureBuffer(texture)) ; read the RGB value from the texture
			r=(RGB1 And $FF0000)Shr 16;separate out the red
			g=(RGB1 And $FF00) Shr 8;green
			b=RGB1 And $FF;and blue parts of the color
			a=(RGB1 And $FF000000)Shr 24 ; extract the alpha

			If r>=r1-tolerance And r=<r1+tolerance And g=>g1-tolerance And g=<g1+tolerance And b=>b1-tolerance And b=<b1+tolerance Then; check RGB lies with the tolerance
				;temp=((Abs(r-r1)+Abs(g-g1)+Abs(b-b1))/3.0)*4.0 ; alpha the values based on the tolerance value
				a=0;temp

			End If

			newrgb= (a Shl 24) Or (r Shl 16) Or (g Shl 8) Or b ; combine the ARGB back into one value

			WritePixelFast(loop,loop1,newrgb,TextureBuffer(texture)); write back to the texture
		Next
	Next
	UnlockBuffer TextureBuffer(texture)
End Function

The example program just blanks out when I run it, but if I put a Stop command in just before calling the function, the program runs okay.

I tried the above code in my program and get an abnormal exit error when I Esc the program.

Edit : It was the prepare_texture() function of the demo that caused the crash out.

Found the crash. The loop was 0 to Texturewidth(), where it needed to stop at texturewidth()-1

Still can't solve my problem though...