Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

DirectX initializing problem

$
0
0

Here is the code Ive used to initialize directX window

#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);
}

But when I run this, it breaks..

Here is a screnshot of that error

But when I replace "D3D_DRIVER_TYPE_HARDWARE" with "D3D_DRIVER_TYPE_REFERENCE" it runs without errors except it's just a normal window..

Why this happens ? What's the wrong with my code ? As I read in the tutorial, the window background must be bluish and it used "D3D_DRIVER_TYPE_HARDWARE"..

Thanks in advance.


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>