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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
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.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734