SFML community forums

Help => General => Topic started by: Xrey274 on August 25, 2019, 11:10:02 pm

Title: RenderTexture offset;
Post by: Xrey274 on August 25, 2019, 11:10:02 pm
I am testing out some code where the rendertexture is acting like a canvas to which things are being drawn. One problem is that when I set the position of the sf::Sprite, that has the rendertexture, to the center of the screen there is an offset.

#include <iostream>
#include <SFML/Graphics.hpp>

sf::CircleShape newCircle(sf::Vector2f pos)
{
        sf::CircleShape circle;

        circle.setPosition(pos);
        circle.setRadius(10);
        circle.setOrigin(circle.getRadius(), circle.getRadius());
        circle.setFillColor(sf::Color::Green);

        return circle;
}

int main()
{
        sf::ContextSettings settings;
        settings.antialiasingLevel = 8;

        sf::RenderWindow window(sf::VideoMode(1600, 1200), "RenderTexture", sf::Style::Default, settings);
        sf::RenderTexture canvas;

        canvas.create(800, 600);
        canvas.clear(sf::Color::White);

        sf::Event event;

        std::vector<sf::CircleShape> circles;

        bool isMoving = false;

        while(window.isOpen())
        {
                sf::Vector2f mouseCoords = window.mapPixelToCoords(sf::Mouse::getPosition(window), window.getView());

                while(window.pollEvent(event))
                {
                        switch(event.type)
                        {
                                case sf::Event::Closed:
                                        window.close();

                                        break;
                                case sf::Event::MouseButtonPressed:
                                        isMoving = true;

                                        break;
                                case sf::Event::MouseMoved:
                                        if(isMoving == true)
                                        {
                                                circles.push_back(newCircle(mouseCoords));
                                        }

                                        break;
                                case sf::Event::MouseButtonReleased:
                                        isMoving = false;

                                        circles.clear();
                        }

                        if(event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                }

                for(int i = 0; i < circles.size(); ++i)
                {
                        canvas.draw(circles[i]);
                }

                canvas.display();

                window.clear(sf::Color(31, 31, 31));

                sf::Sprite sprite(canvas.getTexture());
                sprite.setPosition(sf::Vector2f(400, 300));
                window.draw(sprite);

                window.display();
        }
}
 
 
Title: Re: RenderTexture offset;
Post by: eXpl0it3r on August 26, 2019, 01:00:56 am
You set the position of the sprite that renders the render texture, as such the whole canvas is offset by that position.
Title: Re: RenderTexture offset;
Post by: Xrey274 on August 26, 2019, 12:03:21 pm
Is there a way to fix that offset, except by drawing things with the amount of pixels the rendertextrure is offset?
Title: Re: RenderTexture offset;
Post by: eXpl0it3r on August 26, 2019, 01:30:20 pm
There's nothing to "fix". The render texture is rendered to the screen like any other image, so if you move it, the content of the image is also moved.
If you want your drawing represented 1:1 on the window, then I recommend not to move the render texture.
If you want it to be moved, then you'll have to adjust for the translation yourself and offset the rendered circles on your own.