4
\$\begingroup\$

I want to try something simple with Direct Sound. Just want to know if it's possible.

Or maybe I need some other API?

\$\endgroup\$
1
  • \$\begingroup\$ Possible, yes. Practical, no. \$\endgroup\$ – MichaelHouse Sep 25 '13 at 18:37
5
\$\begingroup\$

If you are for some reason dead set on C, OpenAL may be the audio API to choose as it is a pure C API. OpenAL is a very low-level API. I'm unsure how it compares to DirectSound as I'm used to working with more complete frameworks/engines/games which tend to use something like FMOD or Wwise rather than trying to roll their own high-level audio manipulation library. Depending on what you're trying to do, a library like one of those might be a better option for you, too.

There are ways to bind DirectSound to C, and there at one point was even an official C binding for many DX components (no idea if it's still maintained), but in general this is not the way to go.

Short version: use C++ or use OpenAL.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ DirectSound is a COM based API, which can be perfectly used from C. In fact, it is most likely implemented in C. The fact that COM functions are called in a similar way as you call C++ methods does not mean that COM interfaces are C++ classes. \$\endgroup\$ – Panda Pajama Sep 26 '13 at 7:15
7
\$\begingroup\$

It's not only possible, it's actually been done.

More than once.

OK, I wouldn't go so far as to call out early id Software code as the epitome of good coding standards, but it does demonstrate that using DirectX in pure C isn't something that exists on a theoretical level, but rather something that has shipped in popular working commercial products.

Despite that you may have problems.

In the DirectInput example, typing out all of the wrapper macros can be a total pain-in-the-ass. In the DirectSound example, earlier versions of MSVC didn't have autocomplete on the lpVtbl member, which can also be a total pain-in-the-ass (I haven't bothered checking with more recent versions).

There are other places in other DirectX APIs where some C++ isms have crept in, notably in the D3DX math stuff, and which can be awkward (but not impossible) if you wish to use them from C.

Nonetheless, fundamentally DirectX is a COM-based API, and while COM may look on the surface as if it is C++ based, it's actually not - a COM interface isn't a class, it's a struct, and full C compatibility exists (COM as-designed is intended to be language-neutral). I suggest checking out the Wikipedia article on COM for further info.

\$\endgroup\$
1
\$\begingroup\$

Ofcourse. DirectX is written in plain C. Sure, there are some C++ extension libraries for help on math-related stuff (matrices, vectors etc), but those are typically not long-lived (early deprecated), and rightfully so. Hence you shouldn't even be using those C++ libraries; at least not if you want your code to become deprecated over time.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.