Contact

 
Google
Web www.alanphipps.com

 
   
 
   
www.alanphipps.com

.: Tutorial 3 - Display a 2D Texture

 
 

 

In Tutorial 2 we created a XNA device that we will now use to display a texture on screen. I will not rewrite the code in tutorial 2 so please start by copying the project folder for the previous tutorial and renaming it to Display2DTexture. Load the project and in the Solution Explorer right-click on the project name and select Add - New Folder, name this folder Images. Right-click on the Images folder and select Add - Existing Item, then select the background.png image that is included in the source code at the bottom of this page. You don't need to use this file but if you do use another remember to change the name of the file in the code or you will receive a filenotfound error. Once the image is loaded you should have a solution explorer that looks like this:

 

XNA in VB.NET - Image Added

Now on to the code. First locate the part of the code where you declared the Device variable and add the following declarations:

'Background Texture objects
Private BackgroundTex As Texture2D = Nothing
Private BackgroundSB As SpriteBatch = Nothing
Private BackgroundRect As Rectangle = Nothing
Private BackgoundTexCreationParams As TextureCreationParameters = TextureCreationParameters.Default

BackgroudTex holds the entry for the texture itself. BackgroundSB holds the SpriteBatch that XNA uses to draw the Texture. BackgroundRect defines the rectangle that the spritebatch uses to decide the size and position of the texture. BackgroundCreationParams holds the various conditions that can be used to define how the texture is displayed, these parameters are initially set to their defaults.

Underneath the variable declaration add the following sub:

'Define background Details
Private Sub DefineBackground()

BackgroundRect.Height = Me.Bounds.Height
BackgroundRect.Width = Me.Bounds.Width
BackgroundSB = New SpriteBatch(Device)
BackgroundTex = Texture2D.FromFile(Device, "../../images/BackGround.png", BackgoundTexCreationParams)

End Sub

Here we define the size and position of the texture and also point the BackgroundTex object to the Image file it will use. Me.Bounds.Width is the width of the form window. The part ../ means the folder above, so when the program starts it will look for the image in a folder called images, 2 folders above the program executable file. The backgroundSB is also defined as a new spritebatch that will be drawn with our device.

Next add the Line DefineBackground() just above the line Return True in the InitializeGraphics() function.

Lastly we must add the following code to the render function:

'Draw Game Menu texture to screen with a spritebatch
BackgroundSB.Begin(SpriteBlendMode.AlphaBlend)
BackgroundSB.Draw(BackgroundTex, BackgroundRect, Color.White)
BackgroundSB.End()

Remember to put this code between the beginescene and endscene functions. This code starts the BackgroudSB spritebatch, draws the texture using the 2D image in BackgroundTex inside the BackgroudRect rectangle and with a White tint (white tint means as it would be viewed normally). If you had written the first line as BackgroundSB.Begin() then any Alpha Channel (transparency) data would not appear transparent.

Remember:

Create the variables to hold the texture info, Define the parameters of the texture, add the function call to initialize grahics and then render the texture inside the render function. Run your program and it should now look like this:

XNA in VB.NET - Display 2D Texture Complete

As you can see the blue section which is the transparency in the image is displayed as if it were not there. This will eventually become the play area for our game in another tutorial. Please return to the tutorial's page for the next tutorial.

 

Display 2D Texture Source Code - 45Kb
Next Tutorial - Create Game Area

 

 

     
 
 
     

 

Web site contents © Copyright Alan Phipps 2006, All rights reserved.
Website templates
   
 
 

 

__PayPal

PayPal - Any Amount is Welcome
 
Please Donate to the Nvidia Geforce Go 7950 GTX Fund, All donations welcome. Thanks.

 

XNA in C#

 
 

 

Games at Amazon