Hi Guys,
I have been working on some directX tutorials and am having trouble with one step. I am setting up separate functions for each step of my program, and Visual Studio crashes when it reaches my Render() function. Using the debugger I can see that my backBufferTarget_ has a value of 0x00000000. So even though I think I am initializing it in InitD3D(), it is not remembering or something. My code is below, if anyone has an advice that would be greatly appreciated. I am sure it is a simple thing I am missing.
#include<Windows.h>
#include<memory>
#include<xnamath.h>
#include<d3d11.h>
#include<d3dx11.h>
#include<DxErr.h>
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "dxerr.lib")
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
HWND hwnd = NULL;
HINSTANCE hInstance = NULL;
ID3D11DeviceContext* d3dContext_ = NULL;
ID3D11Device* d3dDevice_ = NULL;
IDXGISwapChain* swapChain_ = NULL;
ID3D11RenderTargetView* backBufferTarget_ = NULL;
//********************
//Function prototypes*
//********************
void MessagePump(void);
bool GetFullScreen(void);
bool InitWindow(void);
void KillWindow(void);
bool InitD3D(void);
void Render(void);
bool InitScene(void);
bool InitObjects(void);
bool progFinished = FALSE;
bool progFullScreen = FALSE;
#define APP_NAME "Kenneth Game"
//************************
//Application entry point*
//************************
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmdLine, int cmdShow)
{
if (!GetFullScreen())
{
OutputDebugString("User abort\n");
exit(5);
}
InitWindow();
InitD3D();
InitScene();
while (!progFinished)
{
MessagePump(); //Check for window messages
Render(); //Draw our graphics
}
KillWindow(); //Unload resources
return 0;
}
//****************************************************************************************
//Initialise a window (full-screen or otherwise) in which our graphics will be displayed.*
//****************************************************************************************
bool InitWindow(void)
{
//UNREFERENCED_PARAMETER(prevInstance);
//UNREFERENCED_PARAMETER(cmdLine);
WNDCLASSEX wndClass = { 0 };
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.hInstance = hInstance;
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = "DX11BookWindowClass";
if (!RegisterClassEx(&wndClass))
return false;
RECT rc = { 0, 0, 640, 480 };
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
HWND hwnd = CreateWindowA("DX11BookWindowClass", "Blank Win32 Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);
if (!hwnd)
return false;
ShowWindow(hwnd, SW_SHOW);
//UpdateWindow(hwnd);
return true;
}
//*************************************************
//Terminate the window that was previously opened.*
//*************************************************
void KillWindow(void)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
DispatchMessage(&msg);
}
//ghAppWindow = NULL;
ShowCursor(true);
}
//***************************
//Windows message processor.*
//***************************
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paintStruct;
HDC hDC;
switch (message)
{
case WM_PAINT:
hDC = BeginPaint(hwnd, &paintStruct);
EndPaint(hwnd, &paintStruct);
break;
case WM_DESTROY:
progFinished = true;
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
//**********************************************
//Process any messages that Windows has sent us*
//**********************************************
void MessagePump(void)
{
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//*************************************
//Prompt selection of full screen mode*
//*************************************
bool GetFullScreen(void)
{
int iResult;
bool bRet = true;
iResult = MessageBox(NULL, "Run fullscreen?", APP_NAME, MB_YESNOCANCEL | MB_ICONQUESTION);
switch (iResult)
{
case IDCANCEL:
bRet = false;
break;
case IDNO:
progFullScreen = false;
break;
case IDYES:
progFullScreen = true;
break;
case 0:
OutputDebugString("Couldn't open MessageBox, closing");
exit(10);
break;
}
return bRet;
}
//**************************************
//Creates a hardware device in Direct3D*
//**************************************
bool InitD3D(void)
{
D3D_DRIVER_TYPE driverType_;
D3D_FEATURE_LEVEL featureLevel_;
RECT dimensions;
GetClientRect(hwnd, &dimensions);
unsigned int width = dimensions.right - dimensions.left;
unsigned int height = dimensions.bottom - dimensions.top;
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE, D3D_DRIVER_TYPE_SOFTWARE
};
unsigned int totalDriverTypes = ARRAYSIZE(driverTypes);
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
unsigned int totalFeatureLevels = ARRAYSIZE(featureLevels);
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
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;
swapChainDesc.Windowed = true;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
unsigned int creationFlags = 0;
#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
HRESULT result;
unsigned int driver = 0;
for (driver = 0; driver < totalDriverTypes; ++driver)
{
result = D3D11CreateDeviceAndSwapChain(0, driverTypes[driver], 0, creationFlags, featureLevels, totalFeatureLevels, D3D11_SDK_VERSION, &swapChainDesc, &swapChain_,
&d3dDevice_, &featureLevel_, &d3dContext_);
if (SUCCEEDED(result))
{
driverType_ = driverTypes[driver];
break;
}
}
if (FAILED(result))
{
DXTRACE_MSG("Failed to create the Direct3D device!");
return false;
}
ID3D11Texture2D* backBufferTexture;
result = swapChain_->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferTexture);
if (FAILED(result))
{
DXTRACE_MSG("Failed to get the swap chain back buffer!");
return false;
}
result = d3dDevice_->CreateRenderTargetView(backBufferTexture, 0, &backBufferTarget_);
if (backBufferTexture)
backBufferTexture->Release();
if (FAILED(result))
{
DXTRACE_MSG("Failed to create the render target view!");
return false;
}
d3dContext_->OMSetRenderTargets(1, &backBufferTarget_, 0);
D3D11_VIEWPORT viewport;
viewport.Width = static_cast<float>(width);
viewport.Height = static_cast<float>(height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
d3dContext_->RSSetViewports(1, &viewport);
return true;
}
//****************************************************
// Initialise DirectX ready for us to start rendering*
//****************************************************
bool InitScene(void)
{
return true;
}
//*******************************************
//Initialise the 3d objects we will be using*
//*******************************************
bool InitObjects(void)
{
return true;
}
void Render(void)
{
float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f };
d3dContext_->ClearRenderTargetView(backBufferTarget_, clearColor);
swapChain_->Present(0, 0);
}