Initializing Direct3D - Swap Chains (DX11)

Hello all, back to ask a question.

So, I have been reading Frank Luna's book on Direct3D 11, and have run into a bit of trouble. When I go to initialize Direct3D (code posted below), the swap chain fails to be created. After searching the internet for a couple days for a solution, I figured that I could ask here.

I tried Frank Luna's method for creating it - no dice. So, I went back to a project I made a couple months ago when I originally tried to learn DirectX (it is mostly just copied code off of directxtutorials.com). Hmm. Nothing. The device, device context, and swap chain fail. I tried the Rastertek method. Still. Nothing. Then I go back to a project I made around the same time I made the other one. When I build that project, everything works fine.

So, I completely revised my program (the one I started a week ago, when beginning the book). I used the Main Window stuff from the book (stuff like calculating the frame stats and setting the window title, using "PeekMessage() instead of "GetMessage()", etc), but then implemented the graphics stuff. Well, even with the tested, confirmed code, I get nothing. By all means, it had different code throughout the program, but the code associated with the 3D initialization was all the same.
Hmm.. Starting to get a little wordy. So, I will post the code of my Init function below, and if it isn't enough, I will post more. So...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
bool Application::InitD3D(){
	D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0 };
	D3D_FEATURE_LEVEL featureLevelOut;
	UINT numFeatureLevels = _countof(featureLevels);

	// Create a struct to hold information about the swap chain
	DXGI_SWAP_CHAIN_DESC scd;
	
	//Clear out the chain for use
	SecureZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
	scd.BufferCount = 1;																
	scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	scd.BufferDesc.Width = mWndWidth;
	scd.BufferDesc.Height = mWndHeight;
	scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	scd.OutputWindow = mHwnd;
	scd.SampleDesc.Count = 1;
	scd.SampleDesc.Quality = 0;
	scd.Windowed = TRUE;
	scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

        // I feel like I am calling this wrong... 
	D3D11CreateDeviceAndSwapChain(NULL,
		D3D_DRIVER_TYPE_HARDWARE,
		NULL,
		0,
		NULL,
		NULL,
		D3D11_SDK_VERSION,
		&scd,
		&mSwapChain,
		&mDevice,
		NULL,
		&mDeviceContext
		);

// Commented this one out, just haven't omitted it yet.
	/*D3D11CreateDevice(
		NULL,
		D3D_DRIVER_TYPE_HARDWARE,
		NULL,
		0,
		&featureLevels[0],
		numFeatureLevels,
		D3D11_SDK_VERSION,
		&mDevice,
		&featureLevelOut,
		&mDeviceContext
		);*/

	if (!mDevice){
		MessageBoxA(0, "mDevice Failed!", 0, 0);
		return false;
	}
	if (!mDeviceContext){
		MessageBoxA(0, "mDeviceContext Failed!", 0, 0);
		return false;
	}
        // Also commented this out. Probably should have omitted it too.
	/*IDXGIFactory* factory;
	CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
	IUnknown* device = nullptr;
	device = static_cast<IUnknown*>(device);
	factory->CreateSwapChain(device, &scd, &mSwapChain);*/
	
	// BOOM! Direct3D == failed to initialize
	if (!mSwapChain){
		MessageBoxA(0, "mSwapChain Failed!", 0, 0);
		return false;
	}

	// Set Render Target
	ID3D11Texture2D *pBackBuffer = 0;
	mSwapChain->GetBuffer(0, _uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pBackBuffer));
	mDevice->CreateRenderTargetView(pBackBuffer, NULL, &mRenderTargetView);
	pBackBuffer->Release();

	// Set RenderTarget as back buffer
	mDeviceContext->OMSetRenderTargets(1, &mRenderTargetView, NULL);

	// Set Viewport
	D3D11_VIEWPORT viewport;
	ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	viewport.Width = mWndWidth;
	viewport.Height = mWndHeight;

	mDeviceContext->RSSetViewports(1, &viewport);

	return true;
}


Anyway, sorry about the long post. Just thought I should try to explain the situation
Topic archived. No new replies allowed.