.: Create an XNA 3D Device
|
|
| |
The first thing that you need to create a game in XNA is a programmatic connection to the graphics card in your PC. In XNA as in Direct 3D the graphics card is called an adapter, the connection to that adapter is called the device. the device is created in the following way:
Open VB.NET and create a new Windows Application project, call it createXNADevice:
|
|

Press OK and save the project to any location of your choosing.
When the project opens, you will be shown the main form, in the properties section for this form, the changes that i make are as follows:
StartPosition = CentreScreen
MaximizeBox = False
FormBorderStyle = Fixed3D
Size-Width = 800
Size-Height = 600
I like to keep the size of the window static, until a later tutorial where we will deal with screen resolution changes and how that affects the look of our project. Setting the MaximizeBox to False stops the user from maximizing the game window. FormBorderStyle = Fixed3D stops the user from resizing the window.
Next, add the XNA references as shown here.
Once they have been added go back to the main form and right-click on it and select view code. Right above the part that says "Public Class Form1" and type:
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
This includes the XNA components in the code of your project, giving your code access to them. Next we must declare the variable that will hold the Device object. Right after the part that says "public Class Form1" type:
'Define Device variables
Private Device As Graphics.GraphicsDevice = Nothing
Although now we have a device, it is not actually connected to any graphics adapters, as you can see it is set to Nothing. So, next, right after the previous code type the following:
Private Function IntializeGraphics()
Try
Dim presentParams As New PresentationParameters
presentParams.SwapEffect = SwapEffect.Discard
Dim XNAGraphicsAdapater As Microsoft.Xna.Framework.Graphics.GraphicsAdapter = Graphics.GraphicsAdapter.Adapters.Item(0)
Device = New Graphics.GraphicsDevice(XNAGraphicsAdapater, DeviceType.Hardware, Me.Handle, presentParams)
AddHandler Device.DeviceReset, AddressOf OnResetDevice
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
This is the function that connects the device to your graphics adapter, first, the function is enclosed inside a Try-Catch block which will ensure that any problems will not crash your program but will be caught and dealt with properly. The variable presentParams is dimensioned (declared) as a new set or Presentation Parameters and hence are given all the default values, next we change the SwapEffect parameter to Discard. Presentation Parameters are used to define how the device is created, feel free to change them at random to see their effect. Next, we declare an XNA Graphics Adapter and select the first adapter on the PC, and then we link this adapter to the Device that we declared earlier. If there is a problem the error message will be displayed by the MsgBox(ex.Message) function.
The Next function that we need is called OnResetDevice, this simply recreates the device if it is lost or used by another process:
Public Sub OnResetDevice(ByVal sender As Object, ByVal e As EventArgs)
If Device Is Nothing Then
'Start the 3d Device
IntializeGraphics()
End If
End Sub
So that the Initialize graphics function is called when the program is started we need to put the function call inside the Form1_Load function as follows:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Start the 3d Device
IntializeGraphics()
'forces windows to only use the Onpaint method to draw the window
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
End Sub
The other part of this Sub (a function that does not return anything to the code that called it), is Me.SetStyle(ControlStyles.AllPaintinginWmPaint Or ControlStyles.Opaque, True). This simply makes the window look the way we want it to.
The next Sub allows us to exit the application by pressing the escape key
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Is = Windows.Forms.Keys.Escape
Application.Exit()
Exit Sub
End Select
End Sub
The second last sub we need is the one that draws the app window, OnPaint.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
'If Device has been lost or reset
OnResetDevice(Device, Nothing)
'Render graphics to screen
Render()
'Force windows to redraw the window
Me.Invalidate()
End Sub
You should think of the Onpaint function as a repeating loop, that runs every few milliseconds ( i don't know exactly how often it runs), therefore any sub inside this one will also be called as part of the loop. First, the OnResetDevice function checks that the device is active, remember that this should be called before the Render function. Next is the Render function that actually does the drawing of any graphics. The Me.invalidate() sub tells windows that the form must be redrawn on the next loop.
Last of all we have the Render Function. Some programmer's put all the rendering inside the OnPaint sub and not have a separate Render sub, but i like to keep the rendering separate from the windows drawing functions.
Public Sub Render()
If Device Is Nothing Then Return
'Clear the backbuffer to a blue color
Device.Clear(ClearOptions.Target, Microsoft.Xna.Framework.Graphics.Color.Blue, 1.0F, 0)
Device.Present()
End Sub
First the sub checks that the Device is active and then clears the device buffer, which would hold the data from the last cycle. Then the Device begins the scene, ends the scene and the present function puts whatever is in the device buffer onto the screen. All drawing will be done between the beginscene and endscene functions.
You code should now look like this:
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Public Class Form1
'Define Device variables
Private Device As Graphics.GraphicsDevice = Nothing
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Is = Windows.Forms.Keys.Escape
Application.Exit()
Exit Sub
End Select
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Start the 3d Device
IntializeGraphics()
'forces windows to only use the Onpaint method to draw the window
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
End Sub
Private Function IntializeGraphics()
Try
Dim presentParams As New PresentationParameters
presentParams.SwapEffect = SwapEffect.Discard
Dim XNAGraphicsAdapater As Microsoft.Xna.Framework.Graphics.GraphicsAdapter = Graphics.GraphicsAdapter.Adapters.Item(0)
Device = New Graphics.GraphicsDevice(XNAGraphicsAdapater, DeviceType.Hardware, Me.Handle, CreateOptions.SoftwareVertexProcessing, presentParams)
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Public Sub OnResetDevice(ByVal sender As Object, ByVal e As EventArgs)
If Device Is Nothing Then
'Start the 3d Device
IntializeGraphics()
End If
End Sub
Public Sub Render()
If Device Is Nothing Then Return
'Clear the backbuffer to a blue color
Device.Clear(ClearOptions.Target, Microsoft.Xna.Framework.Graphics.Color.Blue, 1.0F, 0)
Device.Present()
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
'If Device has been lost or reset
OnResetDevice(Device, Nothing)
'Render graphics to screen
Render()
'Force windows to redraw the window
Me.Invalidate()
End Sub
End Class
Now run your app and you should have a screen that looks like this:

As you can see it doesn't do much, but this is the first step to creating your own game. Now you can return to the tutorial's page and try tutorial 3.
Web site contents © Copyright Alan Phipps 2006, All rights reserved.
Website templates |