Public Class Game1
Inherits Microsoft.Xna.Framework.Game
Private WithEvents graphics As GraphicsDeviceManager
Private WithEvents spriteBatch As SpriteBatch
Private background As Texture2D
Private rtarget As RenderTarget2D
Private blendsub As BlendState
Private blendrevsub As BlendState
Public Sub New()
graphics = New GraphicsDeviceManager(Me)
Content.RootDirectory = "Content"
Dim rnd As New Random
For i As Integer = 1 To 20 'Generate 20 gray balls of varying sizes moving in random directions.
Dim B As New GradientBall(Me, rnd.Next(30, 70), Color.Transparent, _
Color.Gray, New Vector2(rnd.Next(2, 10), rnd.Next(2, 10)))
Components.Add(B)
Next
End Sub
Protected Overrides Sub Initialize()
MyBase.Initialize()
IsMouseVisible = True
Window.AllowUserResizing = True
Window.Title = "Example 3c (Various blendstates)" 'Modified title.
blendsub = New BlendState() With {.AlphaBlendFunction = BlendFunction.Subtract, _
.ColorBlendFunction = BlendFunction.Subtract, _
.AlphaSourceBlend = Blend.One, .AlphaDestinationBlend = Blend.One, _
.ColorSourceBlend = Blend.One, .ColorDestinationBlend = Blend.One}
blendrevsub = New BlendState() With {.AlphaBlendFunction = BlendFunction.ReverseSubtract, _
.ColorBlendFunction = BlendFunction.ReverseSubtract, _
.AlphaSourceBlend = Blend.One, .AlphaDestinationBlend = Blend.One, _
.ColorSourceBlend = Blend.One, .ColorDestinationBlend = Blend.One}
End Sub
Protected Overrides Sub LoadContent()
MyBase.LoadContent()
spriteBatch = New SpriteBatch(GraphicsDevice)
background = Content.Load(Of Texture2D)("Penguins")
rtarget = New RenderTarget2D(GraphicsDevice, background.Width, background.Height)
End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
rtarget.Dispose()
End Sub
Protected Overrides Sub Update(ByVal gameTime As GameTime)
If Keyboard.GetState.IsKeyDown(Keys.Escape) Then Me.Exit()
If Keyboard.GetState.IsKeyUp(Keys.P) Then MyBase.Update(gameTime) 'Will allow you to pause
End Sub
Protected Overrides Sub Draw(ByVal gameTime As GameTime)
'Draw the balls to the rtarget RenderTarget2D
GraphicsDevice.SetRenderTarget(rtarget)
GraphicsDevice.Clear(Color.Transparent)
MyBase.Draw(gameTime)
GraphicsDevice.SetRenderTarget(Nothing)
'Blend with the background.
If Mouse.GetState.LeftButton = ButtonState.Pressed Then
GraphicsDevice.Clear(Color.Black)
spriteBatch.Begin(Nothing, blendsub)
spriteBatch.Draw(background, Vector2.Zero, Color.White)
spriteBatch.Draw(rtarget, Vector2.Zero, Color.White)
spriteBatch.End()
ElseIf Mouse.GetState.RightButton = ButtonState.Pressed Then
GraphicsDevice.Clear(Color.White)
spriteBatch.Begin(Nothing, blendrevsub)
spriteBatch.Draw(background, Vector2.Zero, Color.White)
spriteBatch.Draw(rtarget, Vector2.Zero, Color.White)
spriteBatch.End()
Else
GraphicsDevice.Clear(Color.Black)
spriteBatch.Begin(Nothing, BlendState.Additive)
spriteBatch.Draw(background, Vector2.Zero, Color.White)
spriteBatch.Draw(rtarget, Vector2.Zero, Color.White)
spriteBatch.End()
End If
End Sub
End Class