In a statement like this

void mousebuttonhandler(int button, int state, int x, int y)
{
  int counter;

  if (button == GLUT_LEFT_BUTTON && state == 0)
           counter ++;
   
 printf("Mouse button event, button=%d, state=%d, x=%d, y=%d\n, ", button, state, x, y);
  
  glutPostRedisplay();
}

It appears that im trying to count the mouse clicks, but of course this wouldnt work. After the function call is done for void mousebuttonhandler counter is deleted.

I am brand spanking new to glut and i can't really think of away to accomplish counting mouse clicks. In java this would be simple, any help would be appreciated.

thanks
-sm

I don't know glut, but if you want to make an counter this is not the way, int counter get's out of scope as soon as the function ends, with the data lost. You can best (I think) make an global variable or an automatic variable in main called counter and then add it if the button is pressed. To I don't get what you put in this function. You already tell the function wether what mouse button is pressed / not pressed? How do you call this function (with what arguments)?

In a statement like this

void mousebuttonhandler(int button, int state, int x, int y)
{
  int counter;

  if (button == GLUT_LEFT_BUTTON && state == 0)
           counter ++;
   
 printf("Mouse button event, button=%d, state=%d, x=%d, y=%d\n, ", button, state, x, y);
  
  glutPostRedisplay();
}

It appears that im trying to count the mouse clicks, but of course this wouldnt work. After the function call is done for void mousebuttonhandler counter is deleted.

I am brand spanking new to glut and i can't really think of away to accomplish counting mouse clicks. In java this would be simple, any help would be appreciated.

thanks
-sm

i think u better to use static counter instead of counter so that it can retreive the value in betwwen the function calls
byee

In a statement like this
It appears that im trying to count the mouse clicks, but of course this wouldnt work. After the function call is done for void mousebuttonhandler counter is deleted.

I am brand spanking new to glut and i can't really think of away to accomplish counting mouse clicks. In java this would be simple, any help would be appreciated.

Why not try to make the variable persistent throughout the program by declaring it as global static variable ? Something along the lines given below by me would help you in solving your problem.

#include <stdio.h>

static int counter ;

int get_count () ;
void incr_count () ;

int main (void)
{
    printf ("\nThe counter initially is %d .", get_count ()) ;
    incr_count ( ) ;
    printf ("\nThe counter after first call is %d .", get_count ()) ;
    incr_count () ;
    printf ("\nThe counter after second call is %d .", get_count ()) ;

    getchar () ;
    return 0 ;
}

void incr_count ( )
{
    ++counter ;
    return ;
}

 int get_count ( )
{
    return counter ;
}

Just implement something like above in your code, making the necessary changes. It would run fine according to me. In case of any other doubts do post again.

Hope it helped, bye.

Why not try to make the variable persistent throughout the program by declaring it as global static variable ?

You can best (I think) make an global variable or an automatic variable in main called counter and then add it if the button is pressed.

No, you shouldn't make it global if you don't need it outside the function.
Don't know what you mean by an "automatic variable in main called counter"

i think u better to use static counter instead of counter so that it can retreive the value in betwwen the function calls

Yes. Simply define the counter as static int counter; instead.

No, you shouldn't make it global if you don't need it outside the function.

Yes. Simply define the counter as static int counter; instead.

Yes, it actually depends like you said on the type of feat you want to achieve. If you need to access the variable outside the function call you need to make it global.

So if the OP is just using the print value of the variable counter for debugging aid then he should go ahead by declaring the counter simply as static. But if he wants to use the value of those mouse clicks for some other purpose (like displaying at the end of the game how many mouse clicks performed) then he would have to adopt a different approach.

So in the end it actually depends on a lot more things and the problem stmt here, so either implementations are not wrong.

Hope it helped, bye.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.