Are you interested in
creating games for Windows Phone 7 but don't have XNA experience?
Watch the recording of the recent intro webinar delivered by Peter Kuhn '
XNA for Windows Phone 7'.
This article is compatible with Windows Phone "Mango".
Latest update: Aug 1, 2011.
In the last part of this series we've taken a look at one of the major new features in the upcoming Mango update for Windows Phone: integrating Silverlight and XNA in the same application, which is a thrilling opportunity for both normal application developers to enrich their applications with 2D/3D graphics content as well as game developers to simplify development of menus and other screens that require a rich user interface. In this part we'll take a look at the other new features Mango will bring that are particularly interesting to game developers. Remember that you need the beta version of the developer tools if you want to try any of these features yourself.
Fast Application Switching
With the update, the execution model of Windows Phone will be improved. In particular, a new "Dormant" state will be added to the application life cycle. The way this works is that when the user navigates away from your application, instead of tombstoning it right away, it is suspended in a way that preserves the whole application state intact in memory. The advantage of this is that you do not need to restore this state when the user returns to your application. Only if the user continues to work with the phone and the operating system eventually gets into the situation that it needs to free resources (memory) to guarantee a great user experience for a newly started or the currently running application, your application will be tombstoned.
To give the developer the possibility to distinguish between the dormant and tombstoned state, the Activated event of the PhoneApplicationService now provides an IsApplicationInstancePreserved property in its event arguments. If you want to benefit from the new fast application switching, you should not do any of your normal tombstoning restore procedures when this property is true. Everything is just as the user left it, even running timers and similar things are restored automatically. What you do need to do is check and restore external resources, for example when you had open network connections. The new code samples for the Mango update include an Execution Model Sample that demonstrates the feature in all details.
Debugging Tombstoning/the Dormant State
The Windows Phone emulator you use to develop your applications fully supports the new feature. In some cases this can be undesired, for example when you want to test your application's behavior with tombstoning and activation after it had been terminated. One nice feature of the new Windows Phone development tools is that you can influence this behavior in the project properties in Visual Studio:
When you check the above option, your application will be tombstoned when you debug in the emulator – nice!
Note: for XNA projects, the option is located on the "XNA Game Studio" tab in the project properties.
Support for Sockets
The topic of multiplayer gaming has been treated a bit as an orphan for hobby and indie game programmers in the first release of Windows Phone 7. The platform of course already has networking options, but they're based on web services and hardly allow more than implementing turn-based multiplayer games. The upcoming platform update adds support for sockets to the phone, which is much more suitable to implement a sophisticated, real-time multiplayer gaming experience. Both UDP and TCP clients are supported.
I have written a separate article on socket support in Mango that implements a small sample using a Windows console application as server. You can read this post on my blog; it also provides the full source for download. The code samples for Windows Phone also have a complete sample for this new feature: "Tic-Tac-Toe Over Sockets" is a complete Tic-Tac-Toe game that connects to a remote server an lets you play against a (not so brilliant :]) computer AI.
Sockets surely are a complex topic with a lot of subtle issues you can trip up on, and implementing your own solution on top of it can be troublesome. However, I expect that frameworks and helpers will emerge in the future and open the way for a broader audience to benefit from this feature easier.
Profiling
Games are probably the category of consumer software that has the longest tradition in performance optimization. Even today games on the desktop are optimized at a really low level, for example by implementing the most crucial routines in assembly. On the phone there is nothing like this, but still optimization is an important topic, especially given the limited resources available on the platform. A lot of developers not only make use of tools like profilers once they run into performance problems, but they also are interested in getting that information to optimize for power consumption or response times to user input. Luckily, with the upcoming Mango update, there will be a built-in option for that.
To start a new profiling session, you have to choose the new "Start Windows Phone Performance Analysis" option from the Debug menu in Visual Studio:
The next window enables changing some of the parameters of the session. I don't know if the "Memory" option is disabled because of the beta status or if it is simply not supported in the emulator.
You should take that warning seriously – it usually does not make any sense to profile the emulator as its performance has nothing in common with real devices. You cannot even relate your observations to real world numbers by "scaling the numbers" or similar; often the bottlenecks and limitations are completely different on the real hardware (here is an example of such an analysis I've done on that topic).
After finishing the profiling session, Visual Studio will present the collected data in the form of several graphs that show the frame rate, CPU and memory usage as well as other data like storyboards and garbage collection events. You can then select time spans and take a look at the details:
You can use the menu marked in the screenshot above to get comfortable access to detailed data about the individual frames, the storyboards and visual tree structure, the loaded modules and the executed functions in the selected time frame. The latter one also allows jumping directly to the related source code if it is part of your own application. A full discussion of how to interpret these results to optimize your application is way beyond the scope of this article. However, the MSDN documentation already has a few How-to articles about optimizing the responsiveness and animation smoothness of your application using the profiler. The general information about the profiling options with detailed explanations of the reports can be found here.
Sensor Input
One of the aspects of sensor input that will be improved is the tooling side. In the new emulator, you will be able to simulate accelerometer input with a nice visual interface, making it a lot easier to test this kind of input for your games in the emulator (without relying on third party solutions for this).
In addition to the improved tooling, the sensor APIs are extended by support for gyroscopes and the compass. If you are familiar with the topic then you know that all three of those sensors are needed to accurately determine the orientation of the device. To spare you from implementing the needed math to combine the low-level data from these sensors, the Mango update also introduces the Motion class. This class can be used similarly like the sensors (using an event-based approach and and the possibility to start/stop the reading of values) and provides MotionReading objects as result. The data included by that class contains the attitude (yaw, pitch, roll), acceleration and gravity information and the timestamp of the calculation. These values allow the implementation of interesting applications (for example in augmented reality scenarios) and games.
Note: Gyroscopes are not a hardware requirement for phone vendors. You have to take that into consideration and take proper action if a gyroscope is not available on the device your application is running on.
Summary
I hope part 12 and this article were able to shed some light on some of the interesting new features for game developers that the upcoming Windows Phone update will bring for you. I think it is obvious that this is not only a small update, but a tremendous step forward towards a more feature-complete platform and an even greater experience not only for the end-user, but also for us developers. Have fun building your dream game!