1

I am using raylib 3.5.0 and here is my code:

    Vector2 startMousePos = GetMousePosition();
    Vector2 endMousePos;
    bool mousePressed = false;
    while (1)
    {
        if (!IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
        {
            break;
        }

        mousePressed = true;
        endMousePos = GetMousePosition();
    }

I want to create a line based on the position the mouse starts clicking and where it ends but this code causes a infinit loop.

Rik Smits
  • 31
  • 7
  • So you have to investigate why `IsMouseButtonPressed(MOUSE_LEFT_BUTTON)` is always returning `true`. I would try with a very simple progam, a `main` function calling `IsMouseButtonPressed(MOUSE_LEFT_BUTTON)` within a `while (1)` loop, that is, what you have but stripping everything else, and debug that function. – rturrado Nov 10 '21 at 16:29
  • You might need to call some sort of message processing function to get the mouse events processed so that the release of the button will be noticed. – 1201ProgramAlarm Nov 10 '21 at 16:38
  • will try that tonight – Rik Smits Nov 10 '21 at 16:47

1 Answers1

0

I figured out i should use the glfwPollEvents function that I got from using a function declaration at the top of my file, this declaration gets pulled from glfw as raylib uses this to manage windows. Calling this method yourself could hinder raylibs input handling so be careful. extern "C" void glfwPollEvents(void);

I also changed my code to (no, while(1) can't be replaced with while(!IsMouseButtonDown(MOUSE_LEFT_BUTTON))):

        Vector2 startMousePos = GetMousePosition();
        Vector2 endMousePos;
        bool mousePressed;
        while (1)
        {
            if (!IsMouseButtonDown(MOUSE_LEFT_BUTTON))
            {
                break;
            }
            else
            {
                glfwPollEvents();
                if (!mousePressed)
                {
                    mousePressed = true;
                }
                endMousePos = GetMousePosition();
            }
        }
Rik Smits
  • 31
  • 7
  • 1
    For anyone wondering, the problem happened because glfw only updates the user inputs at the end of each frame. So if you have a while loop that happens in one frame the input values will stay the same for the whole loop. – Tecelli Akıntuğ Nov 27 '21 at 19:58
  • You shouldn't need to call glfwPollEvents() again, it's called each frame by raylib. In addition, while(1) is likely a bad idea. – RobLoach Dec 13 '22 at 18:51