Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Mouse button code problem - Doing something when clicked.

Credits go to HardTranceFan for helping out!
This thread is locked; no one can reply to it. rss feed Print
Mouse button code problem - Doing something when clicked.
Durnus
Member #7,997
November 2006
avatar

--SOLVED--

This may not be as simple as it sounds.

I have (tried to) made a class for a game where it needs to sense a click. Not a click every loop, but once when it is clicked.

I have made a class, and since it is kinda hard to explain, I'll post the code.

main.cpp

1#include <allegro.h>
2#include "controlhandler.h"
3void init();
4void deinit();
5 
6 
7int main()
8{
9 init();
10
11 control cntrl;
12
13 while (!key[KEY_ESC])
14 {
15 cntrl.refresh();
16 if(cntrl.mouseclick(2)) clear_to_color(screen, makecol(0,0,0));
17 if(cntrl.mouseclick(1))
18 {
19 rect(screen,mouse_x,mouse_y,0,0,makecol(255,255,255));
20 }
21
22 }
23 
24 deinit();
25 return 0;
26}
27END_OF_MAIN()
28 
29 
30void init() {
31 int depth, res;
32 allegro_init();
33 depth = desktop_color_depth();
34 if (depth == 0) depth = 32;
35 set_color_depth(depth);
36 res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
37 if (res != 0) {
38 allegro_message(allegro_error);
39 exit(-1);
40 }
41 set_window_title("Shipcraft Universe");
42 install_timer();
43 install_keyboard();
44 install_mouse();
45 show_mouse(screen);
46}
47 
48void deinit() {
49 clear_keybuf();
50 /* add other deinitializations here */
51}

controlhandler.h

1#ifndef CONTROLHANDLER_H
2#define CONTROLHANDLER_H
3 
4class control
5{
6
7 public:
8
9 void refresh();
10 bool mouseclick(int button);
11
12 bool mouse_1_last; //Was the mouse button 1 pressed last loop?
13 bool mouse_2_last; //Was the mouse button 2 pressed last loop?
14
15 bool mouse_1;
16 bool mouse_2;
17 
18};
19 
20#endif // CONTROLHANDLER_H

controlhandler.cpp

1#include "controlhandler.h"
2#include <allegro.h>
3 
4 
5void control::refresh()
6{
7 if(mouse_1_last == false)
8 {
9 if(mouse_b & 1)
10 {
11 mouse_1 = true;
12 mouse_1_last = true;
13 }
14 }
15 if(mouse_2_last == false)
16 {
17 if(mouse_b & 2)
18 {
19 mouse_2 = true;
20 mouse_2_last = true;
21 }
22 }
23 if(mouse_1_last == true)
24 {
25 if(!(mouse_b))
26 {
27 mouse_1_last = false;
28 mouse_1 = false;
29 }
30 }
31 if(mouse_2_last == true)
32 {
33 if(!(mouse_b))
34 {
35 mouse_2_last = false;
36 mouse_2 = false;
37 }
38 }
39}
40 
41bool control::mouseclick(int button)
42{
43 if(button == 1)
44 {
45 if(mouse_1) {return true;}
46 else {return false;}
47 }
48 if(button == 2)
49 {
50 if(mouse_2) {return true;}
51 else {return false;}
52 }
53}

There are no compile errors, it just doesn't work like it would be expected to. :-/

Please help me, this could be a cool game. :-[

Kris Asick
Member #1,424
July 2001

This is what is called "overcomplication". In other words, the following code will likely work the way you want. Just replace your entire controlhandler.cpp with this:

1#include "controlhandler.h"
2#include <allegro.h>
3 
4 
5void control::refresh()
6{
7 mouse_1_last = mouse_1;
8 mouse_2_last = mouse_2;
9 mouse_1 = mouse_b & 1;
10 mouse_2 = mouse_b & 2;
11}
12 
13bool control::mouseclick(int button)
14{
15 if (button == 1)
16 if (!mouse_1 && mouse_1_last) return true; else return false;
17 if (button == 2)
18 if (!mouse_2 && mouse_2_last) return true; else return false;
19 
20 return false;
21}

Much simpler. As I understand it, you're trying to detect the moment when the mouse button has been released as a single click. I believe this will do that.

.
EDIT: You may need to convert your bool variables to ints for the above replacement code to work. I'm not certain since I never use bools.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

HardTranceFan
Member #7,317
June 2006
avatar

Does the following work? I haven't tried it, just written it:

Scrap that, spotted a flaw straight away. And Kris beat me to it.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Durnus
Member #7,997
November 2006
avatar

Wow, that works perfectly! Thanks! Sometimes I overcomplicate things. :P

I'm going to be using this header throughout my games now. Thanks a bundle! ;D

EDIT: Dang it... gave credit to wrong person :-/

HardTranceFan
Member #7,317
June 2006
avatar

Wohoo Free cookies ;D

Kris, if there's a way to give them to you, let me know.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Durnus
Member #7,997
November 2006
avatar

Last time (:P) I could edit the post to change it... but it isn't letting me this time.

Sorry Kris :-/

Peter Wang
Member #23
April 2000

    if (!mouse_1 && mouse_1_last) return true; else return false;

:P

    return (!mouse_1 && mouse_1_last);

Durnus
Member #7,997
November 2006
avatar

Whoa, even more simplicity. I really wish I could change the credit. :-/

Kris Asick
Member #1,424
July 2001

shrugs Quite frankly, I never pay attention to that "credits" thing for questions asked. Don't worry about it. ;)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Go to: