In a WP7 game I’m building my Enemy classes are wrapped in an EnemyControl.
The Enemy Control uses a spritesheet of all the different animation states for that enemy type. There are only 4 images which I statically cache the bitmaps for.
The problem I have is that despite both the image being cached (BitmapImage statically and CacheMode="BitmapCache"), when the Image is loaded by the control it adds approximately 2 meg of Texture/System memory per control.
This is a significant problem as it limits the number of enemies I can conceivably have on screen.
Does anyone have any ideas how I could improve this situation? or otherwise solve it?
Here’s the xaml if interested:
<Canvas x:Name="canEnemyControl">
<Canvas.RenderTransform>
<TranslateTransform x:Name="enemyControlTransform" X="0"/>
</Canvas.RenderTransform>
<Canvas.Clip>
<RectangleGeometry x:Name="clipGeometry" Rect="0,0,60,60" />
</Canvas.Clip>
<Image x:Name="imgEnemy" Stretch="None" CacheMode="BitmapCache" >
<Image.RenderTransform>
<TranslateTransform x:Name="enemyImageTransform" X="0" Y="0" />
</Image.RenderTransform>
</Image>
</Canvas>
I don’t think your implementation will scale because I don’t think the multiple
Imageelements know to share the same bitmap cache.Solution: CompositionTarget.Rendering and WriteableBitmapEx
One possible solution would be to use CompositionTarget.Rendering for rendering like in my answer here. This fires an event before the screen is updated so you can do your own drawing. I believe this is how Krashlander and some other Silverlight games work (though I think he may have switched over to XNA in later versions).
Then I think that you can use WriteableBitmapEx to use one instance of your source image to draw it into multiple places on a buffer. You should scale much better because you have your buffer the size of your screen/viewport, and you have your one copy of each image. The memory requirements for displaying 1 enemy and 1000 should be the same.
I will add a disclaimer that I haven’t tried this exact approach, but I do use
CompositionTarget.Renderingin one of my apps for smooth screen updates.