Choosing Between Silverlight and XNA for a Windows Phone 7 Game
Submitted by smartyP on Sat, 05/15/2010 - 13:17In my last blog post on XNA for Windows Phone 7 I said that I was working on a game in both Silverlight and XNA, and that if I got a chance to test out performance I would report back. As you can see in the image below, I got a chance to test out my Silverlight game on a real device last week at ReMIX Atlanta - so this post is to cover some insights gained.
me testing my game prototype on a Windows Phone 7 device
First off, if you are trying to get a basic understanding of when to choose between XNA and Silverlight for WP7, you should take a look at this post by Michael Klucher. Michael lays down the basic differences between the two platforms - Silverlight has great controls with great tool support for styling them, and XNA has a crazy fast sprite rendering pipeline which supports full 3D.
But, let's say that you don't need controls for your game, and you don't need 3D - is Silverlight good enough for writing a basic 2D game? After playing with a device briefly last weekend I can tell you the performance of Silverlight on the device looked great - but you will need to analyze the game you're creating a bit before making the ultimate decision.
(click 'read more' to keep reading..)
The game you see in the screenshot above is a simple game I am working on with a good friend. I don't want to get into the details about this game since it is still early on, so instead I'm going to pretend we are trying to build Breakout - or Arkanoid - hopefully something we are all familiar with.
My game, like Arkanoid, requires the user to constantly move an object left to right on the screen. As you can imagine with Arkanoid on a mobile device, this would be accomplished by the user using their finger to drag the paddle left and right via the touch screen.
For this kind of game we want a game loop, something which can be easily accomplished in Silverlight via the CompositionTarget.Rendering event (example here). This game loop mimics the game loop most game developers are already familiar with, and something which is built into XNA. The basic jist is that there is a never-ending game loop and every iteration has an Update() method which lets you update your game logic, player positioning, etc., and a Draw() method which lets you paint your objects to the screen.
Where Silverlight differs in emulating this game loop is that Silverlight is an event driven platform. While in XNA you may check the status of a touch point each time the Update() method is called, in Silverlight you must listen to events for touch points via ManipulationStarted, ManipulationDelta, and ManipulationEnded events. In these events you can update the variable storing the position of your object and then update their position only in your Draw() method, but the events are going to be constantly thrown as the user drags their finger around - and as best as I can tell this can have a direct impact on performance.
When trying out my game I noticed that it animated at a full frame rate with no interaction - it also worked reasonably well as I dragged my finger around for shorter/slower movements, but thrashing my finger left to right caused the frame rate to drop noticeably, and it seems to make sense as to why. The slower and less frequent the user moves the paddle, the fewer events get fired, and the faster and more frequent the user moves the paddle, the more events get fired - and as the app gets slammed with events it can directly affect performance.
Now, keep in mind that I was testing my game on a early beta of the phone, obviously performance I saw last weekend may be totally different from performance I might see even on a build next week. I also recently found out there was a bug in the Silverlight runtime that was causing landscape apps to run at half the framerate of portrait apps - and I don't know if the phone I played with had this issue fixed or not. But, also keep in mind that the basic guidance is that Silverlight is for apps (and some games), and XNA is for games (and some apps). The best way I have seen this phrased so far was like so:
XNA - platform for GAMES (and apps)
Now, before I tick anyone off for trying to push people away from Silverlight for games - let me be very clear - Silverlight on Windows Phone 7 is a great platform for building games, and I will be writing several Silverlight games (hint: one of my first Silverlight projects was a game) for WP7. That said, you have to decide between XNA and Silverlight for the games and the apps you write (perhaps before you get a device), and you should try to factor in the differences between an event driven architecture like Silverlight, and a Update/Draw/Present architecture like XNA. Michael Klucher summarizes it well:
- Silverlight is a modern event driven application and UI framework, this is very much what you see with most .NET technologies today.
- XNA uses an Update/Draw/Present frame loop that’s designed around game development and high performance graphical applications.
Now that I've had a chance to evaluate, I can get back to work on my first WP7 game - this one in XNA. I can't wait until I can get my hands on a real device long-term so that I can make these sorts of evaluations a lot quicker. I think trying to create a game like Arkanoid (or my game in progress) in Silverlight will be a great way to help figure out where the line between XNA and Silverlight for games lies.
Comments
I am a Microsoft MVP and a
I am a Microsoft MVP and a hard core Silverlight programmer, but even I have no problem admitting that XNA is going to be superior at game performance (and also graphics?). Now, for your average "business like" app, XNA is going to be serious overkill, but hey I'm sure someone will make a killer "Business App" in XNA so I'm not about to say it wont work...
That fact that you can use XNA or Silverlight, wow...
I'm curious about the reason
I'm curious about the reason for the framerate drop you observed. You mentioned you thought it was because of overhead of a large number of events being fired. Were you able to observe the framerate drop while not subscribed to any events? Could you observe it while subscribed to the events, but not doing anything in the event handlers? Could you observe it in both debug and release builds of your game?
I don't doubt that XNA is a superior choice when it comes to performance, but having the framerate drop just because of too many events being fired seems pretty bad.
Tom, I really wish I could be
Tom,
I really wish I could be more accurate in my description, but I got all of 2 minutes to try it on a device. As best I could tell the clouds/sun/etc. animated fine and the framerate was steady until i started doing my touch manipulations.. if it is of any use, this is what my ManipulationDelta event consisted of:
private void BoatDragArea_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
double translationY = e.CumulativeManipulation.Translation.Y;
if ((App.Current.RootVisual as PhoneApplicationFrame).Orientation == PageOrientation.LandscapeRight)
translationY = translationY * -1.0D;
_lastFingerLocation = boatStartTranslateX + translationY;
}
as you can see I'm basically taking the cumulative Y manipulation, inverting it if im in landscape right, and then updating the _lastFingerLocation - which is then used in the Composition.Rendering pass to update the boat's location. in fast movement/thrashing the framerate would drop from 30fps to 22-24fps..
if i get more time with a device I will do a head-to-head with an XNA version - so far I only tried the Silverlight version and assumed/inferred that the slowdown was relative to Silverlight and not touch in general.
Do you know if current XNA
Do you know if current XNA engines will work on Wp7?
Or do you do the engine yourself?
there is XNA game studio 4.0
there is XNA game studio 4.0 that is specifically for WP7 - you can find it on creators.xna.com
I have the same problem on
I have the same problem on emulator
It is enough to create Windows Phone application with an empty CompositionTarget_Rendering event handler. Moving mouse with the pressed left button causes UI frame rate counter to drop from 30 to 12 (There is not any mouse event handler)
Animated fine and the
Animated fine and the framerate was steady until i started doing my touch manipulations..!!