- Joined
- Jan 2, 2020
- Messages
- 1,282
Blendmodes are powerful, and often you can use this inbuilt power to save other resources. This also avoids techniques such as stencilbuffers, and it adds the possibility to mask using soft 0-255 level alpha.
Here's how you can use it to mask graphics.
Here's how you can use it to mask graphics.
Cerberus:
Import Mojo2
Function Main()
New Game
Return 0
End
Class Game Extends App
Field canvas:Canvas, sprite:Image[8]
Method OnCreate ()
canvas = New Canvas() ; SetSwapInterval 1 ; SetUpdateRate 0
sprite[0] = New Image(64,64,.0,.0,0)
Return 0
End
Method OnRender : Int()
' Draw the background
canvas.Clear
canvas.SetColor 0,0,1 ; canvas.DrawRect 0,0,640,480
canvas.SetColor 1,1,1 ; canvas.DrawLine 0,0,180,180 ; canvas.DrawLine 180-0,180-10,180,180 ; canvas.DrawLine 180-10,180-0,180,180
canvas.SetColor 1,0,0 ; canvas.DrawRect Millisecs()/10 Mod 640,220,20,20
' ------------------------------------------------------------------------------
' Mask (draw inside a 64x64 sprite)
canvas.SetRenderTarget sprite[0] ' Set sprite as active canvas...
canvas.Clear 0,0,0,0 ' ...and clear it
canvas.SetBlendMode BlendMode.Alpha ; canvas.SetColor 0,1,0,1 ; canvas.DrawOval MouseX()-225,MouseY()-225,50,50 ' Draw something
canvas.SetBlendMode BlendMode.Multiply2 ; canvas.SetColor 0,0,0,0 ; canvas.DrawOval 20,20,10,10 ' Draw something, this will mask a hole in the above
canvas.SetBlendMode BlendMode.Alpha ; canvas.SetColor 1,1,1,1 ' We need to reset canvas properties even if we changed them while it was directed to sprite
canvas.SetRenderTarget Null ' Set canvas to be main canvas once again
' ------------------------------------------------------------------------------
canvas.DrawImage sprite[0],200,200 ' Now draw that sprite on the canvas, this will be the foreground.
canvas.Flush
Return 0
End
End