Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » what's the function to check the mouse click?

This thread is locked; no one can reply to it. rss feed Print
what's the function to check the mouse click?
Vijay K
Member #7,668
August 2006
avatar

hi,

i'm having lots of images like options or radio buttons,

what i want to do is i want to select the option on click on that position?

my code is

int onoffflg = 1;

int optionss()
{
int mx,my;
while(!key[KEY_ESC] || 0){
draw_sprite(screen,options, 115,130);
if(onoffflg==0)
{
draw_sprite(options, opton, 195,70);
draw_sprite(options, optoff, 360,70);
}
else
{
draw_sprite(options, optoff, 195,70);
draw_sprite(options, opton, 360,70);
}

if(mouse_b)
{

mx=mouse_x;
my=mouse_y;
play_sample(sound2, 300, 128, 1000, 0);
if(mx>=195 && mx<=223 && my>=70 && my<=91)
{
if(onoffflg==1)
onoffflg=0;
else
onoffflg=1;
}
}

i'm calling this function and it's working but what it does is when i click the images are changing but too quickly but once i click completely i.e a click and a release it is working properly but when the mouse is in click (only a pressdown) the images are changing,

what should i do here?

help me on this...

Regards,
Viju

Ceagon Xylas
Member #5,495
February 2005
avatar

if(mouse_b&1)

Vijay K
Member #7,668
August 2006
avatar

hi ,

i tried that one but still happening in the same way..

i call that options inside a while loop

while(!key[KEY_ESC]){
if(key[KEY_R]){
garbagecollection();
loader();
}
options();
.
.
.
.

}

would this cause the probs?

Regards,
Viju

kronoman
Member #2,911
November 2002
avatar

this happens because the mouse button is not released fast enough
a cheap trick would be, after detecting the mouse click, wait until the mouse is released in order to proceed, like this: (took your source and modified it)

1if(mouse_b & 1)
2{
3 
4mx=mouse_x;
5my=mouse_y;
6play_sample(sound2, 300, 128, 1000, 0);
7if(mx>=195 && mx<=223 && my>=70 && my<=91)
8{
9if(onoffflg==1)
10onoffflg=0;
11else
12onoffflg=1;
13}
14 
15// here is my part to wait user for mouse button release
16while (mouse_b & 1) ;
17 
18}

let me know if it works...

miran
Member #2,407
June 2002

The proper way is to remember that the mouse button has been click and then look for the release. Sample code:

1bool button_is_being_pressed_down = false;
2...
3while (!done) {
4 if (!button_is_being_pressed_down) {
5 // watch for a mouse down event
6 if (mouse_b&1) {
7 button_is_being_pressed_down = true;
8 }
9 }
10 else {
11 // watch for a mouse up event
12 if (!(mouse_b&1)) {
13 // here it is!!!!!!11
14 button_is_being_pressed_down = false;
15 }
16 }
17}

--
sig used to be here

Arthur Kalliokoski
Second in Command
February 2005
avatar

http://www.allegro.cc/manual/api/mouse-routines/mouse_callback

You could have this callback keep track of how long it's been since the last click for doubleclick capability as well as signaling button press/release.

They all watch too much MSNBC... they get ideas.

Go to: