Skip to content

Instantly share code, notes, and snippets.

@eXpl0it3r
Created June 3, 2017 18:32
Show Gist options
  • Save eXpl0it3r/9fa3b0771f243f9e6cdcda7230523e3b to your computer and use it in GitHub Desktop.
Save eXpl0it3r/9fa3b0771f243f9e6cdcda7230523e3b to your computer and use it in GitHub Desktop.
SFML System Cursor Test Code
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window{{1024, 756}, "Hello World"};
window.setVerticalSyncEnabled(true);
sf::Cursor cursor;
cursor.loadFromSystem(sf::Cursor::Arrow);
window.setMouseCursor(cursor);
sf::Image cursor_img1;
cursor_img1.loadFromFile("cursor1.png");
sf::Image cursor_img2;
cursor_img2.loadFromFile("cursor2.png");
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
else if(event.type == sf::Event::KeyPressed)
{
if(event.key.code == sf::Keyboard::Num1)
cursor.loadFromSystem(sf::Cursor::Arrow);
else if(event.key.code == sf::Keyboard::Num2)
cursor.loadFromSystem(sf::Cursor::ArrowWait);
else if(event.key.code == sf::Keyboard::Num3)
cursor.loadFromSystem(sf::Cursor::Wait);
else if(event.key.code == sf::Keyboard::Num4)
cursor.loadFromSystem(sf::Cursor::Text);
else if(event.key.code == sf::Keyboard::Num5)
cursor.loadFromSystem(sf::Cursor::Hand);
else if(event.key.code == sf::Keyboard::Num6)
cursor.loadFromSystem(sf::Cursor::SizeHorizontal);
else if(event.key.code == sf::Keyboard::Num7)
cursor.loadFromSystem(sf::Cursor::SizeVertical);
else if(event.key.code == sf::Keyboard::Num8)
cursor.loadFromSystem(sf::Cursor::SizeTopLeftBottomRight);
else if(event.key.code == sf::Keyboard::Num9)
cursor.loadFromSystem(sf::Cursor::SizeBottomLeftTopRight);
else if(event.key.code == sf::Keyboard::Num0)
cursor.loadFromSystem(sf::Cursor::SizeAll);
else if(event.key.code == sf::Keyboard::Q)
cursor.loadFromSystem(sf::Cursor::Cross);
else if(event.key.code == sf::Keyboard::W)
cursor.loadFromSystem(sf::Cursor::Help);
else if(event.key.code == sf::Keyboard::E)
cursor.loadFromSystem(sf::Cursor::NotAllowed);
else if(event.key.code == sf::Keyboard::R)
cursor.loadFromPixels(cursor_img1.getPixelsPtr(), cursor_img1.getSize(), {20, 20});
else if(event.key.code == sf::Keyboard::T)
cursor.loadFromPixels(cursor_img2.getPixelsPtr(), cursor_img2.getSize(), {75, 75});
window.setMouseCursor(cursor);
}
}
window.clear(sf::Color(0x440544FF));
window.display();
}
}
@Gashmoh
Copy link

Gashmoh commented May 8, 2020

Hi,

I have a problem with cursor loaded from images, the image is not set properly.
Here is the image:
brush
and here is the cursor:
image

Even a 10x10 black square image has display problems.

Here is my code:

if (selectedTool == _toolsMap[sf::Keyboard::B]) {
	_mouseCursorImage->loadFromFile("./assets/cursors/brush.png");
} else if (selectedTool == _toolsMap[sf::Keyboard::E]) {	
	_mouseCursorImage->loadFromFile("./assets/cursors/eraser.png");
} else if (selectedTool == _toolsMap[sf::Keyboard::F]) {
	_mouseCursorImage->loadFromFile("./assets/cursors/bucket.png");
} else if (selectedTool == _toolsMap[sf::Keyboard::S]) {
	_mouseCursorImage->loadFromFile("./assets/cursors/select.png");
}
_mouseCursor->loadFromPixels(_mouseCursorImage->getPixelsPtr(), _mouseCursorImage->getSize(), sf::Vector2u(0, 0));
_editingWindow->setMouseCursor(*_mouseCursor);

Can you please send the source images ?
Do you know why I have this problem ?

Thanks,

@eXpl0it3r
Copy link
Author

What OS are you using? Seems to work fine for me.

image

@Gashmoh
Copy link

Gashmoh commented May 8, 2020

What OS are you using?

I'm on fedora 30.

I also found a post on the SFML forum of someone who seems to have the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment