#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow app(sf::VideoMode(512,512),"Indeed..");
app.setFramerateLimit(60);
sf::Shader shader;
shader.loadFromFile("shader.glsl",sf::Shader::Fragment);
sf::Texture tex;//this texture has to be same size as the screen
tex.loadFromFile("tiles.png");//load some 512x512 image
shader.setParameter("tex",tex);
float dist=200.f;//this is multiplied by 100 so that float coordinates don't lose so much precision on cpu
shader.setParameter("dist",dist);
sf::Event eve;
while(app.isOpen())
{
while(app.pollEvent(eve))if(eve.type==sf::Event::Closed)app.close();
sf::Vector2f vec=sf::Vector2f(sf::Mouse::getPosition(app));
vec.x/=app.getSize().x;
vec.y/=app.getSize().y;
shader.setParameter("centre",sf::Vector2f(vec));
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
shader.setParameter("dist",dist-=1.5f);//this WILL get onto negatives but catching/preventing that is up to you
}
app.clear(sf::Color::Magenta);
app.draw(sf::Sprite(tex),&shader);
app.display();
}
return 0;
}
uniform sampler2D tex;
uniform vec2 centre;
uniform float dist;
void main()
{
gl_FragColor=(1.0-(100.0*distance(gl_TexCoord[0].xy,centre)/dist))*texture2D(tex,gl_TexCoord[0].xy);
//could add clamping to not go over 1.0 and under 0.0
gl_FragColor.a=1.0;
}
I'm not an expert, this needs improvements.
Press left mouse button to make darkness creep towards the cursor, it takes few seconds for it to happen.