I'm learning C++ and graphics programming, and following a tutorial, but I can't seem to be able to find any solutions to this problem.
This is the code:
struct CUSTOMVERTEX { FLOAT X, Y, Z; DWORD COLOR; };
void writetovram(struct CUSTOMVERTEX *verticies)
{
...
memcpy(pVoid, verticies, sizeof(verticies));
...
return;
}
void createshapes()
{
//simple square
CUSTOMVERTEX verticies[] =
{
{ -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },
};
writetovram(&verticies);
}
I need to be able to pass this struct array in its entirety as a pointer to the function. I've found that if I havn't used pointers then the data doesn't get transferred properly.
I've tried:
void writetovram(struct CUSTOMVERTEX *verticies)
void writetovram(struct CUSTOMVERTEX verticies)
void writetovram(struct CUSTOMVERTEX &verticies)
void writetovram(struct CUSTOMVERTEX *verticies[])
void writetovram(struct CUSTOMVERTEX verticies[])
and
writetovram(&verticies);
writetovram(*verticies);
writetovram(verticies);
Edit: When I put the code like this, it works:
void writetovram()
{
CUSTOMVERTEX verticies[] =
{
{ -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },
};
...
memcpy(pVoid, verticies, sizeof(verticies));
...
return;
}
Basically, all the combinations of pointers and syntax I could think of. But none have worked.
Can anyone help?
I've also searched online and found none having this problem.
Inside of writetovram()
, sizeof(verticies)
doesn't return what you think it does. You can't get the size of an array from just a pointer. There are TONS of questions on StackOverflow on this issue.
Your simplest option is to just pass the array size as another parameter, eg:
struct CUSTOMVERTEX { FLOAT X, Y, Z; DWORD COLOR; };
void writetovram(CUSTOMVERTEX *verticies, int num_verticies)
{
...
memcpy(pVoid, verticies, sizeof(*verticies) * num_verticies);
...
}
void createshapes()
{
//simple square
CUSTOMVERTEX verticies[] =
{
{ -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },
};
writetovram(verticies, 4);
// or:
// writetovram(verticies, sizeof(verticies)/sizeof(verticies[0]));
// or:
// writetovram(verticies, std::size(verticies));
}
Other options include:
pass the array by reference, but only if the size is known at compile-time. But if multiple different-sized arrays need to be used, then writetovram()
would have to be made into a template function so the array size can be deduced at compile-time for each call site.
changing the array to use std::array
or std:vector
instead, passed to the function by reference, and then it can use their size()
methods.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments