Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro Buttons

This thread is locked; no one can reply to it. rss feed Print
Allegro Buttons
Glorfindal
Member #10,771
March 2009

Posts: 5| I hope this is the right place to post, I'm new here. I have gotten the first part of my game done, I have all the animations the menu loads and everything. How do I make it when the user left clicks an image the screen changes and load new images? Any help would be greatly appreciated.

Jeff Bernard
Member #6,698
December 2005
avatar

Something like:

if (state == menu_state)
{
   pos = mouse_pos;
   x = pos >> 16;
   y = pos & 0x0000ffff;

   if (mouse_b&1 // mouse click
      &&  x > button_x && x < button_x+button_w // mouse on button
      && y > button_y && y < button_y+button_h) // mouse on button
          load_new_things_and_change_state();
}

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

Kris Asick
Member #1,424
July 2001

Making a mouse-driven interface that's bug-free can be difficult. Allegro has a GUI system you can use if you so desire. The simplest approach you can take is to track two coordinates: Where the mouse was when the left button was pressed down and where the mouse was when the left button is released. If both coordinates are within the bounds of the image you want clicked, then it was clicked properly and you can proceed with your program.

Here's some pseudocode:

1// Global Variables:
2int mx_down = -1, my_down = -1, mx_up = -1, my_up = -1;
3int prev_mouse_b = 0;
4 
5void MouseScan (void)
6{
7 if (prev_mouse_b & 1)
8 {
9 // Mouse button was held down last we checked. Is it released?
10 if (!(mouse_b & 1))
11 {
12 mx_up = mouse_x;
13 my_up = mouse_y;
14 }
15 }
16 else
17 {
18 // Mouse button wasn't held down last we checked. Has it been pressed down yet?
19 if (mouse_b & 1)
20 {
21 mx_down = mouse_x;
22 my_down = mouse_y;
23 mx_up = my_up = -1;
24 }
25 else
26 {
27 // If not, we should clear the coordinates so as not to generate a second click!
28 mx_down = my_down = mx_up = my_up = -1;
29 }
30 }
31 prev_mouse_b = mouse_b;
32}
33 
34void GameLoop (void)
35{
36 do {
37 MouseScan();
38 
39 // Scan to see if both the m_down and m_up coordinates are within your image rectangle
40 // and if so, go to your next subroutine.
41 
42 // Process Logic and Render.
43 } while (gameloop_active);
44}

If you want to get into anything more complicated such as scroll bars or checkboxes, you really should look into using Allegro's GUI functions or attempt to build your own.

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

Glorfindal
Member #10,771
March 2009

Posts: 5| I don't understand. Could somebody give me code that when the Example.bmp is left clicked a box could come up and say you left clicked?

Kris Asick
Member #1,424
July 2001

There's no direct link between the mouse and images. You have to create one, either by using Allegro's GUI, or by making your own. It's hard to get much simpler without doing all the work for you.

Use the code I already provided to detect where clicks are made. Use the following routine to determine if both coordinates are within a rectangle.

int BothPointsInRect (int rect_x1, int rect_y1, int rect_x2, int rect_y2, int p1x, int p1y, int p2x, int p2y)
{
    if (p1x >= rect_x1 && p1x <= rect_x2 && p1y >= rect_y1 && p1y <= rect_y2 &&
    p2x >= rect_x1 && p2x <= rect_x2 && p2y >= rect_y1 && p2y <= rect_y2)
        return 1;
    else
        return 0;
}

You can get the height and width of a BITMAP object by doing:

mybitmap->h // for vertical height
mybitmap->w // for horizontal width

If you want to draw a mouse cursor you'll either need to load one into memory and draw it manually, or use Allegro's built-in pointer routines. If you haven't looked yet, I urge you to take a good look through the mouse section of the Allegro Manual: http://www.allegro.cc/manual/api/mouse-routines

From there, just experiment. The worst you'll do is lock everything up and have to restart. :P

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

Johan Halmén
Member #1,550
September 2001

Learn to use any GUI library/tool. I use Allegro GUI, but most of us would recommend MasKing (including me :)). Of course it's good exercise to create a button from scratch, but in the long run you probably want to do more programs with more buttons and GUI gadgets. At some point you want to create your own GUI system, which is not a bad idea. But at some point you may be satisfied with using someone else's GUI system. And before creating one of your own, you should learn to use f.i. Allegro's GUI, just to get to know everything that a GUI system should take care of (not that Allegro GUI does a good job there :P).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Glorfindal
Member #10,771
March 2009

Posts: 5|Btw: That's the amount of posts I have made.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I need something like this:

if (mouse_b & 1)                            
   printf("Left button is pressed\n");

but the only with certain coordinates and I'll draw the rectangle with the drawing primitives computer.

LennyLen
Member #5,313
December 2004
avatar

Quote:

I need something like this:

if (mouse_b & 1)                            
   printf("Left button is pressed\n");

but the only with certain coordinates and I'll draw the rectangle with the drawing primitives computer.

You've already been given examples of how to do this. If you don't understand them, let us know what you don't understand so that it can be explained to you.

Quote:

Posts: 3|Btw: That's the amount of posts I have made.

We really don't care how many posts you've made.

Glorfindal
Member #10,771
March 2009

Posts: 5|I don't understand the rectangle thing in the button code. In other words what does (p1x >= rect_x1mean?:-/

count
Member #5,401
January 2005

p1x and rect_x1 are the int values passed to the function.
You are supposed to pass
- the x and y position of the top left corner of the button
- the x and y position of the bottom right corner of the button
- the x and y position of the mouse when the mouse button was clicked
- the x and y position of the mouse when the mouse button was released.

The function will return true when the mouse was inside/on your button when pressed and released.
Otherwise it will be false.

Quote:

Posts: 4

WHAT are you trying to do there?
Are you that incapable of posting in a forum without a postcounter?
You don't need to provide this useless info in every of your posts.

Glorfindal
Member #10,771
March 2009

What's so bad about post counters? And is there any button code in the Windows library, and if so is it simpler?

karistouf
Member #5,126
October 2004
avatar

usually windows library is non sense reading... some kind of barbarian language of outer space... working with allegro you will constitute your own libraries of functions, with the feel and look you like. and far more convenient to your usage ( never seen boxes to check with 3rd button roll for example, despite this is handy) ;D

someone972
Member #7,719
August 2006
avatar

Glorfindal said:

What's so bad about post counters?


Post counters give a false sense of experience in my view. Someone who posts a lot may be seen to be more experienced than someone with few posts, when it can actually be the opposite. Just my $0.02.

With no post counters there's also no status for the number of posts you've made. This helps to prevent certain types of people from spamming or posting useless info just to get their post count up and raise their status. (I'm in no way saying that you are this kind of person by the way ;))

Just think of it this way: If you don't use a post counter you wont have to edit every one of your posts when you make a new one.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Speedo
Member #9,783
May 2008

Quote:

And is there any button code in the Windows library, and if so is it simpler?

Not buttons that you'll use in allegro games. And I hate to break it to you, but the examples you've been given are about as simple as they get.

Onewing
Member #6,152
August 2005
avatar

Quote:

What's so bad about post counters?

Well, it is on your profile. You're also ranked there according to your post count. No need to stamp an id for each post.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

karistouf
Member #5,126
October 2004
avatar

well its normal to discover that programming is not that simple.
examples about buttons are very simple, and the verry first step on how to do things.

but do not desesperate, this will come with time and practice. in 3 months you will laugh about buttons codes....

everybody passed by this step, i think ( or human being has got a C library in the brain somewhere ?).

and at the end, despite things are growing more complex in their realisation, you may discover that more and more you know amount of things, and more and mmore there is things to discover ;)

Go to: