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
    Location
    South Louisiana
    Posts
    11,698

    Setting up XNA graphics in VB.Net

    Contents
    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 GraphicsDevice (documentation), not set up yet.
    Code:
        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.
    Code:
        Private Function initialize(ByRef surface As PictureBox) As Boolean
            Try
                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"!
    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            If initialize(pbGame) Then
                BackgroundWorker1.RunWorkerAsync()
            Else
                MessageBox.Show("There was a problem initializing XNA.")
                Me.Close()
            End If
        End Sub
    In our background worker, we call an infinate loop to present the cornflower blue.
    Code:
            Do Until quit = True
                grafix.Clear(Color.CornflowerBlue)
                grafix.Present()
            Loop
    Now simply debug your program and behold the blues!

    More XNA in Vb.Net to come later!
    Last edited by dday9; Jun 16th, 2021 at 11:23 AM. Reason: Updated the reference URL to the GraphicsDevice documentation
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    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