Learning the (XNA) basics

I’ve been messing about with XNA3 for about a year now, and recently decided to move up to XNA4. Microsoft have obviously decided to leverage the XNA user base to provide content for the Windows Phone 7 platform, so you’d be forgiven for thinking that XNA4 is only for phone develooment as much of the tools, educational resources, etc are all re-branded and aimed squarely at Windows Phone 7. XNA still supports the PC, Xbox 360 and Zune as target platforms alongside the Windows Phone.

XNA default screenGetting started with XNA is incredibly simple. XNA4 provides us with quite a solid framework to build our game / engine upon. Much of the hard work involved in getting DirectX up and running is handled for us.

For instance, working with C++ and DirectX, the first step is usually to create a windowed application and set up a game loop rendering a blank scene. You would have to, amongst other things;

  • create a window for your application
  • provide a windows message handler
  • intialise Direct3D
  • create the Direct3D Device
  • start the message pump / game loop
  • render to the window

XNA does all of that and a bit more for us without us having to enter a single line of code. The default XNA4 template will generate a window like the image shown and provide us with the framework code listed below to give us the basics that we need to get started.

using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
            // TODO: Add your initialization logic here


        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
            // TODO: Unload any non ContentManager content here

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

            // TODO: Add your update logic here


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)

            // TODO: Add your drawing code here


Within XNA there is an underlying Game class that provides all of the basic functionality to produce the window, graphics device, the message pump / game loop, etc. All of the initial hard work that C++ programmers have to handle is done for us, and this design is mirrored throughout XNA Game Studio.

The framework above, the Game1 class, overrides the underlying Game class and provides us with the ability to write our own code over the top. We add code to the Initialize, LoadContent, Update, and Draw methods and add our own components, methods, content, etc. to build up our own game.

Related: Microsoft’s App Hub – Getting Started

, , , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: