Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Button Pressing

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Button Pressing
Rich Y
Member #15,033
April 2013

Hey allegro community!

I'm rather new here however I'm seeking some assistance on a little application I'm attempting to create within allegro. I would like to display an image on the screen (in a static location) and when it is clicked upon I would like it to be destroyed.

I've attempted this in a few ways but nothing seems to be working, here is what I have so far..

#SelectExpand
1 2int XposMax = 520, XposMin = 320, YposMax = 290, YposMin = 240; 3... //I have defined the maximum and minimum click areas of the image here - the screen size is 640 x 480 and the image size is 200 x 50 4 5 6 7if(mouse_b & 1) // this is if the left mouse button is clicked 8{ 9if(mouse_x < XposMax && mouse_x > XposMin) //and if the mouse position is lower than 520pixels and higher than 320 10{ 11 if(mouse_y < YposMin && mouse_y > YposMin) // here is the same only if the mouse position is lower than 290 on the Y axis but larger than 240 pixels. 12 { 13 destroy_bitmap( testtext ); //finally, this is the outcome, I want to hide the image (destroy it) when the image has been clicked on 14 } 15 } 16}

I have managed get it working so that I can destroy bitmaps by pressing specific keys however I can't figure out how to do it when you are clicking on them in a precise location (or infact how to make it do it when you are just clicking the mouse at all!)

If anyone could help me out that would be amazing!

Thank you!!

gnolam
Member #2,030
March 2002
avatar

  1. Code tags are <code></code>, not [code][/code]. Please edit your post to make your code readable for future viewers. EDIT: guess you found the formatting help. :)

  2. You've got your y coordinate test wrong. You're testing both inequalities against YposMin. So the mouse's y coordinate has to be both less than 240 and larger than 240 at the same time to trigger your conditional. Obviously, this is impossible.

  3. Are you really, really sure that destroying the bitmap is what you want to do? Destroying it and then attempting to draw it will cause a segfault.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

iamxzki
Member #15,002
March 2013
avatar

Need this tooo...

Rich Y
Member #15,033
April 2013

Hey there,

Haha yeah I found the formatting help! - Basically I want to remove/hide the bitmap image - Ideally what I hope to do is make something like for example, the player has to click on the image, this will then be hidden and trigger another image to be displayed (somewhat like a game). Just trying to get my head around things!

Can't seem to edit the first post but yes, I should have put if(mouse_y < YposMax && mouse_y > YposMin) instead!!

Thank you

Rich

gnolam
Member #2,030
March 2002
avatar

... I'd reply again, but I'm wary of anyone who has sockpuppet accounts. With semi-pornographic avatars.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Rich Y
Member #15,033
April 2013

Haha sorry about that, I have a seperate account automatically logged in on Chrome (I'm currently using IE9) from a while back and didn't realise I wasn't on the right account - have no idea what the log-in details for that account are so had to make a new account specifically for this post! (Now look a bit of a wally talking to myself after changing the text as I thought I'd get bashed for making multiple accounts!) Sorry!!

- Oh, and you can't not like the lovely Amanda Holden can you?! =p - Would you prefer me change the avatar?

I have tried updating the code so that it reads better however I'm stil having no luck! ): - Update:

if((mouse_x <= XposMax) && (mouse_x >= XposMin))
{
    if((mouse_y <= YposMax) && (mouse_y >= YposMin))
    {
        destroy_bitmap( testtext ); //Destroy bitmap when button is pressed
    }

Is there any chance you could continue to help me with this issue please?

Thank you again

Dizzy Egg
Member #10,824
March 2009
avatar

Have you installed the mouse?

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Rich Y
Member #15,033
April 2013

Hey Dizzy, I have indeed - the mouse button doesn't seem to be functioning how it should though ):

I tried simply doing

if(mouse_b & 1){
destroy_bitmap( testtext )
}

which I thought would just simply destroy my image when clicking the mouse but that doesn't work either?

Thanks

Dizzy Egg
Member #10,824
March 2009
avatar

Are you clearing the screen or back-buffer after this?

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Rich Y
Member #15,033
April 2013

The only thing that happens after this is the following (checking if the escape key is pressed which is close the program)

Here is the end of the code from the mouse cick point

#SelectExpand
1if(mouse_b & 1) // 1 is the left mouse button, 2 is the right mouse button 2{ 3if((mouse_x <= XposMax) && (mouse_x >= XposMin)) 4{ 5 if((mouse_y <= YposMax) && (mouse_y >= YposMin)) 6 { 7 destroy_bitmap( testtext ); //Destroy bitmap when button is pressed 8 } 9 } 10} 11 12 13 14 15 16 while(!key[KEY_ESC]){} // if the escape key is pressed then the bitmaps are destroyed and application is closed 17 18 destroy_bitmap( menubackground ); //Frees the memory allocated to bmp when closing the program 19 destroy_bitmap( testtext ); 20 return 0; 21} 22 23 24END_OF_MAIN() //Allegro specific end main function

Thanks

Dizzy Egg
Member #10,824
March 2009
avatar

So how do you know it ISN'T being destroyed then!!?!? You don't even check!?

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Jeff Bernard
Member #6,698
December 2005
avatar

destroy_bitmap frees the memory allocated for the bitmap, it does not do any rendering.

gnolam said:

I'd reply again, but I'm wary of anyone who has sockpuppet accounts. With semi-pornographic avatars.

You need a fully-pornographic avatar to get gnolam's attention. ;)

--
I thought I was wrong once, but I was mistaken.

Neil Roy
Member #2,229
April 2002
avatar

You should probably set a flag that indicates whether or not to display the image.

You don't want to destroy the image unless you no longer plan to use it again.

So, if the image is clicked on, set a flag like image[x] = false; to let the game know that image number x should not be displayed. Then when you get to your drawing code, you can loop through the images to be displayed and skip drawing any that are flagged as false. Then when your game exits, you can destroy them.

You should be very careful when destroying anything though, as you're telling the compiler than you're no longer using that image and it frees up the memory it was pointing to and in order to use it again, you will need to reload it.

---
“I love you too.” - last words of Wanda Roy

Rich Y
Member #15,033
April 2013

Thanks for the replies guys,

at the moment destroying the image is fine as I won't be using the same image again in the application - I would just currently like to get it so when the image is pressed it dissapears (not too bothered how it dissapears, just want to make sure it does) - I think the trouble I am having is with the application understanding exactly where the mouse is, is the code I provided sufficient enough to make the program carry out exactly what I want it to do and detect if the mouse is over the image or not??

Thank you guys!

Jeff Bernard
Member #6,698
December 2005
avatar

Your check also needs to be in a loop, otherwise good luck pressing on the image exactly when that line of code is run. AND you still need to render the changes. Ie-

clear_to_color(screen, makecol(255,255,255));

Right not you're not drawing anything after the image is clicked, so it won't matter if your logic for detecting the press is correct.

--
I thought I was wrong once, but I was mistaken.

Rich Y
Member #15,033
April 2013

Hey Jeff thanks for posting some help,

I will be drawing something after the image is destroyed however above I was told that it couldn't be the same image so I understood this that I could draw a different image yes?

I read your comment about that it needs to be in a loop (which when you mention it is now obvious) - Could you check the update below to see if I'm putting the updates in the correct places? Thank you!

 while(mouse_b & 1) // this is if the left mouse button is clicked
  {
  if(mouse_x < XposMax && mouse_x > XposMin) //and if the mouse position is lower than 520pixels and higher than 320
 {
    if(mouse_y < YposMin && mouse_y > YposMin) // here is the same only if the mouse position is lower than 290 on the Y axis but larger than 240 pixels.
    {
        clear_to_color(screen, makecol(255,255,255));
//instead off...
        destroy_bitmap( testtext ); //finally, this is the outcome, I want to hide the image (destroy it) when the image has been clicked on
  }
 }
 }

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Your hit box test has been fine for a while now. And you don't want to wait forever while the mouse button is down (while(mouse_b & 1)) {/*...*/} What is it that isn't working? Add in a cout to say you destroyed it (to indicate whether the mouse click was successful).

do {
while (ticks < 1) {rest(0);}
if (mouse_b & 1) {
   if (x < xmax && y < ymax && x > xmin && y > ymin) {
      if (current_image) {
         destroy_bitmap(current_image);
         current_image = 0;// or load a new one, whatever.
         cout << "Mouse click detected, bitmap destroyed.}
      }
   }
}
} while (!quit);

Rich Y
Member #15,033
April 2013

Hey there Edgar,

The thing that isn't working is simply the image does not destroy or disspear when clicked on and as I'm quite new to this I'm not to sure as to why this is, I'll giveg what you posted above a try however does the cout function work in allegro?

Cheers

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

cout will work if you compile as a command line program, or if you run it from the command line.

What do you mean it doesn't destroy? Or disappear? Both of those are things you have to do.

Just show your full logic and display code.

Rich Y
Member #15,033
April 2013

my application is a graphical one using images therefore it can't be done in command line ): - I will message you the full code - it's for a normal allegro empty project.

Cheers

Neil Roy
Member #2,229
April 2002
avatar

You're misunderstanding what destroy_bitmap() does. It only de-allocated the memory that the image used, it doesn't effect what you see on screen. Once you draw the image on screen, you have essentially copied the image from the memory location where it was stored to the memory location that is your screen you see. When you destroy it you de-allocate the memory used by the image, but you don't effect the screen at all, so you still see it on screen. you're basically destroying the original, but the copy on screen is still there. You need to draw something else at the same location the image used to be located at, to show it gone.

---
“I love you too.” - last words of Wanda Roy

Rich Y
Member #15,033
April 2013

Thank you nite,

You're right my understanding of destroy bitmap was totally incorrect! - The image I am trying to hide sits ontop of another background image, essentially I would like it so that when the user presses on the image it changes both the background image and the main image which has been pressed, what would you suggest doing instead of destroy bitmap?

Cheers

beoran
Member #12,636
March 2011

Store a value in a variable that determines whether or not you will display the bitmap. :)

Rich Y
Member #15,033
April 2013

Can't for the life of me work this one out - I've gone through everyones comments but don't seem to be experienced enough to put them into working use. :/ - here is the end of my code, does this while loop for the mouse button click look like it should actually work or not??

#SelectExpand
1while(mouse_b & 1) // this is if the left mouse button is clicked 2 { 3 if(mouse_x < XposMax && mouse_x > XposMin) //and if the mouse position is lower than 520pixels and higher than 320 4 { 5 if(mouse_y < YposMin && mouse_y > YposMin) // here is the same only if the mouse position is lower than 290 on the Y axis but larger than 240 pixels. 6 { 7 clear_to_color(screen, makecol(255,255,255)); 8 9 } 10 } 11 } 12 13 14 15 16 17 while(!key[KEY_ESC]){} // if the escape key is pressed then the bitmaps are destroyed and application is closed 18 19 destroy_bitmap( menubackground ); //Frees the memory allocated to bmp when closing the program 20 destroy_bitmap( testtext ); 21 return 0; 22} 23 24 25END_OF_MAIN() //Allegro specific end main function

Thanks and sorry about this everyone

Manimecker
Member #15,031
April 2013

This might be a noob comment, as I'm not any allegro expert, but you can try clearing your BITMAP, then filling your BITMAP with a rectfill() with transparent color (makecol(255,0,255)), then blit it to the screen, then destroy the original BITMAP and print "Bitmap destroyed."

No idea if this is right coded, but it might help you.

 1   2   3 


Go to: