Archive for category Programming

Revision Control / Software Versioning

TortoiseHgOne of the challenges that programmers face when making changes to their codebase is not  breaking the existing code. Many programmers will use an incremental test process where changes are made in small steps and tested at each change to make sure that the program still works as expected.

As programs grow larger and more complex it becomes more and more likely that changes made now may have a negative impact on other areas that are not immediately apparent, or that you may miss including a new test case into your testing process, allowing the impact to go undiscovered until a later date.

At other times you may be exploring a new approach and realise that it is not going to achieve what you wanted therefore you need to go back to an earlier version and pursue a different approach.

This becomes even more complex when more than one programmer is working on a project. How do you make sure that if each person is working on a different, or even the same, area of the program that all the changes get made, that they do not conflict with each other, and that everyone has the current working code to work with?

This is where revision control comes in.

The basic idea is to have a system that tracks changes to the project code, and sometimes other files, so that you can efficiently monitor the codebase, as well as share and integrate code from all of the programmers working on the project. Most work via a central code repository that you ‘check’ code in and out of, and the repository manages different branches of code so that changes can be undone, merged, revised, ‘forked’ (creating a new branch seperate from other code), etc. For most of the currently available systems the repository is stored on a web server (can be local, intranet or internet) for easy sharing and administration.

In the past I’ve used Microsoft’s Visual SourceSafe (which used to be integrated with Visual Studio 6), and dabbled with Subversion (SVN) when playing with other people’s code, but as I am a lone programmer I thought that SVN might be a bit over-the-top for my needs, so I had a look around for a lightweight local repository solution.

I came across Mercurial, a free distributed source control management tool, and went looking for a Windows shell, which lead me to TortoiseHg, which provides an all-in-one solution of a shell and toolbox for Mercurial.

Installation and set up are quick and easy, it provides all of the functionality that you’d expect from a revision system, and it is amazingly easy to use. It can be used by one or many programmers, and with a local or remote repository stored as a folder or on a web server. Branching and merging are simple to do, and the documentation for both TortoiseHg and Mercurial present basic tutorials to in-depth descriptions of the software.

If you’re embarking on any large coding projects I’d recommend giving TortoiseHg a look.

Advertisements

, , , ,

Leave a comment

Learning the (XNA) basics – part 2

XNA codeContinuing from Learning the (XNA) basics lets take a quick look at what all that XNA framework code actually does.

We write XNA code in C# (C sharp) so you should at least be familiar with the basics of C# before starting in on XNA (there are a lot of introductory tutorials for both C# and XNA on the internet).

Taking it section by section;

Using statements reference other namespaces which are typically linked libraries or assemblies within .NET. They basically enable us to use methods from within those namespaces without having to specify the namespace every time, so instead of typing Microsoft.Xna.Framework.Graphics.Effect we can just type Effect and the compiler will select the correct namespace for us because we have a using reference to Microsoft.Xna.Framework.Graphics.

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;

Here XNA has specified a namespace for our game, WindowsGame1. You may end up using multiple namespaces within your game to help seperate out functional components and to provide another level of encapsulation. You should see that code will only be able to access code within another namespace by fully qualifying the namespace or via the using method.

namespace WindowsGame1

This is the class definition and shows that our Game1 class inherits from the Microsoft.Xna.Framework.Game class, which provides all of the base functionality for our game. Within the definition XNA has provided member variables for the GraphicsDeviceManager which is our graphics device and a SpriteBatch which may be used for drawing sprites.

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

The class constructor initialises the GraphicsDeviceManager member, graphics, and defines a root directory for the game to find it’s Content (content is also known as the game assets, or the models, textures, sounds, etc that your game will use).

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

The Initialize method can be used to perform other initialisation actions. It is also important to call base.Initialize(); as this causes the correct creation of any other GameComponents.

/// <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

base.Initialize();
}

LoadContent can be used to, load your content 🙂 You will see that XNA has initialised the SpriteBatch member, spriteBatch, 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
}

UnloadContent, unsurprisingly, unloads your content 🙂

/// <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
}

This is a key method, Update. Upate and Draw are (usually) called in sequence over and over providing your game loop. The Update step is usually where you would gather user input, and update your game logic. You will see that Update also takes a parameter, gameTime, which allows us to perform time-related calculations. XNA has provided minimal code here, it checks to see if a controller button has been pressed and if so exits the game loop.

Like, Initialize, the call to base.Update(); causes any other GameComponents to update as well.

/// <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)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

The other key method within your game loop, Draw. Draw, like Update, takes a gameTime parameter. Here XNA clears the window to a pretty blue, and calls base.Draw(); so that any other DrawableGameComponents can also draw themselves.

/// <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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here

base.Draw(gameTime);
}

As you can see XNA has provided a simple framework for a game / game loop. There is a lot more going on under the hood, but we don’t actually need to concern ourselves with how XNA does what it does.

Now its time to think about how to use that framework to start building your game.

, , , , ,

Leave a comment

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

            base.Initialize();
        }

        /// <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)
                this.Exit();

            // TODO: Add your update logic here

           base.Update(gameTime);
        }

        /// <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)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

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

, , , , ,

Leave a comment

I’m late to the party (XNA Game Studio)

Visual Basic 6 and DirectX 8 are looking very long in the tooth now, pretty much dead, but where to go?

I dabbled briefly with an early version of XNA (Game Studio 2.0 I seem to recall) when I was looking at alternatives for game programming languages/libraries, but wasn’t particularly impressed with it. Yes, it allowed rapid development, and most of the basics were done for you, but it seemed slow, and required doing things in a particular way, and seemed more aimed at the casual 2D game market on the Xbox 360 (although it supports Windows development as well).

With the advent of XNA 4 (due later this year) I thought I’d have another look at it and so installed the latest iteration XNA Game Studio 3.1.

Install Microsoft Visual C# 2008 Express Edition

Install Microsoft XNA Game Studio 3.1

Visit the XNA Creators Club Online, in particular their Getting Started section (based upon XNA Game Studio 3.0)

The video tutorials walk you through all aspects of getting started with C#/XNA;  setting up a Creators Club account, creating your first simple 2D and then 3D game, and there are ‘Starter Kits’ that help springboard you into different projects.

Bear in mind that you don’t need to purchase the full membership to develop games for both Windows and the Xbox 360, but you will only be able to deploy your games on Windows PCs without the full membership.

Its slicker than I remember, and much more streamlined, I’m actually going to give this another go 🙂

, , , ,

Leave a comment

Early steps (laying the groundwork)

Once you have produced a rough design document then you can use the ‘technical features’ statements to help you to start thinking about the systems that you will be using to implement your design. I like to identify the large scale systems / requirements at this stage to guide the overall architecture.

Some systems will be common to all games / engines although most will have multiple possible approaches to satisfy the system requirements. For instance, pretty much all games require systems to manage the rendering, input, sound effects, music, scene management, loading / saving, etc. etc. but how they are implemented can be completely different.

Jason Gregory, in his excellent in-depth book ‘Game Engine Architecture’, discusses how the runtime engine architecture is built in layers, each layer depending upon the layers below to operate. Layers include the hardware, drivers, and Operating System, 3rd party libraries, a Platform Independence Layer, the Core Systems, Resources, low-level systems, front end and gameplay foundations, and finally game-specific systems.

Microsoft XNA Game Studio enables us to skip over some areas as it will handle those for us, acting as a framework to build our own work upon. As long as the Operating System is supported (and the hardware/drivers meet certain requirements) we can ignore much of the lowest levels of the architecture. Many 3rd party libraries are available for C# and XNA including physics, animation, scripting or other technologies. We don’t directly need a Platform Independence Layer as XNA handles that (as long as we code with alternate platforms in mind) and also provides some of the Core System and low-level rendering functionality as well.

The idea here is not to get too bogged down in the minutiae of the individual system implementations but to have a rough idea of the overall systems that will be used and how they will connect / communicate. If you are also using a particular design methodology or pattern(s) then this is the place to consider it and how that may guide the architecture.

Try not to over-analyse what you will need / implement or you may end up spending so much time on this that you lose impetus (known as analysis paralysis).

Another issue to avoid is becoming ‘locked in’ to particular design choices. You may find that an early design choice is not really appropriate later on, so rather than rigidly stick to the early design you have to be willing to adapt and evolve.

The balance between flexibility and the original goals should be a personal / team decision and clearly communicated early on, don’t be afraid to explore new ideas or change / trial different elements but keep the main aspects / key features in sight to keep you moving forward.

,

Leave a comment

The importance of design

In my forays around the internet looking for examples and advice I have noticed that the majority of these sorts of projects flounder, fail, or stall out for varying reasons, but one key reason appear to be common; Lack of a clear, documented design goal.

Design for the win!

Having an idea is a good start but to keep things moving in the right direction you need a design document.

If you’re working with a team the design document is a way to clearly communicate what you want to achieve, what decisions you have already made, and the motivation behind them while for a solo programmer it is a way to ensure that you stay on target for what you want to achieve.

Design documents come in many shapes and sizes, but I would suggest that the key points to address are;

  1. Clearly state the ultimate goal
  2. Provide an overview of the main aspects
  3. Specify the key features
  4. Provide detail by section of each key area

1. For Rain City my goal is; ‘Rain City will be an immersive 3D role-playing environment utilising a ‘free-play’ system that does not constrain players to a set role or storyline. You will be able to develop relationships with the other inhabitants of the city and pursue your chosen role and any of the diverse sub-plots…or just roam freely.’

2. From this I can identify several aspects that form the goal; ‘immersive’, ‘role-playing’, ‘free-play’, ‘relationships’, etc and provide a bit more detail, including why these are important to our goal.

I also include in this section a set of common questions and answers, such as; ‘What is Rain City?’, ‘What is the setting?’, ‘What is the main focus?’, etc.

3. This section details the technical features; ‘3D engine’, ‘1st/3rd person view’, ‘interpersonal interaction model’, ‘character design system’, ‘storylines influenced by player actions’, etc. This allows me to build upon and demonstrate decisions that have been made about what to include, why, and further strengthen the design to meet the goal.

4. I then take each section in turn; ‘game world’, ‘characters’, ‘scenarios’, etc., and flesh out what decisions have been made and why, and what each section needs to achieve.

At this point I have made few technical statements, beyond the feature set, but have focused on look and feel, where necessary providing the background to that focus.

Because this is going to be based in part on a pen-n-paper role-play system I also have a separate Rules document that details the underlying game system mechanics, and a Background document that details the game world background.

The combination of these documents provides the basis for all future decision that will be made during development. Where you would normally be quite strict in constraining the computer game within the bounds of the RPG game system and background, I have the flexibility to adjust those if it simplifies or enhances the computer game.

A design document doesn’t have to be complete when you begin coding, and will evolve during actual implementation, but it should be as complete as it can be by that point. This avoids going off target, or exploring other avenues already rejected, which both wastes time and can be divisive in a team environment.

,

Leave a comment

Blade Runner Screen Saver

In early 2006 at the request of a couple of the denizens of the usenet newsgroup, alt.fan.blade-runner, I created a Windows screen saver using (without permission) some sampled video from the Westwood Studios computer game version of Blade Runner and sound files sourced from the web.

It shows the balcony scene (which itself was borrowed from a scene within the movie), with ‘Blade Runner Blues’ playing in the background and some general ambience sound effects. It has some configurable options, like volume and frequency of sounds, and should run on any Windows box from Windows 95 up to Windows 7 (it has been tested on 98SE, 2K, XP and Win 7 x64).

I can’t host the file here due to file restrictions, so if you are interested in having a copy please leave me an email address in a comment and I will post it to WeTransfer for you.

Note: this program was written in visual Basic 6 and requires the VB6 runtime which is not included with Windows 7 or 8. You can download the runtime at Microsoft VBRUN60.exe.

, , ,

3 Comments