This tutorial explains how to make an XNA project without using the XNA templates. Why would you want to do that? Because VB.NET doesn’t have Templates. Includes C# and VB.NET.
First of all you need XNA Game Studio 2.0, and Visual C# Express 2005. If you don’t already have them, get them from Microsoft.com. This tutorial will walk you through the basic code to initialize an XNA window in C# and VB.NET-without the template files or windows forms. First we have to reference the XNA libraries. You can do this by clicking Project->Add Reference, and select everything that starts with Microsoft.xna.
Next you must remove the Windows Form (Form1.cs or Form1.vb) from the project, and replace it with an empty Code File (C#) or Module (VB). If you are using VB, remove ALL code from the new Module1.vb. If you are using C#, the code is already gone. Now you have to reference the XNA libraries in the code. Type the following lines into your new code file:
C#
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
VB.NET
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Design
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Net
Imports Microsoft.Xna.Framework.Storage
Now that we have the framework imported, we have to define the class. Note that the class MUST inherit XNA’s Game class. VB has a default Namespace that you don’t need to define, but C# requires that you define a namespace first. The code will look like this:
C#
namespace MyGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
//INSERTION POINT
}
}
VB.NET
Public Class Game1
Inherits Game
‘INSERTION POINT
End Class
The commented “INSERTION POINT” is where we will add the rest of our code. Keep that in mind; do not add the code after End Class.
Notice that our class “Game1″ inherits Game, just like a Windows Form class (i.e. Form1) inherits Form. This is more obvious in the VB code where it actually says Inherits Game. The C# code defines its parent class with a Colon:
public class Game1 : Microsoft.Xna.Framework.Game
Either way, this line is absolutely vital to the correct working of our Form-independent game.
The next step is a bit harder. We must setup all of the Override functions. I should probably explain what an Override function is. The Game class contains several core functions like Draw, Update, Initialize, etc. These functions are vital to the way that the game works, but you need to be able to add your own code to them. This is where Override functions come in. An Override function replaces the base function with custom code. Of course, you don’t want to completely replace it, just add more code. So you use base.function(param); (C#) or MyBase.function(param) (VB.NET). The functions you need to override are:
Initialize
LoadContent
UnloadContent
Update
Draw
Luckily for you, simply typing “protected override” (C#) or “protected overrides” (VB) will bring up a list of overridable functions. Choose one and Visual Studio will automatically type the basic framework of an override function for you. If you did it correctly you’ll come up with:
C#
//INSERTION POINT
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
base.LoadContent();
}
protected override void UnloadContent()
{
base.UnloadContent();
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
}
VB.NET
‘INSERTION POINT
Protected Overrides Sub Update(ByVal gameTime As GameTime)
MyBase.Update(gameTime)
End Sub
Protected Overrides Sub Draw(ByVal gameTime As GameTime)
MyBase.Draw(gameTime)
End Sub
Protected Overrides Sub Initialize()
MyBase.Initialize()
End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
End Sub
Protected Overrides Sub LoadContent()
MyBase.LoadContent()
End Sub
Now we must add a few variables to make the game work. These will be declared at the insertion point above. The two variables we need are a GraphicsDeviceManager, and a SpriteBatch. The GraphicsDeviceManager is a class that controls the viewport of the game. Here’s the code for declaring them:
C#
GraphicsDeviceManager gDev;
SpriteBatch sprBatch;
VB.NET
Private gDev As GraphicsDeviceManager
Private sprBatch As SpriteBatch
Now that we have the basic variables of our class, we need to add a constructor function to set them up. A constructor is defined in C# by a typeless function with the same name as the class. In VB, it is defined with a Public Subroutine called New.
C#
public Game1()
{
gDev = new GraphicsDeviceManager(this); //Setup gDev
this.Window.Title = “Game”; //Set the window title
this.Content.RootDirectory = “Content”; //Choose data Directory
}
VB.NET
Public Sub New()
gDev = New GraphicsDeviceManager(Me) “Setup gDev
Me.Window.Title = “Game” “Set the window title
Me.Content.RootDirectory = “Content” “Choose Data Directory
End Sub
Notice the New keyword when setting up the GraphicsDeviceManager. This keyword calls the constructor function of the GraphicsDeviceManager, which sets it up for use. The this (C#)or Me (VB) keyword refers to the class that owns that function, in this case, Game1.
Since Game1 inherits Game, it has all the same properties and functions. Also, the GraphicsDeviceManager”s constructor function requires a Game in order to work, so we give it Game1 (this or Me) as a parameter.
Since Game1 has the same properties as Game, we can use this or Me to set the properties of the Game. Game.Window.Title is a String value that determines the text that appears at the top of the window. In this example we have set it to “Game”.
Game.Content.RootDirectory is the property that determines what folder the data files are in. In this example we chose “Content”. Now that our game class has a constructor and basic variables, we need to add some code to the Override functions we made earlier in order for the game to work. We will start with the LoadContent function. This is called as the game loads, and is used to load all the textures, models, etc. Add a new line before base.LoadContent(); or MyBase.LoadContent(). In it type:
C#
sprBatch = new SpriteBatch(gDev.GraphicsDevice);
VB.NET
sprBatch = New SpriteBatch(gDev.GraphicsDevice)
This simple line initializes the SpriteBatch, a class that is used to draw 2D textures (like menus) on the screen.
Next for the Draw function, we will add:
C#
gDev.GraphicsDevice.Clear(Color.CornflowerBlue);
VB.NET
gDev.GraphicsDevice.Clear(Color.CornflowerBlue)
This will color the viewport a bluish color, preparing things to be rendered onto it.
Next we need to make it so Pressing Escape quits the game. To do this, add the following If block to the Update function.
C#
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
this.Exit();
}
VB.NET
If Keyboard.GetState.IsKeyDown(Keys.Escape) Then
Me.Exit()
End If
The If Block above calls Keyboard.GetState.IsKeyDown(Keys.Escape) to check if the Escape key is currently being pressed. If it is, it continues to the next line, and if it isn’t, it skips to the end of the If block.
Note the use of this or Me again. In this case, we are calling the function Exit in Game. This closes everything and terminates the process.
Congratulations! You have just built an XNA project with a working Device, WITHOUT A TEMPLATE. C# has a built in template you can use if you downloaded XNA, but VB.NET does not. This tutorial was written mostly to compare C# to VB.NET and to make XNA available for VB even though a template does not exist.
Note that if you are using VB, there will be no way to get this onto an Xbox, but you can still use XNA to build PC games. Before we finish, let’s take a look at he finished code:
C#
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace MyGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager gDev;
SpriteBatch sprBatch;
public Game1()
{
gDev = new GraphicsDeviceManager(this);
this.Window.Title = “Game”;
this.Content.RootDirectory = “Content”;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
sprBatch = new SpriteBatch(gDev.GraphicsDevice);
base.LoadContent();
}
protected override void UnloadContent()
{
base.UnloadContent();
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
this.Exit();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
gDev.GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
VB.NET
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Design
Imports Microsoft.Xna.Framework.GamerServices
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input
Imports Microsoft.Xna.Framework.Net
Imports Microsoft.Xna.Framework.Storage
Public Class Game1
Inherits Game
Private gDev As GraphicsDeviceManager
Private sprBatch As SpriteBatch
Public Sub New()
gDev = New GraphicsDeviceManager(Me)
Me.Window.Title = “Game”
Me.Content.RootDirectory = “Content”
End Sub
Protected Overrides Sub Initialize()
MyBase.Initialize()
End Sub
Protected Overrides Sub LoadContent()
sprBatch = New SpriteBatch(gDev.GraphicsDevice)
MyBase.LoadContent()
End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
End Sub
Protected Overrides Sub Update(ByVal gameTime As GameTime)
If Keyboard.GetState.IsKeyDown(Keys.Escape) Then
Me.Exit()
End If
MyBase.Update(gameTime)
End Sub
Protected Overrides Sub Draw(ByVal gameTime As GameTime)
gDev.GraphicsDevice.Clear(Color.CornflowerBlue)
MyBase.Draw(gameTime)
End Sub
End Class












Leave Your Response