Direct3D 9 ATL Window Class
A few years ago I realized Direct3D lacks the Windows framework I’d like to use.
The DXUT didn’t inspire me at all.
I use C++, I like the language, and I’m (hopefully) good in using it. I don’t need that C-style, or maybe OpenGL-style, callbacks. I think the virtual functions were designed into the language for the reason. Besides, I’d like to do the windows message handling myself. And, I think most low-level Direct3D stuff should be encapsulated into the one simple сlass that should be easily customized to output your content.
Since I was unable to find what I needed, I’ve created my own solution. This window class is called CDxWnd. You'll find it in the Direct3D9 subfolder of the project. It's base class is CWindowImpl from the ATL library. Read the MSDN for more information on lifetime, message handling, and such stuff.
Download the demo project and see yourself. The project is for Visual Studio 2008, if you're using the 2005 it should work too: I'm only using the 2008 since winter 2010.
There’re only 6 virtual functions to implement — and you’re rendering your own 3D scene, instead of my stupid color bubbles. BTW I've got no illusions, they are awful. I only painted them for one reason: I suspect I wouldn't bother evaluating the "Direct3D 9 ATL Window Class" where the demo application only shows the black screen.
- CreateResources: override this to create your D3D resources. The function will be called only once, from WM_CREATE handler.
- DestroyResources: the function will be called only once, from WM_DESTROY event handler.
- OnPreReset: override this to free all D3DPOOL_DEFAULT resources.
- OnPostReset: override this to recreate D3DPOOL_DEFAULT resources.
- InitScene: prepare the scene for the rendering. Will be called after resizing window and immediately after each OnPostReset()
- DrawScene: actual rendering.
Besides the CDxWnd class, there’re also 2 helper classes to simplify resource management routines: CTexture for texture resources, and CVertexBuffer template class for vertex buffer resource.
The best part is, due to the nature of ATL Windows framework, the code can be easily integrated into most Windows applications written in C++, requiring no additional dependencies. And thanks to Windows Vista with its Aero, most modern PCs, and even laptops, already have a D3D9-class GPU.
So if you want to use Direct3D to visualize a rapidly changing 3D pie chart in your business application, well, feel free to go ahead, and, hmm, visualize your rapidly changing pie chart.