#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Image.hpp>
const std::string FRAG_SHADER = "#version 130\n\
\
uniform sampler2D texture; \
\
void main () { \
int window_x = int(gl_FragCoord.x); \
int window_y = int(gl_FragCoord.y); \
int tex_x = window_x / 4; \
int tex_y = window_y / 4; \
int xoff = window_x % 4; \
int yoff = window_y % 4; \
vec4 here = texelFetch(texture, ivec2(tex_x,tex_y), 0); \
vec4 up1 = texelFetch(texture, ivec2(tex_x,tex_y+1), 0); \
vec4 down1 = texelFetch(texture, ivec2(tex_x,tex_y-1), 0); \
if (yoff == 0) \
gl_FragColor = (here + down1) / 2.0; \
else if (yoff == 3) \
gl_FragColor = (here + up1) / 2.0; \
else \
gl_FragColor = (2.0 * here + up1 + down1) / 4.0; \
}\
";
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
sf::RenderTexture renderTex;
if (!renderTex.create(200, 150))
return -1;
sf::CircleShape shape(10.f);
shape.setFillColor(sf::Color::Black);
shape.setOrigin(shape.getRadius(), shape.getRadius());
sf::Shader upscaleShader;
upscaleShader.loadFromMemory(FRAG_SHADER, sf::Shader::Fragment);
//set up the 'texture' variable in the shader
upscaleShader.setParameter("texture", renderTex.getTexture());
sf::RenderStates upscaleRenderStates(&upscaleShader);
//FIXME: hardcoding
sf::Vertex winRect[4] = {
sf::Vector2f(0,0),
sf::Vector2f(800,0),
sf::Vector2f(0,600),
sf::Vector2f(800,600)};
while(window.isOpen()) {
sf::Event event;
while(window.pollEvent(event)) {
if(event.type == sf::Event::Closed)
window.close();
}
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
//shape.setPosition(sf::Vector2f(static_cast<float>(mousePos.x)/4.f, static_cast<float>(mousePos.y)/4.f));
shape.setPosition(sf::Vector2f(static_cast<float>(mousePos.x/4), static_cast<float>(mousePos.y/4)));
//Draw everything
renderTex.clear(sf::Color(200,200,200));
renderTex.draw(shape);
renderTex.display();
window.clear();
//this is where the upscaling happens
window.draw(winRect, 4, sf::TrianglesStrip, upscaleRenderStates);
window.display();
}
return 0;
}