如何在ALLEGRO 5中创建USER DEFINED EVENTS

问题描述 投票:1回答:1

我正在研究allegro 5中的游戏,我想在屏幕上动态创建矩形对象,并使用鼠标按钮进行点击

al_register_event_source( event_queue, al_get_timer_event_source(timer));
al_register_event_source( event_queue, al_get_mouse_event_source());

al_clear_to_color(al_map_rgb(0, 0, 0));
al_flip_display();

al_start_timer(timer);

while ( !exit )
{
    ALLEGRO_EVENT ev;
    al_wait_for_event( event_queue, &ev);

    if (ev.type == ALLEGRO_EVENT_TIMER)
    ;
    else if ( ev.type == ALLEGRO_EVENT_MOUSE_AXES )
     {
       x = ev.mouse.x;
       y = ev.mouse.y;
     }
    else if ( ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN )
     {
       if ( x >= rect.x && x <= rect.maxx && y >= rect.y && y <= rect.maxy )
             destory ( rect );
     }
    else if ( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE )
        break;
    if ( redraw && al_event_queue_is_empty(event_queue)){
        redraw = false;
        al_draw_rectangle ( rect.x, rect.y, rect.maxx, rect.maxy, blue, 1 );
        al_flip_display();
        al_clear_to_color(al_map_rgb(0, 0, 0));
    }
}

但这只是一个矩形的硬编码。如何为此事件制作可以处理按钮等矩形的事件。

c allegro5
1个回答
0
投票

响应按钮按下不需要用户事件。

相反,你应该从不同的角度来看待这个问题。将ALLEGRO_EVENT传递给您的按钮类,并让它返回是否被点击。

bool Button::ButtonPressed(ALLEGRO_EVENT ev) {
   if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && ev.mouse.button == 1) {
      if (our_area.Contains(ev.mouse.x , ev.mouse.y)) {return true;}
   }
   return false;
}

但是,您问过如何在Allegro 5中创建用户事件,所以我也会回答这个问题。

有关详细信息和代码示例,请参阅https://liballeg.org/a5docs/trunk/events.html#allegro_user_event。基本上,您创建并初始化ALLEGRO_EVENT_SOURCE,将其注册到事件队列,然后使用al_emit_user_event侦听您发出的消息。

© www.soinside.com 2019 - 2024. All rights reserved.