SFML community forums

Help => Window => Topic started by: ThatOneGuyThatDoesStuff on January 11, 2022, 10:13:13 pm

Title: [SOLVED] Mouse cursor reverting to default when leaving the window
Post by: ThatOneGuyThatDoesStuff on January 11, 2022, 10:13:13 pm
I set the mouse cursor to a custom one. But when the mouse cursor leaves the games window, it reverts back to Windows 7 default cursor, and doesn't go back. Is this just something I need to watch for in the code? Do I need to reset the cursor each time the cursor leaves the window and returns?
Title: Re: Mouse cursor reverting to default when leaving the window
Post by: eXpl0it3r on January 14, 2022, 12:07:22 am
Are you setting it through sf::Cursor?
Title: Re: Mouse cursor reverting to default when leaving the window
Post by: ThatOneGuyThatDoesStuff on January 15, 2022, 09:52:23 pm
Are you setting it through sf::Cursor?

I am.

void ChangeGameState::PrepareMouseCursor(){
    sf::Image cursorImage;
    cursorImage.loadFromFile("images/mouse_cursor.png");
    sf::Cursor cursor;
    cursor.loadFromPixels(cursorImage.getPixelsPtr(), sf::Vector2u(CURSOR_SIZE, CURSOR_SIZE), sf::Vector2u(0, 0));
    renderWindow.setMouseCursor(cursor);
}
Title: Re: Mouse cursor reverting to default when leaving the window
Post by: kojack on January 16, 2022, 03:24:33 am
I haven't played around with custom cursors myself, but looking at the sfml source the sf::Cursor class destroys the allocated system specific cursor when it goes out of scope.
To me, the code looks like sf::Cursor should exist for the lifetime of the program, not be a temporary in PrepareMouseCursor(). So perhaps it's holding a residual image while in the window, when it leaves the window then returns the cursor you set no longer exists.

Try moving the sf::Cursor out of the function and maybe put it where the renderWindow is, so it has a similar lifetime.

(Technically: Win32 DestroyCursor() is called when sf::Cursor destructs)
Title: Re: Mouse cursor reverting to default when leaving the window
Post by: ThatOneGuyThatDoesStuff on January 16, 2022, 12:43:09 pm
I haven't played around with custom cursors myself, but looking at the sfml source the sf::Cursor class destroys the allocated system specific cursor when it goes out of scope.
To me, the code looks like sf::Cursor should exist for the lifetime of the program, not be a temporary in PrepareMouseCursor(). So perhaps it's holding a residual image while in the window, when it leaves the window then returns the cursor you set no longer exists.

Try moving the sf::Cursor out of the function and maybe put it where the renderWindow is, so it has a similar lifetime.

(Technically: Win32 DestroyCursor() is called when sf::Cursor destructs)

Eh, this worked. Thanks for the assist man.