Archived

This topic is now archived and is closed to further replies.

GeekPlusPlus

Problem with direct3d init... (code supplied)

Recommended Posts

I''m hoping someone here can tell me why my init function is failing at the device creation? It compiles fine, but m_pDevice is unable to be created and initilization returns 2 (I put in the ints so I knew where it was closing).
int CGraphics::Initilize(int iWidth, int iHeight, D3DFORMAT Format, HWND hWnd)
{
	HRESULT hr;

	m_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
	if(!m_pDirect3D)
		return 1;

	ZeroMemory(&m_D3DCaps, sizeof(m_D3DCaps));
	if(FAILED(m_pDirect3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_D3DCaps)))
		return false;

	D3DDISPLAYMODE	displayMode;	// Holds Monitor Display Mode


	hr = m_pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
	if(FAILED(hr))
		return 4;

	// and here are the D3D present parameters

	//ZeroMemory(&m_D3DPresent, sizeof(m_D3DPresent));

	//m_D3DPresent.SwapEffect			= D3DSWAPEFFECT_DISCARD;

	//m_D3DPresent.SwapEffect		= D3DSWAPEFFECT_FLIP;

	m_D3DPresent.hDeviceWindow		= hWnd;
	m_D3DPresent.Windowed			= TRUE;
	m_D3DPresent.SwapEffect			= D3DSWAPEFFECT_DISCARD;
	m_D3DPresent.BackBufferCount	= 1;
	m_D3DPresent.BackBufferFormat	= displayMode.Format;
	m_D3DPresent.BackBufferWidth	= displayMode.Width;
	m_D3DPresent.BackBufferHeight	= displayMode.Height;
	//m_D3DPresent.BackBufferFormat	= Format;


    // lets see if vertex processing is available...

    if(m_D3DCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
		hr = m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,
			D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_D3DPresent, &m_pDevice);        
	else
		hr = m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,
			D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_D3DPresent, &m_pDevice);

	if(FAILED(hr))
		return 2;

	m_pDevice->SetVertexShader(NULL);
	m_pDevice->SetFVF(D3DFVF_VERTEX);
	m_pDevice->CreateVertexBuffer(sizeof(VERTEX) * 4, NULL, D3DFVF_VERTEX, D3DPOOL_MANAGED, &m_pVertexBuffer, NULL);
	m_pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(VERTEX));

    // last but not least... rendering states...

	m_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
	m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

    return 3;
}
What is really weird is the following code worked fine in another program I did, and they are a different setup, but I don''t see why it would fail above...
int Direct3DInit()
{
	g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION); // Create Direct 3D

	if (g_pDirect3D == NULL)						// Note: Add d3d.lib!

		return false;

	D3DDISPLAYMODE	displayMode;	// Holds Monitor Display Mode


	// If not full screen then fill displayMode with current settings.

	if(g_bFullScreen == false)
	{
		g_hr = g_pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
		if(FAILED(g_hr))
			return false;
	}
	else
	{
		displayMode.Width		= WND_WIDTH;
		displayMode.Height		= WND_HEIGHT;
		displayMode.RefreshRate	= 0;	// default

		// Set the color format to 565 16 bit

		displayMode.Format		= D3DFMT_R5G6B5;
	}

	// Tells Direct3D how it''s supposed to show shit, then nulls it.

	D3DPRESENT_PARAMETERS presentParameters;
	memset(&presentParameters, 0, sizeof(D3DPRESENT_PARAMETERS));

	// Set Window Mode & Other Present Paramaters for D3D

	if(g_bFullScreen == false)
		presentParameters.Windowed	= TRUE;
	else
		presentParameters.Windowed	= FALSE;

	presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
	presentParameters.BackBufferFormat = displayMode.Format;
	presentParameters.BackBufferWidth = displayMode.Width;
	presentParameters.BackBufferHeight = displayMode.Height;

	// Create our D3D device

	g_hr = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters,  &g_pDevice);
	
	if(FAILED(g_hr))
		return false;

	// Matrix code

	D3DXMATRIX mat;

	D3DXMatrixPerspectiveFovLH(&mat, D3DX_PI/6, WND_WIDTH/WND_HEIGHT, 1.0, 100.0 );
	g_pDevice->SetTransform(D3DTS_PROJECTION, &mat );

	D3DXMatrixIdentity(&mat);
	g_pDevice->SetTransform(D3DTS_WORLD, &mat);

	D3DXMatrixTranslation(&mat, 0, 0, 14.0);
	g_pDevice->SetTransform(D3DTS_VIEW, &mat);

	// Setup Vertex Buffers

	g_pDevice->CreateVertexBuffer(sizeof(CVertex)*4, NULL, D3DFVF_CVertex, D3DPOOL_MANAGED, &g_pVertexBuffer, NULL);

	CVertex *vtxQuad;
	g_pVertexBuffer->Lock(0, 0, (void**)&vtxQuad, D3DLOCK_DISCARD);

	// Create Object Vertices

	vtxQuad[0].Create(0, 0, 0, 1, 1, 1, 0, 1);
	vtxQuad[1].Create(0, 2, 0, 1, 1, 1, 0, 0);
	vtxQuad[2].Create(2, 0, 0, 1, 1, 1, 1, 1);	// Front Face

	vtxQuad[3].Create(2, 2, 0, 1, 1, 1, 1, 0);
	
	vtxQuad[4].Create(0, 0, 2, 1, 1, 1, 0, 1);
	vtxQuad[5].Create(0, 2, 2, 1, 1, 1, 0, 0);
	vtxQuad[6].Create(2, 0, 2, 1, 1, 1, 1, 1);	// Back Face

	vtxQuad[7].Create(2, 2, 2, 1, 1, 1, 1, 0);

	//vtxQuad[8].Create(2, 0, 0, 0, 1, 1, 0, 1);

	//vtxQuad[9].Create(2, 2, 0, 0, 1, 0, 0, 0);

	//vtxQuad[10].Create(2, 0, 2, 0, 0, 1, 1, 1);	// Right Side

	//vtxQuad[11].Create(2, 2, 2, 0, 0, 0, 1, 0);


	//vtxQuad[12].Create(0, 0, 0, 0, 1, 1, 0, 1);

	//vtxQuad[13].Create(0, 2, 0, 0, 1, 0, 0, 0);

	//vtxQuad[14].Create(0, 0, 2, 0, 0, 1, 1, 1);	// Left Side

	//vtxQuad[15].Create(0, 2, 2, 0, 0, 0, 1, 0);


	//vtxQuad[12].Create(0, 0, 2, 0, 0, 1, 0, 1);

	//vtxQuad[13].Create(0, 1, 2, 0, 0, 0, 0, 0);

	//vtxQuad[14].Create(0, 1, 2, 0, 0, 0, 0, 0);

	//vtxQuad[14].Create(0, 1, 2, 0, 0, 0, 0, 0);


	//vtxQuad[12].Create(0, 0, 1, 0, 0, 1, 0, 1);

	//vtxQuad[13].Create(0, 1, 1, 0, 0, 0, 0, 0);

	//vtxQuad[14].Create(0, 1, 1, 0, 0, 0, 0, 0);

	//vtxQuad[14].Create(0, 1, 1, 0, 0, 0, 0, 0);


	g_pVertexBuffer->Unlock();

	g_pDevice->CreateIndexBuffer(36*sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIndexBuffer, NULL);

	WORD cube_indices[18] = {0,1,2,1,2,3,
							4,5,6,5,6,7/*,
							8,9,10,9,10,11/*,
							12,13,14,13,14,15*/};

	VOID* pIndices;
	g_pIndexBuffer->Lock(0, sizeof(cube_indices), &pIndices, 0);
    
	// copy over our index information into our index buffer

	memcpy(pIndices, cube_indices, sizeof(cube_indices));

	// now unlock the index buffer so that we can use it

	g_pIndexBuffer->Unlock();


	// Set it up to blend between the vertex color and the texture color

	g_pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	g_pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	g_pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	// Turn off the alpha channel since the texture doesn''t have one

	g_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);

	// Load our Texture

	D3DXCreateTextureFromFile(g_pDevice, "blstar.jpg", &g_pTexture);
	g_pDevice->SetTexture(0, g_pTexture);

	g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

	return true;
}
- Newb Programmer: Geek++

Share this post


Link to post
Share on other sites
Oh, thees are the CGraphic member variables, don''t know if they are needed...


private:

D3DPRESENT_PARAMETERS m_D3DPresent;
D3DCAPS9 m_D3DCaps;
IDirect3D9 *m_pDirect3D;
IDirect3DDevice9 *m_pDevice;
IDirect3DVertexBuffer9 *m_pVertexBuffer;
};


- Newb Programmer: Geek++

Share this post


Link to post
Share on other sites
The problem is that you commented out the code that zeroes out the present parameters. Since you aren''t explcitly setting each field, the unspecified fields have garbage in them. Put back the ZeroMemory() call and all should be well.

Share this post


Link to post
Share on other sites
Well... that was easy...

Hopefully this next problem is too. I don''t know why Direct3D is giving me such problems, I already did the spinning cube just fine and understood all that stuff. I thought this 2D would be easier... anyway...

This is my Graphics.h below. Using that in a window, can someone tell me why Direct3D isn''t turning my backgrond Gray?

The windows background is set to black when it''s created, but if I do

CGraphics DX;
DX.Initilize(Window.Width(), Window.Height(), D3DFMT_A8R8G8B8, Window.GetHWND())
DX.ClearBuffer();
DX.Present();


The clear should make the back buffer gray, and then present would stick it in front right?

here''s the stff:


#ifndef __Graphics_H_
#define __Graphics_H_

// my custom vertex

class VERTEX
{
public:
float x;
float y;
float z;
float rhw;
D3DCOLOR colour;
float u;
float v;
};

const DWORD D3DFVF_VERTEX = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;

class CGraphics
{
public:
CGraphics() {}
~CGraphics();

inline void ClearBuffer() { m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(170, 170, 170), 1.0f, 0); }
inline void Present() { m_pDevice->Present(NULL, NULL, NULL, NULL); }
inline void BeginDrawing() { m_pDevice->BeginScene(); }
inline void EndDrawing() { m_pDevice->EndScene(); }

bool Initilize(int iWidth, int iHeight, D3DFORMAT Format, HWND hWnd);
void Close();

private:

D3DPRESENT_PARAMETERS m_D3DPresent;
D3DCAPS9 m_D3DCaps;
IDirect3D9 *m_pDirect3D;
IDirect3DDevice9 *m_pDevice;
IDirect3DVertexBuffer9 *m_pVertexBuffer;
};

CGraphics::~CGraphics()
{
}

bool CGraphics::Initilize(int iWidth, int iHeight, D3DFORMAT Format, HWND hWnd)
{
HRESULT hr;

m_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
if(!m_pDirect3D)
return false;

ZeroMemory(&m_D3DCaps, sizeof(m_D3DCaps));
if(FAILED(m_pDirect3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_D3DCaps)))
return false;

D3DDISPLAYMODE displayMode; // Holds Monitor Display Mode


hr = m_pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
if(FAILED(hr))
return false;

// and here are the D3D present parameters

ZeroMemory(&m_D3DPresent, sizeof(m_D3DPresent));
//m_D3DPresent.SwapEffect = D3DSWAPEFFECT_DISCARD;

//m_D3DPresent.SwapEffect = D3DSWAPEFFECT_FLIP;

m_D3DPresent.hDeviceWindow = hWnd;
m_D3DPresent.Windowed = TRUE;
m_D3DPresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
m_D3DPresent.BackBufferCount = 1;
//m_D3DPresent.BackBufferFormat = displayMode.Format;

m_D3DPresent.BackBufferWidth = displayMode.Width;
m_D3DPresent.BackBufferHeight = displayMode.Height;
m_D3DPresent.BackBufferFormat = Format;

// lets see if vertex processing is available...

if(m_D3DCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
hr = m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_D3DPresent, &m_pDevice);
else
hr = m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_D3DPresent, &m_pDevice);

//hr = m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,

// D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_D3DPresent, &m_pDevice);


if(FAILED(hr))
return false;

m_pDevice->SetVertexShader(NULL);
m_pDevice->SetFVF(D3DFVF_VERTEX);
m_pDevice->CreateVertexBuffer(sizeof(VERTEX) * 4, NULL, D3DFVF_VERTEX, D3DPOOL_MANAGED, &m_pVertexBuffer, NULL);
m_pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(VERTEX));

// last but not least... rendering states...

m_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
m_pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
m_pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

return true;
}

void CGraphics::Close()
{
//if(m_pVertexBuffer)

// m_pVertexBuffer->Release();

m_pVertexBuffer = NULL;

if(m_pDevice)
m_pDevice->Release();
m_pDevice = NULL;

if(m_pDirect3D)
m_pDirect3D->Release();
m_pDirect3D = NULL;

}

#endif


- Newb Programmer: Geek++

Share this post


Link to post
Share on other sites
Hrm, I figred it out.

Aparently droping my bttons on the window AFTER doing the Direct3D stuff covers it up.

I just movied this:

MoveWindow(butt_single, 300, 200, 250, 25,true);


above the Direct3D stuff and it works fine.


- Newb Programmer: Geek++

Share this post


Link to post
Share on other sites