I’m working on a project that is to simulate many functions seen in Microsoft paint. To accomplish this I’ve used a glutMotionFunc to control a paint brush that draws a given shape when mouse-drag occurs. It changes color, rotates, etc… I want to be able to cycle brush stroke shapes every time I press ‘b’ but I have been unable to iterate within the case ‘b’ to track how many times it has been pressed.
I’m also not sure if even when this works that my mouseMove function will recognize the change is brush stroke.
void keyboard ( unsigned char key, int x, int y )
{
Paint_begin newPaint;
int bPressed = 0;
switch ( key )
{
case 'b':
bPressed=bPressed+1;
newPaint.readyPaint(true);
printf("bPressed: %d\n", bPressed);
newPaint.setShape(bPressed);
break;
//... extra code unrelated to my problem
}
glutPostRedisplay ( );
}
And the mouseMove function (motion function):
void mouseMove ( int x, int y )
{
Paint_begin paintNew;
if(paintNew.shape==0){
glRecti(x,y, x+paintNew.sizeDraw, y+paintNew.sizeDraw);
}
glFlush();
if(paintNew.shape==1){
glBegin(GL_TRIANGLES);
glVertex2f(x, y+paintNew.sizeDraw);
glVertex2f(x+paintNew.sizeDraw, y);
glVertex2f(x, y);
glEnd();
}
//glutPostRedisplay ( );
}
And the Paint_begin class
class Paint_begin{
public:
static int sizeDraw, readyP, shape;
void readyPaint(int paint){
sizeDraw = 1;
readyP = paint;
shape = 0;
}
void setShape(int shape){
shape = shape;
}
int getShape(){
return shape;
}
};
The rectangle works properly but I fear that is only because the shape variable is initialized to 0. Tracking the printf statement for “bPressed” yields 1,1,1, regardless of how many times it is actually pressed.
If you want to keep track of how many times you have pressed the ‘b’ button, you have two possibilities:
So that the variable is initialized only once and then it will store the previous value everytime you call your function.
An alternative is to declare
bPressedas a global variable, so that its value isn’t initialized when you call your method.