Archive for the 'XNA' Category

Map Tiles (4) - XNA Program

Monday, August 13th, 2007

The last three posts discussed map tiles in some detail but without getting into programming. First, we described terminology surrounding map tiles. Then we setup a sample TileMap from an image of the world. And finally, we covered some “tile math” examples. Now we will wrap everything up by developing a mapping program that uses map tiles.

The program we created was built off one of our previous projects (see World Mapper parts 1,2,3) and, as always, the XNA framework. This project turned out to be much more complex than any of our previous programs so it would be impossible to cover all the source code in detail. Instead, this post will provide an overview of the program’s structure and then provide the source code for you to download and explore on your own.
(more…)

Mapscript and XNA (3)

Sunday, July 8th, 2007

Continuing with our program that combines Mapserver (via the Mapscript API) and XNA, we next added functionality to the update method our of XNA program. We check for a mouse click to initiate a map pan and also for key presses to zoom in, out and return to full extent. At the end, if any map event has occurred, the map will be updated. It is in the updating that a new Mapserver image is generated and prepared for displaying on-screen.

protected override void Update(GameTime gameTime)
{
    // check if a mouse click has occured
    mouse.Update();
    if (mouse.LeftButton == ButtonState.Pressed)
    {
        mapserver.ZoomMap(1, mouse.X, mouse.Y);
    }
 
    // check if a key has been pressed
    KeyboardState keyState = Keyboard.GetState();
    if (keyState.IsKeyDown(Keys.F))
    {
        graphics.ToggleFullScreen();
    }
    else if (keyState.IsKeyDown(Keys.Z))
    {
        mapserver.ZoomMap(2, mapserver.MapPixelCenter.X, 
                      mapserver.MapPixelCenter.Y);
    }
    else if (keyState.IsKeyDown(Keys.X))
    {
        mapserver.ZoomMap(-2, mapserver.MapPixelCenter.X, 
                      mapserver.MapPixelCenter.Y);
    }
    else if (keyState.IsKeyDown(Keys.C))
    {
        mapserver.ZoomMapFullExtent();
    }
    else if (keyState.IsKeyDown(Keys.Escape))
        this.Exit();
 
    // check if a new mapserver image needs to be generated
    if (mapserver.RefreshMap)
    {
        mapserver.UpdateMap(graphics.GraphicsDevice);
    }
 
    base.Update(gameTime);
}

(more…)

Mapscript and XNA (2)

Sunday, July 1st, 2007

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
(more…)

Mapscript and XNA (1)

Sunday, June 24th, 2007

We’ve demonstrated a number of smaller projects over the past couple of months showcasing some simple mapping and XNA applications. Now things are starting to get more interesting as we build upon what we’ve learned in an effort to create more interesting products. This next series of posts combines aspects from several of our projects, including World Mapper, Using Mapserver, Mapscript Demo, and the Image Swapping example. Basically, we now have the opportunity to incorporate Mapserver-generated images into an XNA program.

(more…)

XNA and GIS (4) - Image Swapping

Sunday, June 17th, 2007

We’ve already discussed in general how XNA could be used as our graphics engine for building a mapping application. Today we will demonstrate a specific example of XNA in action as it relates to map images.

Let’s assume that we have a map image already generated by a mapping engine (Mapserver/Mapscript for example).

Image 1

(more…)

Console Demo (1)

Sunday, May 13th, 2007

This project demonstrates how to use the new font support in XNA by building a basic console type application. Eventually it could be combined into a more robust program but for now we just want to focus on text input. For an introduction to the new XNA font support, see our last post.

Our definition of a console consists of two parts. First is the command prompt line where the user can enter text, press enter, and something occurs if the user enters a valid command. Below that line are past lines of text that the user has typed or it could be used to display information about the program.

Console1

(more…)

XNA and GIS (3) - Using Fonts

Sunday, May 6th, 2007

The recent release of Microsoft’s XNA Game Studio Refresh offers one important new feature: font support. If we want to build any sort of mapping application, text is an absolute necessity. So here is a quick tutorial of how to incorporate font into a XNA program. For a complete description, check out the Game Studio documentation.

Font Preparation

Font is treated similar to any other 2D sprite in XNA except for the source of the texture. Instead of relying on graphics, XNA obtains the font from the font files already installed on your computer. Examples include Times New Roman, Courier New, and Arial.

Preparing to use a font is simple. In any XNA program, right-click in the Solution Explorer and select Add New Item. Scroll to the bottom and select the Sprite Font icon:

xna_spritefont

(more…)

XNA Game Studio Express Refresh

Saturday, April 28th, 2007

Microsoft has released an update (”refresh”) for the XNA Game Studio Express so I encourage everyone to go and download it. This update includes official support for fonts which we will start using very soon here at Spatial Horizons. Before you can install this 83MB update, you must have the C# Express Service Pack 1 installed first.

World Mapper (3)

Monday, April 16th, 2007

This project continues our initial exploration into creating mapping applications using XNA. Last time we setup the basic application and today we will add some new functionality.

Resizing the Image

I first created a larger background image (800 pixels x 400) to be used for the rest of this project. Two new variables were added:

int mapWidth = 800;
int mapHeight = 400;

…and used in the Initialize Function:

graphics.PreferredBackBufferWidth = mapWidth;
graphics.PreferredBackBufferHeight = mapHeight;

(more…)

World Mapper (2)

Monday, April 2nd, 2007

We will now create a simple XNA program that uses some of the functionality discussed in our last 2 posts (XNA and GIS [1][2]). It will also introduce how to include input from the keyboard and mouse.

For the first time, we’ve added the option to download the source code at the bottom of this page. The primary reason for doing this is because our projects are becoming too complex to list the entire source code in the post itself. Instead, we will now try to highlight only the new and most important code snippets.

(more…)