Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Mouse cursor reverting to default when leaving the window  (Read 5124 times)

0 Members and 1 Guest are viewing this topic.

ThatOneGuyThatDoesStuff

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
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?
« Last Edit: January 16, 2022, 12:43:47 pm by ThatOneGuyThatDoesStuff »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Mouse cursor reverting to default when leaving the window
« Reply #1 on: January 14, 2022, 12:07:22 am »
Are you setting it through sf::Cursor?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ThatOneGuyThatDoesStuff

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Mouse cursor reverting to default when leaving the window
« Reply #2 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);
}

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Mouse cursor reverting to default when leaving the window
« Reply #3 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)

ThatOneGuyThatDoesStuff

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: Mouse cursor reverting to default when leaving the window
« Reply #4 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.