-
Why dosent this work?
Can someone tell me why this dosent change the value of sel thats declared in main?
void function(float *sel);
float number = 3;
main() {
float *sel = new float[3];
function(sel);
//sel[0] donsent == number ????
delete[] sel;
}
functon(float *sel) {
sel[0] = number;
}
-
Apparently function() is a reserved name, I got an error on that until I changed it to func(). After I did that the program worked just fine.
-Prelude
-
Since functon(float *sel) is different from function(float *sel). That's why, you made a typing error.
-
regardless of the typos I posted here, why dosent this work right.
My real program is much more complex, this is just the part that I have narrowed down the problem to.
-
If you correct the typo it does work.
-
>If you correct the typo it does work.
Actually it doesn't on my compiler. Now that I'm more awake I noticed that in your function definition you forgot to include the return type. This also may be a typo though.
Code:
function(float *sel) { // Will flag an error
sel[0] = number;
}
Code:
void function(float *sel) { // Works fine
sel[0] = number;
}
-Prelude
-
It is, I will post my real code for clarity.
heres the function thats starting from
Code:
void menus::Mouse(short type, int x, int y) {
float *sel = new float[3];
glh.Selectf(sel);
if(glh.Checkf(sel,skm_btn->col)) battle.Start();
if(glh.Checkf(sel,ted_btn->col)) ted.Start();
delete[] sel;
}
void glhs::Selectf(float sel[]) {
float r,g,b;
if(selectmode == false) {
display(GLH_SELECT);
selectmode = true;
}
glReadPixels(curx,tempy,1,1,GL_RED,GL_FLOAT,&r);
glReadPixels(curx,tempy,1,1,GL_GREEN,GL_FLOAT,&g);
glReadPixels(curx,tempy,1,1,GL_BLUE,GL_FLOAT,&b);
sel[0] = r;
sel[1] = g;
sel[2] = b;
}
bool glhs::Checkf(float *sel, float *colors) {
bool stater = false, stateg = false, stateb = false;
if(sel[0] > colors[0]-0.049 && sel[0] < colors[0]+0.049) stater = true;
if(sel[1] > colors[1]-0.049 && sel[1] < colors[1]+0.049) stateg = true;
if(sel[2] > colors[2]-0.049 && sel[2] < colors[2]+0.049) stateb = true;
if(stater == true && stateg == true && stateb == true) return true;
else return false;
}
I know that the r,g,b variables int selectf have the right values, but those values never make it to the checkf function? I can make it work without arrays, but I really want to use them here.
-
void glhs::Selectf(float sel[]) {
should be
void glhs::Selectf(float* sel) {
-
That was just something I was testing, but ive tried it both ways.
I cant for the life of me figure out why it isnt working.
-
add some trace statements after calling selectf() to print out the values stored in sel. what do you get?
-
It gives me 0,0,0 for the array after calling Selectf, but inside the function I know that r,g,b are not equil to 0,0,0.
I have a sister function that just returns the r,g,b variables, useing that I can say something like this
sel[0] = glh.Selectf(RED);
sel[1] = glh.Selectf(GREEN);
sel[2] = glh.Selectf(BLUE);
and it works fine? I checked for variables with the same name, the only other sel variable in the program is in a class thats not even active when this is. Do you have any other ideas on what could cause this to mess up indirectly?
-
please post glReadPixels()
-
Thats an OpenGL library function, it dose return the values that I want, They just never make it to the sel array?
-
now print out the values returned from glReadPixels. Are they all zero too?
-
what compiler are you using? the code looks ok.