我刚刚开始学习 DirectX,我正在尝试使用 C++ 语言来实现它。我已经开始阅读一本关于这个问题的书,名为Beginning DirectX 10 Game Programming。没关系,我遇到了问题。我已经添加了所需的库,并在 Visual Studio 中包含了必要的文件。错误如下:
Error 26 error LNK2019: unresolved external symbol "bool __cdecl InitDirect3D(struct HWND__ *,int,int)" (?InitDirect3D@@YA_NPAUHWND__@@HH@Z) referenced in function _WinMain@16 C:\Users\Robert\documents\visual studio 2013\Projects\DirectXBook\exampleWithDirectX1\SourceCode.obj exampleWithDirectX1
Error 27 error LNK1120: 1 unresolved externals C:\Users\Robert\documents\visual studio 2013\Projects\DirectXBook\Debug\exampleWithDirectX1.exe 1 1 exampleWithDirectX1
#include <Windows.h>
#include <tchar.h>
#include <d3d10.h>
#include <d3dx10.h>
#pragma comment (lib, "d3dx10.lib")
#pragma comment (lib, "d3d10.lib")
HINSTANCE hInstance; //Instanszkezelo
HWND hWnd; //Ablakkezelo
ID3D10Device *pD3DDevice = NULL;
IDXGISwapChain *pSwapChain = NULL;
ID3D10RenderTargetView *pRenderTargetView = NULL;
int width = 640;
int height = 480;
//fuggveny prototipusok definialasa
bool InitWindow(HINSTANCE hInstance, int width, int height);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool InitDirect3D(HWND hWnd, int width, int height);
void Render();
void shutDownDirect3D();
//WinMain, belepopont a windows applikacioba
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdline, int nCmdShow){
//ablakinicalizalas
if (!InitWindow(hInstance, width, height)){
return false;
}
if (!InitDirect3D(hWnd, width, height)){
return 0;
}
//main message loop
MSG msg = { 0 };
while (WM_QUIT != msg.message){
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//renderelest kezelo fuggveny meghivasa
Render();
}
shutDownDirect3D();
return msg.wParam;
}
bool InitWindow(HINSTANCE hInstance, int width, int height){
WNDCLASSEX wcex;
// WNDCLASSEX wcex objektum kitoltese
// meghatarozza, hogyan fog kinezni az ablakunk
wcex.cbSize = sizeof(WNDCLASSEX); // struktura merete
wcex.style = CS_HREDRAW | CS_VREDRAW; // az osztaly tipusa
wcex.lpfnWndProc = (WNDPROC)WndProc; // az ablak procedure visszahivas (callback)
wcex.cbClsExtra = 0; // extra byteok lefoglasasa az osztalynak
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // kezelo az applikacios esemenynek
wcex.hIcon = 0; // applikaciohoz hozzarendelendo ikon
wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // kurzor hasznalatanak definialasa
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // hatterszin
wcex.lpszMenuName = NULL; // eroforrsanev a menunek
wcex.lpszClassName = TEXT("DirectXExample"); // az osztalynev letrehozva
wcex.hIconSm = 0; // kezelo a kisikonhoz
RegisterClassEx(&wcex); //wcex objektum regisztralasa
RECT rect = { 0, 0, width, height }; // ablakatmeretezes
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
// ablak letrehozasa a felso osztalybol
hWnd = CreateWindow(TEXT("DirectXExample"),
TEXT("DirectXExample"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
hInstance,
NULL);
if (!hWnd){
return false;
}
// ablak kirajzoalsa a kijelzore
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
return true;
}
//WPARAM == typedef UINT_PTR WPARAM passing and returning polymorphic values
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
//megnézi, hogy van - e üzenet a sorban
switch (message){
case WM_KEYDOWN:
switch (wParam){
//ha a felhasznalo az escape billentyu megnyomja kilepes
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
//ha a felhasznalo az X gombra kattint kilepes
case WM_DESTROY:
PostQuitMessage(0);
break;
}
//uzenet elkuldese az alapveto ablak proceduranak, hogy az tovabb elemezze
return DefWindowProc(hWnd, message, wParam, lParam);
}
bool InitDirect3d(HWND hWnd, int width, int height){
// Create and clear the DXGI_SWAP_CHAIN_DESC structure
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
// Fill in the needed values
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.Windowed = TRUE;
// Create the D3D device and the swap chain
HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,
D3D10_DRIVER_TYPE_REFERENCE,
NULL,
0,
D3D10_SDK_VERSION,
&swapChainDesc,
&pSwapChain,
&pD3DDevice);
// Error checking. Make sure the device was created
if (hr != S_OK){
return false;
}
// Get the back buffer from the swapchain
ID3D10Texture2D *pBackBuffer;
hr = pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)
&pBackBuffer);
if (hr != S_OK){
return false;
}
// create the render target view
hr = pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
// release the back buffer
pBackBuffer->Release();
// Make sure the render target view was created successfully
if (hr != S_OK){
return false;
}
// set the render target
pD3DDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
// create and set the viewport
D3D10_VIEWPORT viewPort;
viewPort.Width = width;
viewPort.Height = height;
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;
pD3DDevice->RSSetViewports(1, &viewPort);
return true;
}
void Render(){
if (pD3DDevice != NULL){
//a celpuffer tisztitasa
pD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f));
//IDE JON A RAJZOLAS
//a kovetkezo elem kijelzese a SwapChain - bol
pSwapChain->Present(0, 0);
}
}
void shutDownDirect3D(){
//release the rendertarget
if (pRenderTargetView){
pRenderTargetView->Release();
}
if (pSwapChain){
pSwapChain->Release();
}
if (pD3DDevice){
pD3DDevice->Release();
}
}
提前致谢!