Jump to content
  • Advertisement
Sign in to follow this  
adder_noir

Vertex list as std::vector not standard array :oO

This topic is 3036 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

Hi,

I making some revisions to an old program I wrote. It means having to have an array that is not initialised at the beginning. The array in question is the array of vertices using a format called CUSTOMVERTEX. Normally I can declare it like this:

CUSTOMVERTEX vertices[1000];


But not this time. I want the number the array size will become - to be found by reading data out of an external file. In this case a binary file as it happens.

Great and all as it will add alot of automation to what I'm doing. What's not great is that te memcpy call now spits the std::vector I used instead of the old style array:

std::vector<CUSTOMVERTEX> vertices;


When inputted into the buffer like this:

VOID* pVoid;    // a void pointer

// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, &vertices[0], sizeof(vertices));
v_buffer->Unlock();



or like this:

VOID* pVoid;    // a void pointer

// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, &vertices[0], vertices.size());
v_buffer->Unlock();



It generates an error. The usual send don't send screen Microsoft 'has to close' error box.

No before I go racing off trying to find another solution I'd like some advice on two things if possible thanks:

1)Can I use an std::vecotr for this?

2)If I have to use a dynamic array (not even sure what that is right now) can I use it with an auto_pointer to avoid memory leaks?

Many thanks ;o)

Chris

Share this post


Link to post
Share on other sites
Advertisement
What did your memcpy statement look like before (with your standard-array code)?

The number of bytes in your vector is: sizeof(CUSTOMVERTEX)*vertices.size()

Also, how do you create this buffer that you're locking?

Share this post


Link to post
Share on other sites
Hi, this is exactly what it looked like I will explain the paramters below:

// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(INDEXARRAYSIZE*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);

VOID* pVoid; // a void pointer

// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();



INDEXARRAYSIZE - number of vertex entries in the index array.
CUSTOMFVF - a vertex format define.
v_buffer - pointer to a vertex buffer.

I hope that helps a bit. Thanks for your reply.

**EDIT** I think you're suggestion might have fixed it. Please give me some more testing time and I will respond thankyou so much sir.

Share this post


Link to post
Share on other sites
Instead of creating a vertex buffer for "INDEXARRAYSIZE*sizeof(CUSTOMVERTEX)" bytes, you'll probably want allocate the same number of bytes that you intend to copy (i.e. "sizeof(CUSTOMVERTEX)*vertices.size()" bytes).

Share this post


Link to post
Share on other sites
I've done exactly that mate here's the latest code:

// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(vertices.size() * sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);

VOID* pVoid; // a void pointer

// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, &vertices[0], vertices.size() * sizeof(CUSTOMVERTEX));
v_buffer->Unlock();


Great advice thanks alot ;o)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!