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

Started by
3 comments, last by adder_noir 13 years, 8 months ago
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
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?
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.
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).
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)

This topic is closed to new replies.

Advertisement