XNA 2D Map Rendering

Hello everyone,

Map rendering in XNA is kind of a hollow process at the moment because of how much work there is involved in getting this up and running.  Bottom line is you will probably end up doing everything within the game class in the beginning, but there is a better way.  To overview the architecture that I started with, I created a static World class that was responsible for holding the current state of the application.  The Game itself passes all commands to the world class in a separate object library so the objects become encapsulated from the application.  This class looks something like this:

public static class World
{
    public static void LoadContent(ContentManager CurrentManager)
    {
        Maps = CurrentManager.Load<Map[]>("maps");
        MapCells = CurrentManager.Load<MapCell[]>("mapcells");
    }
    public static void Draw(SpriteBatch CurrentBatch)
    {
        CurrentScreen.Draw(CurrentBatch);
    }
    public static void Update(GameTime CurrentGameTime)
    {
        CurrentScreen.Update(CurrentGameTime);
    }
}

The nice thing about this class, is now I can change the screen at any point in the application and expect it to render.  In this example, we will be rendering a map.  I’ve created a map object which is responsible for being loaded from the XML content file and drawing itself as the current screen on the world.  Also, as a note, I’ve created an interface called IGameScreen which is implemented by the map.  I also implement this interface in menu screens as well for moving from a menu to a map etc.   Each individual object is responsible for receiving the update and draw commands from the master world, and representing themselves on the application.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>