Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, Sort of a newbie question, but here goes...

I am trying to use CompositionTarget.Rendering in my WPF 2D game to update the window. However I suck at event handlers etc so nothing happens when I try this and need some help.

I have my MainWindow and a canvas in there where the action is. The game starts when I click a Play button. The Button's click event (in MainWindow.xaml.cs) calls GameController.cs which takes care of the game logic (like the game loop).

A third class, BoardSetup's, DrawGame method is then called from the GameController's GameLoop method and redraws the objects on the canvas. (This works with a DispatcherTimer but not otherwise.) I was advised to use CompositionTarget.Rendering (c# - How do I refresh the UI smartest in a 2D WPF game? - Stack Overflow[^]) but don't know how to add it.

So how and where (what file and method (if any) should I add the CompositionTarget.Rendering event handler to make my DrawGame method redraw the game?

Actually I don't care of exactly how often the game renders as long as it's at least say 12 times per second. What's important to me is that the game runs at the same speed regardless of how many objects are in the game (and the gameloop does that today, it's just that the result is never updated onscreen). Therefore I'm throwing out my DispatcherTimer to use CompositionTarget.Rendering instead.

Please help!

What I have tried:

I've tried to use CompositionTarget.Rendering in MainWindow.XAML.cs and in the game loop but the UI is still not redrawn. I have also simply tried myCanvas.UpdateLayout() but it doesn't draw anything onscreen (because of the running gameloop I guess).
Posted
Comments
johannesnestler 19-Oct-17 8:17am    
So you posted several questions to this topic... From what you ask, I'd say you have to move away from your "timed" aproach. If the game-state changes the visual objects should do the same. So I would create a control/view to represent the game-state visually (with bindings) - so no need for an "update-loop". The Loop is only in the logic of the game.
BUT: What I'd question, is the use of WPF for this kind of "game" - why not use something better suited for games (and with much better perfomance) - like "Unity".
For some things maybe WPF-animations (they perform well and use fixed time-Intervalls) could be a good fit - but that depends on the nature of your game...
petter2012 19-Oct-17 12:26pm    
Hi, Thanks Johannes!
Actually, calling my gameloop from the CompositionTarget.Rendering event does the trick. I have now successfully tried with 30 dynamic objects as well as 300 dynamic objects. The objects are not moving smoothly if I have 300 moving objects, but the time is accurate: I have run the game for four minutes (exactly, and the time is correct to the second (at least - I'm confirming with my cellphone's timer :) ).

I will write an article about y game engine within a few weeks hopefully. If anyone needs my code now, then just PM me or so.

1 solution

Actually, calling my gameloop from the CompositionTarget.Rendering event does the trick. I have now successfully tried with 30 dynamic objects as well as 300 dynamic objects. The objects are not moving smoothly if I have 300 moving objects, but the time is accurate: I have run the game for four minutes (exactly, and the time is correct to the second (at least - I'm confirming with my cellphone's timer :) ).

I will write an article about my game engine within a few weeks hopefully. If anyone needs my code now, then just PM me or so.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900