'Change alpha component in a function returning a float *color
I've got a function returning a (float *) color. It works, until I tried to change the alpha component of one color, and now almost all colors have alpha issue, even if the color don't pass the test "if (alpha)".
eg:
float * colorTest = getColor(0); // don't give me the good result anymore, once any getColor(1) is called before.
float * getColor(float alpha) {
float * col;
if (val == 1)
col = colorWhite; // colorWhite is a float *
else if (val == 2)
col = colorRed;
else if (etc..)
col = colEtc;
if (alpha) {
col[3] = alpha; // this doesn't works
}
return col;
}
I also have tried to change the alpha value through the col pointer, with same result:
if (alpha) {
float *a;
a = (float *)(&col + 3);
*a = alpha;
}
Any advice?
Edit
Got it to works, thx to hyde who told me that I was changing my global colors. (I was used to falsely considered float * as array) So, I just copy the result in a second variable and then modify this new color.
float * getColor(float alpha) {
float * col;
float * tmp;
if (val == 1)
tmp = colorWhite; // colorWhite is a float *
else if (val == 2)
tmp = colorRed;
else if (etc..)
tmp = colEtc;
memcpy(col, tmp, sizeof(float)*4);
if (alpha) {
col[3] = alpha; // this now works without issue
}
return col;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
