Hey everyone,
So, when someone right clicks with the mouse, I display an image. When they right click again, the image should disappear. Here's a big chunk of what I have to accomplish this:
1 | //globals |
2 | int mx, my, mb; |
3 | int exist = 0; |
4 | |
5 | |
6 | int draw_bitmap() |
7 | { |
8 | BITMAP *ship = load_bitmap("spaceship.bmp", NULL); |
9 | mx = mouse_x; |
10 | my = mouse_y; |
11 | masked_blit(ship, screen, 0, 0, mx, my, 64, 64); |
12 | |
13 | } |
14 | |
15 | int clear_b() |
16 | { |
17 | //clear off bitmap |
18 | rectfill(screen, mx, my, mx+63, my+63, BLACK); |
19 | } |
20 | |
21 | int main() |
22 | { |
23 | //set up stuff |
24 | ... |
25 | .. |
26 | . |
27 | show_mouse(screen); |
28 | |
29 | //if user doesn't hit ESC, then remain in loop |
30 | while(!key[KEY_ESC]) |
31 | { |
32 | if(mouse_b & 1) |
33 | textout(screen, font, "Left mouse button was pressed", 20, 20, WHITE); |
34 | |
35 | else if(mouse_b & 2) |
36 | { |
37 | textout(screen, font, "Right mouse button was pressed", 20, 20, WHITE); |
38 | |
39 | //clear_b(); |
40 | //draw_bitmap(); |
41 | if(exist == 1) |
42 | { |
43 | clear_b(); |
44 | exist = 0; |
45 | } |
46 | else |
47 | { |
48 | draw_bitmap(); |
49 | exist = 1; |
50 | } |
51 | |
52 | } |
53 | else if(keypressed()) |
54 | { |
55 | k = readkey(); |
56 | textprintf(screen, font, 20, 20, WHITE, "%c keyboard button was pressed", (char)k); |
57 | } |
58 | |
59 | ... |
60 | .. |
61 | . |
62 | //exit properly |
So, when I run this and right-click over and over, most of the time, the image appears where I've right clicked. Then a few times it disappears. Remember, goal is to right-click 1: image appears; right-click2: image disappears, and so on.
So what obvious thing am I messing up with?
From first glances: at the end of your "else if(mouse_b & 2)" section, put a "while (mouse_b & 2);". You're not waiting for the mouse button to go up, and so the code is repeatedly run while you press the right mouse button.
Jeroen
[edit]
However, if you want the rest of the program to keep executing. In which case, try:
1 | // best a global variable |
2 | bool rmb_down = false; // keep track of the right mouse button pressed state |
3 | |
4 | ... |
5 | |
6 | else if((mouse_b & 2) & rmb_down == false) |
7 | { |
8 | textout(screen, font, "Right mouse button was pressed", 20, 20, WHITE); |
9 | rmb_down = true; |
10 | |
11 | //clear_b(); |
12 | //draw_bitmap(); |
13 | if(exist == 1) |
14 | { |
15 | clear_b(); |
16 | exist = 0; |
17 | } |
18 | else |
19 | { |
20 | draw_bitmap(); |
21 | exist = 1; |
22 | } |
23 | } |
24 | else if (!(mouse_b & 2) & rmb_down == true) |
25 | { |
26 | rmb_down = false; |
27 | } |
Sorry, at work at the moment, so I can't determine if the code is 100% correct.
1 | // best a global variable |
2 | bool rmb_down = false; // keep track of the right mouse button pressed state |
3 | |
4 | ... |
5 | |
6 | else if((mouse_b & 2) && !rmb_down) |
7 | { |
8 | textout(screen, font, "Right mouse button was pressed", 20, 20, WHITE); |
9 | rmb_down = true; |
10 | |
11 | //clear_b(); |
12 | //draw_bitmap(); |
13 | if(exist == 1) |
14 | { |
15 | clear_b(); |
16 | exist = 0; |
17 | } |
18 | else |
19 | { |
20 | draw_bitmap(); |
21 | exist = 1; |
22 | } |
23 | } |
24 | else if ( !(mouse_b & 2) && rmb_down) |
25 | { |
26 | rmb_down = false; |
27 | } |
Hehehe. Ta for the correction. My C++ syntax is still lacking.
Jeroen
hey again both of you!
sorry I haven't replied, busy with my other coures as well.
Of course, your explanation makes sense. I'm trying to make changes like you've stated. I'm declaring bool rmb_down = false right after I do exist so that it is global.
However, I keep getting this stubborn errors:
19 C:\ind study\Project 1\main.c syntax error before "rmb_down"
19 C:\ind study\Project 1\main.c `false' undeclared here (not in a function)
Why does it complain about using a bool and false..?
P.S. I think I tried to use exist as a bool and I was getting same errors.
P.S.S I am using rmb_down as an int and using 0 and 1 for false and true. So yeah, thanks, now it works as it's suppose to.
Could you post your new code?
Why does it complain about using a bool and false..?
You are compiling as C++, and not just C, right?
Lenny,
I believe it is C.
I believe it is C.
Bool is a C++ only type.
You can implement a similar type in C using enumerations though.
typedef enum { FALSE, TRUE } bool;
Actually, C99 has bool, amongst other types that were formerly C++ only. Not that his compiler seems to support C99.
int draw_bitmap() { BITMAP *ship = load_bitmap("spaceship.bmp", NULL); mx = mouse_x; my = mouse_y; masked_blit(ship, screen, 0, 0, mx, my, 64, 64); }
I think this might keep causing your program to take up more and more memory. You only have to load the bitmap once, not every time you use it. Once you are done with it you have to free it.
Don't forget to delete your pointer have you're done with it... Which, if you were going to keep doing it this way, then you'd need
int draw_bitmap() { BITMAP *ship = load_bitmap("spaceship.bmp", NULL); mx = mouse_x; my = mouse_y; masked_blit(ship, screen, 0, 0, mx, my, 64, 64); destroy_bitmap(ship); }
Otherwise destroy_bitmap() at the end of your program. =]
hey guys,
thanks for catching that I wasn't deleting the bitmap. I made that change. One weird thing though:
when I right-click, all is well. say I just right-click 3 times and the image appears on screen, then left-click. this gets rid of the image (which it shouldn't!). then if you right-click, it doesn't bring up the image till you right-click once more.
what is with that...? Other than this little thing, it works. I'd just like to know what's going on.
TIA!
We'll have to see your code again probably.