Drawing Rectangles with SpriteBatch

Just a quick code snippet which adds an extension method for drawing Rectangles to SpriteBatch:

public static class SpriteBatchHelper
{
    static Texture2D pixel;

    private static void LoadPixel(GraphicsDevice graphicsDevice)
    {
        if(pixel == null)
        {
            pixel = new Texture2D(graphicsDevice, 1, 1);
            pixel.SetData<Color>(new Color[] { Color.White });
        }
    }

    public static void DrawRectangle(this SpriteBatch spriteBatch, Rectangle rectangle, Color color)
    {
        LoadPixel(spriteBatch.GraphicsDevice);
        spriteBatch.Draw(pixel, rectangle, color);
    }
}

C# Extension Methods in your own Library

Normally I use extension methods in C# to extend a library that I did not write and therefore I have no control over. There are situations where it makes sense to use extension methods for a library that you yourself are writing.

For example, when you have interfaces in your library. You want to keep the number of methods in that interface as low as possible so that classes implementing the interface don't have to do a lot of heavy lifting. This means cutting out methods in an interface that are for the most part just syntactic sugar for another method in the interface.


public interface IServiceContainer
{
    void AddService(Type type, Object provider);
    object GetService(Type type);
}

public static class IServiceContainerExtensions
{
    public static void AddService<T>(this IServiceContainer services, object provider)
    {
        services.AddService(typeof(T), provider);
    }

    public static T GetService<T>(this IServiceContainer services) where T : class
    {
        return services.GetService(typeof(T)) as T;
    }

    public static T GetRequiredService<T>(this IServiceContainer services) where T : class
    {
        T service = services.GetService(typeof(T)) as T;

        if(service == null)
            throw new ServiceNotFoundException(typeof(T));

        return service;
    }
}

All of the methods in IServiceContainerExtensions are just helper methods for method in IServiceContainer. By making them extension methods in our own library though, we've made the barrier to entry lower. Other people can implement the interface and in a sense "inherit" the helper methods as well.

Creating and consuming services in your XNA Game

February 18, 2010.net

The GameServiceContainer implements the IServiceProvider interface and the MSDN documentation says about the IServiceProvider interface:

Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.

This article will "attempt" to describe how can you use the GameServiceContainer in your XNA game, in both your GameComponent(s) and your game's entity objects.

Read More

Changing the PlatformTarget in Visual C# Express

January 18, 2010.netmsbuild

Some project types in Visual C# Express (Empty Project) will not allow you to change the PlatformTarget from the UI. You can still change the target platform though by editing the .csproj file in a text editor. Close the project and open it up in your favorite text editor (I use Notpad++). The .csproj file is really just a XML file. You should see somewhere in the file something like:

    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        ....
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        ....
    </PropertyGroup>

Inside the PropertyGroup elements, add the PlatformTarget element:

    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        ....
        <PlatformTarget>x86</PlatformTarget>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        ....
        <PlatformTarget>x86</PlatformTarget>
    </PropertyGroup>

Save the file and open your project back up. Your project's output should now target only x86.

Calculating an angle from a Vector2

February 28, 2009.netvectorsxna

When you need to calculate an angle from a Vector2 structure, you can use this piece of code:

public static class Vector2Helper
{
    public static float CalculateAngle(Vector2 v)
    {
        float angle = 0.0f;

        if(v != Vector2.Zero)
        {
            v.Normalize();

            angle = (float)Math.Acos(v.Y);

            if(v.X &lt; 0.0f)
            angle = -angle;
        }

        return angle;
    }
}

I used this to calculate an angle from the Vector2 of the Left Stick.

The original credit for this source code comes from here.