views:

80

answers:

5

Is there any way to do this in a condensed form?

GLfloat coordinates[8];
...
coordinates[0] = 1.0f;
coordinates[1] = 0.0f;
coordinates[2] = 1.0f;
coordinates[3] = 1.0f;
coordinates[4] = 0.0f;
coordinates[5] = 1.0f;
coordinates[6] = 0.0f;
coordinates[7] = 0.0f;
return coordinates;

Something like coordinates = {1.0f, ...};?

+2  A: 

Exactly, you nearly got it:

GLfloat coordinates[8] = {1.0f, ..., 0.0f};
Felix Kling
What if i've done it and want to reassign after?
smsteel
@smsteel That syntax only applies to declarations
Michael Mrozek
This is bad.. Is there other way to simplify it? :)
smsteel
+5  A: 

If you really to assign values (as opposed to initialize), you can do it like this:

 GLfloat coordinates[8]; 
 static const GLfloat coordinates_defaults[8] = {1.0f, 0.0f, 1.0f ....};
 ... 
 memcpy(coordinates, coordinates_defaults, sizeof(coordinates_defaults));

 return coordinates; 
James Curran
This is good idea, maybe i'll do it this way.
smsteel
Better even, make it "static const" and compilers can optimize the variable right out of the code.
Zan Lynx
@Zan: Good point!
James Curran
@Zan Lynx: Won't most compilers be smart enough to do that anyways? Oh well. Good practice to be explicit I suppose.
Chris Cooper
@Chris Cooper: not if it's a global (which might be modified in code the compiler can't see). You can't really tell from these code snippets whether the "..." elides the start of a function.
Steve Jessop
@Steve: Good call. Thanks for clarifying.
Chris Cooper
+1  A: 

You can use:

GLfloat coordinates[8] = {1.0f, ..., 0.0f};

but this is a compile-time initialisation - you can't use that method in the current standard to re-initialise (although I think there are ways to do it in the upcoming standard, which may not immediately help you).

The other two ways that spring to mind are to blat the contents if they're fixed:

GLfloat base_coordinates[8] = {1.0f, ..., 0.0f};
GLfloat coordinates[8];
:
memcpy (coordinates, base_coordinates, sizeof (coordinates));

or provide a function that looks like your initialisation code anyway:

void setCoords (float *p0, float p1, ..., float p8) {
    p0[0] = p1; p0[1] = p2; p0[2] = p3; p0[3] = p4;
    p0[4] = p5; p0[5] = p6; p0[6] = p7; p0[7] = p8;
}
:
setCoords (coordinates, 1.0f, ..., 0.0f);

keeping in mind those ellipses (...) are placeholders, not things to literally insert in the code.

paxdiablo
A: 

There's a trick to wrap the array into a struct (which can be initialized after declaration).

ie. struct foo { GLfloat arr[10]; }; ... struct foo foo; foo = (struct foo) { .arr = {1.0, ... } };

domen
Oh, you didn't need that, just plain initialization would do. Disregard my answer.
domen
You can click "delete" in the bottom left of your post if you want. =P
Chris Cooper
Oh, well... maybe someone finds it useful :-)
domen
A: 

The old-school way:

GLfloat coordinates[8];
...

GLfloat *p = coordinates;
*p++ = 1.0f; *p++ = 0.0f; *p++ = 1.0f; *p++ = 1.0f;
*p++ = 0.0f; *p++ = 1.0f; *p++ = 0.0f; *p++ = 0.0f;

return coordinates;
Pavel Minaev