I’m using SFML to develop a game, and I need to hide the mouse cursor within the game window. However, calling setMouseCursorVisible(false) doesn’t seem to hide the cursor as expected. Here is the code that results in a cursor displayed:
#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Minimal Example");
// Attempt to hide the mouse cursor
window.setMouseCursorVisible(false);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.display();
}
return 0;
}
I even tried using a transparent cursor:
#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Custom Cursor Example");
// Create a transparent image for the cursor
sf::Image cursorImage;
cursorImage.create(32, 32, sf::Color::Transparent);
sf::Cursor cursor;
if (cursor.loadFromPixels(cursorImage.getPixelsPtr(), sf::Vector2u(32, 32), sf::Vector2u(0, 0))) {
window.setMouseCursor(cursor);
} else {
std::cerr << "Failed to load custom cursor" << std::endl;
}
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.display();
}
return 0;
}
But when I do this it just default back to the default cursor. If I do
cursorImage.create(32, 32, sf::Color::Red);
instead, the cursor becomes a red square.