In 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.