tags:

views:

951

answers:

3

I am transitioning from WinForms/XNA to WPF/SlimDX because:

  • a) all of the benefits of WPF over WinForms (but learning curve = ouch!)
  • b) I would like to have multiple SlimDX viewports attached to Panels. XNA has 1 "game" screen.
  • c, last and least) DirectX 10 support

All of my previous XNA code is in C#. I am having trouble figuring out how to port this over to SlimDX and WPF on a high level. I have searched like crazy. The closest I have found are:

1) http://www.gamedev.net/community/forums/topic.asp?topic_id=507941 Many articles point to this discussion, however it is incomplete and I can't figure out the XAML, and I get device errors after attaching all of the _slimDXDevice and Window1 events that were left out.

2) http://www.codeproject.com/KB/WPF/D3DImage.aspx This article assumes the user is porting C++. I am porting XNA code which is very close to MDX code.

If I could get to the point where I have a WPF form with a custom SlimDX driven viewport that was just a blue box, I could go from there. In XNA I rendered many separate RenderTargets and placed them all over the screen, now I want to attach them to controls. But first, just 1 blue box! :D

Any ideas? I feel that this is either simple or that there's some "cookie cutter" code that I'm missing. Greatly appreciated!

A: 

D3DImage is the class you want to use. Even though the codeproject tutorial is C++, it is very applicable to SlimDX and WPF.

All you have to do with your SlimDX, is run your code normally, but DO NOT run a Present(...) on your device or swap chain. At the point where you would put a Present(...), do a D3DImage.SetBackBuffer(...) and send your SlimDX surface's ComPointer property to it. Then do D3DImage.AddDirect(...) and you now have D3D composited in WPF.

Also, make sure you create a IDirect3DDevice9Ex or else your performance will be terrible in anything but XP!

Jeremiah Morrill
A: 

I recently was messing around with D3DImage and SlimDX and didn't find it too difficult to get it working (with DirextX9). I have some code at my home pc that I'll post later, but it's pretty similar to the code in the links provided.

I was never able to get it working with a higher version of directx though. Jeremiah has a nice blog post about using a directx9 device as a link between directx 10/11/d2d and the D3DImage, but I couldn't get it working with Slimdx. I didn't put a whole lot of effort into though as directx9 did what I needed it to do and I kind of wanted it to work on XP.

mdm20
+3  A: 

You can look at the sample now. It's just been checked in to our repository, so you'll need to use SVN to get it (or wait until we ship the Feb 2010 release):

http://code.google.com/p/slimdx/source/detail?r=1356

Josh Petrie
This is cool, thanks.
mdm20
Thank you Josh, this is a great example. Nicely done with ViewModels as well. You guys are good. I hope others who are searching for WPF/SlimDX end up here. Thanks again!
bufferz
Promit actually wrote the sample, not me, but I'll pass that along.
Josh Petrie
There's a newer revision with some updates:http://code.google.com/p/slimdx/source/detail?r=1367
Promit
Thanks, Promit. Looks like I saw Josh's answer late enough to end up with r1367 anyways. I think you guys are breaking cool ground with this example. There's not a whole lot on the net about alternative solutions.I must throw my $0.02USD out there to help out those (such as me) who may not be very familiar with SlimDX, WPF, and good MVVM design practices. These 2 small additions to the WPF example would get newbies going faster:
bufferz
1) Set up a simple UI control that interacts with the SlimDX control. Right now there is left/right click to change colors of the lizard. Something like 3 Sliders to control the RGB color of the lizard could demonstrate this. In WpfWindow.xaml.cs I added:private void sliderRed_ValueChanged(...) { m_dataModel.Color.R= (int)sliderRed.Value; }
bufferz
2) Have the SlimDXControl, or its model, Bind to an arbitrary property somewhere else in the example in XAML.
bufferz
While these additions are very trivial, they would help illustrate, again to a novice, how such features should be implemented in the context of the Window>SlimDXControl>DataModel hierarchy set up by the original authors. Thanks again!
bufferz