Sign in to follow this  

Help with CreateVertexBuffer()

This topic is 4854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This compiles and when the window runs it comes up with a referenced memory error. Am I not setting something right. And my vertices isn't done yet so thats why there all the same, could that be the reason? It errors out on the CreateVertexBuffer() function.

typedef struct {
	FLOAT x,y,z;
	D3DCOLOR Diffuse;
} sVertex;

#define VERTEXFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)

IDirect3DVertexBuffer9 *g_pVB = NULL;

sVertex Verts[4] = {
		{10.0f, 10.0f, 10.0f, D3DCOLOR_RGBA(0,0,0,255)},
		{10.0f, 10.0f, 10.0f, D3DCOLOR_RGBA(0,0,0,255)},
		{10.0f, 10.0f, 10.0f, D3DCOLOR_RGBA(0,0,0,255)},
		{10.0f, 10.0f, 10.0f, D3DCOLOR_RGBA(0,0,0,255)}
	};

	if(FAILED(g_pD3DDevice->CreateVertexBuffer(4, 0, VERTEXFVF, D3DPOOL_DEFAULT, 
											   &g_pVB, NULL))){
		MessageBox(NULL, "Could not create vertex buffer.", "FAILED", MB_OK);
		return FALSE;
	}

	VOID *Ptr = NULL;

	if(FAILED(g_pVB->Lock(0, 0, (VOID**)Ptr, 0))){
		MessageBox(NULL, "Could not lock vertex buffer.", "FAILED", MB_OK);
		return FALSE;
	}

	memcpy(Ptr, Verts, sizeof(Verts));

	if(FAILED(g_pVB->Unlock())){
		MessageBox(NULL, "Could not unlock vertex buffer.", "FAILED", MB_OK);
		return FALSE;
	}


Share this post


Link to post
Share on other sites
The first parameter is the size in bytes. Its failing because it doesn't have enough room for one vertex, try it like this:

if(FAILED(g_pD3DDevice->CreateVertexBuffer(4 * sizeof(sVertex), 0, VERTEXFVF, D3DPOOL_DEFAULT, &g_pVB, NULL)))
{
MessageBox(NULL, "Could not create vertex buffer.", "FAILED", MB_OK);
return FALSE;
}

Share this post


Link to post
Share on other sites
It still comes up with the same error. It says the instruction at " 0x00477342" referenced memory at "0x00000000". The memory could not be written. I changed the code to use the number of bytes for the first parameter too. Any help is greatly apreciated, thanks.

if(FAILED(g_pD3DDevice->CreateVertexBuffer(4 * sizeof(sVertex), 0, VERTEXFVF, D3DPOOL_DEFAULT,
&g_pVB, NULL))){
MessageBox(NULL, "Could not create vertex buffer.", "FAILED", MB_OK);
return FALSE;
}

Share this post


Link to post
Share on other sites
I put a breakpoint on the create function and it went past it and errored out on the Lock() function. it said Unhandled exception. My g_pD3DDevice is some hexidecimal value 0x0014cbe0

Share this post


Link to post
Share on other sites
Change it to:

if(FAILED(g_pVB->Lock(0, 0, (VOID**)&Ptr, 0))){
MessageBox(NULL, "Could not lock vertex buffer.", "FAILED", MB_OK);
return FALSE;
}

you have to pass the address of the pointer, not the pointer itself.

If it still doesn't work, g_pVB is NULL.

Share this post


Link to post
Share on other sites

This topic is 4854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this