2
\$\begingroup\$

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; 

#define GPA(d3dModule, func) GetProcAddress(d3dModule, func)

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 LoadDriver();
    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.
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – user1430
    Commented Nov 16, 2017 at 15:02
  • \$\begingroup\$ Just out of curiosity, how could I change the project settings to compile as a C++ project? \$\endgroup\$
    – Hermetix
    Commented Nov 16, 2017 at 15:40
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – user1430
    Commented Nov 16, 2017 at 16:29
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 16, 2017 at 17:17
  • \$\begingroup\$ @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... \$\endgroup\$
    – Hermetix
    Commented Nov 16, 2017 at 21:20
1
\$\begingroup\$

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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .