Private GDevice As GraphicsDevice
Private Sbatch As SpriteBatch
Private blnQuit As Boolean = False
Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
Try
If InitialiseGraphics(pbGame) = False Then
MessageBox.Show("problem initialising the game. About to quit")
Application.Exit()
End If
BackgroundWorker1.RunWorkerAsync()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If Not blnQuit Then
' don't allow the Form to close until we've released our resources
blnQuit = True
e.Cancel = True
End If
End Sub
Private Function InitialiseGraphics(ByRef surface As PictureBox) As Boolean
' graphics initialisation code here
' including:
'
GDevice = New GraphicsDevice(gAdapter, GraphicsProfile.HiDef, pParam)
End Function
Private Sub LoadGraphics()
' load Texture2D's using Content manager here
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Call GameLoop()
End Sub
Private Sub GameLoop()
' blnQuit set to True outside of the background thread
' when we want to quit
Do Until blnQuit
GDevice.Clear(xnaColor.CornflowerBlue)
Sbatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
'Sbatch.Draw whatever goes here
Sbatch.End()
GDevice.Present()
Loop
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
' tidy up by releasing any used resources
GDevice.Dispose()
Sbatch.Dispose()
' Dispose any Texture2D's
' no doubt I've overlooked something
' and finally close the form
Me.Close()
End Sub