Direct 3d C API

I was searching the web and found that in order to use d3d in c you had to interface with COM . What is COM and why doesn't Microsoft directly write c api like for opengl.
Direct3D and any other COM interface can be used from C without problems. COM "objects" are just a struct of function pointers.
It is just an agreement/specification how you should create function pointers in memory and how first three (QueryInterface, AddRef, Release) should look like and what should they do. Everything else is up to you. As long as you follow these rules, you can use and implement COM objects in C, C++, C# and almost any language that call C function pointer. Extra thing to take care in COM is who frees the object. All COM objects are reference counted - that's why there are AddRef and Release methods. I guess their idea was that with reference counting there is less change for memory leak if everyone follows the protocol.

For examples look here:
* D3D11 "hello world" in C: https://git.handmade.network/snippets/19
* COM inheritance in C: https://gist.github.com/mmozeiko/72730704e65161441c1c3b4f1bcd0bd4

If you look at D3D11 sample I wrote, you can see that you use D3D11 in C almost exactly the same as with OpenGL - just bunch of calls to function pointers. Only big difference is how you get those function pointers - in OpenGL you get them by loading extension entry points. In D3D11 everything is specified by D3D11 specification and you get them from D3D11CreateDevice function (+ swap chain if needed).

If you think about it then OpenGL handles (GLuint for texture, framebuffer, vertex buffer, etc...) pretty much are "COM objects" - they specify which specific instance you operate on. Exactly what COM object does in D3D11.

Edited by Mārtiņš Možeiko on
Thanks for the d3d11 and COM implementation example. I'll now try to use it make d3d9 implementation. And A COM object seems similar to c++ classes is it how you implement C++ class in c.

Edited by Shazan Shums on
In C++ you implement COM object with C++ classes. That's not coincidence, they designed COM objects so they are compatible with C++ classes (virtual table placement and layout). But as you have noticed - nothing stops you to implement exactly same memory layout and functions in C.