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();
}
}