Introduction to game programming in Silverlight

Even with an initial release less than two years ago, a thriving community of game developers write games for the Silverlight platform. Still, the Flash platform probably employs the largest game developer community, so in order to help eager Silverlight game developers enter the fray, I will convert Kongregate’s Flash game programming tutorials to Silverlight. Like the rest of my Silverlight tutorials, these tutorials will not require you to install Visual Studio. And, while informative, the Kongregate tutorials (necessarily) don’t address the more advanced concepts in game programming ( i.e. multi-threaded game loops, 3D rendering, etc). I will attempt to address these topics and more in future Silverlight game programming tutorials.

The first Kongregate tutorial hardly shows any code, choosing instead to walk the developer through setting up their development environment. I’ve covered these basics in previous Silverlight tutorials, so in this tutorial I will discuss some the capabilities and limitations of Silverlight as a gaming platform. That said, before continuing on to the next tutorial, you will want to at least skim the tutorials below:

Game Programming in Silverlight

So, with the advent of numerous native OS, multi-platform game engines and game programming references on the web, why programming games in Silverlight? Below, I list some of the reasons you might find Silverlight a compelling game platform:

Of course, these same reasons hold true for writing games for the Flash platform. I have written games for Flash before, and will write more, however, as of now, I prefer Silverlight for a couple of personal reasons:

With that out of the way, let’s dig deeper into the functionality and limitations of Silverlight as a game platform in these areas:

The game loop

Easily the most important part of your game (and sometimes the hardest part to get right), the game loop literally acts as the brain of the game, coordinating the flow of events between each part of the game engine. In its simplest, pseudo-code form, a game loop might look something like this:


while( gameRunning )
{
  updateGame();
  renderGame();
}

In this example, updateGame function takes care of moving game objects on the screen, computing AI, etc. while the renderGame function actually draws the game on the screen. While this loop works conceptually, it will not work in practice because you do not want to monopolize the UI thread in Silverlight with an infinite loop and the execution of the game would wildly fluctuate depending on speed of the computer it runs on. So, typically Silverlight game developers use one of these methods:

So, all this begs the question: which one do you use? Well, it depends on your requirements. Most developers will probably use a StoryBoard since it provides a low friction method of getting frame rate independent game updates. However, some developers may want to at least prototype their games with the CompositionTarget.Rendering event since it provides the quickest way to get up and running.

Graphics

Silverlight supports 2-D graphics very well. Objects like Points, Rectangles, Images (JPG and PNG) come readily defined. Functionality like hit testing, 2-D primitive drawing and transforms already exist, ready to be used. Silverlight can even go full screen, so you games don’t have to execute within the confines of a little canvas on a web page.

All that sounds great, but now for the bad news: unfortunately, Silverlight does not support accelerated 3-D rendering. But, as you may have guessed, some enterprising developers with more spare time than me, have already developed 3-D libraries for Silverlight. I’ve listed two of them below:

Audio & Video

The System.Windows.Controls.MediaElement class provides support for playing audio and video. In terms of audio, of course Silverlight supports all of Microsoft’s own audio formats as well as the MP3 format. However, you do not have direct access to the sound device, and thus, can not mix dynamic sounds on the fly. Still, preloading MP3s and playing them when needed should work for most games.

Video has a similar story with all of Microsoft’s own video formats supported out of the box, but lacking the ability to apply any real video filters to them. For completeness, I should also mention that Silverlight provides excellent support for streaming both audio and video.

Input

For input, Silverlight will send you both key press events and mouse move and click events. Silverlight also allows you to “capture” the mouse or keyboard, giving one element singular focus to all events. I demonstrate this in my Silverlight mouse events tutorial.

Storage

Silverlight provides developers with something called Isolated Storage. Basically, Silverlight allows an application to request from the user a certain quota of space, and at the consent of the user, that application and only that application can use that requested space. This gives game developers a place to store save games and possibly make certain game assets available off-line. I have a links to working with Isolated Storage on my Silverlight Portal page

Networking

With a focus on enabling rich Internet applications, of course, Silverlight allows developers to leverage Windows Communication Foundation to call Web Services and REST APIs. However, thankfully, with a few caveats, Microsoft also allows Silverlight applications to create raw socket connections through the System.Net.Sockets namespace. This opens up a whole new realm of possibilities for playing multi-player games in the browser.

Conclusion

While not being the perfect gaming platform, Silverlight does give developers plenty of functionality to create compelling browser-based games. Over the course of the next few months, I will write several Silverlight game programming tutorials, starting with converting Kongregate’s Flash tutorials, that will hopefully give you a solid base for developing games in Silverlight.

Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • StumbleUpon
  • Reddit
  • del.icio.us
Related Posts:
10 Minute Silverlight Game Programming Tutorial - Shootorial Conversion #4 (C#)
10 Minute Silverlight Game Programming Tutorial - Shootorial Conversion #2 (C#)
10 Minute Game Programming Tutorial: Silverlight - Shootorial #1 Conversion (C#)
Channel 9 video review: Tyler Ballance - live from the SilverlightDevCamp in San Francisco

Comments

Comments are closed.