Hi all:
I have a question concerning the use of the mouse.
I have a yellow rectangle at the bottom of the screen.
I have an image showing in the middle of the screen.
What I want to do is click inside the yellow rectangle on the left side,
move the mouse to the right to make the image larger, move the mouse back
to the left to make the image smaller. Is this even possible using Allegro5?
I hope my explanation is clear. Thanks for your time.
Scooter
I’m assuming you know the correct commands of A5 so I’ll paraphrase what I would do. Keep in mind I’m a novice with A5 so some of the others might give a more concise way of doing this.
I’m also assuming you already have the smaller image drawn as a default size.
int pos_x = 0;
int pos_y = 0;
bool Img_Size = false; // default smaller size
//With in the program while loop
// I would record mouse position.
if(event.type == ALLEGRO_EVENT_MOUSE_AXES)
{
pos_y = event.mouse.y;
pos_x = event.mouse.x;
} // end mouse axis
// Then I would react when button pressed.
if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN){ // The pos_X & Y are coordinates from a program I’m writing. You would use yours of course.
if(pos_x >= 180 && pos_y >= 240){ // Confirming I’m within the upper left hand corner of the “Large Image” section of the yellow rectangle.
if(pos_x <= 290 && pos_y<= 280){ // And that I’m within lower right hand corner of the “Large Image” section of the yellow rectangle.
Img_Size = true;
}}// end location if
if (pos_x >= 440 && pos_y >= 240){ // Confirming I’m within the upper left hand corner of the “Small Image” section of the yellow rectangle.
if(pos_x <= 560 && pos_y<= 280){ // And that I’m within lower right hand corner of the “Smaller Image” section of the yellow rectangle.
Img_Size = false;
}}// end location if
}// end button down
// Then draw specific image based on Img_Size
if( Img_Size == true)
al_draw_bitmap(lrg_bmp, x, y, 0);
if( Img_Size == false)
al_draw_bitmap(small_bmp, x, y, 0);
Let me know if I’ve misunderstood your question. Good Luck.
Thanks AceBlkwell for your quick reply.
Looks like we both are in the same boat. I also am not an expert but I do
enjoy programming. Great post. That is real close to what I have now. I can
click anywhere in the yellow rectangle and the image will respond.
What I would like to do now is click in the left side of the rectangle,
hold the left mouse button down, move the mouse to the right and the image gets
larger in real time. Still holding the mouse button down, move the mouse to the
left and the image gets smaller in real time. My question is if this is possible
to do in allegro5.
Have a great day!
Scooter,
I enjoy programming as well. I usually decide to program something just to see if I can do it. Most of my programming is never seen by anyone but me.
In any case, sounds like you are asking about real time scaling. And that is well above my abilities. Like you, I'm not sure Allegro has that capability. Maybe one of the site experts might chime in.
Good luck and keep me updated on how it turns out.
BTW I found these doing a quick check...
al_draw_scaled_bitmap
&
https://www.youtube.com/watch?v=vkWivB3aCAo Rotating, Scaling and Tinting BMP in Allegro 5
something like this. if only one rect, don't need the index var
Thanks AceBlkwell:
That is exactly the function I am using.
Here is the code that does the trick:
pos_x = ev.mouse.x; // get mouse x position
pos_y = ev.mouse.y; // get mouse y position
// al_set_mouse_xy(display, pos_x, pos_y);
image_height = al_get_bitmap_height(image);
image_width = al_get_bitmap_width(image);
al_clear_to_color(al_map_rgb(0,0,0));
draw_work_area();
scale_factor = working_screen_height / image_height;
dist = pos_x - 1030; // 1030 is the distance to left edge of rectangle
scale_factor = scale_factor * (dist / 125);
al_draw_scaled_bitmap(image,
0, 0, image_width, image_height,
(screen_width - (scale_factor * image_width)) / 2.0, // x position
((working_screen_height - scale_factor * image_height) / 2.0), // y position
image_width * scale_factor, image_height * scale_factor, 0);
al_flip_display();
I am going to redo some of the code to see if I can figure this out.
I will keep you posted!
Nice Scooter. You'll have to upload the finished program so I can see it in action.
AceBlkwell:
Sorry, could not work it out.
Can move the mouse when in the rectangle
and mouse coordinates will change like they
should but image does not change.
Probably screwed up events. Don't even
know if it is possible using Allegro.
Have a great day!
If you can draw a rectangle with allegro, it is possible. :/
Think about it in psuedocode. What do you need data wise? You need the mouse coordinates, you need the original rectangle and the new rectangle.
If you get a mouse button down event, check if it is over the edge of the rectangle. If so, you're now dragging the rectangle. drag = true;
If you get a mouse button up event, drag = false, and you should 'let go' of the rectangle and save its last position as the original.
If you get a mouse axes event, check if you are dragging (drag == true) and if so, update the rectangle position.
If you really need the scale, calculate it from the two rectangles.
It's perfectly possible with Allegro5!!
Attached is an example program (tested on Windows), and the source code:
Thanks Edgar and Dizzy
This is exactly what I was trying to do.
I had everything right except the events part.
I could pick inside the rectangle and the image
would appear correctly. When I clicked inside the
the rectangle and moved the mouse nothing would happen.
Dizzy:
I had to modify your program to work on Linux.
Worked perfectly. Great job!
Thanks to both of you for helping me on this!