• 14
• 15
• 11
• 10
• 9

# memcpy() (I Think) Problem

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

## Recommended Posts

Sorry guys, i hate to keep nagging you for help but im moving forward with my project quite rapidly and keep finding problems that i can never find documentation on. So here goes... I have written a wrapper for a vertex buffer and it has a pair of write functions that look like this... A write function for writing a single vertex to the buffer...
	template<class T>
void Component_VB<T>::Write(T& v)
{
if (numberWritten < size)
{
void* a;
pV = NULL;
// lock the buffer
Lock(v, (numberWritten * sizeof(v)) );

a = memcpy(pV, &v, sizeof(v));

Unlock();

if (a != NULL)
{
numberWritten++;
}
else
{
assert(0);
}
}
else
{
assert(0);
}
}


A write function for writing an array of vertices to the buffer...
	template<class T>
void Component_VB<T>::Write(T v[], unsigned int qty)
{
if (numberWritten < size)
{
T tempVerts[1000]; // <<Used for simple test code

VOID* a;
VOID* vPointer;

vertexBuffer->Lock(0, (qty *sizeof(T)),(void**) &vPointer, 0);

a = memcpy(&tempVerts[0], &v[0], qty*sizeof(T));

a = memcpy(vPointer, &v[0], qty*sizeof(T));

vertexBuffer->Unlock();

if (a != NULL)
{
numberWritten++;
}
else
{
assert(0);
}
}
else
{
assert(0);
}
}


The first one works fine by moving through the array by one vertex every time the function is called. Now this is the function that i am currently using. This function obviously is slow to use because of the Lock() Unlock() which would be called thousands of times. I want to implement the second function which takes a pointer to an array of vertices and the number to be copied. As a test to see if i was recieving the vertices through the parameter as i wanted i added an internal array to also copy to, using the top memcpy(). Then second memcpy() is to copy to the void* pointing to the VB. Now the vertices do copy correctly from the the parameter to the internal array but they obviously dont copy to the VB correctly since the on-screen object doesnt render. The on screen object does render if they are written one at a time. Also i should add that the Lock function is wrappedVB function that does work for simply usage. In the second function i havnt used it. So if you can take that function for granted in the first example. Can anyone see why this might be the case? I have tried alot of different syntax's to try and solve this but non have worked obviously. Sorry to take up more of your time, ace

##### Share on other sites
Hmmm, at a glance, I don't see any VertexBuffer creation in that second function. Did you forget to add it?

##### Share on other sites
Are you sure that vPointer receives a valid address? Have you single-stepped through the code?

##### Share on other sites
No its a class so that is managed elsewhere.

ace

##### Share on other sites
Yes vPointer does recieve a valid address.

ace

##### Share on other sites
Quote:
 Original post by ace_lovegroveYes vPointer does recieve a valid address.
Quote:
 Original post by OluseyiHave you single-stepped through the code?
This second part can give you insight into what's wrong, though I'll be the first to admit that debugging Direct3D is challenging. You very often need a multimon setup to do it effectively.

Try using trace statements to see what's up.

##### Share on other sites
I'm just guessing it may have something to do with the VB creation or rendering. What kind of VB is this? Dynamic?

##### Share on other sites
Yes it is dynamic, but the setup o teh VB is fine because i have used it when writing 1 vertex to the VB at a time, so it isnt that. All return codes are correct when stepping through. I guess i could try looking at the memory as it is written but i wouldn't really know what to look for.

Any further ideas?

ace

All sorted now.

TY all

ace