Mixing DirectX headers with C mod project not working

I'm trying to add a DirectX 11 renderer (not sure if that is even doable in a "simple" manner) to the Return To Castle Wolfenstein source code and I'm having some problems. I have a rendering class in a header that I included in the project, and whenever I include it in one of the RTCW files I get this error:

1>c:\program files (x86)\windows kits\8.1\include\um\directxmath.h(17): fatal error C1189: #error: DirectX Math requires C++

Now I am wondering what would be a way to use DirectX math functions/classes (and all DirectX classes actually) in a C project such as the RTCW code? Is it possible to change the entire project settings so it compiles as C++? I am using Visual Studio 2017.

This is my renderer class declaration in XD3D.h header:

#include <d3d11.h>
#include <DirectXMath.h>
#include "XGeomObject.h"
#include "XCamera.h"

using namespace DirectX;

class XD3DRenderer
{
protected:
HMODULE                     m_XD3DModule;
ID3D11Device*               m_pD3DDevice;
ID3D11DeviceContext*        m_pDeviceContext;
IDXGISwapChain*             m_pSwapChain;
ID3D11RenderTargetView*     m_pRenderTargetView;
ID3D11Texture2D*            m_pBackBuffer;
ID3D11Texture2D*            m_pDepthStencilBuffer;
ID3D11DepthStencilState*    m_pDepthStencilState;
ID3D11DepthStencilState*    m_pDisabledDepthStencilState;
ID3D11RasterizerState*      m_pRasterState;
ID3D11DepthStencilView*     m_pDepthStencilView;

HWND                        m_wnd;
int                         m_screenWidth, m_screenHeight;
bool                        m_fullScreen;
D3DXMATRIX                  m_world, m_view, m_proj;
bool                        m_lightMapsOn;

public:
XD3DRenderer();
XD3DRenderer(HWND wnd, int screenwidth, int screenheight,bool fullscreen);
~XD3DRenderer();

ID3D11Device* GetD3DDevice() { return m_pD3DDevice; }
ID3D11DeviceContext* GetDeviceContext() { return m_pDeviceContext; }

bool CreateD3DDevice(HWND wnd, int screenwidth, int screenheight, bool fullscreen );
bool CreateRenderTargetView();
bool CreateDepthBuffer();
bool CreateDepthStencilView();
bool CreateDisabledDepthStencilState();

bool Setup();
bool ClearScene(const D3DXCOLOR& col);
bool Render(XGeomObject*, XCamera*);
bool ShowScene();
void TurnOnZBuffer();
void TurnOffZBuffer();
};


And I include it in the C project in the RTCW win_glimp.c file like this:

#include <assert.h>
#include "../renderer/tr_local.h"
#include "../qcommon/qcommon.h"
#include "resource.h"
#include "glw_win.h"
#include "win_local.h"

#include "../renderer/XD3D.h"

extern void WG_CheckHardwareGamma( void );
extern void WG_RestoreGamma( void );

... rest of win_glimp.c file omitted.


The #error pragma is there because DirectXMath uses a bunch of C++ functionality that C doesn’t have.

Direct3D itself is a COM API and can be used that way via C, if you like (there are helpful macros for calling functions in many headers). But DirectXMath is not and cannot be used directly in C code. You could create a wrapper: write a C header that declares all the functions you need to use, implement them in a C++ source file that includes DirectXMath and is separately compiled as C++ (or a static library).

But you may be better off finding a C math library and saving yourself the trouble.

You could change the whole game project to compile as C++, but that may not work: if the project uses C features that don’t exist in C++, you’ll have to fix those errors instead. And the change may introduce subtle behavioral differences in places that may be hard to track down. It would not be my first choice for so large a codebase.

• github.com/ferreiradaselva/mathc is one such library I know of, although I can’t speak to the quality as I’ve never used it. – user1430 Nov 16 '17 at 15:02
• Just out of curiosity, how could I change the project settings to compile as a C++ project? – Hermetix Nov 16 '17 at 15:40
• That depends entirely on the toolchain you're using. It's usually a switch to the compiler, eventually, but depending on your build system it may be done earlier or elsewhere as well, or exposed via UI for your IDE. For example, for cl.exe you'd probably want to ensure this is set for all source files. – user1430 Nov 16 '17 at 16:29
• You could also in theory go back to the much older XNAMath which does build in C. See this post and GitHub. While you can in theory use DirectX COM APIs from C, that usage is not tested, documented, or used much at all. It's just a hold-over from the fact that the MIDL compiler still generates all those C macros. – Chuck Walbourn Nov 16 '17 at 17:17
• @Josh Petrie Just remembered that the RTCW engine actually uses DirectInput and DirectSound in C. So I guess I know where to look for an actual example... – Hermetix Nov 16 '17 at 21:20

Well, I was able to find a math library that does not require C++. The D3DX (Direct3D 9) library does the same thing as the DirectXMath, basically.

d3dx9math.h is needed.

Reference:

https://msdn.microsoft.com/en-us/library/windows/desktop/bb147179(v=vs.85).aspx