Blog

XNA 3.0 CTP Available

in

You can now download the first XNA 3.0 CTP from Microsoft: Announcing: XNA Game Studio 3.0 Community Technical Preview (CTP).

The CTP already integrates into Visual Studio 2008 (both Express and the full Visual Studio). Even though the XNA Team Blog entry says x64 is not supported in this CTP, I'm happily running it on Windows Vista x64 without any issues.

One of the most interesting features in XNA 3.0 for me will be music playback. Older XNA releases required you to load an entire sample to memory in order to play it, with the new XAudio2 bindings in XNA 3.0 it should be possible to actually stream music.

Camera Class

in

When you're working with complicated matrix operations in your game, even when seeing the actual results on screen, it's often quite hard to tell whether your math was really right.

In these cases, I often wished that I could just pause my game and move the camera around in the scene to verify the positions and orientations of all the objects or examine the shader for an explosion that is normally only visible for half a second.

After much deliberation, I settled for the simplest thing that could possibly work: Implement some low-level camera controls directly into my camera class. Whenever the game is paused in debug mode, I can now freely move the camera around in the scene. This turned out to be so useful I thought I'd share it here:

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Nuclex.Graphics {

  /// <summary>Stores the location and orientation of a camera in a 3D scene</summary>
  public class Camera {

    /// <summary>Initializes a new camera with the provided matrices</summary>
    /// <param name="view">View matrix defining the position of the camera</param>
    /// <param name="projection">
    ///   Projection matrix controlling the type of projection that is
    ///   performed to convert the scene to 2D coordinates.
    /// </param>
    public Camera(Matrix view, Matrix projection) {
      this.View = view;
      this.Projection = projection;
    }

    /// <summary>Turns the camera so it is facing the point provided</summary>
    /// <param name="lookAtPosition">Position the camera should be pointing to</param>
    public void LookAt(Vector3 lookAtPosition) {

      // Use a local variable because we can't take a reference to the value returned
      // by a property get. The ref variant is still faster than copying entire
      // matrices around.
      Vector3 cameraPosition = this.View.Translation;
      Matrix.CreateLookAt(
        ref cameraPosition, ref lookAtPosition, ref up, out this.View
      );

    }

    /// <summary>
    ///   Debugging aid that allows the camera to be moved around by the keyboard
    /// </summary>
    /// <param name="gameTime">Game time to use for scaling the movements</param>
    /// <remarks>
    ///   <para>
    ///     This is only intended as a debugging aid and should not be used for the actual
    ///     player controls. As long as you don't rebuild the camera matrix each frame
    ///     this will allow you to control the camera in the style of the old "Descent"
    ///     game series.
    ///   </para>
    ///   <para>
    ///     To enable the camera controls, simply call this method from your main loop!
    ///   </para>
    /// </remarks>
    public void HandleControls(GameTime gameTime) {
      float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

      KeyboardState keyboardState = Keyboard.GetState();

      // Translational controls
      if(keyboardState[Keys.A] == KeyState.Down)
        this.View.Translation += Vector3.Right * delta * 10.0f;
      if(keyboardState[Keys.D] == KeyState.Down)
        this.View.Translation -= Vector3.Right * delta * 10.0f;
      if(keyboardState[Keys.W] == KeyState.Down)
        this.View.Translation -= Vector3.Forward * delta * 10.0f;
      if(keyboardState[Keys.S] == KeyState.Down)
        this.View.Translation += Vector3.Forward * delta * 10.0f;
      if(keyboardState[Keys.R] == KeyState.Down)
        this.View.Translation -= Vector3.Up * delta * 10.0f;
      if(keyboardState[Keys.F] == KeyState.Down)
        this.View.Translation += Vector3.Up * delta * 10.0f;

      // Rotational controls
      if(keyboardState[Keys.NumPad4] == KeyState.Down)
        this.View *= Matrix.CreateRotationY(-delta);
      if(keyboardState[Keys.NumPad6] == KeyState.Down)
        this.View *= Matrix.CreateRotationY(+delta);
      if(keyboardState[Keys.NumPad8] == KeyState.Down)
        this.View *= Matrix.CreateRotationX(+delta);
      if(keyboardState[Keys.NumPad2] == KeyState.Down)
        this.View *= Matrix.CreateRotationX(-delta);
      if(keyboardState[Keys.NumPad7] == KeyState.Down)
        this.View *= Matrix.CreateRotationZ(-delta);
      if(keyboardState[Keys.NumPad9] == KeyState.Down)
        this.View *= Matrix.CreateRotationZ(+delta);
    }

    /// <summary>View matrix defining the camera's position within the scene</summary>
    public Matrix View;

    /// <summary>
    ///   Controls the projection of 3D coordinates to the render target surface
    /// </summary>
    /// <remarks>
    ///   The term 'projection' comes from the fact that this matrix is projecting
    ///   3D coordinates onto a flat surface, normally either the screen or some
    ///   render target texture. Typical projection matrices perform either an
    ///   orthogonal projection (CAD-like) or perspective projections (things get
    ///   smaller the farther away they are).
    /// </remarks>
    public Matrix Projection;

    /// <summary>
    ///   Default world up vector for the camera, copied to a variable here because the
    ///   Matrix.CreateLookAt() method needs a reference to a Vector3
    /// </summary>
    private static Vector3 up = Vector3.Up;

  }

} // namespace Nuclex.Graphics

Writing Good Comments

in

One of the problems people often seem to be having when commenting code is to actually know what to write. A telltale sign is when you find people commenting their class' constructors with // Constructor or an argument for providing a file name with something like /// <param name="filename">File name</param>.

At this level, commenting is a real pain and seems to be a menial task whose cause might be well justified, but which is just too annoying to comply with.

Now the secret to writing good comments and enjoying the process of doing so seems to be the same that makes people write excellent code: your own mentality. The difference is this:

Are you just trying to accomplish some task and all that matters is that your code works? Now commenting becomes a pain.

Or are you creating an unprecedented piece of art that will solve the task at hand so precisely and elegantly that there cannot be a more efficient or clearer solution? Then comments are just one facet in beauty of your creation.

If you see yourself as an artist instead as a grunt, writing good comments and writing great code becomes a a self-sustained motivation: you're only happy with your creation if it looks like a piece of art. Now you want to write clear, concise and useful comments and you're looking for ways to achieve them.

My trick here is to always think about which clues I would need if I was reading the code in front of me for the first time. After some time, you'll become adept at this way of thinking and you'll quickly see what questions a reader of your code will have and how to answer them.

Havok goes C#?

in

Check this out: Havok Recruitment Ad for a Software Engineer.

Desired skills:

  • Strong C# and .NET Experience
  • Managed DirectX
  • Experience with newer MS technologies: WPF/XNA/Vista
  • Knowledge of C++/CLI

Sounds quite a bit as if Havok was thinking about a managed interface to their physics library. C++/CLI indicates that it might be a managed wrapper, not a port (bad luck for us XNA/XBox360 people).

7 Engines You Should Know

in

This post is not exactly related to .NET/XNA game development, but I like to keep track of what's going on in the world out there and I'm still thinking that C++ is a far better language than C#, after all ;)

Having played the whole engine shopping game more than once, here are 7 engines I'd take a closer look at if I were to write a game in C++. I've listed them in order of personal preference, but be warned, this is entirely my opinion and not based on proper research!

1. C4 Engine

The C4 Engine Logo

This is the engine I would choose as an indie developer. Everything you need for rapid development is already there, including a fully interactive world editor. You basically have the development speed of a tool like 3D Game Creator but all the power of a conventional engine designed for programmers first and foremost.

Feature-wise, it's got the already mentioned world editor, is shader driven, does per pixel lighting, uses a scene graph, can do visibility determination with portals, will soon feature a terrain and foliage rendering system (in the upcoming "fireball" release) and you'll receive excellent support from its author and forum members.

www.terathon.com/c4engine

2. Ogre 3D

The Ogre 3D Logo

Ogre 3D was an almost instant hit due to its power and its clean design. Ogre uses a plugin system that can extend the functionality of specific areas in Ogre's architecture. For example, Ogre uses a scenegraph-like system for managing the world. The actual visibility determination system is entirely provided by plugins. You can switch between an Octree, BSP or terrain/distance based system easily.

Because of the very active community, there are lots of great addons for Ogre 3D, including several state-of-the-art terrain rendering systems, game frameworks and bindings for other languages, including a .NET wrapper.

The only drawback to Ogre 3D is the missing development tools. It's a nice programmer's engine but you'll have to invest some time to set up a production chain yourself. Still, there are importers for all kinds of file formats, the engine can load many different file formats natively and Blender, a 3D modeling tool, is moving towards becoming something like Ogre 3D Game Creator (Blender Game Engine uses Ogre!).

www.ogre3d.org

3. Irrlicht

The Irrlicht Logo

Irrlicht is in some ways a contender to Ogre 3D. It might even be more convenient than Ogre 3D in that, as long as you're not doing anything exotic, you can get a basic rendering loop going in just a page of code. Irrlicht also comes with its own GUI system and its own math library. There also is a .NET wrapper called Irrlicht.NET that allows you to use Irrlicht in C# and other .NET languages.

The Irrlicht community is very small compared to other engines, maybe that's because, as soon as you ask for a 3D engine on gamedev.net, you'll quickly get 3 people recommending Ogre 3D and only then someone comes along and suggests Irrlicht.

Irrlicht is also lacking a production tool chain. It's not like Ogre where the tools are there and you just have to find a way to organize everything and make it work together, you probably won't get away without coding some tools yourself if you want to

irrlicht.sourceforge.net

4. Torque

The Torque Game Engine Logo

Torque is the 3D engine that Dynamix originally created to power their Starsiege: Tribes series of games from part 2 onwards. It has got several nice features, amongst them Terrain rendering and a skinnable GUI system. There also is an editor you can use to create your game worlds, design your GUIs and import assets.

While I haven't tried it myself, Torque seems to have its problems as well. I keep hearing again and again that major portions of the code are an ugly mess and that documentation is very lacking. This might be attributable to the fact that a lot of beginners (include lots of those annoying MMORPG-wannabes) switched to Torque, so I'm not making up my mind before I get a look at it myself.

www.garagegames.com

5. Crystal Space

The Crystal Space Logo

Crystal Space is one of the oldest game engines out there. Originally designed for software rendering in the era before 3D acceleration, this engine has grown into a mammoth repository for nearly anything the 3D engine world has to offer.

I can't tell much about Crystal Space, because for my taste, it simply got too large. The learning curve is quite steep and tool support is often lacking. This engine's size seems to make it quite hard for its developers to catch up with the latest technology -- at least I personally got the impression that new features in Crystal Space just take longer and end of looking not as cool as they do in other engines.

www.crystalspace3d.org

6. NeoEngine

The NeoEngine Logo

While I don't have much experience with NeoEngine, it's been around longer than Ogre 3D and is in most terms equivalent to Irrlicht. I would have ranked NeoEngine as equal or maybe even higher than Irrlicht if it had the same level of publicity.

Just like Irrlicht, the developers rolled their own maths library, their own GUI system and they're not using libraries such as Boost either.

I can't really say much about this engine as I don't know of any games that have actually used it. Everything seems to be feature complete and ready for prime time, but I would have to try and create a game with it to see whether it works out...

www.neoengine.org

7. Unity 3D

The Unity 3D Logo

Unity would have been a contender for first or second place, was it not for the fact that all unity development tools work on MacOS exclusively. Once done, you can deploy your game to Windows PCs alright, but to get to that point, you're forced to install MacOS on your system.

What you get with Unity is a game creation system like no other, supporting any feature you can think of. If this sounds like a description released by their marketing department, let me state that their marketing department, if they got or need any, would only be making objective reviews if they wrote that. Yes, I'm that impressed. :)

unity3d.com/unity

XNA 2.0 Beta

in

The XNA Logo

Another piece of great news today: The XNA team has released a beta of the upcoming XNA Game Studio 2.0. Some of the new features are integration into the full Visual Studio editions, a new networking library and the ability to pass parameters to content pipeline importers. The SpriteFont class now supports kerning and the Game class incorporates a default instance of the SpriteBatch class.

Sadly, integration Visual Studio 2008 support is not yet supported. But let's wait and hope for the final release or XNA 2.0 Refresh, if it must be ;)

Download link:
XNA Game Studio 2.0 Beta1

Visual Studio 2008

in

The Visual Studio 2008 Logo

Great news! Today, Microsoft released Visual Studio 2008, including Visual C# 2008 Express and Visual C++ 2008 Express.

Grab them here:
http://www.microsoft.com/express/

Windows Vista users no longer need to run the IDE with administrative rights in order to use the debugger. C++ developers can enjoy better IntelliSense integration and support for x64 targets, which had been exclusive to the full versions of Visual Studio before. C# developers will be able to use the class designer known from the full Visual Studio and of course, C# 3.0 (which, if you ask me, is a horrid abomination and has no justification for its existence :))

Also released together with Visual Studio 2008 is the .NET Framework 2.0 SP1, which you can obtain as redistributable package as well from here:

XNA Game Installer, Part 2

in

My GameSetupHelper library is making good progress. I was finally able to get some insight into the workings of WiX and Windows Installer.

I'm just about to create a small installer template for XNA games that will automatically check whether the required system components are available (for XNA games, these are DirectX 9.0c with the D3DX library from October 2006, the .NET Framework 2.0 and of course the XNA Framework).

The WiX XNA installer template's component slide

What I'm looking for now is feedback on the installer (you can find a download link for an example .msi at the end of this article).

  • Is the detection working for you?
  • Do you think the presentation is okay?
  • Do the logo bitmaps look abhorrent? (I'd happily accept any contributions *wink*)
  • Would people be scared by the additional slide during setup? (I tried to be honest about what the installer does to your system and at the same time follow MS recommendation to just silently install anything that's required)
  • Do I violate any licenses with this? (I am including the official redistributables inside the MSI file, which to my understanding is legal)
  • Anything else that you think might be wrong or missing?

I plan to release the sources of this template for free once I'm done. If you absolutely need to get that installer sources NOW, just send me a mail :)

XNA Game Installer

in

This weekend, I've been thinking about ways to create an installer for my game project. For desktop applications, the technology of choice is of course Microsoft Windows Installer, which generates handy .msi files. It also automatically tracks which steps are neccessary to uninstall the application and it has the convenient repair function. However, when it comes to installing the .NET Framework, the DirectX Runtime or the XNA Framework, there seems to be no existing solution for integrating these steps into a single .msi package.

Another option would be to use NSIS, which requires you to write the uninstall actions yourself and also uses a rather cryptic scripting language, but otherwise provides all the features you expect from a modern installer. It even has LZMA compression (known from 7-Zip), which is far superior to the compression ratio of the .cab files embedded in .msi packages.

I couldn't get my head wrapped around the declarative programming style used by WiX yet, so unless I discover a nice free MSI authoring toolkit that can be in automated builds, I'll probably go with NSIS.

Well, whatever I'll choose in the end, I've written a small DLL that can detect whether the .NET Framework (1.0/1.1/2.0/3.0) is installed, whether DirectX (9.0) is available, whether the XNA Framework (1.0/1.0 Refresh) is installed and whether the AGEIA PhysX System Software is in place. The DLL is written in pure C++ and doesn't rely on anything, not even the C++ runtime libraries. It can be interfaced from both NSIS and MSI.

My GameSetupHelper DLL being unit-tested

If anyone wants to help create an installer template for XNA games, be welcome!

GUI Concept Phase

in

My first concept tried to completely separate the controls and their visual representation by using external visualization objects. As a result, the controls would have to contain pure logic and delegate everything else, like hit-testing, drawing and focus switching to the visualization objects.

In this design, the GUI is completely independent of its visualization, theoretically you could have created a big 3d scene and have some meshes act as controls for the GUI.

GUI library object model concept #1

The problem with that there's complex data exchange between the GUI logic and visualization. For example, a text box needs a visual that draws the text box shape, the text within the text box (including clipping), highlight selected text or draw a cursor.

The refined concept would have the controls provide their complete state via an internal or possibly public interface while the renderer picks the visual responsible for drawing the specific kind of control. The user can still derive from any control to modify its behavior and the theme will still be available for the control. The control tree can still be saved and managed independently of any graphics device or rendering scheme.

GUI library object model concept #2

To hide the control's internals from the user (in order to simplify the public interface of my controls) I experimented with the idea do themeing by deriving from the control classes. This is exactly what CeGui does and it works in this aspect, the controls are not forced to publish their internal state, but I can't quite get myself to like this design.

If you still want the user to be able to derive from controls (for example to modify their behavior), you will either have to reimplement the drawing code for the theme you're using. In C++, this could be solved by using templates, but C# disallows a generic class to derive from one of its type parameters (like class MyThemeButton : ButtonType {}).

GUI library object model concept #3

So only the second concept seems worthwhile to investigate further and there's still some design work to be done to allow for controls to be notified of clicks, mouse movement and other events that depend on the theme (because of collision detection and placement).