Contact

 
Google
Web www.alanphipps.com

 
   
 
   
www.alanphipps.com

.: Code Cleanup - Round 1

 
 

 

 

In this tutorial I will be making only a few changes, mainly, moving the declared objects into a module and using only one spritebatch to draw all the textures. So, copy the Source Code from the last tutorial and rename it's folder to CodeCleanup1. Open the project and right-click the project name, then select Add Module, name this module GraphicsModule and move all the object declarations from the Form1 code to the module:

 

Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics

Module GraphicsModule

Public Device As Graphics.GraphicsDevice = Nothing

'Define SpriteBatch, Textures, and Rectangles for all Non-ball Textures
Public NonBallSB As SpriteBatch = Nothing
Public NonBallTexCreationParams As TextureCreationParameters = TextureCreationParameters.Default
Public BackgroundTex As Texture2D = Nothing
Public BackgroundRect As Rectangle = Nothing
Public PlayAreaTex As Texture2D = Nothing
Public PlayAreaRect As Rectangle = Nothing
Public BallLauncherTex As Texture2D = Nothing
Public BallLauncherRect As Rectangle = Nothing
Public ArrowSBRotate As Single = 0
Public ArrowTex As Texture2D = Nothing

'Function to Define NonBall Details
Public Sub DefineNonBallDetails()
NonBallSB = New SpriteBatch(Device)
BackgroundRect.Height = My.Forms.Form1.Bounds.Height
BackgroundRect.Width = My.Forms.Form1.Bounds.Width
BackgroundTex = Texture2D.FromFile(Device, "../../images/BackGround.png", NonBallTexCreationParams)
PlayAreaRect.Height = 456
PlayAreaRect.Width = 380
PlayAreaRect.X = 212
PlayAreaRect.Y = 62
PlayAreaTex = Texture2D.FromFile(Device, "../../images/380x480AlphaChannelOnly.png", NonBallTexCreationParams)
BallLauncherRect.Height = 107
BallLauncherRect.Width = 380
BallLauncherRect.X = 212
BallLauncherRect.Y = 433
BallLauncherTex = Texture2D.FromFile(Device, "../../images/BallLauncher.png", NonBallTexCreationParams)
ArrowTex = Texture2D.FromFile(Device, "../../images/Arrow.png", NonBallTexCreationParams)

End Sub

End Module

As you can see i have changed the various object declarations from Private to Public, so that they can be accessed from any part of the project. I have also deleted the separate spritebatches and declared a new one called NonBallSB, this spritebatch will draw all textures that as the name suggests are not balls, the balls will be defined in the next tutorial. I have also consolidated all the functions that defined the textures into one new function called DefineNonBallDetails. The function call for this new function is again placed in the InitializeGraphics function as below:


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)
DefineNonBallDetails()
Return True

Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try

End Function

The next change is to redefine the Render function so that the old Spritebatches are removed and the new one is added as below:

Private 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)

'Draw all Non Ball Textures
NonBallSB.Begin(SpriteBlendMode.AlphaBlend)
NonBallSB.Draw(BackgroundTex, BackgroundRect, Color.White)
NonBallSB.Draw(PlayAreaTex, PlayAreaRect, Color.White)
NonBallSB.Draw(BallLauncherTex, BallLauncherRect, Color.White)
NonBallSB.Draw(ArrowTex, New Vector2(405, 480), Nothing, Color.White, ArrowSBRotate, _
New Vector2((ArrowTex.Width / 2), (ArrowTex.Height / 2)), 1.0F, SpriteEffects.None, 0.0F)
NonBallSB.End()


Device.Present()

End Sub

Here the single spritebatch is used to draw all the textures, using only one spritebatch is more efficient and uses less resources.

Next we will add a new framerate class that will display the current framerate of the game. As before right-click on the project name and select Add Class, name this class framerate and add the following code:

Public Class Framerate

Public Shared Function CalculateFrameRate() As Integer

If System.Environment.TickCount - lastTick >= 1000 Then
lastFrameRate = frameRate
frameRate = 0
lastTick = System.Environment.TickCount
End If

frameRate += 1

Return lastFrameRate

End Function 'CalculateFrameRate

Private Shared lastTick As Integer
Private Shared lastFrameRate As Integer
Private Shared frameRate As Integer

End Class

 

In the OnPaint function add the code:


'Diaplay framerate on window title bar
Me.Text = String.Format("The framerate is {0}", Framerate.CalculateFrameRate())

This will display the current framerate in the form title bar. As a last change i have right-clicked on the images folder and selected "Exclude from Project", as long as the images are in the folder they will be found and used.

Now, you can go back to the tutorial's page for the next tutorial.

 

CodeCleanup1 Source Code - 161Kb
Next Tutorial - Ball Class and Ball Movement

 

     
 
 
     

 

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