Jump to content
  • Advertisement
Sign in to follow this  
DNewguy

DX11 dx11 broken sample

This topic is 3001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Seems simple enough. This simple program should show a white windows box without the use of dxut headers, but i get a fatal error instead.


winMain.obj : error LNK2019: unresolved external symbol _D3D11CreateDeviceAndSwapChain@48 referenced in function "bool __cdecl InitDirect3D(struct HWND__ *,int,int)" (?InitDirect3D@@YA_NPAUHWND__@@HH@Z)









#include <windows.h>
#include <tchar.h>
#include "resource.h"

#include <D3D11.h>
#include <D3Dx11.h>
#include <D3DX11tex.h>
#include <D3DX11async.h>
#include <D3DX11core.h>

// includes d3d10 math header for use of the d3dxcolor structure i cant find in dx11

//#include <d3d10.h>
#include <d3dx10.h>

// Globals //////////////////////////////////////////////////////
HINSTANCE hInst;
HWND mainhWnd;
int windowWidth = 640;
int windowHeight = 480;

// Direct3D global vars /////////////////////////////////////////
ID3D11Device* pD3DDevice = NULL;
IDXGISwapChain* pSwapChain = NULL;
ID3D11RenderTargetView* pRenderTargetView = NULL;
ID3D11DeviceContext* pD3DViewPort;

// Forward declarations //////////////////////////////////////////
bool InitWindow(HINSTANCE hInstance, int width, int height);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

// Direct3D functions ///////////////////////////////////////////
bool InitDirect3D(HWND hWnd, int width, int height);
void ShutdownDirect3D();
void Render();

/*******************************************************************
* WinMain
*******************************************************************/
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
// save off the instance handle
hInst = hInstance;

// Set up the application window
if (!InitWindow(hInstance, windowWidth, windowHeight))
{
return 0;
}

// Initialize Direct3D
if (!InitDirect3D(mainhWnd, windowWidth, windowHeight))
{
return 0;
}

// Main message loop
MSG msg = {0};
while (WM_QUIT != msg.message)
{
// Process Windows messages first
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// Render the scene
Render();
}

// Clean up the resources we allocated
ShutdownDirect3D();

return (int) msg.wParam;
}

/*******************************************************************
* InitWindow
* Inits and creates and main app window
* Inputs - application instance - HINSTANCE
Window width - int
Window height - int
* Outputs - true if successful, false if failed - bool
*******************************************************************/
bool InitWindow(HINSTANCE hInstance, int width, int height)
{
// Register class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("DirectXExample");
wcex.hIconSm = 0;
if(!RegisterClassEx(&wcex))
{
return false;
}

// Create window
RECT rect = { 0, 0, width, height };
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);

// create the window from the class above
mainhWnd = CreateWindow(TEXT("DirectXExample"),
TEXT("DirectXExample"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
hInstance,
NULL);
if(!mainhWnd)
{
return false;
}

ShowWindow(mainhWnd, SW_SHOW);
UpdateWindow(mainhWnd);

return true;
}


/*******************************************************************
* WndProc
* The main window procedure for the application
* Inputs - application window handle - HWND
message sent to the window - UINT
wParam of the message being sent - WPARAM
lParam of the message being sent - LPARAM
* Outputs - LRESULT
*******************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
// Allow the user to press the escape key to end the application
case WM_KEYDOWN:
switch(wParam)
{
// Check if the user hit the escape key
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;

// The user hit the close button, close the application
case WM_DESTROY:
PostQuitMessage(0);
break;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}

/*******************************************************************
* InitDirect3D
* Initializes Direct3D
* Inputs - Parent window handle - HWND,
Window width - int
Window height - int
* Outputs - true if successful, false if failed - bool
*******************************************************************/
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)hWnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.Windowed = TRUE;

D3D_FEATURE_LEVEL level;
D3D_FEATURE_LEVEL levelsWanted[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
UINT numLevelWanted = sizeof( levelsWanted )/sizeof( levelsWanted[0] );

D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_REFERENCE
};
UINT numDriverTypes = sizeof( driverTypes ) / sizeof( driverTypes[0] );


// Create the D3D device and the swap chain
HRESULT hr = D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
0,
levelsWanted,
numLevelWanted,
D3D11_SDK_VERSION,
// NULL, NULL, NULL, NULL, NULL );
&swapChainDesc,
&pSwapChain,
&pD3DDevice,
&level,
&pD3DViewPort
);


// Error checking. Make sure the device was created
if (FAILED(hr))
{
MessageBox(hWnd, TEXT("A DX11 Compliant Video Card is Required"), TEXT("ERROR"), MB_OK);
return false;
}

// Get the back buffer from the swapchain
ID3D11Texture2D *pBackBuffer;
hr = pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
if (FAILED(hr))
{
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 (FAILED(hr))
{
return false;
}

// set the render target
pD3DViewPort->OMSetRenderTargets(1, &pRenderTargetView, NULL);

// create and set the viewport
D3D11_VIEWPORT viewPort;
viewPort.Width = (float)width;
viewPort.Height = (float)height;
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;
pD3DViewPort->RSSetViewports(1, &viewPort);

return true;
}

/*******************************************************************
* ShutdownDirect3D
* Closes down and releases the resources for Direct3D
* Inputs - void
* Outputs - void
*******************************************************************/
void ShutdownDirect3D()
{
// release the rendertarget
if (pRenderTargetView)
{
pRenderTargetView->Release();
}

// release the swapchain
if (pSwapChain)
{
pSwapChain->Release();
}

// release the D3D Device
if (pD3DDevice)
{
pD3DDevice->Release();
}

if (pD3DViewPort)
{
pD3DViewPort->Release();
}
}

/*******************************************************************
* Render
* All drawing happens in the Render function
* Inputs - void
* Outputs - void
*******************************************************************/
void Render()
{
if (pD3DViewPort != NULL)
{
// clear the target buffer
pD3DViewPort->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f));

// display the next item in the swap chain
pSwapChain->Present(0, 0);
}
}

Share this post


Link to post
Share on other sites
Advertisement
Yes i double checked the links. Currently using vc2010 if it helps.

[Edited by - DNewguy on July 4, 2010 2:17:07 PM]

Share this post


Link to post
Share on other sites
I pasted your code into VC++ 2008, and got the same error as you had, but when linking to d3d11.lib it worked fine. Try adding #pragma comment(lib, "d3d11.lib") at the top of your code. If that doesn't work, make sure your directories are set correctly, but if they aren't you should get another error than that...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!