Skip to content

Instantly share code, notes, and snippets.

@eXpl0it3r
Last active January 14, 2019 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eXpl0it3r/beb4bf26b4595c59fb77e26d43efc8eb to your computer and use it in GitHub Desktop.
Save eXpl0it3r/beb4bf26b4595c59fb77e26d43efc8eb to your computer and use it in GitHub Desktop.
SFML Cursor API
#include <SFML/Graphics.hpp>
struct Cursor
{
Cursor() :
changed{ false },
current{ sf::Cursor::Arrow }
{
arrow.loadFromSystem(sf::Cursor::Arrow);
hand.loadFromSystem(sf::Cursor::Hand);
text.loadFromSystem(sf::Cursor::Text);
}
void set_type(const sf::Cursor::Type type)
{
changed = (current != type);
current = type;
}
bool has_changed() const
{
return changed;
}
const sf::Cursor& get_current() const
{
changed = false;
if (current == sf::Cursor::Arrow)
return arrow;
else if (current == sf::Cursor::Hand)
return hand;
else
return text;
}
private:
sf::Cursor arrow;
sf::Cursor hand;
sf::Cursor text;
mutable bool changed;
sf::Cursor::Type current;
};
int main()
{
sf::RenderWindow window{ {1280, 720}, "Cursors!" };
window.setFramerateLimit(60);
Cursor cursors;
while (window.isOpen())
{
for (sf::Event event; window.pollEvent(event);)
{
if (event.type == sf::Event::Closed)
{
window.close();
}
else if (event.type == sf::Event::KeyReleased)
{
if (event.key.code == sf::Keyboard::Left)
{
cursors.set_type(sf::Cursor::Hand);
}
else if (event.key.code == sf::Keyboard::Right)
{
cursors.set_type(sf::Cursor::Arrow);
}
}
}
if (cursors.has_changed())
{
window.setMouseCursor(cursors.get_current());
}
window.clear();
window.display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment