Posts Tagged ‘Tutorials’

Building an Xml Serialization Provider

 
October 13th, 2008 by John Sedlak

While building Galactic Wars 3 version 2.0, I quickly ran into a serious problem with Xml serialization and more specifically deserialization. A few months ago I rolled my own solution for serializing data to and from Xml because the built in classes were bloated and yet not feature rich. A week or two ago I managed to get my class (XmlProvider) to compile for the Zune and Xbox 360. But there was still a problem…

  1. References

    The first problem (the reason for the creation of XmlProvider) was that the built-in classes could not handle references. Every object had to be serialized straight through from top to bottom. Thus if you had an object with two properties that referenced the same object (in memory), the object would be serialized twice.
  2. Inheritance

    The new problem is that with XmlProvider, deserialization was based on the information given by property attributes in your code. This is limiting because you may not know at compile time what exact type of object your property will be holding a reference to. In Galactic Wars 3 this is a problem when I am saving the profile of the user and want to save his specific ship.

So what is the solution? Start from scratch of course and solve these two problems at once! The first one is easy: provide a mechanism with which to uniquely identify any objects in memory and in Xml. We can then build a cache which we can reference from as we find objects with references. The solution to the second problem is also simple: rely on the type name in the Xml rather than the type on the property’s attributes. Read the rest of this entry »

Advanced State System: Queues and Removal

 
October 1st, 2008 by John Sedlak

During the last part of this little article series I mentioned how I used a queue in each state object to allow for continuous transitioning to occur. The reason for this is to allow for complex transitioning to occur. One example for this is a logo screen where it needs to fade in, stay on screen and then fade out again. Rather than relying on managing the states manually, we can add states on to a queue so they occur automatically.

Read the rest of this entry »

Advanced State System: Concept and Base

 
October 1st, 2008 by John Sedlak

There comes a time when simple state management just will not cut it anymore. I found this out while building up the code base for Thrust’s GUI library. As the objects on the screen became more complex, so did their states. This article covers a new way of thinking about an object’s state, a design that has been proven in Thrust (v1.x). The goals of this state system include the following:

Read the rest of this entry »

Restoring Application.LocalUserAppDataPath Functionality in .NET 3.5 / WPF

 
August 29th, 2008 by John Sedlak

With the arrival of .NET 3.5 and consequently WPF applications, Microsoft has done away with the old Application WinForms class. No longer can you call Application.LocalUserAppDataPath to get where the user’s files are stored. To restore this functionality as well as add a bit more, I have tapped into an uncommon resource: extension methods. Let’s dig a little deeper, shall we?

The first thing we need to do is be able to get the Company Name from the assembly. Inside of a blank static class, add the following functions. I have named my class ApplicationExtensions.

Fortunately the company name is kept in an attribute:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static string GetCompanyName(this Assembly assembly)
{
    string name = string.Empty;
 
    object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
 
    if (attributes != null && attributes.Length > 0)
    {
        AssemblyCompanyAttribute aca = attributes[0] as AssemblyCompanyAttribute;
 
        if (aca != null)
        {
            name = aca.Company;
        }
    }
 
    return name;
}

Note my lack of any formal error handling. I will leave this up to you to handle. For now, returning an empty string will do. Next up, we need to be able to format the version number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static string GetFormattedVersion(this Assembly assembly)
{
    string version = string.Empty;
 
    try{
        Version assemblyVersion = assembly.GetName().Version;
 
        version = string.Format("{0}.{1}.{2}.{3}",
            assemblyVersion.Major,
            assemblyVersion.Minor,
            assemblyVersion.Build,
            assemblyVersion.Revision);
    }
    catch{}
 
    return version;
}

And finally, the LocalUserDataPath:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static string GetLocalUserAppDataPath(this Application application)
{
    string path = string.Empty;
 
    Assembly assembly = Assembly.GetEntryAssembly();
 
    if (assembly != null)
    {
        path = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
            Path.Combine(
                Path.Combine(
                    assembly.GetCompanyName(),
                    assembly.GetName().Name),
                assembly.GetFormattedVersion()
                )
            );
    }
 
    return path;
}

MGS: Pong (Pages 4-6)

 
July 4th, 2008 by John Sedlak

In this fourth [page, second post] installment of the MGS: Pong article series, I am going to cover how to draw a background for the game. Before I start, I should tell you that there will be two more articles: one for creating the menu system and one for pulling all the classes together. Okay, let’s begin! The background class (Background.cs) is incredibly easy to create. First we need some private members to draw the background.

Read the rest of this entry »

MGS: Pong (Pages 1-3)

 
July 4th, 2008 by John Sedlak

In part one of the mini game article series, I am going to show you how to create a very simple Pong game from scratch. The tools you will need include Photoshop (or any other painting program such as Paint.NET or even MSPaint), Visual C# 2005 Express, and of course the Xna framework.

First we need to create our project, to give us a home base for all of our work. Go ahead and fire up C# Express and create an empty windows project. I am going to call mine MGSPong for lack of any imagination or any better title. Once the project has been created, add a folder called “Content” and then another underneath that called “Textures.” This is where we will store out art, no matter how bad it may be!

Read the rest of this entry »

Hardware Instancing (Windows)

 
June 6th, 2008 by John Sedlak

Hardware Instancing is an interesting topic that has quite a few uses. The idea of instancing is to use the hardware to generate transformed vertices to reduce draw call overhead. Now I know that sounded like a bunch of senseless technical jargon, and it was. In plain English, sometimes it is faster to let the GPU do all the work for us so the CPU can go off and do other work; and this is what Hardware Instancing does. This article covers how to setup a simple program that uses instancing to render a few rotating squares. Read the rest of this entry »

Introduction to SpriteBatch

 
May 26th, 2008 by John Sedlak

Now that you have created you First Game, it is time to actually start drawing something. Right? How do you expect to make a real game without drawing anything? This tutorial shows you how to use the SpriteBatch object to efficiently draw 2D items on the screen. In the end you will be experienced with rotations, scaling, sprites and sprite sheets. So let’s get started! Read the rest of this entry »

My First (XNA) Game

 
May 25th, 2008 by John Sedlak

This tutorial is dedicated to those who are new to the world of XNA. After installing Visual Studio 2008 (or C# Express 2008), DirectX 9.0c, and Game Studio 3.0 (currently CTP), open up Visual Studio. If this is your first time opening the software up, it will probably ask you what kind of settings to use. I recommend C# settings, but don’t worry because the layout is completely changeable. Once the IDE (Integrated Development Environment) is opened fully, we can create our first game!

Read the rest of this entry »