1
\$\begingroup\$

Could i get some suggestions on how to implement a game loop whilst using WPF(MVVM) and C#, I am a professional developer and understand the technology well.

The game loop usually contains a Init(), Render() and Update().

I know how to implement the Init() and Update(), but i would like ideas of how the render() fits in, using data binding the Render will happen on update(). Should forget the Render method or suppress the bindings and force them with a render?

Hope that makes sense :)

\$\endgroup\$
1
  • \$\begingroup\$ For me it's unclear what you are asking. Do I need to know WPF in order to answer your question? You should also check the grammar on the sentence "Should forget the Render method or suppress the bindings and force them with a render?" too. It's missing a subject. \$\endgroup\$ Commented Feb 1, 2016 at 16:41

2 Answers 2

2
\$\begingroup\$

With WPF, the UI ( or render I guess ) thread is kind of special. It'll try and run every 60th of a second. All of your View->ViewModel bound property read/writes are sort of banked up and updated on the UI thread. So you can alter a property a ton of times on a background thread but it will be updated on the UI thread. Command bindings ( like a button click bound to a command ) will happen on the UI thread so dealing with those on the same thread will block the UI update. Starting views and closing views can only be done on the UI thread.

I've not done much in the way of interactive 'gamey' stuff using WPF but I have done 3d scenes with moving camera's. The way I did this was to have an update thread which updated properties which were bound to the UI objects ( like position, orientation ). There was no UI thread specific things to do really. Apart from allocating the geometry and hooking it into the visual tree.

So WPF isn't really set up to work in a way you may be used too. But you could probably get something close by using a BackgroundWorker.

The BackgroundWorker class has a RunWorkerComplete method https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx. This allows you to start a background worker task and then update any UI specific stuff when its complete. And so I guess with some clever timing this would effectively give you an update & render hook which you may find to be more game loop familiar.

\$\endgroup\$
1
\$\begingroup\$

I guess the CompositionTarget.Rendering event fits your bill. You can attach a method to the Rendering event like this:

CompositionTarget.Rendering += Loop;

The Loop function can then update the properties of an element, in this example an element positioned on a Canvas:

private void Loop(object sender, EventArgs e)
{
    LeftPos++;
    Canvas.SetLeft(ball, LeftPos);
}

Mircosoft API Documentation: compositiontarget.rendering

(This stackoverflow answer pointed me in the right direction.)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .