Results 1 to 2 of 2

Thread: Setting up XNA graphics in VB.Net

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    South Louisiana

    Setting up XNA graphics in VB.Net

    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:
    1. Microsoft.Xna.Framework
    2. Microsoft.Xna.Framework.Graphics

    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.
        Private quit As Boolean = False
        Public grafix As GraphicsDevice
    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 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
            End Try
        End Function
    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.

    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"!
        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.")
            End If
        End Sub
    In our background worker, we call an infinate loop to present the cornflower blue.
            Do Until quit = True
    Now simply debug your program and behold the blues!

    More XNA in Vb.Net to come later!
    Last edited by dday9; Apr 5th, 2013 at 12:20 AM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002

    Re: Setting up XNA graphics in VB.Net

    This is for whole program XNA development using a game loop. Over in the CodeBank I have an example of using XNA for drawing individual controls within a form that doesn't otherwise use XNA. You might add a tutorial on that, too, as it is a subject that is likely valuable and very poorly covered.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width