Sprite used as mask

BlitzMax Forums/BlitzMax Beginners Area/Sprite used as mask

One way to do it would be to draw the image you want masked onto a newly cleared screen, draw the mask over it, then use GrabImage() to grab the masked portion. Then you draw that image over whatever background you want. Like this:
Move the masked smiley face with the mouse, move the portion which is masked with the cursor keys.
SuperStrict

Graphics 800,600

AutoMidHandle True

'create the smiley face image
Cls
SetColor 255,255,0
DrawOval 0,0,100,100
SetColor 0,0,0
DrawOval 23,23,20,20
DrawOval 56,23,20,20
DrawRect 33,66,33,20
Local Smiley:TImage = CreateImage(100,100)
GrabImage(Smiley,0,0)

'create the sprite mask
Local mask:TImage = CreateImage(200,200)
Cls

Local Pixmap:TPixmap = LockImage(mask)
For Local x:Int = -100 To 99
	For Local y:Int = -100 To 99
		If (x*x+y*y) > 900
			WritePixel(Pixmap,x+100,y+100,$FF000000) 'black pixel with alpha = 1.0
		Else
			WritePixel(Pixmap,x+100,y+100,$00000000) 'black pixel with alpha = 0.0
		End If
	Next
Next

SetMaskColor 0,0,0 'mask color is black by default
Local MaskedSmiley:TImage = CreateImage(100,100,1,DYNAMICIMAGE|MASKEDIMAGE) 'MASKEIMAGE flag will set alpha to 0 for any black pixels

Local MaskOffsetX:Int = 0 'the mask offset from the smileyface
Local MaskOffsetY:Int = 0

SetBlend AlphaBlend 'using alpha blend mode
While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
	Cls
	DrawImage Smiley,50,50 'draw the smileyface
	DrawImage mask,MaskOffsetX+50,MaskOffsetY+50 'draw the mask over the smileyface
	GrabImage (MaskedSmiley,0,0) 'grab the now masked smileyface
	SeedRnd(1) 'Make sure random background is drawn same every frame
	
	Cls
	For Local i:Int = 1 To 20 'drawing some random shapes
		SetColor Rand(0,255),Rand(0,255),Rand(0,255) 'random color
		DrawOval Rand(0,800),Rand(0,600),Rand(10,100),Rand(10,100) 'random ovals
	Next
	SetColor 255,255,255 'set the color back to white
	
	Local MX:Int = MouseX()
	Local MY:Int = MouseY() 'get the mouse coordinates
	
	DrawImage maskedSmiley,MX,MY 'draw the image on the screen
	Flip
	
	If KeyDown(KEY_LEFT) Then MaskOffsetX :- 1 'move mask left
	If KeyDown(KEY_RIGHT) Then MaskOffsetX :+ 1 'move mask right
	If KeyDown(KEY_UP) Then MaskOffsetY :- 1 'Move mask up
	If KeyDown(KEY_DOWN) Then MaskOffsetY :+ 1 'Move mask down
	If MaskOffsetX > 50 Then MaskOffsetX = 50 'Limit the movement of the mask
	If MaskOffsetX < -50 Then MaskOffsetX = -50
	If MaskOffsetY > 50 Then MaskOffsetY = 50
	If MaskOffsetY < -50 Then MaskOffsetY = -50
	
	

Wend