DirectX & C

Started by
4 comments, last by notme 21 years, 4 months ago
Is it possible to use DirectX with C, or do you need to use C++?
Advertisement
No you need C++ because almost every function in DirectX (not D3DX though) is a method from some interface/class.

---
Brent Gunning | My Site
IIRC DirectX does support C, but it''s more complex than in C++. I''m not sure about that though.
You can indeed use DirectX with C.

However, as baldurk says it''s more complex. The COM interfaces used by DirectX are not much more than structures containing pointers to functions. The only complication is that under DirectX they''re all virtual functions so pass through an extra level of indirection.

A C call to say IDirect3D8::GetCaps would look something like:
lpD3D8->lpVtbl->GetDeviceCaps( lpD3D8, 0, D3DDEVTYPE_HAL, &c );

The same call in C++ would be:
lpD3D8->GetDeviceCaps( 0, D3DDEVTYPE_HAL, &c );


Two things of note:

1) the indirection through lpVtble (virtual function table), in C++ this is still done, but automatically by the compiler.

2) the IDirect3D8 interface pointer being passed in as an extra first parameter. Once again, the same happens in C++, but the compiler hides it. The pointer being passed is the C++ "this" pointer, the reason the function needs it is DirectX itself was written in C++.

To make life easier, Microsoft provide helper macros to call from C. Take a look inside any DirectX header file you''re interested in and you''ll see macros which look like:

#define IDirect3D8_GetDeviceCaps(p,a,b,c) (p)->lpVtbl->GetDeviceCaps(p,a,b,c) 


You use them like so:

IDirect3D8_GetDeviceCaps( lpD3D8, D3DDEVTYPE_HAL, &c );


I''m not sure how well none-MS compilers will cope with the lpVtble. They should be ok I think.

Having shown you that though, I don''t see any reason *why* you''d want to call from C these days. Much better if you still want to program in a C style is to write C in .CPP files and take advantage of some of the nicer things added on top of ANSI/K&R C. Just because you''re using C++ it doesn''t mean everything has to be an object etc (even Strustrup himself says something similar in his book).


--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

You may physically be ABLE to write C w/ DirectX, but assuming you want to write ''good code'', you must think in modular terms (hence ''classes''). That is the main strength of C++. Every good game programmer out there thinks in terms of OOP and you should train yourself to do the same. Hope this helps.

''Chris
quote:Original post by wiseachoo
You may physically be ABLE to write C w/ DirectX, but assuming you want to write ''good code'', you must think in modular terms (hence ''classes''). That is the main strength of C++. Every good game programmer out there thinks in terms of OOP and you should train yourself to do the same.

rubbish. Carmack didn''t use c++ until now, does that make him a bad programmer? get some sense.

This topic is closed to new replies.

Advertisement