<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The Right Moves : performance</title><link>http://blogs.msdn.com/ben_anderson/archive/tags/performance/default.aspx</link><description>Tags: performance</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Performance Tips (Part 2)</title><link>http://blogs.msdn.com/ben_anderson/archive/2008/10/14/performance-tips-part-2.aspx</link><pubDate>Wed, 15 Oct 2008 01:25:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9000126</guid><dc:creator>Ben Anderson</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ben_anderson/comments/9000126.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben_anderson/commentrss.aspx?PostID=9000126</wfw:commentRss><description>&lt;p&gt;Yesterday we began our discussion of performance optimizations in &lt;a href="http://www.popfly.com" target="_blank"&gt;&lt;a href="http://www.popfly.com/gamecreator" target="_blank"&gt;Popfly Game Creator&lt;/a&gt;.&lt;/a&gt;&amp;#160; In &lt;a href="http://blogs.msdn.com/ben_anderson/archive/2008/10/13/performance-tips-part-1.aspx"&gt;that post&lt;/a&gt; we discussed how collision detection impacts performance as you add actors and ways to mitigate that impact.&amp;#160; Today I’d like to talk a bit about the other bottleneck people hit and that is &lt;a href="http://silverlight.net"&gt;Silverlight&lt;/a&gt; drawing perf. along with some best practices in creating and using your actors to keep your games snappy.&amp;#160; Today’s article contains discussion and techniques that are fairly advanced, so if you feel a little over your head, definitely try the tips outlined in &lt;a href="http://blogs.msdn.com/ben_anderson/archive/2008/10/13/performance-tips-part-1.aspx"&gt;Part 1&lt;/a&gt; first.&lt;/p&gt;  &lt;p&gt;Silverlight has great support for a number of drawing mechanisms.&amp;#160; It supports static images in the form of jpegs and pngs, as well as XAML based vector art which can be generated either by hand, with a tool like Expression Blend or exported from art packages like Expression Design or Adobe Illustrator.&amp;#160; Vector art has the advantage that it can be smoothly scaled (as well as rotated, skewed etc) as large as you want, and since it’s all computed geometry it will look as sharp as it did in it’s natural size.&amp;#160; Some people also like to say that it has a smaller file size than traditional image files which define their content pixel by pixel, but this isn’t necessarily true.&amp;#160; It’s more accurate to say that the file size remains more or less constant regardless of it’s final rendered size.&amp;#160; With traditional image files, the file size grows as the image dimensions in pixels grows, however it shrinks when the dimensions shrink.&amp;#160; There is plenty of vector art (XAML or otherwise) with file sizes at reasonable resolutions that far exceed the equivalent png or jpeg representation.&amp;#160; This is because XAML file sizes grow as the complexity of the image they represent grows.&amp;#160; Designers can easily create files that weigh in the megabyte range by adding more and more detail to their images.&amp;#160; They can also create relatively small files by sticking to a few simple geometric shapes in their designs.&amp;#160; &lt;/p&gt;  &lt;p&gt;So what does that mean for all you budding &lt;a href="http://www.popfly.com" target="_blank"&gt;Popfly&lt;/a&gt; game designers out there?&amp;#160; Actors in Popfly are defined as XAML.&amp;#160; First, realize that the complexity of a XAML file doesn’t just increase the file size, it also increases the amount of time it takes Silverlight to draw an image.&amp;#160; So if you’re using XAML to draw your actors, you can shorten the time it takes Silverlight to draw them (and thereby increase your framerate) by simplifying their XAML – the fewer and the simpler elements the better.&amp;#160; This also has the added benefit of reducing their download size for your users.&amp;#160; Sometimes though, a complex image can do the job much better than a simple one.&amp;#160; If that’s the case, realize that unless you are scaling your actors to fairly large sizes, you can simply use an Image element in the actor to incorporate a static .png file of the appropriate dimensions containing your actor’s appearance.&amp;#160; Because with a static image, Silverlight can simply copy the bits to the appropriate location on the screen (an operation often called bit-blitting), it doesn’t have to do any processing to draw the image.&amp;#160; This can dramatically increase the speed at which it draws as bit-blitting is an operation that is highly optimized and has been hardware accelerated by graphics cards for many years – since well before consumer 3D graphics accelerators even existed.&amp;#160; Most Microsoft provided actors are XAML based so that they scale smoothly, but if you’re not using this functionality, you can always screenshot them while your game is running (press Print Screen to copy the current screen to the clipboard then paste the results into Paint or &lt;a href="http://www.getpaint.net/"&gt;Paint.Net&lt;/a&gt;), fill in the background with transparency, then save the image as a png of the appropriate size and replace the actor’s appearance with the static image.&amp;#160; Particularly with a scrolling viewport, this can save a lot of cycles.&amp;#160; &lt;/p&gt;  &lt;p&gt;Finally, if your game uses a tiled surface to represent areas of the game such as fences or the ground, you don’t necessarily have to tile multiple actors to achieve a tile effect.&amp;#160; Doing so increases the number of actors active in the game which hurts performance as we discussed yesterday.&amp;#160; Instead, you can tile the image in your XAML.&amp;#160; This is especially easy with image elements since you can just copy the image element, change it’s Canvas.Left or Canvas.Top and tile the image to any length you want.&amp;#160; To see an example of this technique check out the game I’ve shared &lt;a href="http://www.popfly.com/users/andersbe/RPGPNG" target="_blank"&gt;here&lt;/a&gt;.&amp;#160; If you rip the game you will notice that I did not use a single fence actor but rather several – Tall Fence, Wide Fence and Medium Fence.&amp;#160; This is because I simply wanted the fence to stretch across the sides of the screen.&amp;#160; If I just stretched the actor the image skewed and if I tiled the actor things slowed down.&amp;#160; So instead I manually did the tiling.&amp;#160; Click on the Wide Fence’s appearance and select XAML to see the technique.&amp;#160; Tools like Expression Blend can help you do this if you’re having a hard time doing it in Popfly’s text editor:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/ben_anderson/WindowsLiveWriter/PerformanceTipsPart2_D8FD/splitview_2.png"&gt;&lt;img title="Editing XAML using Expression Blend" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="272" alt="Editing XAML using Expression Blend" src="http://blogs.msdn.com/blogfiles/ben_anderson/WindowsLiveWriter/PerformanceTipsPart2_D8FD/splitview_thumb.png" width="504" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Hopefully these articles will help out those of you who have been running into performance barriers when creating your games.&amp;#160; To discuss these tips and tricks, to share your own tips or to get additional help, checkout the Popfly Game Creator &lt;a href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=2125&amp;amp;SiteID=1" target="_blank"&gt;forums&lt;/a&gt;.&amp;#160; You can also check back at this blog for future tips and tricks.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9000126" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Popfly/default.aspx">Popfly</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Popfly+Game+Creator/default.aspx">Popfly Game Creator</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Expression+Blend/default.aspx">Expression Blend</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/How+To/default.aspx">How To</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/performance/default.aspx">performance</category></item><item><title>Performance Tips (Part 1)</title><link>http://blogs.msdn.com/ben_anderson/archive/2008/10/13/performance-tips-part-1.aspx</link><pubDate>Mon, 13 Oct 2008 18:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8996804</guid><dc:creator>Ben Anderson</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ben_anderson/comments/8996804.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ben_anderson/commentrss.aspx?PostID=8996804</wfw:commentRss><description>&lt;P&gt;While we’re constantly working to improve the performance of the &lt;A href="http://www.popfly.com/" target=_blank mce_href="http://www.popfly.com"&gt;&lt;A href="http://www.popfly.com/GameCreator/" target=_blank mce_href="http://www.popfly.com/GameCreator/"&gt;Popfly Game Creator&lt;/A&gt;,&lt;/A&gt; there are a number of tweaks you can make to your games right now to improve your users’ frame rate.&lt;/P&gt;
&lt;P&gt;Right now, there are two main bottlenecks for games created using &lt;A href="http://www.popfly.com/" target=_blank mce_href="http://www.popfly.com"&gt;Popfly&lt;/A&gt; and depending on the characteristics of your game, one or the other can probably help you.&lt;/P&gt;
&lt;P&gt;Today we’ll talk about the biggest perf. hit for most games, which is collision detection between a large number of actors.&amp;nbsp; There are a number of ways to address this.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The easiest trick is to make as many actors as possible non-solid:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/ben_anderson/WindowsLiveWriter/PerformanceTipsPart1_B579/non-solid_2.png" mce_href="http://blogs.msdn.com/blogfiles/ben_anderson/WindowsLiveWriter/PerformanceTipsPart1_B579/non-solid_2.png"&gt;&lt;IMG title=non-solid style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; DISPLAY: inline; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=68 alt=non-solid src="http://blogs.msdn.com/blogfiles/ben_anderson/WindowsLiveWriter/PerformanceTipsPart1_B579/non-solid_thumb.png" width=244 border=0 mce_src="http://blogs.msdn.com/blogfiles/ben_anderson/WindowsLiveWriter/PerformanceTipsPart1_B579/non-solid_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;This is a per state property, so if your actor has multiple states, make sure you set it for each one.&amp;nbsp; When an actor is non-solid, it’s collision bounds will be drawn in blue instead of red.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;When an actor is non-solid, it will no longer push or get pushed when it collides with other actors.&amp;nbsp; This allows us to skip testing it for collisions except when there is a collision listener whose event includes the actor.&amp;nbsp; Even then we only have to test it against other actors that are included as the other actor on the same event.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The next approach is to limit the number of actors you use on any scene.&amp;nbsp; While this can lead to a less compelling game, but if your game is bottlenecked on collision checking, it is guaranteed to increase performance.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;If neither of the above is desirable, you can also increase performance by limiting the motion of your actors.&amp;nbsp; When an actor has not moved, it often allows us to skip testing it in parts of our collision detection.&amp;nbsp; This also improves &lt;A href="http://silverlight.net/" target=_blank mce_href="http://silverlight.net"&gt;Silverlight&lt;/A&gt; drawing performance (which we’ll talk more about tomorrow) since we will not have to update the Silverlight plugin and Silverlight will not have to re-render the actor in its new position.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Finally, if you are spawning new actors throughout the course of your game (through shoot behaviors, Appear behaviors, effects etc) there are a couple of things you can do to keep things snappy.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;First, make sure you limit the lifetime of the spawned actors.&amp;nbsp; Leaving the “disappear on scene leave” behaviors intact helps with this since once the actor reaches the edge of the screen, the actor will be removed from the game keeping the total number of actors low.&amp;nbsp; Second, limit the rate at which new actors are added.&amp;nbsp; For example, if you allow the player to shoot, try limiting the rate of fire by setting a property to 1 on your actor when the player presses the fire button, then clear the property to 0 on a timer.&amp;nbsp; Then simply add a filter to the fire behavior to only allow the player to fire when the property is 0.&amp;nbsp; Alternately, you can limit the # of bullets the player can fire at once by filtering on the # of BulletName property (you may have to copy the value of this property locally to your actor from the scene using an “every frame” timer).&amp;nbsp; Lastly, you can throttle down the rate at which your enemies fire when there are a lot of enemies on the screen through custom code or using a filter on # of EnemyBulletName.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Hopefully these tips will help those of you with games whose performance is bound by the total number of actors.&amp;nbsp; Because naive collision detection’s performance is an O(N^2 ) problem (where N is the number of actors, adding one more actor will cause another N + 1 collision checks to take place), adding actors can quickly cause problems.&amp;nbsp; Using the tips above can really improve performance.&amp;nbsp; If you’re having trouble after following these tips or they aren’t helping, be sure to head to the &lt;A href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=2125&amp;amp;SiteID=1" target=_blank mce_href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=2125&amp;amp;SiteID=1"&gt;forums&lt;/A&gt; where other users and members of the Popfly team can help.&lt;/P&gt;
&lt;P&gt;Tomorrow I’ll follow up with Part 2 where I will provide some tips for creating actors that are easy for Silverlight to draw and also help keep Popfly’s game engine running fast.&amp;nbsp; Stay tuned.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8996804" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Popfly/default.aspx">Popfly</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Popfly+Game+Creator/default.aspx">Popfly Game Creator</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/PGC/default.aspx">PGC</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/How+To/default.aspx">How To</category><category domain="http://blogs.msdn.com/ben_anderson/archive/tags/performance/default.aspx">performance</category></item></channel></rss>