Export (0) Print
Expand All

Appendix B - Silverlight and XNA in Windows Phone 7

Windows® Phone 7 supports development of applications using both the Silverlight® and XNA® frameworks. You can also interact with XNA from a Silverlight application to perform tasks that are not supported in Silverlight if you choose it as the primary development framework for your application.

This appendix will help you understand the differences between the Silverlight and XNA development models, and choose the appropriate model for your application. It describes the basic differences between Silverlight and XNA, and it discusses how XNA is focused on game-oriented applications rather than business applications. However, you can write games using Silverlight if you want. Windows Phone 7 devices contain a graphics processing unit (GPU) that can provide acceptable performance for games written in Silverlight.

Basic Differences between Silverlight and XNA

The following sections and the table that follows them indicate the basic differences between Silverlight and XNA.

Execution Model

The execution model for Silverlight is event-driven and application-focused. Interactive controls and components expose events that you can handle to perform processing based on user input, environmental effects, and more. XNA uses a gaming loop and is focused on the content displayed on the screen.

Performance

XNA is optimized for performance and GPU usage in order to support a fully immersive gaming experience. Unlike Silverlight, XNA supports three-dimensional (3-D) graphics. You cannot interop with the graphics mechanism from Silverlight.

Sounds

XNA supports more advanced options for playing sounds, including multi-channel audio. You can only record sounds by using XNA. If you are developing using Silverlight, you can use interop with the XNA framework to record sounds using the built-in microphone on the device.

Screen Display

XNA uses the full screen all the time, and you must generate all the content for display within your code. There are no controls available. Silverlight provides a comprehensive range of controls, and it can interact with the built-in launchers and choosers within the Windows Phone 7 operating system to provide a wide range of capabilities for interactive applications.

Summary of the Basic Differences

Platform features

Windows Phone application

(Silverlight-based application model)

Windows Phone game

(XNA Framework application model)

Application programming model

Event-driven application model that is ideal for mainly user interface (UI)–based applications

Traditional frame loop for a more simulation-based experience and content.

Control-based, data-bound UI development

UIElement and related types

None, developers must write their own.

Video playback

Rich video integration via MediaElement

Full-screen playback via the system media player.

2-D graphics

Rich paths, shapes, brushes, and more

High performance 2-D rendering for a large number of sprites.

3-D graphics

Perspective effects using PlaneProjection transform

Provides hardware-accelerated 3-D APIs.

Primary data serialization model

XAML

Content manager.

Designer tooling

Microsoft® Expression Blend® design software, Microsoft Visual Studio® XAML designer

XNA Content Pipeline integrates third-party tools for creating 3-D model and texture assets for the application.

Device sensors

Same API in both programming models.

Access to user’s songs and pictures

Same API in both programming models.

For more information about Silverlight, see the Silverlight website (http://www.silverlight.net/). For more information about XNA, see the App Hub website (http://www.xna.com/).

The XNA Game Execution Model

XNA uses a game loop that is generated by the design tools. The life cycle for a game consists of four main stages, as shown in Figure 1.

Gg490774.1391872e-21c9-4f98-909c-c5cde29600e4-thumb(en-us,PandP.10).png

Figure 1

The life cycle for an XNA game

The game first initializes and then loads content such as sprites, images, sounds, and other components of the game. It then initiates the game loop. The game loop consists of two processes that run asynchronously: code that draws the frame containing the complete screen to a buffer, and code that updates the screen from the buffer. This loop runs until the user closes the game, at which point the content is unloaded.

Gg490774.note(en-us,PandP.10).gifNote:
Draw and Update are executed at different rates. Draw is executed as frequently as possible based on the maximum frame rate that the device can provide, whereas Update is executed at a regular rate. The draw rate should generally be higher than the update rate.

For more information about creating games using XNA, see "Getting Started with XNA Game Studio Development" on MSDN® (http://msdn.microsoft.com/en-us/library/bb203894.aspx). For a walkthrough of creating a simple game using XNA, see "Your First Game: Microsoft XNA Game Studio in 2D" at on MSDN (http://msdn.microsoft.com/en-us/library/bb203893.aspx).

Using Interop from Silverlight to XNA

The following are some of the features of Windows Phone 7 that you may want to use in a Silverlight application where interop with XNA is required:

  • Accessing input gestures on the device, such as drag, pinch, and hold gestures
  • Playing multi-channel audio
  • Advanced sound effects (such as using the XNA DynamicSoundEffectsInstance class, which lets you submit packets on the fly, vary the pitch, and apply 3-D effects to the sound)
  • Recording audio

For the last two features in the preceding list, you must create an XNA asynchronous dispatcher service and initialize it in the constructor or startup event in your App.xaml.cs file. This is required to raise the events in Silverlight when they are raised by the XNA objects.

Creating and Using an XNA Dispatcher Service

The XNA run-time event mechanism is different from the Silverlight event mechanism. Events raised by XNA objects are available within the XNA game loop, but they are not automatically available to the Silverlight event mechanism. To expose XNA events to Silverlight application code, you can take advantage of the Silverlight ability to use application extension services. These are services that add functionality to an application, and that can be reused by many applications. They circumvent the requirement to use inheritance. For more information, see "Application Extension Services" on MSDN (http://msdn.microsoft.com/en-us/library/dd833084(VS.95).aspx).

The following code example shows an XNA asynchronous event dispatcher service that you can use in your Silverlight applications.

using System;
using System.Windows;
using Microsoft.Xna.Framework;
using System.Windows.Threading;

namespace YourAppNamespace
{
  public class XNAAsyncDispatcher : IApplicationService
  {
    private DispatcherTimer frameworkDispatcherTimer;

    public XNAAsyncDispatcher(TimeSpan dispatchInterval)
    {
        this.frameworkDispatcherTimer = new DispatcherTimer();
        this.frameworkDispatcherTimer.Tick 
             += new EventHandler(frameworkDispatcherTimer_Tick);
        this.frameworkDispatcherTimer.Interval = dispatchInterval;
    }

    void IApplicationService.StartService(ApplicationServiceContext context) 
         { this.frameworkDispatcherTimer.Start(); }
    void IApplicationService.StopService() 
         { this.frameworkDispatcherTimer.Stop(); }
    void frameworkDispatcherTimer_Tick(object sender, EventArgs e) 
         { FrameworkDispatcher.Update(); }
  }
}

The DispatcherTimer class is a .NET Framework class that is included in Silverlight. It executes on the application thread and can raise events at preset intervals. In the preceding XNAAsyncDispatcher example, the Tick event executes the Update method of the FrameworkDispatcher class, which is part of the XNA Framework. The FrameworkDispatcher class is an alternative mechanism to drive the update loop if the base class is not an XNA game.

For more information, see "DispatcherTimer Class" (http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx) and "FrameworkDispatcher.Update" (http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.frameworkdispatcher.update.aspx) on MSDN.

After you create an XNA dispatcher service class, you can instantiate it in the constructor of your application class in the App.xaml.cs file. The typical tick rate for capturing and exposing XNA events is 50 milliseconds.

public App()
{
  // other constructor code here ...
  this.ApplicationLifetimeObjects.Add(new
                  XNAAsyncDispatcher(TimeSpan.FromMilliseconds(50)));
}

For information about using these and other device capabilities, see Appendix C, "Leveraging Device Capabilities".

Excluded Classes and Assemblies

There are limitations on the classes and assemblies you can use or deploy to Windows Phone 7, depending on whether you are using the XNA or the Silverlight development model. For a full list of these, see "The Silverlight and XNA Frameworks for Windows Phone" on MSDN (http://msdn.microsoft.com/en-us/library/ff402528(VS.92).aspx).

These and all links in this book are accessible from the book's online bibliography. The URL to the bibliography can be found in the preface, in the final section, entitled, "Where to Go for More Information."


Show:
© 2014 Microsoft