In this tutorial I will cover the following:
- Set up XNA graphics in visual basic.net
A few prerequisites:
Following my prior tutorial, go ahead and add the correct XNA references and Import:
We will first add two variables, the first would be a boolean value set to false. The second will be a graphics device, not set up yet.
The next step in our process is to start a boolean function. Now in this function, we will try to set up the graphics device. If for whatever reason we fail, in the form_load event an error will be thrown.
Private quit As Boolean = False
Public grafix As GraphicsDevice
The first thing we declare is a presentation parameter. You can think of presentation parameters kinda similar to data parameters, but for the graphics device. The two properties we set are the device handle, which in this case I'm using a picturebox, as well as setting the full screen to false. There are more properties that you can set, but for the informative process, these will be the only two we are setting. The next thing that we declare is the Graphics adapter. All computers should have 1 graphics card, that's why we set the item to 0(remember index are 0 based). Finally we try to set the graphics device using the variables we just declared. If we suceed then return a true value, and vice-versa.
Private Function initialize(ByRef surface As PictureBox) As Boolean
Dim pparam As New PresentationParameters
pparam.DeviceWindowHandle = surface.Handle
pparam.IsFullScreen = False
Dim grafixAdapt As GraphicsAdapter = GraphicsAdapter.DefaultAdapter
grafix = New GraphicsDevice(grafixAdapt, GraphicsProfile.HiDef, pparam)
initialize = True
Catch ex As Exception
initialize = False
The next step in our process is to check if we had any errors in setting our graphics device. If we don't have any errors then display the traditional XNA "cornflower blue"!
In our background worker, we call an infinate loop to present the cornflower blue.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
If initialize(pbGame) Then
MessageBox.Show("There was a problem initializing XNA.")
Now simply debug your program and behold the blues!
Do Until quit = True
More XNA in Vb.Net to come later!