Upcoming Events
Southwest Gaming Expo
11/20 - 11/22 @ Dallas, TX

Workshop on Network and Systems Support for Games (NetGames 2009)
11/23 - 11/25 @ Paris, France

ICIDS 2009 Interactive Storytelling
12/9 - 12/11 @ Guimarães, Portugal

Global Game Jam
1/29 - 1/31  

More events...


Quick Stats
1118 people currently visiting GDNet.
2341 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!



Link to us

Link to us

  Intel sponsors gamedev.net search:   

Hosting a C++ D3D engine in C# Winforms


Motivation

So you have invested man-years of effort in your C++ D3D graphics/visualization/game engine. Now you want to build a nice GUI that utilizes this engine, such as a scene editor or modeler, but low and behold you are inundated by potential hosting solutions: MFC, wxWidgets, QT, Winforms, WPF. All with different challenges and subtleties.

Back in the day I solved this problem with MFC. Microsoft has not yet abandoned MFC (given the recent release of MFC 9.0), but they have seemed to shift their GUI development focus to Winforms (and more recently WPF). Having spent considerable time struggling with MFC in the past, I have found Winforms’ ease of use, consistency of design, and community support, more than enough reason to switch.

When adapting an existing engine to be hosted in Winforms, you must forgo creating your own window, and be able to attach to any given window. You must also be able to react to changes that window might undergo (loss of focus, minimization, resize, etc). Also keep in mind that your engine does not really own the window it is given, so changing properties of the window should be discouraged, as this may break Winforms’ management of that window.

For this article I am going to adapt a small unmanaged C++ D3D9 engine to be hosted in a C# Winforms panel via a C++/CLI glue DLL (and I will then explain how to adapt this to a WPF app). The actual rendering engine is kept to an absolute minimum, as to not obscure the actual goal of the article. Also note that the techniques described in this article are not necessarily restricted to D3D, and can be adapted to OpenGL as well

This article will be broken up into:

  • Page 1
    • Creating a simple D3D management class
    • Hooking an HWND for messages
  • Page 2
    • Application specific derived class
    • Exposing the derived class in a Win32 DLL
  • Page 3
    • Creating a C++/CLI wrapper DLL
    • Utilizing the wrapper in a C# application
    • Extracting an HWND from a panel control
    • Setting up an efficient render loop
  • Page 4
    • What about WPF???
    • Conclusion




Page 1


Contents
  Introduction
  Page 1
  Page 2
  Page 3
  Page 4

  Source code
  Printable version
  Discuss this article