Reputation: 1705
I have to place several integer values into a (32 byte) buffer, where each integer must be at a specific place in the buffer (I'm going to use the data in OpenGL VBOs).
The way I'm doing it now is kind of ugly. All the content is supposed to be placed at the address buf2. Don't bother looking at the values I'm assigning to the buffer, as those are irrelevant to the problem.
uint32_t *const coords = reinterpret_cast<uint32_t *>(buf2 + CoordDataOff);
uint16_t *const texcoords = reinterpret_cast<uint16_t *>(buf2 + TcoordDataOff);
uint32_t *const color = reinterpret_cast<uint32_t *>(buf2 + ColorDataOff);
coords[0] = x + bearing[0]; // left
coords[1] = y + bearing[1] - 64*(tcoords[3] - tcoords[1]); // bottom
coords[2] = x + bearing[0] + 64*(tcoords[2] - tcoords[0]); // right
coords[3] = y + bearing[1]; // top
copy_n(tcoords, 4, texcoords);
*color = c.color;
Is there any nicer, portable way of accomplishing it?
Upvotes: 0
Views: 548
Reputation: 637
Does your buffer just contain this data for each vertex? You could just generate a struct
struct TData
{
uint32_t coords[4];
uint16_t texcoords[4];
uint32_t color;
};
then it would be as simple as treating your buffer as an array of TData
TData verts[100];
vert[0].coord[0] = x;
vert[0].coord[1] = y;
...
FunctionToWriteToBuffer((void *)verts, sizeof(TData) * 100);
or in the code you have above
TData *vert = (TData *)buf2;
That should also make things easier to read to maintain
Upvotes: 2