Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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;

//to initialize and prepare the direct3D for use
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;

	//creating a device using above information
	D3D11CreateDeviceAndSwapChain
		(
		NULL,
		D3D_DRIVER_TYPE_HARDWARE,
		NULL,
		NULL,
		NULL,
		NULL,
		D3D11_SDK_VERSION,
		&scd,
		&swapChain,
		&dev,
		NULL,
		&devcon
		);

	//get the address of the back buffer
	ID3D11Texture2D *pBackBuffer;
	swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
	//swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)pBackBuffer);

	//create the render target using back buffer address
	dev->CreateRenderTargetView(pBackBuffer, NULL, &backBuffer);
	pBackBuffer->Release();

	//set the render target as the back buffer
	devcon->OMSetRenderTargets(1, &backBuffer, NULL);

	//set the viewport
	D3D11_VIEWPORT viewPort;
	ZeroMemory(&viewPort, sizeof(D3D11_VIEWPORT));

	viewPort.TopLeftX = 0;
	viewPort.TopLeftY = 0;
	viewPort.Width = 500;
	viewPort.Height = 400;

	devcon->RSSetViewports(1, &viewPort);
}

//to render a single frame
void renderFrame()
{
	devcon->ClearRenderTargetView(backBuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));

	//switch the backbuffer and the frontbuffer
	swapChain->Present(0, 0);
}

//to cleanup direct3D and COM
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);

			//break the loop if message is quite
			if (msg.message == WM_QUIT)
				break;
		}
		else
		{
			//everything 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

C++
D3D11CreateDeviceAndSwapChain
		(
		NULL,
		D3D_DRIVER_TYPE_HARDWARE,
		NULL,
		NULL,
		NULL,
		NULL,
		D3D11_SDK_VERSION,
		&scd,
		&swapChain,
		&dev,
		NULL,
		&devcon
		);


as this

C++
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.
Posted
Comments
KarstenK 21-Jul-14 9:13am    
check the return values and make some debug output in your code.

is mixing v10 and v11 a good idea?

#include <d3dx11.h>
#include <d3dx10.h>

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

PS: consider switching to OpenGL. It is a industry standard on most platforms and really fast. ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900