Mapscript and XNA (2)

In order to use Mapserver in XNA, we need to create a class that primarily serves as an interface between XNA and Mapscript (Mapserver’s API). Our last post discussed in general how to accomplish this but now let’s add some code.

MapserverXNA Class

We named the class MapserverXNA and the properties and methods look like this:

UML MapserverXNA Class

The properties include information on the map’s size (height, width, and center), the location of the map file, and a boolean value (RefreshMap) that tracks whether the map needs to be updated.

The methods include the ability to update the map (i.e. generate a new image from Mapserver) and draw the map onscreen. There are also two zoom methods: ZoomMap allows for zooming in, out, and panning while ZoomMapFullExtent returns the map to the full extent.

We won’t discuss in detail the code inside of this class. It is included in the source code download below so you are welcome to take a look yourself.

Using the MapserverXNA Class

The next step is to setup a XNA program to use the newly designed class. We could start from scratch but instead this project will loosely build off the structure of World Mapper project from a while back. It does follow the standard form of a XNA game application as discussed in a previous post. We also incorporated what we learned in our Mapscript Demo and our post on XNA font.

With our XNA game program setup, we added several more fields (private variables) to the main game class:

GraphicsDeviceManager graphics;
ContentManager content;        
SpriteBatch spriteBatch;
shMouse mouse;
shMapserver mapserver;
SpriteFont font;

In the constructor, we then setup our mouse class and initialized the MapserverXNA class with a valid map file string and size in pixels.

public MapscriptXNA()
{
    graphics = new GraphicsDeviceManager(this);
    content = new ContentManager(Services);
 
    mouse = new shMouse();
    mapserver = new shMapserver(@"C:\ms4w\apps\gmap\htdocs\gmap75.map", 
        600, 600);            
}

In the initialize method, we resize the screen to match the map size:

protected override void Initialize()
{
    graphics.PreferredBackBufferWidth = mapserver.MapWidth;
    graphics.PreferredBackBufferHeight = mapserver.MapHeight;
    graphics.ApplyChanges();
    base.Initialize();
}

In the load graphics method, we load both the mouse cursor and font from using XNA’s Content Pipeline:

protected override void LoadGraphicsContent(bool loadAllContent)
{
    if (loadAllContent)
    {
        spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
        mouse.Cursor = content.Load<Texture2D>(@"Content\Textures\MouseCursor");
        font = content.Load<SpriteFont>(@"Content\Font\MapFont");
    }            
}

We will continue in our next post with the rest of the application’s code but the entire source code can be downloaded below. For new readers, we encourage you to glance over our past projects or read the new FAQs page.

Download Source Code

Comments are closed.