I wrote a function that calculates all for vertex points for a square given its position with and height. Since one cannot return array's in C I have to do it through a pointer. This is the code I ended up writing:
// Creates a rectangle for mapping a texture. Array must be 20 elements long.
void make_vertex_rect(float x, float y, float w, float h, float *vertex_positions) {
/* -1.0,+1.0 +1.0,+1.0
+----------------------+
| |
| |
| |
+----------------------+
-1.0,-1.0 +1.0,-1.0 */
float new_positions[20] = {
// We start at the top left and go in clockwise direction.
// x, y, z, u, v
x, y, 0.0f, 0.0f, 0.0f,
x + w, y, 0.0f, 1.0f, 0.0f,
x + w, y - h, 0.0f, 1.0f, 1.0f,
x, y - h, 0.0f, 0.0f, 1.0f
};
for (int i = 0; i < 20; ++i) { vertex_positions[i] = new_positions[i]; }
}
Now since C99 provides designated initializers I thought there might be a way to do this without writing a for loop but could not figure it out. Is there a way to do this directly, like:
// Creates a rectangle for mapping a texture. Array must be 20 elements long.
void make_vertex_rect(float x, float y, float w, float h, float *vertex_positions) {
// Does not compile, but is there a way to get it to compile with a cast or something?
*vertex_positions = { ... };
}