Search code examples
c++menumouseallegroallegro5

Issues while making some kind of menu


I made this thing as a part of my program. Language is C++, with Allegro libraries. I want to make it do the next: when clicking, a border appears around one of the two rectangulars.

It happens, but only once, at the beginning. After that, the border disappears all the time when I move the mouse away. Also, anywhere I click, the border appears on the proper place.

mouseX and mouseY works perfectly, even the numbers are the same. But the action happens only once on the way I want. How to expand it to every cases when I'm clicking?

    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
                }   
            }
        }

    }

Solution

  • The border is disappearing because you are calling your drawing code within the button down event. This means as soon as the button is released, the rectangle will no longer show up. It's usually better practice to have events update values, and then have everything on the screen drawn when the event queue is empty and a redraw is called by a timer.

    //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));
        }
    }