VertBuffer-GetDesc() crashing my program, code included

Started by
1 comment, last by Defend 18 years, 3 months ago
I'm getting some strange errors trying to use DrawPrimitive() while looping around to switch the current vertex buffer and FVF. When I do try to change VBs and FVFs, usually the contents of the first VB aren't displayed, or display is messed up, moved, or everything disappears... stuff like that. Nothing actually crashes, the program keeps running fine, just the display isn't right. However, I tried adding a call to VertexBuffer->GetDesc(). My program doesn't need it, but I wanted to check the VB. Now GetDesc() actually crashes the program, so I've written a basic Draw-A-Single-Quad replacement for my rendering code. GetDesc() still crashes what seems to be as simple a render as it can get. Can anyone suggest why GetDesc() is crashing? Is there some flaw in it, or can the bug be found in my code? It would provide a clue as to why my program isn't displaying correctly. If I comment out the call to GetDesc(), and use numTriangles = 2; it runs as expected. Purple rectangle down the left of my screen. 77 lines. http://www.rafb.net/paste/results/ixs2ad21.html

/*struct MYFIXEDCOLOUREDVERTEX
{
	FLOAT	fX,			// Apparently the orders of these are important.
			fY,		
			fZ,		
			fRHW;		// Set to 1.0 to use pretransformed pixel coordinates. No matrix transformation will work.
	DWORD	dwColour;	
};*/
 void Renderer::RenderFrame(void) 
{
	HRESULT							h;
	UINT							numTriangles	= 0;
    LPDIRECT3DVERTEXBUFFER9			tempVB;
	D3DVERTEXBUFFER_DESC			*pDesc			= NULL;	// Only needed to check the size of the VB

	MYFIXEDCOLOUREDVERTEX			arrayay[6];
	for (int w = 0; w < 6; w++)
	{
		arrayay[w].fRHW = 1.0f;
		arrayay[w].fZ	= 
		arrayay[w].fX	= 
		arrayay[w].fY	= 0.0f;
		arrayay[w].dwColour = 0xffff00ff;
	}

	arrayay[1].fX = arrayay[4].fX = arrayay[5].fX	= 30.0f;
	arrayay[2].fY = arrayay[3].fY = arrayay[5].fY	= 500.0f;
	
	m_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, BGCOLOUR, 1.0f, 0);		
	m_pDirect3DDevice->BeginScene();

	if (m_pDirect3DDevice->CreateVertexBuffer(sizeof(arrayay), D3DUSAGE_WRITEONLY,D3DFVF_FIXEDCOLOUREDVERTEX,D3DPOOL_MANAGED, &tempVB,NULL) != D3D_OK)
	{
		ERRPOP("Problem creating vertex buffer.");	// Pops up a window reporting error
	}
	else
	{
		VOID *pData;
		tempVB->Lock(0,sizeof(pData),(void**)&pData,0);		
		memcpy(pData,arrayay, sizeof(arrayay));
		tempVB->Unlock();
	}

	h	= m_pDirect3DDevice->SetFVF(D3DFVF_FIXEDCOLOUREDVERTEX);					
	if (h != D3D_OK)
	{
		ERRPOP("SetFVF() failed.");
	}

	h	= m_pDirect3DDevice->SetStreamSource(0, tempVB, 0, sizeof(MYFIXEDCOLOUREDVERTEX));	
	if (h != D3D_OK)
	{
		ERRPOP("SetStreamSource() failed.");
	}


ERRCON("GetDesc()");                            // Just output to console
h = tempVB->GetDesc(pDesc);			// A way I could have avoided using the RENDERDATA.numQuads field
if (h != D3D_OK)
{
	ERRCODE(h, "GetDesc() failed.");
}

numTriangles = (pDesc->Size / sizeof(MYFIXEDCOLOUREDVERTEX)) / 3;

//	numTriangles = 2;

	ERRCON("Drawing next VB");
	h =	m_pDirect3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0,			numTriangles);	
	if (h != D3D_OK)					//	 Shape				 Start		No. of shapes
	{
		ERRCODE(h, "DrawPrimitive() failed.");
	}

	m_pDirect3DDevice->EndScene();
	m_pDirect3DDevice->Present(NULL,NULL,NULL,NULL);								// Finally, lump it all on screen
} 



Advertisement
I believe GetDesc is meant to be used as:
D3DVERTEXBUFFER_DESC pDesc;
tempVB->GetDesc(&pDesc);


Otherwise you're passing a NULL pointer so it will copy data to address 0, which would cause a crash. When it crashes it's an error writing to memory, right? Stepping through that code with a debugger might be a good idea too.
Stay Casual,KenDrunken Hyena
Ah yeah, that's right. Yeah it was an error writing to memory too.
Shame though, that was just a silly mistake.. no clue to help with my display bug.

This topic is closed to new replies.

Advertisement