Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » set mouse rate

This thread is locked; no one can reply to it. rss feed Print
set mouse rate
thirdy
Member #8,409
March 2007
avatar

Hi! Is there a function for mouse like set_keyboard_rate(900, 0); in allegro? The mouse in my program produces too many clicks per actual 1 click in the mouse. Thanx!

James Stanley
Member #7,275
May 2006
avatar

It doesn't work like that.
I guess you're using mouse_b...?
If so, it just says whether or not the mouse button is down. Rate has nothing to do with it. You just need a variable that says whether or not the button was down last frame. This might work for your needs (untested!):

1int mouse_clicked(void) {
2 static int mouse_down;
3 
4 if(!mouse_down) {
5 if(mouse_b & 1) {
6 mouse_down = 1;
7 return 1;
8 }
9 } else {
10 if(!(mouse_b & 1)) {
11 mouse_down = 0;
12 }
13 return 0;
14 }
15}

thirdy
Member #8,409
March 2007
avatar

Still too fast.

1 if(mickey_left==FALSE)
2 {
3 if (mouse_b & 1)
4 {
5 //allegro_message("b 1");
6 mickey_left=TRUE;
7 }
8 }
9 else
10 {
11 if(!(mouse_b & 1))
12 mickey_left=FALSE;
13 }
14 //-----------------------------------//
15 if(mickey_right==FALSE)
16 {
17 if (mouse_b & 2)
18 {
19 //allegro_message("b 2");
20 mickey_right=TRUE;
21 }
22 }
23 else
24 {
25 if(!(mouse_b & 2))
26 mickey_right=FALSE;
27 }

Onewing
Member #6,152
August 2005
avatar

Quote:

Still too fast.

Ooh, so you're wanting a minimum amount of time a user can click? So if the user is clicking blazing fast, the mouse event that occurs will happen slower? In this situation, you'll have to add some timer logic to your code.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

thirdy
Member #8,409
March 2007
avatar

No,here's the situation. One careful left click = three left clicks. I need to make it like a normal mouse click. What could be missing in my code? Can you give me a short program that includes normal mouse, not a crazy one. Thanks!

Onewing
Member #6,152
August 2005
avatar

What you have above looks fine, what's the code you use for actually doing something when the mouse button is clicked?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

thirdy
Member #8,409
March 2007
avatar

Here's my actual code

1if(hr_newdialog.hover==TRUE)
2 {
3 hr_newdialog.hover=FALSE;
4 textprintf_ex(buffer, font, set_node.left+130,set_node.top+35, makecol(255,0,0),
5 -1, "Hour control button");
6 textprintf_ex(buffer, font, set_node.left+130,set_node.top+55, makecol(255,0,0),
7 -1, "detected!");
8 if(mickey_left==TRUE)
9 {
10 comp[selected_comp].hr++;
11 mickey_left=FALSE;
12 }
13 if(mickey_right==TRUE)
14 {
15 if(comp[selected_comp].hr>0)comp[selected_comp].hr--;
16 mickey_right=FALSE;
17 }
18 }

Here's also my actual initialization code

1allegro_init();
2 alfont_init();
3 install_keyboard();
4 set_keyboard_rate(900, 0);
5 install_mouse();
6 //poll_mouse();
7 install_timer();
8 set_color_depth(desktop_color_depth());
9 //get_desktop_resolution(&monitorx, &monitory);
10 if(set_gfx_mode(GFX_AUTODETECT_WINDOWED,monitorx,monitory,0,0)!=0)
11 {
12 allegro_message("Please set your screen resolution larger than 800x600.");
13 return 1;
14 }
15 install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
16
17 acquire_screen();
18 set_display_switch_mode(SWITCH_BACKGROUND);
19
20 enable_hardware_cursor();
21 select_mouse_cursor(MOUSE_CURSOR_ARROW);
22 //-------------/
23 //bitmaps here*****************//
24 //-----------/
25 show_mouse(buffer);

Onewing
Member #6,152
August 2005
avatar

The only time the mickey_left and/or mickey_right should be set to FALSE is when the "mouse_b & 1 (or 2)" is not set, like you did in the above code (a few posts up). You're setting it to false in the event, the mouse is still being pressed, so it sets it back to true and then the event sets it back to false. You've effectively defeated the purpose of the mickey_left/right flags. ;)

------------
Solo-Games.org | My Tech Blog: The Digital Helm

thirdy
Member #8,409
March 2007
avatar

Actually there was no mickey_left=FALSE; before posting here. But I think there's no effect when you make mickey FALSE, it would still be TRUE in the next loop if mickey is clicked.

Onewing
Member #6,152
August 2005
avatar

You're right, I'm reading too skipilly (definitely not a word). Anyway, on the post above (the one where you first said "Still too fast"), notice how when you set mickey_left to true that that only happens once until mouse_b & 1 is not true? That's the logic you need for the actual event to happen. You can either put the event code in that logic above or create another boolean flag for each mouse button event.

Setting a flag to be true and then checking if that flag is true is the exact same thing as seeing if mouse_b & 1 is true.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

ImLeftFooted
Member #3,935
October 2003
avatar

1int mouse_clicked(void) {
2 static int mouse_down = 0;
3 
4 if(!mouse_down) {
5 if(mouse_b & 1) {
6 mouse_down = 1;
7 return 1;
8 }
9 } else {
10 if(!(mouse_b & 1)) {
11 mouse_down = 0;
12 }
13 }
14 
15 return 0;
16}

Paul Rowan
Member #8,612
May 2007
avatar

I don't know if this might help, but in a game i've developed I had the same problem with the keyboard in that when a key was pressed the sprite moved multiple times instead of just the once.

The way I solved it was to use a variable for a key delay so that as soon as the key was pressed the key delay variable was set to a number, say 10, then each time through the game loop this variable was reduced by 1 until it reached 0. While ever the key delay variable was more then 0, any key presses for that key were ignored. This slowed down the key presses and you can use a different number in your key delay variable to slow the presses down to what you want.

So, I suppose you could use the same method for your mouse button, use a button delay variable so that as soon as the mouse click is registered, the button delay variable is set so that any future clicks will not register until the button delay is back at 0, and reduce the button delay variable each time through the game loop until it gets to 0.

thirdy
Member #8,409
March 2007
avatar

Thanks! Finally, 1 click is now exactly one click. Thanks!

Paul Rowan
Member #8,612
May 2007
avatar

You're welcome ;D;D;D;D;D

I'm quite a newbie to C and Allegro and only written 1 game so far, but loads of ideas for new ones - is this code for a game ur developing? and if it is what, can u say wot ur developing?

Onewing
Member #6,152
August 2005
avatar

I don't think that's a really good idea. One, if you hold down the mouse button the action is going to keep happening every time the variable runs down to 0 (since mouse_b & 1 is still true, the code will register another click). Secondly, it limits how fast a user can click the button. Thirdly, the rate at which a variable decrements to 0 is going to range widely across different computers. For what seems good to you could be horrible elsewhere.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Paul whoknows
Member #5,081
September 2004
avatar

Quote:

Thirdly, the rate at which a variable decrements to 0 is going to range widely across different computers.

Yes, this is a very bad thing, try to use just flags.

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

thirdy
Member #8,409
March 2007
avatar

1 while(game_status==TRUE)
2 {
3 m--;
4
5 if(mickey_left==FALSE)
6 {
7
8 if (mouse_b & 1)
9 {
10 m=1;
11 mickey_left=TRUE;
12 }
13 }
14 else
15 {
16 if(!(mouse_b & 1))
17 mickey_left=FALSE;
18 }
19
20 //codessssssssssssssss------//
21 /*then somewer in the code*/
22 if(mickey_left==TRUE)
23 {
24 if(m==0)
25 comp[selected_comp].hr++;
26 //mickey_left=FALSE;
27 }
28 }//end of main loop

this code satisfy my problem which increments hr by only one per one left click.

Paul Rowan
Member #8,612
May 2007
avatar

If you use a timer in your game the delay should reduce at the same rate on different computers. I thought the idea was not to get multiple mouse clicks from 1 click, so if someone holds the mouse button down it should register another click when the delay reached 0 after a short delay, and if the game requires it you should control how fast a user should be able to click a button :-/

That's my opinion, but I've only just started developing games and that's the solution i've found so far :)

Go to: