Wednesday, February 19, 2014

Mixing Managed C# /C++ with Unmanaged DirectX Engine

So far, I had created an engine which could host a web server. Once you log into the local IP address through a web browser, it would show all the debug widgets on that web page. This was nice for me to debug. But it was a big pain to keep refreshing the web page when I reload the game engine. And also I had to keep switching between different windows in order to change the values in the debug widgets.

I decided that I needed both the debug controls and the actual engine to be in the same window. So I started researching on how to get this done. There were a few options:

1. Keeping it completely unmanaged: Basically, I would have complete control on the entire application. But, I didn't really care about how the debug controls would run. And in order to achieve this, I had to learn about COM programming and I wasn't in the mood to learn that (too much of a learning curve)
2. Keeping it completely managed: With this option, I could create both the engine and the debug controls using the .Net framework. Easier to manage, but then I lose some control on my engine. I know there are debates about not losing much performance. Here's a link to understand the cost of going completely managed:
http://code4k.blogspot.com/2011/03/benchmarking-cnet-direct3d-11-apis-vs.html
After going through this link, I decided not to go completely managed.
3. Keep the controls managed, and the engine unmanaged: This was a good choice, because of performance on Engine and ease of managing the controls and adding windows.

Here's a good link on setting this up:
http://www.delmarlearning.com/companions/content/1592009638/bonus/009638_BonusCh02.pdf

This link talks about setting up an unmanaged DirectX Engine withing a C# applicaion, and how to define the interfaces of the unmanaged application to the managed application. I managed to use C++\CLI with my  application (because I like C++, it's more raw !!). Folliowing is the results I got with combining managed window with unmanaged directX Engine.

As you can see, I've added panels  and the yellow window is the unmanaged DirectX Engine running. All my debug controls goes into the panels and I can render stuff with my unmanaged Engine.