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