C++ error

Started by
3 comments, last by eltharynd 10 years, 6 months ago

G'day... I'm trying to develop a little engine in C++ but i got a strange error i couldn't explain... here's the code:

Engine.h


#ifndef __ENGINE_H__
#define __ENGINE_H__
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#include "Settings.h"

class Engine
{
public:
	Engine(HWND HWnd, Settings set);
	~Engine();

	void InitD3D();
	void CleanD3D();
	void RenderFrame();

	IDXGISwapChain* pSwapChain;
	DXGI_SWAP_CHAIN_DESC scd;
	ID3D11Device* pDevice;
	ID3D11DeviceContext* pDeviceContext;
	ID3D11RenderTargetView* pBackBuffer;
	Settings set;
	HWND hWnd;
};
#endif

Engine.cpp


#include "Engine.h"

Engine::Engine(HWND HWnd, Settings sett)
{
	hWnd = HWnd;
	set = sett;
}

void Engine::InitD3D()
{
	//create the description of the swwap chain
	ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
	scd.BufferCount = 1;                                    // one back buffer
	scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;     // use 32-bit color
	scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;      // how swap chain is to be used
	scd.OutputWindow = hWnd;                                // the window to be used
	scd.SampleDesc.Count = set.mapSettings["MULTI_SAMPLING"];							   // how many multisamples
	scd.Windowed = set.mapSettings["WINDOWED"];

	//create device, dev con and swap chain
	D3D11CreateDeviceAndSwapChain(NULL,
		D3D_DRIVER_TYPE_HARDWARE,
		NULL,
		NULL,
		NULL,
		NULL,
		D3D11_SDK_VERSION,
		&scd,
		&pSwapChain,
		&pDevice,
		NULL,
		&pDeviceContext);

	//get the adress of the back buffer
	ID3D11Texture2D* pTexture;
	pSwapChain -> GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pTexture);
	pTexture -> Release();

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

	//set the viewport
	D3D11_VIEWPORT viewport;
	ZeroMemory (&viewport, sizeof(D3D11_VIEWPORT));
	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	viewport.Width = set.SCREEN_AREA.right;
	viewport.Height = set.SCREEN_AREA.bottom;

	pDeviceContext -> RSSetViewports(1, &viewport);
		
}

void Engine::RenderFrame()
{
	//clear the back buffer to black
	pDeviceContext -> ClearRenderTargetView(pBackBuffer, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f));

	//3D rendering goes here

	//switch the back buffer and the front buffer
	pSwapChain -> Present (0,0);
}

Engine::~Engine()
{
	CleanD3D();
}

void Engine::CleanD3D()
{
	pSwapChain -> Release();
	pBackBuffer -> Release();
	pDevice -> Release();
	pDeviceContext -> Release();
}

when the application starts i get a debugger error as soon as it gets to this line:

pDeviceContext -> OMSetRenderTargets(1,&pBackBuffer, NULL);

I'm still pretty new to Directx and I havn't been programming a lot in C++ lately :/

I guess I just can't find the error.... could any of you help me?

Advertisement

never mind... i must have deleted something when i was trying to solve it....

here's the solution


//get the adress of the back buffer
	ID3D11Texture2D* pTexture;
	pSwapChain -> GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pTexture);
	// use the back buffer address to create the render target
	pDevice ->CreateRenderTargetView(pTexture, NULL, &pBackBuffer);
	pTexture -> Release();
	//set the render target as the back buffer
	pDeviceContext -> OMSetRenderTargets(1,&pBackBuffer, NULL);

Maybe D3D11CreateDeviceAndSwapChain failed and your device context is not initialized properly. You should definitely check your result values (i.e.):


HRESULT hr = D3D11CreateDeviceAndSwapChain(NULL,
		D3D_DRIVER_TYPE_HARDWARE,
		NULL,
		NULL,
		NULL,
		NULL,
		D3D11_SDK_VERSION,
		&scd,
		&pSwapChain,
		&pDevice,
		NULL,
		&pDeviceContext);

if (FAILED(hr))
  // do your error handling

Oh, ok.. I see! :) But check your result values anyway.

yeah you probably are right :P but i check them via debugger till they work and then add error handling code normally... i should loose this habit

This topic is closed to new replies.

Advertisement