Click here to Skip to main content
15,885,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am creating a game engine for WPF. I am trying to move away from DispatcherTimer to StopWatch, to get a more constant speed of the game (as DispatcherTimer skips turns if there are too many objects in the game).

I can write the code for updating the game correctly using a StopWatch (that is, my game objects' speeds and positions are calculated correctly), but when I omit the DispatcherTimer the GUI will never redraw.

I know there are several ways to force a redraw of the window, but what would be my best option for a 2D game in WPF? I'm just looking for something simple that works rather efficiently without a lot of hassle.

This is my current code for placing objects onscreen, many times per second:

for (int i = 0; i < Bodies.NumberOfBodies(); 
{
    Body body = Bodies.Bodylist[i];
    var shape = body.GetShape();
    game.Children.Add(shape); //each movable body's shape is added again. 

    Canvas.SetLeft(shape, body.PosX - offsetX);
    Canvas.SetTop(shape, body.PosY - offsetY);
}


Thanks!

Petter

What I have tried:

I have read up on the subject, but there are many solutions and I don't know what would be the most suitable.
Posted
Updated 20-Oct-17 3:29am

1 solution

I found an answer working with a colleague of mine. Check here for answer:

Adding an eventhandler (compositiontarget.rendering) in WPF[^]
 
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