![]() |
|
I have some questions about mouse routines |
alexis escalona
Member #16,118
November 2015
|
Hi im new at programming at allegro 5. I´m making a menu for a game. My question is when I run the program it does not detect the mouse position, I dont know what I´m doing wrong. What I want to do is to evaluate if the mouse is in a certain position it will draw a rounded rectangle. Here is my code #include <stdio.h> enum GAME_KEYS int main() } while(!key[KEY_ESCAPE]); |
SiegeLord
Member #7,827
October 2006
![]() |
Your event handling is a bit muddled, and I think a whole bunch of confusion is happening because you're mixing drawing and logic and event handling. It is best to separate all 3. I suggest looking at this tutorial https://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Input . The general idea is that you have a loop, and during each iteration you only process a single event (so you'll have only a single al_wait_for_event call). In your case, you could have a loop like this (see the tutorial for the rest of the code): 1 bool draw_rectangle = false;
2 while (1) {
3 ALLEGRO_EVENT ev;
4 al_wait_for_event(event_queue, &ev);
5
6 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
7 int mouse_x = ev.mouse.x;
8 int mouse_y = ev.mouse.y;
9 if (mouse_x > 0 && mouse_x < 100 && mouse_y > 0 && mouse_y < 200) {
10 draw_rectangle = true;
11 } else {
12 draw_rectangle = false;
13 }
14 redraw = true;
15 }
16
17 if (redraw && al_is_event_queue_empty(event_queue)) {
18 redraw = false;
19
20 al_clear_to_color(al_map_rgb(0, 0, 0));
21
22 if (draw_rectangle) {
23 al_draw_rounded_rectangle(0, 0, 100, 200, 1, 1, al_map_rgb(255, 0, 0),
24 1);
25 }
26 al_flip_display();
27 }
28 }
"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
kenmasters1976
Member #8,794
July 2007
|
You should wrap your code in <code> tags for better reading (click on Formatting Help while replying to get help). It seems that you're only reading a mouse event whenever you get a key up event; that will most likely return you only one of several events already in the mouse queue. As suggested, try reading the tutorials. Using events migh seem a bit hard at first but it's totally worth it.
|
|