Wednesday, November 21, 2012 10:20 PM
Hi, I am trying to write a 2D game in VB/XAML, which is proving a challenge as the documentation is extremely limited and there is very little discussion yet available on the web...
However through trial and error I have got something up and running quite well. Although I have no idea how well it will run on ARM...
I have observed a phenomenon and I would like clarification on what I am seeing. If memory use (private bytes in ProcExp) gets above roughly 100MB, I start getting regular "glitches" in the frame rate. These are in time with Gen 2 garbage collections (as seen in .Net performance tab of ProcExp). If memory use is lower, I get more gen 2 collections, but no glitches, the game continues to run smoothly.
My theory is that at a certain memory size, the garbage collector switches from running asynchronously to running synchronously? Is that correct, or am I seeing something else?
If anyone is trying to write an arcade game in managed XAML, here are some tips I learnt the hard way:
- Don't use a timer (dispatch or threadpool) for your game loop, add a handler to CompositionTargeting.Rendering (goes off 60 times/second). EDIT just found bug in that you can't Remove handler from Rendering event - use with due caution.
- Don't try and be clever and use a View Model raising PropertyChanged events to update the page. This is very CPU intensive for some reason that I can't get to the bottom of.
- Directly alter properties of RenderTransforms/Opacities of UIElements in code, which does not use much CPU (on x86 at least).
- You can (sort of) use the MVVM pattern by creating UserControls for your "sprites", with properties that write to RenderTransforms/Opacities. Bind collections of these UserControls to an ItemsControl and the object itself will be used for the items, rather than binding to the ItemsTemplate.
I hope that helps someone else. I have now done a LOT of trial and error trying to find the best way to do this, from a performance and practicality POV.