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

Author Topic: RenderTexture Canvas Touch Offset Problem  (Read 1467 times)

0 Members and 1 Guest are viewing this topic.

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
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;
}

 

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: RenderTexture Canvas Touch Offset Problem
« Reply #1 on: January 12, 2023, 09:06:09 am »
Okay i fix it, the following line was the problem
sprite.setPosition(50, 300);
 
But now i have to know how i change the Position of the canvas?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: RenderTexture Canvas Touch Offset Problem
« Reply #2 on: January 12, 2023, 12:03:43 pm »
if the canvas is just a RenderTexture, it doesn't have a position, only the sprite does. are you trying to paint only part of the screen? or all of it is part of the painting area? just to know how we could adjust the positions.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: RenderTexture Canvas Touch Offset Problem
« Reply #3 on: January 12, 2023, 12:28:45 pm »
I just want to paint on the canvas(RenderTexture, Sprite). I need a offest Frame to position Buttons around the canvas, to controll brushsize etc. But when i want to change the Position from the Canvas by the Sprite -> sprite.setPosition, i have a offset position between the direct fingertouch and the brush