|
Game Looping with enhanced performance VB.Net (Silverlight) |
|
|
Written by: Justin Rich
Game Looping with enhanced performance VB.Net Early in the days of Silverlight, the people informing you how to do game loops were using the DispatcherTimer and the .Tick event to call the main loop of your Silverlight application. I have a previous article that showed how to convert all the available C# versions of this to VB.Net. That DispatcherTimer class is part of the System.Threading library and it does work, but a better way to do constant looping (primarily for gaming) is to use CompositionTarget.Rendering
Public Sub New() InitializeComponent()
AddHandler CompositionTarget.Rendering, AddressOf MainGameLoop
End Sub
Private Sub MainGameLoop(ByVal sender As Object, ByVal e As EventArgs)
End Sub
When you place this in your XAML page's New Sub, you are pointing to a Sub (MainGameLoop) that you want to call every time you render your composition from the last loop. The nice thing about doing this is that if you want to do a separate looping system to handle different events, you can still use the DispatcherTimer. I noticed that since Silverlight wasn't designed specifically for game programming that it can get slow in your main game loop if you've got it doing too much. You should really just have it do your rendering. Put your hard math asyncronously in your DispatcherTimer.Tick event updating global variables and let your CompositionTarget.Rendering do the new rendering those globals.
If you use two CompositionTarget.Rendering calls to two different functions (rendering & logic), you will improve your performance as well, but currently, I'm seeing a slight better performance out of using that for the main, and the Tick for the math.
Making the Rendering call twice and pointing it to the same main game loop will make your application look like it's running double FPS, but really, it's just running double fast doing 2 game loops per render. Ideal if you coded the game to be slow and wanted to speed it up without changing all your math ;) |
|
|
|
|
|