Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: RenderTexture offset;  (Read 1229 times)

0 Members and 1 Guest are viewing this topic.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
RenderTexture offset;
« 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();
        }
}
 
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: RenderTexture offset;
« Reply #1 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.
« Last Edit: August 29, 2019, 12:59:31 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: RenderTexture offset;
« Reply #2 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: RenderTexture offset;
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything