0

我想做一个 Allegro 5 程序,当按下鼠标按钮时,光标必须改变它的外观。据我了解,这句话events.type!=ALLEGRO_EVENT_MOUSE_BUTTON_UP永远不会成为错误的。但我不明白为什么,因为释放按钮后循环不会停止。你能告诉我我的错误在哪里,是否有更好的替代方法?

        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();

    }
4

1 回答 1

1

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();

}
于 2013-06-22T13:12:37.100 回答