1

我正在尝试遵循Riemer 的 DirectX 与 C++ 教程

我做了一些更改,例如在这里我将我的InitializeDevice()函数放在一个单独的文件 ( tdirect.cpp/ tdirect.h) 中。

当我按 F5 编译并运行时,程序运行良好。但是,当我在不选择“重建”选项的情况下更改值(例如0xff00ffff,将0xff0000ff青色变为蓝色)时,我的控制台中会出现一连串错误,这会阻止我编译程序。你可以想象,这很烦人。好像要编译两次。tdirect.cpp

下面是 , 的源代码tdirect.cpp,tdirect.h相关部分来自main.cpp(tdirect.h仅包含于,包含于)main.cppbasicvertex.htdirect.cpp

tdirect.cpp

#include "tdirect.h"
#include "basicvertex.h"

IDirect3D9 *pD3D;
D3DPRESENT_PARAMETERS D3DParams; 

LPDIRECT3DDEVICE9 InitializeDevice(HWND Wnd)
{
    pD3D = Direct3DCreate9(D3D_SDK_VERSION);
    if (pD3D == NULL)
    {
        MessageBox(Wnd, "DirectX is not installed.", "No DirectX!", MB_OK);
        return NULL;
    }

    ZeroMemory(&D3DParams, sizeof(D3DPRESENT_PARAMETERS));

    D3DParams.Windowed = TRUE;
    D3DParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
    D3DParams.BackBufferFormat = D3DFMT_UNKNOWN;

    LPDIRECT3DDEVICE9 pDevice;
    if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Wnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &D3DParams, &pDevice)))
    {
        if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, Wnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DParams, &pDevice)))
        {
            MessageBox(Wnd, "Failed to create reference device.", "No software device!", MB_OK);
        }
        else {
            MessageBox(Wnd, "Falling back to software mode.", "No hardware device!", MB_OK);
        }
    }

    return pDevice;
}

LPDIRECT3DVERTEXBUFFER9 vBuffer;
void Init(LPDIRECT3DDEVICE9 pDevice)
{
    BASICVERTEX Vertices[3];
    Vertices[0].x = 150;
    Vertices[0].y = 100;
    Vertices[0].weight = 1;
    Vertices[0].colour = 0xffff0000;

    Vertices[1].x = 350;
    Vertices[1].y = 100;
    Vertices[1].weight = 1;
    Vertices[1].colour = 0xff00ff00;

    Vertices[2].x = 250;
    Vertices[2].y = 300;
    Vertices[2].weight = 1;
    Vertices[2].colour = 0xff00ffff;

    if (FAILED(pDevice->CreateVertexBuffer(sizeof(BASICVERTEX)*3, 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &vBuffer, NULL)))
    {
        MessageBox(NULL, "Failed to create vertex buffer", "Fail", MB_OK);
    }
    else {
        void* pVertices;
        if (FAILED(vBuffer->Lock(0, sizeof(BASICVERTEX)*3, (void**)&pVertices, 0)))
        {
            MessageBox(NULL, "Failed to lock vertex buffer", "Fail", MB_OK);
        }
        else {
            memcpy(pVertices, Vertices, sizeof(BASICVERTEX)*3);
            vBuffer->Unlock();
        }
    }
}

void DrawScene(LPDIRECT3DDEVICE9 pDevice)
{
    pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
    pDevice->BeginScene();

    pDevice->SetStreamSource(0, vBuffer, 0, sizeof(BASICVERTEX));
    pDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
    pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

    pDevice->EndScene();
    pDevice->Present(NULL, NULL, NULL, NULL);
}

tdirect.h

#pragma once
#include <Windows.h>

main.cpp包括部分

#include <Windows.h>

#include "dxheader.h"
#include "tdirect.cpp"

编译器错误:

1>Debug\tdirect.obj : warning LNK4042: object specified more than once; extras ignored
1>main.obj : error LNK2005: "struct IDirect3DDevice9 * __cdecl InitializeDevice(struct HWND__ *)" (?InitializeDevice@@YAPAUIDirect3DDevice9@@PAUHWND__@@@Z) already defined in tdirect.obj
1>main.obj : error LNK2005: "void __cdecl Init(struct IDirect3DDevice9 *)" (?Init@@YAXPAUIDirect3DDevice9@@@Z) already defined in tdirect.obj
1>main.obj : error LNK2005: "void __cdecl DrawScene(struct IDirect3DDevice9 *)" (?DrawScene@@YAXPAUIDirect3DDevice9@@@Z) already defined in tdirect.obj
1>main.obj : error LNK2005: "struct IDirect3DVertexBuffer9 * vBuffer" (?vBuffer@@3PAUIDirect3DVertexBuffer9@@A) already defined in tdirect.obj
1>main.obj : error LNK2005: "struct _D3DPRESENT_PARAMETERS_ D3DParams" (?D3DParams@@3U_D3DPRESENT_PARAMETERS_@@A) already defined in tdirect.obj
1>main.obj : error LNK2005: "struct IDirect3D9 * pD3D" (?pD3D@@3PAUIDirect3D9@@A) already defined in tdirect.obj
1>C:\Users\me\Documents\Visual Studio 2010\Projects\DirectX\Debug\DXStuff.exe : fatal error LNK1169: one or more multiply defined symbols found

提前致谢 :)

4

1 回答 1

4

您从 main.cpp 中包含了一个实现文件,因此它再次添加了函数。你不能这样做:

 #include "tdirect.cpp"

相反,您需要使用函数原型创建一个头文件,以便您可以引用它们......

您是否在“tdirect.h”中定义了这些(这是 main.cpp 应该包含的内容)?

LPDIRECT3DDEVICE9 InitializeDevice(HWND Wnd);

ETC...

于 2011-05-04T21:36:29.717 回答