如何解决此DirectX代码中的错误?

时间:2014-09-09 14:01:17

标签: c++ directx

首先,我在编程方面还不是很好,所以如果我无法解决琐碎的问题,请原谅我。我想在一个窗口中显示一个基本的屏幕,所以我从3个不同的来源为它创建了一个框架,因为我想逐步编写它以完全理解它。但是,我仍然得到这3个错误,我不知道如何解决它们。在你帮助之前感谢你。

第一个代码:

#ifndef WINAPPFRAME_H
#define WINAPPFRAME_H


#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#include <string>


#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")


#define Release(x) { if(x){ x->Release(); x = 0; } }

#ifndef HR
    #define HR(x) (x)
#endif

class D3Dwap
{

public:
    D3Dwap(HINSTANCE hInstance);
    ~D3Dwap();

    HINSTANCE AppInst()const;
HWND      MainWnd()const;

bool Initialize();
int Run();
void Draw();

static LRESULT CALLBACK MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

protected:
bool InitMainWindow();
bool InitDirect3D(); 

protected:
HINSTANCE hD3DwapInst;
HWND      hMainWnd;

ID3D11Device* D3Dwap_device; 
ID3D11DeviceContext* D3Dwap_dev_context;
IDXGISwapChain* D3Dwap_SwapChain;
ID3D11Texture2D* D3Dwap_DepthStencilBuffer;
ID3D11RenderTargetView* D3Dwap_RenderTargetView;
ID3D11DepthStencilView* D3Dwap_DepthStencilView;
D3D11_VIEWPORT D3Dwap_ScreenViewport;
ID3D11Texture2D* backBuffer;

std::wstring MainWndCaption;
D3D_DRIVER_TYPE d3dDriverType;
int ScreenWidth;
int ScreenHeight;
};

D3Dwap* d3dwapl = 0;

#endif

第二部分:

#include "WinAppFrame.h"


LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    return d3dwapl->MsgProc(hwnd, msg, wParam, lParam);
}

D3Dwap::D3Dwap(HINSTANCE hInstance):
hD3DwapInst(hInstance),
MainWndCaption(L"Engine 2.0"),
d3dDriverType(D3D_DRIVER_TYPE_HARDWARE),
ScreenWidth(800),
ScreenHeight(600),
hMainWnd(0),
D3Dwap_device(0),
D3Dwap_dev_context(0),
D3Dwap_SwapChain(0) {}


D3Dwap::~D3Dwap()
{
Release(D3Dwap_SwapChain);

if( D3Dwap_dev_context )
    D3Dwap_dev_context->ClearState(); //Ha még aktív az ezköz tartalma, törli azt

Release(D3Dwap_dev_context);
Release(D3Dwap_device);
}

HINSTANCE D3Dwap::AppInst()const
{
return hD3DwapInst;
}

HWND D3Dwap::MainWnd()const
{
return hMainWnd;
}

int D3Dwap::Run()
{
MSG msg;

while(msg.message != WM_QUIT)
{
    if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }

    Draw();
}

return msg.wParam;
}

bool D3Dwap::Initialize()
{
if(!InitMainWindow())
    return false;

if(!InitDirect3D())
    return false;

return true;
}

LRESULT D3Dwap::MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
}

return DefWindowProc(hwnd, msg, wParam, lParam);
}

bool D3Dwap::InitMainWindow()
{
WNDCLASSEX wc;

wc.style         = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc   = MainWndProc; 
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;
wc.hInstance     = hD3DwapInst;
wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
wc.hCursor       = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
wc.lpszClassName = L"D3Dwap_test";

if( !RegisterClassEx(&wc) )
{
    MessageBox(0, L"Az ablakot nem sikerült regisztrálni", 0, 0);
    return false;
}

RECT Rect = { 0, 0, ScreenWidth, ScreenHeight };
AdjustWindowRect(&Rect, WS_OVERLAPPEDWINDOW, false);
int width  = Rect.right - Rect.left;
int height = Rect.bottom - Rect.top;

hMainWnd = CreateWindow (L"D3Dwap_test",
                         MainWndCaption.c_str(),
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         width, 
                         height,
                         0,
                         0,
                         hD3DwapInst,
                         0);

if( !hMainWnd )
{
    MessageBox(0, L"Az ablakot nem sikerült létrehozni!", 0, 0);
    return false;
}

ShowWindow(hMainWnd, SW_SHOW);
UpdateWindow(hMainWnd);

return true;
}

bool D3Dwap::InitDirect3D()
{
UINT create_D3Dwap_device_flags = 0;

D3D_FEATURE_LEVEL featurelevel;

HRESULT hr = D3D11CreateDevice(0,
                               d3dDriverType,
                               0,
                               create_D3Dwap_device_flags = 0,
                               0,
                               0,
                               D3D11_SDK_VERSION,
                               &D3Dwap_device,
                               &featurelevel,
                               &D3Dwap_dev_context);

if( FAILED(hr) )
{
    MessageBox(0, L"A D3D11 eszközt nem sikerült létrehozni!", 0, 0);
    return false;
}

if( featurelevel != D3D_FEATURE_LEVEL_11_0 )
{
    MessageBox(0, L"A DirectX 11.0 a hardware által nem támogatott!", 0, 0);
    return false;
}

DXGI_SWAP_CHAIN_DESC sd;

sd.BufferDesc.Width  = ScreenWidth;
sd.BufferDesc.Height = ScreenHeight;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.SampleDesc.Count = 1;                               
sd.SampleDesc.Quality = 0;
sd.BufferUsage  = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount  = 1;
sd.OutputWindow = hMainWnd;
sd.Windowed     = true;
sd.SwapEffect   = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags        = 0;

IDXGIDevice* dxgiDevice = 0;    
if(FAILED(HR(D3Dwap_device->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice))))
{
    return false;
}

IDXGIAdapter* dxgiAdapter = 0;
if(FAILED(HR(dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter))))
{
    return false;
}

IDXGIFactory* dxgiFactory = 0;
if(FAILED(HR(dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory))))
{
    return false;
}

HR(dxgiFactory->CreateSwapChain(D3Dwap_device, &sd, &D3Dwap_SwapChain));


ID3D11Texture2D* backBuffer;

if(FAILED(HR(D3Dwap_SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&backBuffer)))))
{
    return false;
}

if(FAILED(HR(D3Dwap_device->CreateRenderTargetView(backBuffer, 0, &D3Dwap_RenderTargetView))))
{
    return false;
}

Release(backBuffer);


D3D11_TEXTURE2D_DESC depthStencilDesc;

depthStencilDesc.Width     = ScreenWidth;
depthStencilDesc.Height    = ScreenHeight;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format    = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count   = 1;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage          = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags      = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0; 
depthStencilDesc.MiscFlags      = 0;

if(FAILED(HR(D3Dwap_device->CreateTexture2D(&depthStencilDesc, 0, &D3Dwap_DepthStencilBuffer))))
{
    return false;
}

if(FAILED(HR(D3Dwap_device->CreateDepthStencilView(D3Dwap_DepthStencilBuffer, 0, &D3Dwap_DepthStencilView))))
{
    return false;
}

D3Dwap_dev_context->OMSetRenderTargets(1, &D3Dwap_RenderTargetView, D3Dwap_DepthStencilView);

D3Dwap_ScreenViewport.TopLeftX = 0;
D3Dwap_ScreenViewport.TopLeftY = 0;
D3Dwap_ScreenViewport.Width    = static_cast<float>(ScreenWidth);
D3Dwap_ScreenViewport.Height   = static_cast<float>(ScreenHeight);
D3Dwap_ScreenViewport.MinDepth = 0.0f;
D3Dwap_ScreenViewport.MaxDepth = 1.0f;

D3Dwap_dev_context->RSSetViewports(1, &D3Dwap_ScreenViewport);

Release(dxgiDevice);
Release(dxgiAdapter);
Release(dxgiFactory);

return true;
}

和第三个代码:

#include "WinAppFrame.h"

class InitDirect3Dwap : public D3Dwap
{
public:
InitDirect3Dwap(HINSTANCE hInstance);
~InitDirect3Dwap();

bool Initialize();
void UpdateScene(float dt);
void Draw(); 
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
               PSTR cmdLine, int showCmd)
{
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

InitDirect3Dwap theApp(hInstance);

if( !theApp.Initialize() )
    return 0;

return theApp.Run();
}

InitDirect3Dwap::InitDirect3Dwap(HINSTANCE hInstance)
: D3Dwap(hInstance) 
{
}

InitDirect3Dwap::~InitDirect3Dwap()
{
}

bool InitDirect3Dwap::Initialize()
{
if(!D3Dwap::Initialize())
    return false;

return true;
}

void InitDirect3Dwap::UpdateScene(float dt)
{

}

void InitDirect3Dwap::Draw()
{
    D3Dwap_dev_context->ClearRenderTargetView(D3Dwap_RenderTargetView, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
D3Dwap_dev_context->ClearDepthStencilView(D3Dwap_DepthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

HR(D3Dwap_SwapChain->Present(0, 0));
}

1st:错误29错误LNK2005:“class D3Dwap * d3dwapl”(?d3dwapl @@ 3PAVD3Dwap @@ A)已在main.obj中定义C:\ Users \ Seraph \ documents \ visual studio 2012 \ Projects \ Engine_2.0 \ Engine_2.0 \ WinAppFrame.obj Engine_2.0

2nd:错误30错误LNK2019:未解析的外部符号“public:void __thiscall D3Dwap :: Draw(void)”(?Draw @ D3Dwap @@ QAEXXZ)在函数“public:int __thiscall D3Dwap :: Run(void)”中引用“(?运行@ D3Dwap @@ QAEHXZ)C:\ Users \ Seraph \ documents \ visual studio 2012 \ Projects \ Engine_2.0 \ Engine_2.0 \ WinAppFrame.obj Engine_2.0

第3次:错误31错误LNK1120:1个未解析的外部C:\ Users \ Seraph \ documents \ visual studio 2012 \ Projects \ Engine_2.0 \ Debug \ Engine_2.0.exe 1 1 Engine_2.0

1 个答案:

答案 0 :(得分:1)

两次包含“WinAppFrame.h”,这使得在那里定义的类可以复制。这个问题的一个常见解决方案是放

#pragma once

在WinAppFrame.h文件的开头。

然后,在类D3Dwap中声明一个方法Draw,但在相应的.cpp文件中没有定义。