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 Mani,

Unfortunetely I'm way less of an expert than you as well and not really sure how I would go about coding that!

Thank you though :/

Manimecker
Member #15,031
April 2013

#SelectExpand
1#define transparent makecol(255,0,255) 2 3do { 4if (mouse_b & 1) { 5 if (mouse_x <= XposMax && mouse_x >= XposMin && 6 mouse_y <= YposMax && mouse_y >= YposMin) { 7 clear_bitmap(image); 8 rectfill(image,0,0,XposMax, YposMax, transparent); 9 masked_blit(image, screen, 0,0, XposMin, YposMin, 200, 50); 10 destroy_bitmap(image); 11 textout_ex(screen, font, "Bitmap destroyed.", SCREEN_W/2, SCREEN_H/2, 2, -1); 12 quit = true; 13 } 14 } 15} while (!quit);

Hope this works!

Or you can just clear the current screen to a color, with clear_to_color() function, as another member already told you.

Rich Y
Member #15,033
April 2013

thanks for the help, I'm getting an error as 'quit' is undefinied, do I need to include a specific header file to use quit? if not, what should I define it as?

Cheers

Neil Roy
Member #2,229
April 2002
avatar

Just add in a quit variable before the do loop.

bool quit = false;
do {

etc...

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

Rich Y
Member #15,033
April 2013

Unfortunetely still no luck with the following code:

bool quit = false;

do {
 if (mouse_b & 1) {
     if (mouse_x <= XposMax && mouse_x >= XposMin && 
         mouse_y <= YposMax && mouse_y >= YposMin) {
           clear_bitmap(testtext);
          rectfill(testtext,0,0,XposMax, YposMax, transparent);
           masked_blit(testtext, screen, 0,0, XposMin, YposMin, 200, 50);
          destroy_bitmap(testtext);
          textout_ex(screen, font, "Bitmap destroyed.", SCREEN_W/2, SCREEN_H/2, 2, -1);
          quit = true;
       }
    }
 } while (!quit);

The help means so much though - desperately want to get this working!

Jeff Bernard
Member #6,698
December 2005
avatar

It doesn't work because you're blitting a transparent rectangle over your image.

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

Rich Y
Member #15,033
April 2013

But it still doesn't seem to work after changing it so that it is not transparent transparent makecol(255,255,255) :/

Neil Roy
Member #2,229
April 2002
avatar

Rich Y said:

Unfortunetely still no luck with the following code:

Please elaborate. When I replied you were just asking about the quit variable. So, what is/is not happening with this? The code itself looks fine.

Also, where are you setting your XposMax, XposMin etc... at? What are they set to? The only problem I could see with this is if they aren't set the way you expect them to be.

The same question with testtext, how are you creating this bitmap? Are you checking the return value to make sure it was created successfully? Or are you loading in an image, again, are you checking the return values?

You should have something like:

XposMax = 500;
YposMax = 500;
BITMAP *testtext = NULL;
testtext = create_bitmap(400, 400);
if(!testtext) {
   printf("Error creating testtext bitmap.\n");
   exit(1);
}

etc...

Also... I'm not sure why you are blitting a transparent bitmap (or whatever colour) onto your screen then destroying it.

Why not just...

bool quit = false;

do {
 if (mouse_b & 1) {
     if (mouse_x <= XposMax && mouse_x >= XposMin && 
         mouse_y <= YposMax && mouse_y >= YposMin) {
          rectfill(screen, 0, 0, XposMax, YposMax, transparent);
          textout_ex(screen, font, "Bitmap destroyed.", SCREEN_W/2, SCREEN_H/2, 2, -1);
          quit = true;
       }
    }
 } while (!quit);

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

Rich Y
Member #15,033
April 2013

Hey Nite thanks again for the input,

okay so the minimum and maxiumum points are defined at the very top of the code with the following: int XposMax = 520, XposMin = 320, YposMax = 290, YposMin = 240;

is this ok?

for the bitmap I use as you mentioned the BITMAP *menubackground, *testtext;

defining exactly what bitmaps they are is done just above the loop we are trying to fix, here is the code in slightly more depth

#SelectExpand
1 menubackground = load_bitmap( "picture.bmp", NULL ); //Loads the bitmap file 2 testtext = load_bitmap( "testtext.bmp", NULL ); //Loads the bitmap file 3 4 blit( menubackground, screen, 0, 0, 0, 0, menubackground->w, menubackground->h ); //Draw the bitmap 5 masked_blit( testtext, screen, 0, 0, 320, 240, testtext->w, testtext->h); //Draw the bitmap 6 7bool quit = false; 8 9do { 10 if (mouse_b & 1) { 11 if (mouse_x <= XposMax && mouse_x >= XposMin && 12 mouse_y <= YposMax && mouse_y >= YposMin) { 13 clear_bitmap(testtext); 14 rectfill(testtext,0,0,XposMax, YposMax, transparent); 15 masked_blit(testtext, screen, 0,0, XposMin, YposMin, 200, 50); 16 destroy_bitmap(testtext); 17 textout_ex(screen, font, "Bitmap destroyed.", SCREEN_W/2, SCREEN_H/2, 2, -1); 18 quit = true; 19 } 20 } 21 } while (!quit); 22 23 24 25 26 27 28 while(!key[KEY_ESC]){} // if the escape key is pressed then the bitmaps are destroyed and application is closed 29 30 destroy_bitmap( menubackground ); //Frees the memory allocated to bmp when closing the program 31 destroy_bitmap( testtext ); 32 return 0; 33} 34 35 36END_OF_MAIN() //Allegro specific end main function

Thank you

Dizzy Egg
Member #10,824
March 2009
avatar

Using your original code, just have this in your main() loop (obviously after loading the bitmap etc...)

while(!key[KEY_ESC]){
  clear_to_color(screen, makecol(20, 20, 20));

  if(testText != NULL){
    blit(testText, screen, 0, 0, xPosMin, yPosMin, testText->w, testText->h);
  }
  
  if(mouse_b & 1 && (mouse_x > xPosMin && mouse_x < xPosMax && mouse_y > yPosMin && mouse_y < yPosMax)){
    destroy_bitmap(testText);
  }
}

return(0);

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

Rich Y
Member #15,033
April 2013

MASSIVE UPDATE: one of my visual studio files had duplicated without me realising, I had been working off a default file where every update which was being made was never being carried out when pressing run! (stupid me)

this following code makes it so that when the picture is pressed the program closes (not exactly what I want but so much closer now!!!)

#SelectExpand
1bool quit = false; 2 3do { 4 if (mouse_b & 1) { 5 if (mouse_x <= XposMax && mouse_x >= XposMin && 6 mouse_y <= YposMax && mouse_y >= YposMin) { 7 clear_bitmap(testtext); 8 rectfill(testtext,0,0,XposMax, YposMax, transparent); 9 masked_blit(testtext, screen, 0,0, XposMin, YposMin, 200, 50); 10 destroy_bitmap(testtext); 11 destroy_bitmap(menubackground); 12 textout_ex(screen, font, "Bitmap destroyed.", SCREEN_W/2, SCREEN_H/2, 2, -1); 13 quit = true; 14 } 15 } 16 } while (!quit); 17 18return 0; 19}

Going to try the other suggested code now and will update you!

Thanks guys

Jeff Bernard
Member #6,698
December 2005
avatar

Quote:

if(mouse_b & 1 && (mouse_x > xPosMin && mouse_x < xPosMax && mouse_y > yPosMin && mouse_y < yPosMax)){
     destroy_bitmap(testText);
  }

That's dangerous, you're likely to destroy testText multiple times.

You want to simply remove the testText image when you click on it, correct? Replace lines 13-15 of your previous code post with:

// draw what was behind testText on top of it.
blit(menubackground, screen, 0, 0, XposMin, YposMin, testtext->w, testtext->h);

Rich Y said:

so confused as to why this isn't working :/

Next time, say what is happening and what you want to happen. Just saying that you're confused doesn't help anyone to help you.

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

Rich Y
Member #15,033
April 2013

So, on a positive note, an exit button has been created - the user clicks on the picture and it exits (which is great as I will use this) however, in the process the code which meant the user could press escape and the application closes no longer works and has had to be removed otherwise it causes it to crash.

How could the following code be changed so that...

this

#SelectExpand
1bool quit = false; 2 3do { 4 if (mouse_b & 1) { 5 if (mouse_x <= XposMax && mouse_x >= XposMin && 6 mouse_y <= YposMax && mouse_y >= YposMin) { 7 clear_bitmap(testtext); 8 rectfill(testtext,0,0,XposMax, YposMax, transparent); 9 masked_blit(testtext, screen, 0,0, XposMin, YposMin, 200, 50); 10 destroy_bitmap(testtext); 11 destroy_bitmap(menubackground); 12 textout_ex(screen, font, "Bitmap destroyed.", SCREEN_W/2, SCREEN_H/2, 2, -1); 13 quit = true; 14 } 15 } 16 } while (!quit); 17 18return 0; 19} 20 21 22END_OF_MAIN() //Allegro specific end main function

also includes the following so that the user can also press escape to exit...

      while(!key[KEY_ESC]){} // if the escape key is pressed then the bitmaps are destroyed and application is closed
 
   destroy_bitmap( menubackground ); //Frees the memory allocated to bmp when closing the program
   destroy_bitmap( testtext );
     return 0;
 }
 
 
 END_OF_MAIN() //Allegro specific end main function 

So to some it up, does anybody have any idea how to combine the above to pieces of code so that the esc button will still work to exit and ideally stop the button from closing the application and instead maybe open another bitmap?
Thank you!!

- also thanks Jeff, hopefully this is a slightly more useful post for people to understand!!

and Dizzy - unfortunetely the code you provided ended up crashing after having a flashy purple rave D: not sure what went on there! =p thank though!

Neil Roy
Member #2,229
April 2002
avatar

Here is what I would change. Note, I would compile this as a console app so you get a separate DOS style console window you can printf() debug messages that will appear there and not on your game window. Then when you compile as a release game, you compile as a GUI app.

#SelectExpand
1 int XposMax = 520, XposMin = 320, YposMax = 290, YposMin = 240; 2 3 BITMAP *menubackground = NULL, *texttext = NULL; //initialize with NULL 4 5 menubackground = load_bitmap("picture.bmp", NULL); //Loads the bitmap file 6 if(!menubackground) { //ALWAYS check to see if it was loaded 7 printf("Error loading menubackground\n") 8 exit(1); 9 } 10 11 testtext = load_bitmap("testtext.bmp", NULL); //Loads the bitmap file 12 if(!testtext) { //ALWAYS check to see if it was loaded 13 printf("Error loading testtext\n") 14 exit(1); 15 } 16 17 blit( menubackground, screen, 0, 0, 0, 0, menubackground->w, menubackground->h ); //Draw the bitmap 18 masked_blit( testtext, screen, 0, 0, 320, 240, testtext->w, testtext->h); //Draw the bitmap 19 20 bool quit = false; 21 22 do { 23 if (mouse_b & 1) { 24 if (mouse_x <= XposMax && mouse_x >= XposMin && 25 mouse_y <= YposMax && mouse_y >= YposMin) 26 { 27 clear_bitmap(testtext); 28 rectfill(testtext, XposMin, YposMin, XposMax, YposMax, transparent); 29 masked_blit(testtext, screen, XposMin, YposMin, XposMin, YposMin, 200, 50); 30 textout_ex(screen, font, "Bitmap destroyed.", SCREEN_W/2, SCREEN_H/2, 2, -1); 31 quit = true; 32 } 33 } 34 if(key[KEY_ESC]) quit = true; // If ESC is pressed, exit loop 35 } while (!quit); 36 37 // I always check to make sure they're not NULL before I try and destroy them 38 if(menubackground) destroy_bitmap(menubackground); 39 if(testtext) destroy_bitmap(testtext); 40 41 return 0; 42} 43END_OF_MAIN() //Allegro specific end main function

Edit: Updated, removed the destroy_bitmap() functions from the loop, I don't like doing that inside a loop. It does no harm to wait until your program quits. Also, I added in XposMin and YposMin to your rectfill().

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

Rich Y
Member #15,033
April 2013

Hey Nite,

Thanks for that - does that mean I no longer need to have the XposMin etc defined at the top of my code? I will give this a try in the morning, hopefully all goes well and will report back to you tomorrow, have a good night guys and thank you for all the help.

Honestly thank you, it means a lot

Night all

Neil Roy
Member #2,229
April 2002
avatar

Rich Y said:

does that mean I no longer need to have the XposMin etc defined at the top of my code?

I just noticed you defined it at the top, then when you used rectfill() you started at 0,0 rather than XposMin, YposMin, which I assume was an oversight on your part.

Edit: You might also wish to change...

masked_blit(testtext, screen, XposMin, YposMin, XposMin, YposMin, 200, 50);

to..

masked_blit(testtext, screen, XposMin, YposMin, XposMin, YposMin, testtext->w, testtext->h);

This way no matter what the size of the bitmap, it will blit it properly.

I'm not sure why you're doing the rectfill(), you should probably just do as someone else suggested and blit that area from the background image if you're just trying to remove any former image that was at that spot.

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

Rich Y
Member #15,033
April 2013

Makes sense to be, thank you - Just to finalise, where would the most effective place to put code to load a new image for when the button is pressed?

(using the code in the above post or the same below)

#SelectExpand
1 int XposMax = 520, XposMin = 320, YposMax = 290, YposMin = 240; 2 3 BITMAP *menubackground = NULL, *texttext = NULL; //initialize with NULL 4 5 menubackground = load_bitmap("picture.bmp", NULL); //Loads the bitmap file 6 if(!menubackground) { //ALWAYS check to see if it was loaded 7 printf("Error loading menubackground\n") 8 exit(1); 9 } 10 11 testtext = load_bitmap("testtext.bmp", NULL); //Loads the bitmap file 12 if(!testtext) { //ALWAYS check to see if it was loaded 13 printf("Error loading testtext\n") 14 exit(1); 15 } 16 17 blit( menubackground, screen, 0, 0, 0, 0, menubackground->w, menubackground->h ); //Draw the bitmap 18 masked_blit( testtext, screen, 0, 0, 320, 240, testtext->w, testtext->h); //Draw the bitmap 19 20 bool quit = false; 21 22 do { 23 if (mouse_b & 1) { 24 if (mouse_x <= XposMax && mouse_x >= XposMin && 25 mouse_y <= YposMax && mouse_y >= YposMin) 26 { 27 clear_bitmap(testtext); 28 rectfill(testtext, XposMin, YposMin, XposMax, YposMax, transparent); 29 masked_blit(testtext, screen, XposMin, YposMin, XposMin, YposMin, 200, 50); 30 textout_ex(screen, font, "Bitmap destroyed.", SCREEN_W/2, SCREEN_H/2, 2, -1); 31 quit = true; 32 } 33 } 34 if(key[KEY_ESC]) quit = true; // If ESC is pressed, exit loop 35 } while (!quit); 36 37 // I always check to make sure they're not NULL before I try and destroy them 38 if(menubackground) destroy_bitmap(menubackground); 39 if(testtext) destroy_bitmap(testtext); 40 41 return 0; 42 }

Cheers

Neil Roy
Member #2,229
April 2002
avatar

Also, the rectfill() function could be replaced with...

blit(menubackground, testtext, XposMin, YposMin, XposMin, YposMin, XposMax, YposMax);

If your menubackground covers the area under where testtext is being drawn.

Edit: oops, typo...

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

Rich Y
Member #15,033
April 2013

RESULT!

Nite thank you so much!! It now all works great and it now works so that when the button is pressed a new image appears :D!! Thank you! - I took the printf out as it didn't recognize it, is this just purely because it's not being compiled as a console app?

Thank you again,

you alegdroids are amazing at what you do!

EDIT - I don't think the clear_bitmap(testtext); seems to be working - should this make it not visible? as currently when the button is pressed the "bitmap destroyed" text appears however the actual testtext image still shows?

Cheers once again

LennyLen
Member #5,313
December 2004
avatar

Rich Y said:

I took the printf out as it didn't recognize it, is this just purely because it's not being compiled as a console app?

No, it's because you aren't including stdio.h

Quote:

EDIT - I don't think the clear_bitmap(testtext); seems to be working - should this make it not visible? as currently when the button is pressed the "bitmap destroyed" text appears however the actual testtext image still shows?

clear_bitmap() simply clears a bitmap to color 0. It has no effect on previous versions of the bitmap you've drawn elsewhere.

Neil Roy
Member #2,229
April 2002
avatar

Rich Y said:

EDIT - I don't think the clear_bitmap(testtext); seems to be working - should this make it not visible? as currently when the button is pressed the "bitmap destroyed" text appears however the actual testtext image still shows?

Okay, I can see where you're getting confused. Remember, your testtext bitmap is in a separate area of memory. It is not located on your screen.

When you do something to your testtext bitmap, you're only effecting it, not what is on your screen. Your screen where you actually see things is a separate area of memory from your testtext bitmap. If you clear your testtext bitmap, you only clear that bitmap. If you want the changes you made to it, you need to copy it onto your screen. When you copy it to your screen (with blit()) you're copying what is in testtext into an area of memory where your screen is located.

So each time you make changes to testtext, you have to blit() it to screen if you want to actually see it. Either that, or make changes directly to the screen.

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

Rich Y
Member #15,033
April 2013

Ahhhhhhh okay, So when you use clear_bitmap (clearing the bitmap to the color 0) does this not hide the bitmap at all? Because preferably I'd like it to be hidden after it is pressed - masked_blit is used after clear_bitmap therefore the changes onto the screen have been made.

Thanks!

Slowly but surely understanding this now :)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

There's no such thing as 'hiding a bitmap'. Either you display one or you don't! If you don't change the screen, nothing will look different. So you have to draw to the screen usually using blit all at once from a buffer.

1. clear buffer
2. draw onto buffer
3. draw buffer onto screen using blit
4. goto 1.

If you're asking what makes a bitmap translucent or transparent, that depends on the color depth when using allegro 4. In 32 bit color depth, the alpha controls how much of the image you will see, with 0 alpha transparent, and 255 alpha opaque.

Neil Roy
Member #2,229
April 2002
avatar

If you no longer wish to show testtext onto the screen, you don't need to alter it. Just don't blit() it to the screen.

Each time you update the screen, you could set a flag as to whether or not you need to copy / blit() a certain bitmap onto the screen, or have a pointer to what you wish to draw etc.

You CAN clear the bitmap so that it is completely transparent and doesn't show up, but it is a waste of time to blit() a clear bitmap each update.

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

Rich Y
Member #15,033
April 2013

bingo! Nite you're a genius :) finally have it and can now turn this into a fully working interactive menu thank you so much :)

 1   2   3 


Go to: