#include <Windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <D3DX11.h>
#include <D3DX10.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")
IDXGISwapChain *swapChain;
ID3D11Device *dev;
ID3D11DeviceContext *devcon;
ID3D11RenderTargetView* backBuffer;
void initD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd;
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
scd.BufferCount = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.OutputWindow = hWnd;
scd.SampleDesc.Count = 4;
scd.Windowed = TRUE;
D3D11CreateDeviceAndSwapChain
(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapChain,
&dev,
NULL,
&devcon
);
ID3D11Texture2D *pBackBuffer;
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
dev->CreateRenderTargetView(pBackBuffer, NULL, &backBuffer);
pBackBuffer->Release();
devcon->OMSetRenderTargets(1, &backBuffer, NULL);
D3D11_VIEWPORT viewPort;
ZeroMemory(&viewPort, sizeof(D3D11_VIEWPORT));
viewPort.TopLeftX = 0;
viewPort.TopLeftY = 0;
viewPort.Width = 500;
viewPort.Height = 400;
devcon->RSSetViewports(1, &viewPort);
}
void renderFrame()
{
devcon->ClearRenderTargetView(backBuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
swapChain->Present(0, 0);
}
void cleanD3D(void)
{
swapChain->Release();
dev->Release();
devcon->Release();
}
LRESULT CALLBACK winproc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = winproc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"windowclass";
RegisterClassEx(&wc);
RECT clientRect = { 0, 0, 500, 400 };
AdjustWindowRect(&clientRect, WS_OVERLAPPEDWINDOW, FALSE);
HWND hWnd = CreateWindowEx(NULL, L"windowclass", L"window",
WS_OVERLAPPEDWINDOW,
10, 10,
clientRect.right - clientRect.left,
clientRect.bottom - clientRect.top,
NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, cmdShow);
MSG msg;
while (true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
break;
}
else
{
}
}
return 0;
}
LRESULT CALLBACK winproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_QUIT)
{
cleanD3D();
}
if (message == WM_CREATE)
{
initD3D(hWnd);
renderFrame();
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
Hi.. Im beginner to DirectX programming..
And above is the code I've tried to implement a directX window..
But when I run it, it stops with an error.
It says
"Unhandled exception at 0x00EB167C in myprogram.exe: 0xC000041D: An unhandled exception was encountered during a user callback."
if I change this
D3D11CreateDeviceAndSwapChain
(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapChain,
&dev,
NULL,
&devcon
);
as this
D3D11CreateDeviceAndSwapChain
(
NULL,
D3D_DRIVER_TYPE_REFERENCE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapChain,
&dev,
NULL,
&devcon
);
it works fine..
But the client area is normal. I mean it's not bluish or something like what's in the tutorial. It colored as a normal window..
What's the wrong with my code ? or is there anything wrong with my hardware ?
Thanks in advance.