Project Description
Run XNA games without code changes in Silverlight 2 or 3. SpriteBatch based graphics only, no 3D. Since the game compiles into straight Silverlight code, it will run anywhere that Silverlight can.

SilverSprite is brought to you by Silver Arcade http://www.silverarcade.com, an ad based game site for Silverlight games. Please consider hosting your game with Silver Arcade once you get it working. If your game is also on Xbox Live Community Games, we will feature that prominently and provide a link to the Xbox Live Marketplace page for your game.

Watch the Channel 9 video on SilverSprite here: http://channel9.msdn.com/shows/Continuum/SilverSprite/

See SilverSprite in action:
New for Silverlight 3
The current code on the Source Code tab is for Silverlight 3 and takes advantage of some new Silverlight 3 features. Please download this code and build it if you are working in Silverlight 3. The current release supports Silverlight 2 so use this one if you want to use Silverlight 2. Going forward I will not be supporting 2 versions since the changes are so significant between the two versions.

Tinting when drawing (using a non-white draw color) is now done with a pixel shader so there is no longer the overhead of allocating a new texture per color. Also PNG and JPG content can now be tinted. It is recommended that you use PNG and JPG when possible since the XAP will be smaller than with the corresponding XNB file.

Bitmap font text drawing is now done using WriteableBitmap and an image is made for the entire text instead of having a separate element for each texture. This should speed it up significantly.

GPU acceleration can be enabled. Either set BitmapCacheEnabled and if your game canvas is scaled you will also have to specify RenderAtScale. To turn or hardware acceleration for the entire game, use SpriteBatch.SetGlobalBitmapCache(). This will probably change before the final release of the Silverlight 3 version. The Farser samples have been updated to take advantage of hardware acceleration.

Getting your game going
1. Add all of your source files to your Silverlight application. You can add these "as link" if you want to share them with both XNA and Silverlight.

2. Replace Microsoft.Xna.Framework with SilverArcade.SilverSprite everywhere in your code. You can wrap these in a #if SILVERLIGHT if you want it to compile for both XNA and SilverSprite.

3. Add your Game class (by default this is Game1) to your XAML or in code. Here is an example from the snake game sample (remember the xmlns definition) :

<UserControl x:Class="SnakeGameSilverSprite.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300" xmlns:game="clr-namespace:SnakeGame">
    <Grid x:Name="LayoutRoot" Background="White">
        <Canvas>
            <game:Game1 x:Name="game"/>
        </Canvas>
    </Grid>
</UserControl>


4. Add content to your silverlight app:

Eventually I'd like to read the xnb files for everything if possible. For now, adding content is a bit messy.

Textures -
  • Option 1: Add the .xnb file built by the Windows XNA Debug project, only uncompressed xnb files right now. The texture itself can be compressed, in XNA this is a property you can set, and it can make the texture file 4 to 8 times smaller. Make sure the build action is set to "Content".
  • Option 2: Add the PNG or JPG file using a file name that matches the asset name, making sure the build action is "Content". This can be more size efficient, faster, and smaller in memory, but you cannot "tint" the drawing by specifying a drawing color. The Alpha value of the drawing color will still be honored.

Sound - Add the audio file (not the xnb file) named the same as the asset name to the project, make sure the build action is set to "Content". Only mp3 and wma right now.

SpriteFont - There are two options for SpriteFont.
  • Option 1: Add the spritefont file to the project, setting build action to "Content". This will use Silverlight's built in text support. If you are using a font that is not one of the built in Silverlight fonts, add the ttf file to the project as a resource. You'll also need an extra line of code after creating your game class to map the XNA font name to the Silverlight FontFamily. It will look like this (taken from the snake game sample):

            game.AddFont("Becker Black NF", "./Becker Black NF.ttf#Becker Black NF");


-OR-
  • Option 2: Add the xnb file for the spritefont to the project, setting build action to "Content". This is required for bitmap fonts. Bitmap fonts won't perform quite as well, and you need to be careful if drawing a font with multiple random colors, because a bitmap is generated for each color.

Some things just don't work at all, like XACT sound and networking. If you use a lot of different draw colors when drawing textures, a bitmap is created for each unique color (sorry it's the nature of the beast right now in Silverlight) and they're never cleaned up currently, so you can run into some major memory growth if you're not careful.

A good amount of the code, especially things like Vector2 and math classes come from the Mono.Xna http://code.google.com/p/monoxna/ library.
Last edited Jul 25 at 8:39 PM by billreiss, version 31

 

Want to leave feedback?
Please use Discussions or Reviews instead.

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2009.10.27.15987