DirectX programming in C?
Asked Answered
F

5

11

In the past I have created DirectX applications in the C++ programming language, however, I would like to know if it is possible to do this using the C programming language.

Thanks.

Fernandafernande answered 14/1, 2011 at 21:20 Comment(1)
Is there a reason for the C-only restriction?Alimentation
L
11

Yes it is possible. DirectX exposes a COM interface and C is capable of consuming them. It won't be a whole boat load of fun though!

Loosetongued answered 14/1, 2011 at 21:21 Comment(2)
I've tried. You get used to it, but it's still not pleasant or easy.Emoryemote
This way is possible as well but as David mentioned this will not be fun at all. Best of luck if you attempt to take this route.Calabrese
J
14

The Open Watcom C/C++ compiler comes with DirectX sample applications in both C++ and C. Both work. They are under WATCOM\samples\directx\cpp and WATCOM\samples\directx\c respectively in OW 1.9.

This is what the code looks like in C++:

hr = d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dcaps9);
hr = d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, game_window, D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, &d3dpp, &d3d_dev);

And in C:

hr = IDirect3D9_GetDeviceCaps(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dcaps9);
hr = IDirect3D9_GetAdapterDisplayMode(d3d, D3DADAPTER_DEFAULT, &d3ddm);
hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, game_window, D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, &d3dpp, &d3d_dev);

You don't need to do anything special with COM in C as there seem to be enough macros defined that you can just use.

Jinnah answered 1/11, 2011 at 8:28 Comment(3)
People should be motivated to answer in this way, extending from "Is it possible..." to "This is how it's done...". I really appreciate the information you provided, I'm a beginner and I was starting to think about creating C wrappers for C++ methods. Thank you.Persis
@Ivanzinho This may not be generally applicable or preferable.Jinnah
why is it not preferable? I'm learning C and I think the best way to learn it is by actually using it. (I've been programming in C# for 12+ years by the way, I know what OOP is)Persis
L
11

Yes it is possible. DirectX exposes a COM interface and C is capable of consuming them. It won't be a whole boat load of fun though!

Loosetongued answered 14/1, 2011 at 21:21 Comment(2)
I've tried. You get used to it, but it's still not pleasant or easy.Emoryemote
This way is possible as well but as David mentioned this will not be fun at all. Best of luck if you attempt to take this route.Calabrese
R
2

You can use DirectX in C. It has specific macros to simplify the use of the COM interface. However, it's much easier to use C++.

Rehabilitation answered 14/1, 2011 at 21:31 Comment(0)
C
0

I think that the DirectX libraries have some C++ only components (it's been a while since I've used it and from last I remember it contains classes). Might as well make your life a little easier and do it in C++.

Calabrese answered 14/1, 2011 at 21:22 Comment(6)
Just because you can doesn't mean you should sometimes. Although if he would like to then he can go right on ahead hehe.Calabrese
@Jesus hehe indeed. I recently had to implement a COM interface in C. That sure made my head hurt.Loosetongued
I've done similar things as well, except I have a bigger headache with COM components in .NET and C# than C. Sure would be cool to see some DirectX stuff working efficiently through COM that's for sure.Calabrese
@Jesus Ahh, I'm getting some recollections of some C++ bits. Is that perhaps only in the routines for turning error codes into error messages?Loosetongued
@David: I believe so. Although I'm still not sure as to why someone would want to try this in C unless they're masochistic in some way (no offense if anyone is going to try this, I would just find it more difficult than it needs to be).Calabrese
Reasons to do anything in C: 1 Less invisible code. 2 Stronger guarantees. 3 More mature tools. 4 Better code analysis. 5 Better interoperability story. I'm sure there are more reasons, but if you care about being able to spot errors when reading a line of code, you cannot possibly hope to use C++.Westerfield
J
0

It is possible, but because you lack the higher level functions C++ provides you are making it harder for yourself.

Jordison answered 2/6, 2018 at 18:37 Comment(7)
Firstly, it doesn't make anything harder, please learn how powerful C truly is before stating that C is harder then C++. Second, the tutorial provided is clearly C++.Zadoc
@Zadoc Actually it does. A lot of the API for Windows uses C++ which is built from classes. To use it you will need to access the class in C, is it possible? Yes certaily. Is it practicle? Not at all, it is much easier to use C++ where you already use classes and so you can use the API as is, without needing workarounds.Jordison
@Zadoc Also from your comment you are saying that I suggested C is harder than C++ (I don't know in what context you meant that). The learning curve of C is a lot smaller than C++ (smaller library and overall smaller syntax than C++). However in this case, where you are trying to use APIs implamented for C++ using them in C makes your life a LOT LOT harder.Jordison
I have removed the link as you seem to be right, in part you can run it in C but in others it does seem to be C++. I'll see if I can get the examples to work later. @ZadocJordison
Here is an example in C I am currently working on: github.com/gnif/LookingGlass/blob/master/c-host/windows/capture/…. I really don't see how this is a LOT LOT harder, it's just a little different.Zadoc
Very nice. I'll look at that with interest thank you. @ZadocJordison
My point of experience is that C only 'appears' harder because there's no RAII (thus no com_ptr). Aside from that? Not much difference.Belamy

© 2022 - 2024 — McMap. All rights reserved.