Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

notme

DirectX & C

This topic is 5733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!