Understanding the XNA Framework
The XNA Framework is primarily targeted at game development. Applications built using the XNA Framework run on a game-optimized managed runtime on top of the Windows Phone operating system. As a general rule, if you’re going to write a game, it will be easier to write one on XNA than on Silverlight. XNA offers a unique set of advantages targeted for those wanting very graphically intense applications.
The framework itself is fairly minimalist in regard to user-interface trimmings, such as controls and designers, but provides a lightning-fast runtime execution environment. Programs execute in a loop-based environment, with screen and logic update blocks typically firing 30 times per second. All items on the screen are updated every time the screen is updated, which means it does not show up if you don’t draw it. Unlike Silverlight, by default there is no event-driven programming mechanism in XNA.
To build an XNA game, you use Visual Studio and XNA Game Studio Express. In Part III, “Developing Games with the XNA Framework,” you will dive deeper into using the XNA Framework.
The XNA Framework, built on top of the .NET Framework, uses a specialized common language runtime optimized for games. This runtime is available not only for Windows Phone, but also for Xbox, Windows 7, Vista, Zune, and Windows XP. A program written for one XNA platform may be retargeted with minimal effort to another platform, so a game written for Windows Phone can potentially be extended to target Windows 7 or any of the other XNA runtime environments fairly easily. This cross-targeting capability makes XNA attractive if you want the potential to port your applications to other platforms.
Because the XNA Framework is targeted for game development, it offers a lower memory footprint than Silverlight and provides a high-performance memory framework designed for the demands of near-real-time games. The math and floating-point computational engines are optimized for the phone. Performance is specifically geared toward graphics with high-performance 2D sprite rendering for rotations, transforms, filtering, and others. For 3D scenarios, the phone also offers hardware-accelerated 3D APIs. As a result, XNA provides the optimal platform for graphics rendering.
Game developers often seek unique and innovative game experiences that will make their products stand out. There are no predefined application templates for XNA as there are for Silverlight, which means each application must design and code its own user interface from scratch. This also means there are no design guidelines related to user interfaces for XNA games.
Working with graphics can be very mathematically intense. To help simplify this, XNA embraces the graphical concept of the sprite, which is a flat, 2D texture drawn on the screen to represent a graphical game object. Programmatically, a sprite is an image resource combined with a position telling XNA where to draw it.
When working with images, instructions can be passed that tell how the graphics should be shaded and is a key advantage for the XNA Framework. The four configurable shading effects provided are the following:
In addition to shading effects, the XNA Framework also provides different blend modes to determine how colors interact with each other when they are overlaid and combined. This type of blending is necessary in advanced graphical rendering. Whereas Silverlight for Windows Phone has only Alpha blending, the XNA Framework supports a large number of blend modes, including the following:
If you install the Reach graphics demo from the MSDN Creators site, you can see a number of different rich renderings provided by the XNA Framework. This demo runs in both Windows and Windows Phone. Figure 3.6 shows some screenshots from the Reach graphics demo.
Figure 3.6 Reach graphics demo application.
Although there are no predefined application templates for the XNA Framework, there is a Content Pipeline project template, which translates various media sources into XNA consumable formats. This pipeline is extensible for custom formats, but a large number of formats are supported by default. Standard graphics and audio formats are supported as well as AutoDesk, DirectX, Textures, Microsoft Cross-Platform Audio Creation Tool (XACT), and more. If you have media assets in a large variety of formats you need to utilize, XNA is an excellent choice.
As you have seen, the XNA Framework is designed to be a high-performance game programming framework. As a result, there is not really any visual designer support for user-interface work in XNA. There are also no built-in controls, such as text boxes. In fact, rendering text in XNA can be one of the most difficult tasks because of the lack of support for wrapping text controls, flowable control panels, and other text-rendering elements.
Great XNA Examples
You will find a number of great examples of XNA-based games, such as Glyder: Adventure World, or Flowerz, in the Windows Phone Marketplace. Two sample games, Catapult Wars and Marble Maze, with complete source code are also available from http://create.msdn.com as part of the Microsoft Educational Resource training materials.
Catapult Wars, shown in Figure 3.7, makes use of moving backgrounds, sounds, graphics, fonts, and other content assets inside the XNA Framework. Reproducing this example on Silverlight would be difficult, although not impossible. (With enough time and custom code, anything is possible!)
Figure 3.7 Catapult Wars.
Presented in tutorial format, Marble Maze, shown in Figure 3.8, is another open source sample from Microsoft. The end result shows off the 3D capabilities of Windows Phone 7 and XNA on a very simple level. Even with 3D modeling and code, the game is still smooth and performs well. Developers seeking to learn XNA can use Marble Maze as a starting base for exploring 3D programming on Windows Phone.
Figure 3.8 Marble Maze.