How to use XnaContent XML Files

Google AdSense

There’s a couple methods to working with XML documents in the XNA games. First, you can just serialize and deserialize your class files at run-time.

Why use XML files for game development?

By saving data outside of your executable, you can keep your data and code separate. With external data, you can make updates to the files without needing to write more code. You can also easily update and maintain the files without rebuilding your projects.

What’s the difference between using XnaContent and Serialization methods?

Run-time vs. Design-time Validation

The biggest difference between the two methods, is that using XnaContent XML files allow you to catch errors when you build your project rather than getting XML errors at run-time.

Of course, you could build XSD documents to validate your serialized class files or just trust that however the files were created, that they were created correctly.


The second difference is that the XnaContent XML documents will be converted and compressed to XNB format and your serialized files will still be XML documents.

Continue reading

XNA Game Development

Google AdSense

Well, I’m back trying to make my XNA game again. It feels like forever ago, when XNA was just in beta and I was messing around with it. And it feels like even more forever ago, that I was working in C++ trying to get my game finished.

Today, I don’t get much free time to devote to my hobby of game development. So I’ve got to make the most of the time that I can spend. Between work and life, I do squeeze in a few hours, but I’ve yet to finish a game.

Continue reading

How to load a Google SketchUp model into a XNA game

For casual 3D modelers, Google SketchUp is a good alternative to more complicated and robust software such as Blender and Maya.

For developers who prefer to get decent looking models up and spend their time writing code, then SketchUp could be used to assist in visualizing their concept.

You can use the free version of Google SketchUp to export to the Microsoft DirectX .X format by using a custom built plug-in. I’ll describe the steps below, but if you are just looking for the plug-in then it can be found at:

The script was created by r1kdou (of XNA forums). You can view his original post here.

Installation & Setup

First, install Google SketchUp, install the above plug-in (just copy it to the <Google SketchUp install directory>/plugins folder).

Finding & Loading the Model

Next create your own or find a model via Google SketchUp warehouse. There’s tons of free models built by users and you’ve bound to find at least one or two you can use.

Here’s a good looking model that I’ll be using:

Sketchup Model Example

Export To DirectX

Next, export the model to .X format by choosing the “Export DirectX” menu choice in the “Plugins” menu.

Loading the Model into your XNA Game

Finally, copy the .x file to your “Content” folder, and include it in your project using the default “X File – XNA Framework” content importer and “Model – XNA Processor” content processor.

Don’t forget to include any texture files that the model requires. If you don’t know which textures are in use, you can open the .X file using Notepad and see the TextureFilename values (listed at the top of the .X file).

Place the textures in the same directory as the .X file, but you don’t need to add the texture images to your XNA project. Also, make sure your image dimensions are all resized to a power of 2 (2, 8, 16, 32, 64, 128, 256, etc.).

You will get a compile error if you are missing texture files or an image size is not a power of 2.

Viewing the Model in your XNA Game

Assuming, you have your camera and basic game already coded, you can now just load the model using the ContentManager.Load<Model>(“modelName”) function.

Here’s a screen capture of the above model loaded into my current game framework. You may recognize the terrain and skydome from the XNA Generated Geometry sample.

XNA Game using SketchUp Model

You can also use Google SketchUp for designing entire levels. I’m unsure of the speed involved in loading a level of that size, but hopefully (one day), I’ll test that out when my game makes a little more progress.

How to create a heightmap and skydome using Terragen for XNA

In this example, I’m going to use the Generated Geometry Sample provided by the XNA Creators Club as the code base, so we can just focus on creating heightmaps and the skydome.

Terragen is a great scenery generator and best of all, it’s free for personal (non-commercial) use. However, the unregistered version has some limitations such as the image dimensions that you can generate. There’s a great gallery of Terragen graphics showcasing it’s abilities. Terragen also has numerous plug-ins for other 3D software packages such as Blender, 3D Studio MAX, LightWave 3D, Maya, etc.

The heightmap used by the sample is named “terrain.bmp” and it’s dimensions are 257×257. The sky image used by the sample is named “sky.bmp” and it’s dimensions are 2048×1024. Both of these images are found in the root Content folder.

Generating the heightmap

When Terragen opens, you will find the Landscape dialog at the foreground. To generate a random terrain, click the Generate button. On the Terrain Genesis dialog, you can play with the various options for a bit. Finally, click the Generate Terrain button and then the Close button.

To preview the terrain, click the Render Preview button on the Rendering Control dialog. Without a texture applied, you probably won’t see much.

Exporting the heightmap

To change the file dimensions, click on the Size button of the Landscape dialog (located in the Terrain panel). You can increase the size to 513×513 with the unregistered version of Terragen. Registering the software will allow you to export to higher resolutions.

You can export the surface to a RAW image format and then convert it to an image format such as BMP, JPG, etc. that is more friendly with the XNA environment. Alternatively, you can download the FEO (For Export Only) plug-in that enables you to save your terrains to BMP, DXF, RIB, and OBJ formats (once you follow the install instructions for FEO, you can click on Accessories and Export to BMP).

Once you get the image into a BMP format (or other XNA compatible format), just copy the file into your Content folder. We’ll get back to that after we finish the skybox.

Generating the skydome

Now that we have the heightmap finished, we can move on to the skydome. In this sample, the skydome is just one image that wraps around the terrain. We can create this by taking four images (North, East, South, West) from the center of our scene and stitching them together.

First, we need to setup our scene.

Uncheck the Fixed Height Above Surface for camera and target (2 checkboxes) in the Camera panel.

Find a good spot to use as the center point. I chose the center of my surface (x=128, y=128 terrain units). I played around a bit with the altitude (z) until I wasn’t below the surface.

Increase your detail slider to the max (rightmost) in the Image panel.

We need to apply some texture to the surface so our image will look nice. Click the Open button in the Surface Map panel of the Landscape dialog. Terragen comes with a handful of surface maps that you can use (you’ll may need to browse to your Terragen install directory). I’ll chose GrassAndSnow2.srf for my surface.

Exporting the skydome

Set your Image Width and Height to 960 and 960 (the best resolution you can export with unregistered is 1280×960).

The next few steps will take the longest time. You’ll need to change your camera settings, render an image, and save the image for each of our four directions.

Alternatively, you can download the script at the end of this article to automate the process. It will still take a while to render each of the images, but at least you can walk away for a few minutes.

View Name Camera Heading Camera Pitch Camera Bank
North 0 0 0
East 90 0 0
South 180 0 0
West 270 0 0

Change the camera orientation to the above parameters, render the image, and then export the image. Once all 4 images are done, stitch the images together in a clockwise manner (N->E->S->W or S->W->N->E, etc.).

You may need to reduce the combined width of the image to 2048 to run on your PC.

Testing the scene

Open the GeneratedGeometryWindows solution.

Note: If you overwrote the files from the sample using the same filenames, then you can skip this part and just start the sample application to view your work. You will need to rebuild the solution, so that the XNB files are updated via the pipeline otherwise, it will load the old XNB files.

Adding the files to the project

In the Solution Explorer, make sure you enable the Show All Files option by clicking the button above the solution tree. Expand the Content folder (you may need to refresh the file list).

Select the terrain heightmap image from the previous steps, right click, and choose Include In Project. View the heightmap image properties and change the Content Processor to TerrainProcessor.

Select the sky image from the previous steps, right click, and choose Include In Project. View the sky image properties and change the Content Processor to SkyProcessor

Make code changes

Update the code in the function called GeneratedGeometry.LoadContent. The model loaded for the terrain object should be the filename that you created (do not include the file extension). The sky file loaded for the sky object should be the skybox image that you created (do not include the file extension).

Run the application

Build your solution and then click Start Debugging (or just press F5) to see your newly created heightmap and skydome. You may also want to turn off the Fog effects drawn on the terrain (in the GeneratedGeometry.DrawTerrain function).

Notes on the Generated Geometry sample

The sample code uses a tiled texture for the terrain surfaces, so unless you replace “rocks.bmp” with a more fitting texture, your landscape scene might look a bit out of place.

Skybox Script for Terragen

Copy the following script into Notepad and save as “SkyboxScript.tgs”. Execute this script in the above steps. The below script will generate 4 images in your C:temp directory by changing the camera orientation to the same parameters as listed in the table above. The frend (frame render) command renders the image and saves to your temp folder. The images will be named skydome0001, skydome0002, skydome0003, and skydome0004. So just stitch them together in the same order.

initanim "C:tempskydome", 1 

Zoom 1.0
CamH 0
CamP 0
CamB 0

Zoom 1.0
CamH 90
CamP 0
CamB 0

Zoom 1.0
CamH 180
CamP 0
CamB 0

Zoom 1.0
CamH 270
CamP 0
CamB 0

Update 1 (3/4/2008): Some of my friends remarked that this post was useless without pics. With 3G of upload space remaining here, I might as well upload a few.

Scene example – generated from Terragen, default parameters
Terragen Scene Render

Heightmap example – generated from Terragen, default parameters
Terragen Heightmap Example

Skydome image example – generated from Terragen, default parameters using script above
Terragen Skydome Landscape render

Democratizing Game Development

I’ve long been a fan of games and game development. I created my first game over 20 years ago and saved it to a cassette (remember those things we used to have in cars before CDs?) on my Coleco Adam computer.

I also remember creating numerous text driven games for my Apple IIc Plus using Applesoft BASIC and saving them to my 5 1/4 inch floppy disks. A few months ago, I found my first game development book (for SmartBASIC) and a stack of 5 1/4 inch floppy disks in my parents basement…

Well, that was a long time ago. Today, there’s lots of great gaming libraries and 3D engines for the PC. I experimented with a few of these using C++, but I’ve rarely finished any of games before I got distracted on another project or something else came up.

A few years ago, Microsoft released the XNA framework for Visual Studio. This framework allowed developers to make games compatible with both PCs (Microsoft operating systems) and XBOX 360s. I participated in the version beta prior to it’s release and I’ve worked with the 1.x and 2.x versions, but I still haven’t finished any games so far.

I’m still pretty excited about the XNA framework and it’s impact on the programming industry.  I’ve thought this was great for Microsoft since I heard the first rumors. I believe the XNA framework will bring young developers into the .NET world and even though very, very few will get into the game industry, they will take their .NET skills into the marketplace. Some colleges have even designed programming courses around the XNA framework.

At the Game Developers Conference this week, Microsoft announced their plans for the next version of XNA.  Version 3 will include “XBOX Live Community Games“. They also announced support for game development on the Zune, which I think is a good direction also.

This provides developers the ability to upload their games to a community site where peers can download and review your games. If your game passes the peer reviews, then it will become available to all XBOX owners to download and play.

TalkingAboutGames provides a little more details stating:

Gamers will be able to rate user-created XBLA titles through XNA Creators Club once the service goes live… Once a game is submitted, it goes through a peer review process (which eliminates copyrighted material and/or prohibited content), and if it passes, it’s made available for download over Creators Club, which will be located on the Xbox Live dashboard. If that’s not enough, XNA games can now be played on your Zune as well.

Gamasutra has a lengthy article titled, “Democratizing Game Distribution“. The article provides general instructions on the game submission process. The submission form will contain options for the developer to rate violence, sex, and mature content on a scale of 0 to 5, upload screenshots and movie trailers, and upload the game package (.ccgame file).  Next, fellow XNA Creator Club members can submit reviews and game defects (bugs) for the game. Once enough positive reviews have been submitted, XBOX users can download the game via the XNA Game Launcher.

There is a special preview of this ability available until mid-March. You can download the XNA games of The Dishwasher: Dead Samurai, JellyCar, Little Gamers, Proximity HD, Rocketball, TriLinea, and Culture.

Maybe it’s about time that I get back to work on my own game!