1
Graphics / RenderTexture Canvas Touch Offset Problem
« on: January 12, 2023, 08:51:29 am »
Hello, i try to make a little paint programm on mobilephone. You paint with the finger on a canvas(sf::RenderTexture). But the touch coordinates are not right on the canvas, there is a offset between the touch point and the draw point, i cant find the mistake, someone get an idea? I think there ist something wrong with the mapPixelToCoords between the window and canvas, here is the Code:
int main(int argc, char *argv[])
{
const int xScreen = 1200;
const int yScreen = 2600;
const int borderScreen = 100;
RenderWindow window(VideoMode(xScreen, yScreen), "Hello SFML");
sf::RenderTexture canvas;
canvas.create(1100, 2000);
canvas.clear(sf::Color::White);
sf::Sprite sprite;
sprite.setTexture(canvas.getTexture());sprite.setPosition(50, 300);
View view(canvas.getView());
vector<sf::Shape* > shapes;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event));
Vector2i touchy = Touch::getPosition(0, window);
Vector2f touch = window.mapPixelToCoords(touchy);
CircleShape *shape = new CircleShape(10);
shape->setPosition(touch.x, touch.y);
shape->setOrigin(shape->getRadius(), shape->getRadius());
shape->setFillColor(sf::Color::Black);
FloatRect canvasRect = sprite.getGlobalBounds();
window.clear(sf::Color(80, 80, 80));
if(canvasRect.contains(touch)){
shapes.push_back(shape);
for(auto it=shapes.begin(); it!=shapes.end();it++) {
canvas.draw(**it);
}
}
canvas.display();
window.draw(sprite);
window.display();
}
return 0;
}
{
const int xScreen = 1200;
const int yScreen = 2600;
const int borderScreen = 100;
RenderWindow window(VideoMode(xScreen, yScreen), "Hello SFML");
sf::RenderTexture canvas;
canvas.create(1100, 2000);
canvas.clear(sf::Color::White);
sf::Sprite sprite;
sprite.setTexture(canvas.getTexture());sprite.setPosition(50, 300);
View view(canvas.getView());
vector<sf::Shape* > shapes;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event));
Vector2i touchy = Touch::getPosition(0, window);
Vector2f touch = window.mapPixelToCoords(touchy);
CircleShape *shape = new CircleShape(10);
shape->setPosition(touch.x, touch.y);
shape->setOrigin(shape->getRadius(), shape->getRadius());
shape->setFillColor(sf::Color::Black);
FloatRect canvasRect = sprite.getGlobalBounds();
window.clear(sf::Color(80, 80, 80));
if(canvasRect.contains(touch)){
shapes.push_back(shape);
for(auto it=shapes.begin(); it!=shapes.end();it++) {
canvas.draw(**it);
}
}
canvas.display();
window.draw(sprite);
window.display();
}
return 0;
}