Reputation: 271
I want to make an Allegro 5 program , where the cursor must change it's appearance when the mouse button is pressed . As far as I understand this statement events.type!=ALLEGRO_EVENT_MOUSE_BUTTON_UP
never becomes false . But I can't understand why because after releasing the button the loop doesn't stop . Can you tell me where my mistake is and if there is a better alternative way ?
while(loop){
al_clear_to_color(al_map_rgb(0,0,0));
ALLEGRO_EVENT events;
al_wait_for_event(event_queue, &events);
if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
loop=false;
}
if(events.type == ALLEGRO_EVENT_MOUSE_AXES ){
x=events.mouse.x;
y=events.mouse.y;
buffer = released;
}
if( events.type==ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
while (events.type!=ALLEGRO_EVENT_MOUSE_BUTTON_UP){
x=events.mouse.x;
y=events.mouse.y;
al_draw_bitmap(pressed, x , y , NULL );
al_flip_display();
al_clear_to_color(al_map_rgb( 0 , 0 , 0));
}
al_draw_bitmap(released, x , y , NULL );
al_flip_display();
}
Upvotes: 0
Views: 2129
Reputation:
You never check for new event inside while (events.type!=ALLEGRO_EVENT_MOUSE_BUTTON_UP)
loop and the value of events.type cannot ever change.
Your program is already running in a loop (while(loop){
), there is no need to create another one. You should create a new variable that depends on the state of ALLEGRO_EVENT_MOUSE_BUTTON_UP
and changes the position of your mouse, etc...
Something like that: ( pseudo code!)
while(loop){
al_clear_to_color(al_map_rgb(0,0,0));
ALLEGRO_EVENT events;
_Bool change = false ;
al_wait_for_event(event_queue, &events);
if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
loop=false;
}
if(events.type == ALLEGRO_EVENT_MOUSE_AXES ){
x=events.mouse.x;
y=events.mouse.y;
buffer = released;
}
if( events.type==ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
change = true ;
if( events.type==ALLEGRO_EVENT_MOUSE_BUTTON_UP)
change = false ;
if( change )
al_draw_bitmap(pressed, x , y , NULL );
else
al_draw_bitmap(released, x , y , NULL );
al_clear_to_color(al_map_rgb( 0 , 0 , 0));
al_flip_display();
}
Upvotes: 1