Cursor position at startup is always [0,0]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
svalorzen
Prole
Posts: 22
Joined: Tue Sep 27, 2022 8:25 am

Cursor position at startup is always [0,0]

Post by svalorzen »

I've noticed that when my LOVE app starts, the cursor position is assumed to be at 0,0, independently of where the mouse is. The cursor needs to move at least 1 pixel before the true position is registered. This is true even if, when the windows opens, the cursor is inside the window. I am on Linux, LOVE version 11.4. I am in windowed mode, if that matters.

This is a bit annoying because often I already have my mouse where I know I'll need to click, I open my game and nothing happens. I then move the cursor 1 pixel and it suddenly works. I've confirmed the cursor position is incorrect by logging it at startup. I've also tried calling `setGrabbed` but it didn't have an effect; it did confine the mouse instantly at startup, but until the cursor is moved 1 pixel it is still assumed to be at the 0,0 absolute position.

Is there a way to force LOVE to update the cursor position after startup?
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Cursor position at startup is always [0,0]

Post by Bigfoot71 »

For LÖVE to be able to capture the position of your mouse, it must already be in the window. In windowed mode if the mouse leaves the window the position will be the last one recorded in the window. You can now guess why LÖVE defines a default value at startup.

If that really bothers you maybe you can use the function love.mouse.setPosition but that will actually reposition the mouse at the screen. Otherwise by using Lua FFI and writing a C program to obtain the absolute position of the mouse but this is only a guess and the procedure to follow will vary according to the operating system.

Then there may be a trick to force to update the mouse position directly from LÖVE but I don't know it.

Edit: To get the mouse position out of the window you can see this topic: viewtopic.php?t=85191
My avatar code for the curious :D V1, V2, V3.
svalorzen
Prole
Posts: 22
Joined: Tue Sep 27, 2022 8:25 am

Re: Cursor position at startup is always [0,0]

Post by svalorzen »

Bigfoot71 wrote: Mon Nov 21, 2022 4:59 pm For LÖVE to be able to capture the position of your mouse, it must already be in the window. In windowed mode if the mouse leaves the window the position will be the last one recorded in the window. You can now guess why LÖVE defines a default value at startup.
I might have explained myself badly. My problem is not related to when the cursor is outside the window. The problem happens when the program starts up, and the window is created "under" the cursor. In this scenario, the cursor is already within the window boundaries; in other words, the initial position, width and height of the window at startup are such that they encompass the cursor position when the program was started (sorry to have repeated this in so many ways, I just want to make sure I'm understood).

In this case, even though the cursor is effectively inside the window, LOVE does not report its correct position. This also isn't only a problem on the first frame; the position is constantly reported as 0,0 as long as the mouse is not moved at least once; afterwards everything is OK.
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Cursor position at startup is always [0,0]

Post by Bigfoot71 »

It's strange, it doesn't happen to me, I'm on linux with MATE. Maybe you can report the bug if not to fix the problem temporarily I see two tips, either you force to reposition the mouse in the center of the display during startup, like this:

Code: Select all

local win_w, win_h = love.graphics.getDimensions()
love.mouse.setPosition(win_w/2, win_h/2)
Either you retrieve the absolute position of the cursor with FFI and perform the reposition with the position obtained, this will update the position and you can apply a position off the screen. It will be "fake" but the position will be taken into account at startup, after I grant it, it is clearly not ideal... If I were you and it really bothered me I would report the problem to the developers and use the first solution while waiting.

I will still check if there is no way to force this update of the position, I will let you know if I find.

Otherwise, maybe you can try using love.mouse.getPosition() at the right time or with some conditions? As I cannot reproduce your case, I cannot give you too much guidance.
Last edited by Bigfoot71 on Tue Nov 22, 2022 2:06 am, edited 2 times in total.
My avatar code for the curious :D V1, V2, V3.
svalorzen
Prole
Posts: 22
Joined: Tue Sep 27, 2022 8:25 am

Re: Cursor position at startup is always [0,0]

Post by svalorzen »

Ok, I'm thinking an example might help as well. Consider the simple application:

Code: Select all

function love.load()
    love.window.setMode(300, 300)
end

function love.keypressed(key)
    if key == 'escape' then
        love.event.quit()
    end
end

function love.draw()
    print(love.mouse.getPosition())
end
Now I'm going to startup the program, such that when the window opens the cursor is automatically inside the window, without the need to move it. You can see what happens in the screenshot: it just prints 0,0 endlessly, even though the cursor is indeed in the window. As soon as I move it, the position is updated and everything works fine again.

Image
svalorzen
Prole
Posts: 22
Joined: Tue Sep 27, 2022 8:25 am

Re: Cursor position at startup is always [0,0]

Post by svalorzen »

Thanks for the suggestion, I only saw now your post. Indeed there are workarounds; in my case it's not a huge problem, just a bit annoying since sometimes I click only to realize nothing is happening. Also I guess if I were to publish the project it would be a detail that would feel unpolished which I wouldn't like a lot. But yeah for now I posted mostly just to check whether this is a bug or whether I was doing something wrong.
svalorzen
Prole
Posts: 22
Joined: Tue Sep 27, 2022 8:25 am

Re: Cursor position at startup is always [0,0]

Post by svalorzen »

I have tried the FFI code and, just for completeness, it correctly reports the cursor position (global though), so at least that's working. The FFI code seems to rely on SDL though, which if I'm reading LOVE's source correctly is the same. So if one works, so should the other, no? If you have ideas of stuff I could try to debug this do let me know (incliding writing & compiling C/SDL stuff to see what it does on my system).

EDIT:

I'm adding stuff here to avoid adding a million posts. I've modified the FFI code to use `sdl.SDL_GetMouseState` rather than `SDL_GetGlobalMouseState`, and indeed the FFI code now has the same bug as LOVE, reporting 0,0 rather than the mouse position.

--------

Found an issue on the SDL bugtracker about this: https://github.com/libsdl-org/SDL/issues/2303 .. might be my problem
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Cursor position at startup is always [0,0]

Post by Bigfoot71 »

Does this thing fix your problem by any chance ?
Attachments
mouse_position_test.love
(1.98 KiB) Downloaded 30 times
My avatar code for the curious :D V1, V2, V3.
svalorzen
Prole
Posts: 22
Joined: Tue Sep 27, 2022 8:25 am

Re: Cursor position at startup is always [0,0]

Post by svalorzen »

It kind of works but it moves my mouse a bit at startup. Are you using the global to read the mouse position and then setting it back?
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Cursor position at startup is always [0,0]

Post by Bigfoot71 »

Okay, the 'correction()' function is useless then, for me it prevents the movement of the mouse but for you it doesn't.
This movement is due to the height of the window title bar. It takes the absolute position of the mouse and subtracts it from the position of the window to get the correct position inside the window. If you can find the height of the title bar it should work without moving the mouse.

I'm still looking for another trick.

Edit:

Does this happen to you with SDL too?
Here is an example if you want to try:

Code: Select all

#include <stdio.h>
#include <SDL2/SDL.h>

int main(int argc, char** argv)
{
    (void) argc; (void) argv;
    
    // Create window //

    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fprintf(stderr, "ERROR: %s", SDL_GetError());
        return -1;
    };

    SDL_Window* win = SDL_CreateWindow("Mandala", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 300, 300, 0);

    if (!win)
    {
        fprintf(stderr, "ERROR: %s", SDL_GetError());
        return -1;
    }

    SDL_Renderer* ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);

    if (!ren)
    {
        fprintf(stderr, "ERROR: %s", SDL_GetError());
        SDL_DestroyWindow(win); return -1;
    }

    SDL_Event event;

    // Main loop //
    
    SDL_bool running = SDL_TRUE;
    int x = 0, y = 0;

    while (running)
    {
        while (SDL_PollEvent(&event))
        {
            running = event.type != SDL_QUIT;
        }
        
        SDL_GetMouseState(&x, &y);
        
        printf("X: %d | Y: %d\n", x, y);

    }

    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

    return 0;
}
To compile please check if you have the libsdl2-dev package otherwise install it with this command if you are on a Debian like distribution:

Code: Select all

sudo apt-get install libsdl2-dev
To compile and run:

Code: Select all

gcc file.c -o test -lSDL2 && ./test
Last edited by Bigfoot71 on Mon Nov 21, 2022 7:40 pm, edited 1 time in total.
My avatar code for the curious :D V1, V2, V3.
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests