PDA

View Full Version : how to redraw the window with different data



joker40
01-11-2011, 06:49 AM
Hi,
I am trying to do following. I have structure myStruct that holds data for image.By calling

glutDisplayFunc(myFunction);I put that image in the window.
This is myFunction...

void myFunction(){
/*Use myStruct and draw image to the screen*/

Now at this point image is on the screen.
What I am trying to do now is on the press of the button or menu I would like to execute some other function that will
use another globally accessible struct myAnotherStruct and redraw window with data inside myAnotherStruct.
I guess I cant call glutDisplayFunc(anotherFunction) so I was wondering if anyone can help me or point me into some direction on how to accomplish this.

aqnuep
01-11-2011, 07:32 AM
What about simply using a global variable indicating which struct you have to draw. This way you can use the same display function to draw all your images.

joker40
01-11-2011, 07:38 AM
yeah but how to call it....
I cant call it like this glutDisplayFunc(myFunction(myAnotherStruct))??

joker40
01-11-2011, 09:05 AM
how would i do that?would you be kind to explain please

mobeen
01-11-2011, 09:30 AM
HI,
As aqnuep said. You can do it like this.

Supposing u have some struct (called YourStuct). You having two global objects o1 and o2 of it. The struct has whatever content u want. As as example i add an Image object as the struct member


typedef struct YourStruct {
//put something useful here
Image img;
};
YourStruct o1, o2;

Create a global pointer name it it pCurrentObject.


YourStruct *pCurrentObject;

At init, assign pCurrentObject the reference of the object u want to show first.


void init() {
pCurrentObject= &o1;
}

and in your display func. use the pCurrentObject


void display() {
//setup modelview matrix
//assume drawing the image
DrawImage(pCurrentObject->img);
}

In the keyboard input you may do something like this lets say u want to draw object o1 when '1' is pressed and o2 when '2' is pressed. In that case, u would add the following two cases in the keyboard func.


void OnKey(unsigned char whichKey, int x, int y) {
switch(key) {
case '1': pCurrentObject=&o1; break;
case '2': pCurrentObject=&o2; break;
}
glutPostRedisplay();
}

Just to give u the idea.
Hope this helps,
Mobeen

joker40
01-11-2011, 09:50 AM
This is the function that is always called when window needs to be redrawn...

glutDisplayFunc(myFunction);

In this function there is no way to pass parametric(current object)..

How do you let know function that does redraw which is
myFunction()
that needs to redraw with different "current object"?

Alfonse Reinheart
01-11-2011, 09:57 AM
How do you let know function that does redraw which is

He just showed you how. You have a global variable which is accessible by the drawing method. This tells you what to draw. The display function uses this object to decide what to draw.

This isn't complicated.

joker40
01-11-2011, 10:46 AM
Hi.When i write this assignment..

pCurrentObject= &o1;
I get error saying " request for member in something not structure or union...

My struct looks something like this..


typedef struct YourStruct {
//put something useful here
Image *img;
};
YourStruct o1, o2;

joker40
01-11-2011, 10:52 AM
I fixed it. It works now. Thank you