Edit: this post is now replaced by the more in-depth view here: http://blogs.msdn.com/nikola/archive/2009/08/19/exposed-5-methods-to-create-game-loop-which-is-the-best.aspx
Nice!
I just ported my Shock game to Silverlight 3.
Getting the graphics to work as before (and better), took me about an hour reading through docs. But the changes took about 5 minutes.
I'll just list them here for you in case you're building one of the next generation Silvelight games:
1. In your Silverlight Control's .HTML or .aspx file set EnableGPUAcceleration="true". This allows Silverilght to use the GPU for controls that you tell it to (see below).
Also set MaxFrameRate="1000". I’ll explain why we set the frame rate to such a high value in a bit.
Here’s how Shock’s Silverlight control looks after the change:
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/Shock.xap" MinimumVersion="3.0" Width="640px" Height="480px" EnableGPUAcceleration="true" MaxFrameRate="1000"/>
2. Find elements in your application that move, scale, rotate or blend (use transparency).
For each of these elements turn on hardware acceleration from XAML by setting CacheMode="BitmapCache" like this example:
<Image x:Name="imgBolt" CacheMode="BitmapCache"
VerticalAlignment="Bottom"
HorizontalAlignment="Left"
Opacity="0"
Source="Shock/Images/Explosions/bolt.png"
Height="304"
Stretch="Uniform"
Width="590"
Margin="0,0,0,64"
IsHitTestVisible="False">
<Image.Clip>
<RectangleGeometry x:Name="boltClipRect"
Rect="0,0,590,10000"></RectangleGeometry>
</Image.Clip>
</Image>
This really helps (in my case CPU down from 10% to < 3%)! J I enabled hardware acceleration for the balls and for the lightning bolt in Shock. Now the animations and redraws look smoother.
3. If your game loop uses a timer, storyboard, or thread, change it to use CompositionTarget.Rendering instead. The reason is that Silverlight 3 can render animations more smoothly than Silverlight 2.
Also, your timer will typically be called every 15 msec now (60 FPS). In Shock, the balls move through Storyboard-s. I used to hit-test the balls with bricks, paddle, and screen 60 times per second. Now I see that sometimes animations will be drawn faster than this, and in some cases I could notice balls being drawn on top of bricks, or the paddle. With faster animations and better runtime, the effect is easier to notice.
This is the reason why I wanted to recalculate the hit testing and update animations faster. The easiest way to do this in Silverlight today is by using CompositionTarget.Rendering and setting MaxFrameRate to a high number (1000 is pretty good, although 200 generates almost the same results already). I used MaxFrameRate=”1000” in conjuction with CompositionTarget.Rendering to achieve smoother animations in Shock.
That’s for today! Hope you liked it!
I’m trying to stay away from posting yet-another-“Silverlight 3 is released”-post and listing all the benefits to get a temporary boost in reads. Instead I hope my own findings will bring you unique value and enjoyment developing Silverlight applications!
If you’re still interested in what’s new in SL 3, this is the official list