Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


C++ error


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 eltharynd   Members   -  Reputation: 123

Like
0Likes
Like

Posted 25 September 2013 - 06:22 AM

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?

 

 



Sponsor:

#2 eltharynd   Members   -  Reputation: 123

Like
0Likes
Like

Posted 25 September 2013 - 06:30 AM

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

Edited by eltharynd, 25 September 2013 - 06:34 AM.


#3 David_pb   Members   -  Reputation: 679

Like
0Likes
Like

Posted 25 September 2013 - 06:31 AM

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

@D13_Dreinig

#4 David_pb   Members   -  Reputation: 679

Like
0Likes
Like

Posted 25 September 2013 - 06:33 AM

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


@D13_Dreinig

#5 eltharynd   Members   -  Reputation: 123

Like
0Likes
Like

Posted 25 September 2013 - 06:37 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS