Problem initializing DirectX

Hi, I have a problem when initializing DirectX. The code is as follows:

[code]#include
#include
#include
#include
#include

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

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600

//global declarations
IDXGISwapChain *swapChain; //the pointer to the swap chain interface
ID3D11Device *device; //the pointer to the Direct3D device interface
ID3D11DeviceContext *deviceContext; //the pointer to the Direct3D device context
ID3D11RenderTargetView *backBuffer; //the pointer to the back buffer

//function prototypes
void InitD3D(HWND hWnd);
void RenderFrame(void);
void CleanD3D(void);

//the message handler function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

//the entry point for the Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd; //handle to the window
WNDCLASSEX wc; //holds information for the window class

ZeroMemory(&wc, sizeof(WNDCLASSEX)); // clear out the window class for use

//fill in the window class struct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";

RegisterClassEx(&wc); //register the window class

RECT wr = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT}; //set the size of the client area of the window
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); //adjust the size

hWnd = CreateWindowEx(NULL,
L"WindowClass1", //name of the window class
L"Our First Windowed Program", //title of the window
WS_OVERLAPPEDWINDOW, //window style
300, //x-pos of the window
300, //y-pos of the window
wr.right - wr.left, //width of the window
wr.bottom - wr.top, //height of the window
NULL, //we have no parent window, NULL
NULL, //we aren't using menus, NULL
hInstance, //application handle
NULL); //used with multiple windows, NULL

ShowWindow(hWnd, nCmdShow); //display the window on the screen

//ENTER THE MAIN LOOP:

MSG msg; //holds Windows event messages

//enter the infinite message loop
while(TRUE)
{
//check to see if any messages are waiting in the queue
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//translate keystroke messages into the right format
TranslateMessage(&msg);
//send the message to the WindowsProc function
DispatchMessage(&msg);
//check to see if it's time to quit
if (msg.message == WM_QUIT)
break;
}
else
{
RenderFrame();
}
}
}

//this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//sort through and find what code to run for the message given
switch (message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

//handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);
}

//this function initializes and prepares Direct3D for use
void InitD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd; //a struct holding information about the swap chain
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

//fill the swap chain description struct
scd.BufferCount = 1; //one back buffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; //use 32-bit color
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; //use swap chain as a render target
scd.OutputWindow = hWnd; //the window to be used
scd.SampleDesc.Count = 4; //how many multisamples
scd.Windowed = TRUE; //windowed/full-screen mode

//create a device, device context and swap chain using the information in the scd struct
D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapChain,
&device,
NULL,
&deviceContext);

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

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

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

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

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = WINDOW_WIDTH;
viewport.Height = WINDOW_HEIGHT;
deviceContext->RSSetViewports(1, &viewport);
}

//this function cleans up Direct3D and COM
void CleanD3D(void)
{
//close and release all existing COM objects
swapChain->Release();
backBuffer->Release();
device->Release();
deviceContext->Release();
}

//this is the function used to render a single frame
void RenderFrame(void)
{
//clear the back buffer to a deep blue
deviceContext->ClearRenderTargetView(backBuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));

//do 3D rendering on the back buffer here

//switch the back buffer and the front buffer
swapChain->Present(0, 0);
}[/code]

I get no compiler errors but when I lunch the program it crashes with the following exception:

[italic]Unhandled exception at 0x779f15de in DirectX_Test.exe: 0xC0000005: Access violation reading location 0x00000000.[/italic]

When I remove the ClearRenderTargetView() function call in the RenderFrame() function the application wont crash. Any suggestions?

Best Regards
Robin

Comments

  • Noticed I just forgot to actually call InitD3D (and CleanD3D for that matter) before calling RenderFrame(). How silly of me ^^
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion