I wrote helper functions for setting vertex properties. As you can see the 3 functions are basically the same, except that each function sets a different member variable of the vertices. As it is against the DRY principle and looks ugly I would like to combine them.
void setPositions(const int offset, const sf::Vector2f (&positions)[vertexCount]) {
int index = ensureSizeAndGetIndex(offset);
for(int i = 0; i < vertexCount; i++) {
vertices[index + i].position = positions[i];
}
}
void setTexCoords(const int offset, const sf::Vector2f (&texCoords)[vertexCount]) {
int index = ensureSizeAndGetIndex(offset);
for(int i = 0; i < vertexCount; i++) {
vertices[index + i].texCoords = texCoords[i];
}
}
void setColors(const int offset, const sf::Color (&colors)[vertexCount]) {
int index = ensureSizeAndGetIndex(offset);
for(int i = 0; i < vertexCount; i++) {
vertices[index + i].color = colors[i];
}
}
Things I considered:
- Templates wouldn't work here as they don't handle member variables
- I could combine the first two functions by passing a bool flag of which variable to use. But that wouldn't help for the third function.
- I could add pointers to the member variables of the vertex class and an enum to choose from, but that would be too much (performance) overhead
- Lambdas maybe, meta programming maybe?
Just would be interested for the cleanest solution here.