Skip to content

Instantly share code, notes, and snippets.

@FRex
Created September 23, 2017 22:45
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 FRex/31971e02a9e6d342a575d7ee128d74bf to your computer and use it in GitHub Desktop.
Save FRex/31971e02a9e6d342a575d7ee128d74bf to your computer and use it in GitHub Desktop.
#include <SFML/Graphics.hpp>
#include <cstdio>
static void loadXorTextureInto(sf::Image& img)
{
img.create(256u, 256u);
for(unsigned x = 0u; x < img.getSize().x; ++x)
for(unsigned y = 0u; y < img.getSize().y; ++y)
img.setPixel(x, y, sf::Color(x ^ y, x ^ y, x ^ y));
}
static bool goodimgcoords(sf::Vector2i coords, const sf::Image& img)
{
if(coords.x < 0 || coords.y < 0)
return false;
return coords.x < img.getSize().x && coords.y < img.getSize().y;
}
static sf::Color handleClick(sf::Vector2f coords, const sf::Sprite& spr, const sf::Image& img)
{
if(!spr.getGlobalBounds().contains(coords))
return sf::Color::Black;
const auto itrans = spr.getTransform().getInverse();
const auto inspritecoords = sf::Vector2i(itrans.transformPoint(coords));
if(!goodimgcoords(inspritecoords, img))
return sf::Color::Black;
return img.getPixel(inspritecoords.x, inspritecoords.y);
}
int main(int argc, char ** argv)
{
sf::RenderWindow app(sf::VideoMode(800u, 600u), "Clicking");
sf::Image img;
loadXorTextureInto(img);
sf::Texture tex;
tex.loadFromImage(img);
sf::Sprite spr(tex);
spr.setOrigin(sf::Vector2f(311.f, 74.f));
spr.setPosition(sf::Vector2f(400.f, 400.f));
spr.setScale(2.f, 1.5f);
spr.setRotation(54.f);
sf::Color background = sf::Color(0x7f0000ff);
while(app.isOpen())
{
sf::Event eve;
while(app.pollEvent(eve))
{
if(eve.type == sf::Event::Closed)
app.close();
if(eve.type == sf::Event::MouseMoved)
background = handleClick(app.mapPixelToCoords(sf::Vector2i(eve.mouseMove.x, eve.mouseMove.y)), spr, img);
}//while app poll event eve
app.clear(background);
app.draw(spr);
app.display();
}//while app is open
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment