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>
#include <stdlib.h>
#include<allegro5/allegro.h>
#include "allegro5/allegro_image.h"
#include <allegro5\allegro_primitives.h>
enum GAME_KEYS
{
KEY_ESCAPE
};
int main()
{
ALLEGRO_DISPLAY *ventana=NULL; //crea el display con la ventana
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_EVENT_QUEUE *event_mouse = NULL;
al_init(); //Se declara para señalar que empieza allegro
al_install_keyboard();
al_init_primitives_addon();
al_install_mouse(); //installa el mouse
ventana = al_create_display(640,480); //crea un display de 640x480
al_clear_to_color(al_map_rgb(0,0,0)); //pone la pantalla en negro
al_flip_display(); //intercambia el buffer
al_rest(2);
ALLEGRO_BITMAP *pantalla_principal;
al_init_image_addon();
event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_keyboard_event_source());
pantalla_principal=al_load_bitmap("Menu_principal.jpg");
al_draw_bitmap(pantalla_principal,0,0,0);
al_flip_display();
al_rest(2); //tiempo que se espera para realizar la siguiente operacion
int key[1]={0};
int mouse_x,mouse_y;
int width = 640;
int height = 480;
mouse_x=width/2;
mouse_y=height/2;
event_mouse=al_create_event_queue();
al_register_event_source(event_mouse, al_get_mouse_event_source());
al_register_event_source(event_mouse, al_get_display_event_source(ventana));
printf("mouse_x: %d",mouse_x);
printf("\nmouse_y: %d",mouse_y);
do
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
ALLEGRO_EVENT ev_mouse;
al_wait_for_event(event_mouse, &ev_mouse);
if(ev_mouse.type == ALLEGRO_EVENT_MOUSE_AXES)
{
mouse_x = ev_mouse.mouse.x;
mouse_y = ev_mouse.mouse.y;
if(((mouse_x>0)&&(mouse_x<100))&&((mouse_y>0)&&(mouse_y<200)))
{
al_draw_rounded_rectangle(0,0,100,200,1,1,al_map_rgb(255, 0,0),1);
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
}
}
else
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
key[KEY_ESCAPE]=1;
}
} while(!key[KEY_ESCAPE]);
al_destroy_event_queue(event_queue);
al_destroy_event_queue(event_mouse);
al_destroy_display(ventana);
al_destroy_bitmap(pantalla_principal);
return 0;
}
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):
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.