在制作某种菜单时出现问题

时间:2013-05-23 16:00:49

标签: c++ menu mouse allegro allegro5

我将这件事作为我计划的一部分。语言是C ++,使用Allegro库。我想让它做下一个:点击时,两个矩形中的一个出现边框。

它发生了,但开始时只发生过一次。之后,当我移开鼠标时,边框一直消失。此外,在我点击的任何地方,边框都会显示在正确的位置。

mouseX和mouseY工作正常,即使数字相同。但是这个动作只在我想要的方式上发生一次。当我点击时如何将其扩展到每个案例?

    if(asd.type == ALLEGRO_EVENT_MOUSE_AXES)
    {
        mouseX = asd.mouse.x;
        mouseY = asd.mouse.y;
    }

    if(asd.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
    {
        if(asd.mouse.button & 1)
        {
            if (mouseX > 592 && mouseX < 608 && mouseY > 540 && mouseY < 556)
            {
                Chosen_Cell = 1;
                borderX = 592;
                borderY = 540;
            }

            if (mouseX > 592 && mouseX < 608 && mouseY > 556 && mouseY < 562)
            {
                Chosen_Cell = 2;
                borderX = 592;
                borderY = 556;
            }

            al_draw_rectangle(borderX, borderY, borderX + 16, borderY + 16, al_map_rgb(255, 255, 0),2);

            if (16 < mouseX && mouseX < 528 && 16 < mouseY &&mouseY < 736)
            {
                switch (Chosen_Cell)
                {
                                        //blahblah, not important
                }   
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

边框正在消失,因为您在按钮按下事件中调用了绘图代码。这意味着一旦释放按钮,矩形将不再显示。通常更好的做法是让事件更新值,然后在事件队列为空并且计时器调用重绘时在屏幕上绘制所有内容。

//setup a timer and a redraw var
ALLEGRO_TIMER *timer;
bool redraw = false;

timer = al_create_timer(1.0/FPS);

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

while(running)
{
    if(asd.type == ALEGRO_EVENT_TIMER)
        redraw = true;
    if(asd.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
    {
        if(asd.mouse.button & 1)
        {
           if (mouseX > 592 && mouseX < 608 && mouseY > 540 && mouseY < 556)
           {
               Chosen_Cell = 1;
               borderX = 592;
               borderY = 540;
           }

           if (mouseX > 592 && mouseX < 608 && mouseY > 556 && mouseY < 562)
           {
               Chosen_Cell = 2;
               borderX = 592;
               borderY = 556;
           }
       } 
    if(redraw && al_is_event_queue_empty(asd))
    {
        al_draw_rectangle(borderX, borderY, borderX + 16, borderY + 16, al_map_rgb(255, 255, 0),2);
        redraw = false;
        al_flip_display();
        al_clear_to_color(al_map_rgb(0, 0, 0));
    }
}