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

Author Topic: Using Mouse Position to Draw [SOLVED]  (Read 3230 times)

0 Members and 1 Guest are viewing this topic.

Sir Lulzalot

  • Newbie
  • *
  • Posts: 4
    • View Profile
Using Mouse Position to Draw [SOLVED]
« on: December 20, 2013, 03:53:08 pm »
So I'm fairly new to SFML, but I've been pouring over the API documentation to try and understand perhaps where I went wrong in this design.

I'm attempting to integrate the user's mouse to draw symbols/objects on the screen. But currently when I draw the lines drawn are off from where the mouse pointer actually is. The drawing is consistent though, in that it's following exactly my motions. It just needs to not be off to the side as it is now.

Here's some code that would answer more questions than my words would.

//in main.cpp
        sf::Mouse theMouse;
        sf::Vertex thePoint;
        //All DigiDraw currently contains is a VertexArray named drawnArray.
        DigiDraw theDrawing;
        thePoint.position.x = (float)sf::Mouse::getPosition(window).x;
        thePoint.position.y = (float)sf::Mouse::getPosition(window).y;
        if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
            theDrawing.fillArray(thePoint);
        }
       window.draw(theDrawing.getArray());
//in digidraw.cpp
sf::VertexArray DigiDraw::fillArray(sf::Vertex thePoint){
    this->drawnArray.append(thePoint);
    return this->drawnArray;
}
 

 All I'm trying to do is get it to match the pointer exactly which I suppose should be in relation to the desktop not the window. I don't think floating the ints of a Vector2i to be put into a Vector2f should be causing this. Usually the drawing is only off on the x coordinate.

EDIT: Further testing reveals that as the pointer gets closer to the left side of the window, the difference of x axis position of what is drawn approaches the pointer's position.

EDIT2: So I removed the window relation I accidentally put in, but now it is consistently is down and to the left.

EDIT3: So I tested under fullscreen conditions and it's accurate there, which makes me think that maybe not being window relative is problematic. I readded the window relation but the x axis problem is still there.

EDIT4 (THE FIX): So apparently Video Mode for the window is the marauder here, I was at 1500,500 and decided to change it to 1000,500, and apparently my machine had modified the 1500,500 size to fit its screen, causing the erroneous result.
« Last Edit: December 20, 2013, 04:53:36 pm by Sir Lulzalot »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Using Mouse Position to Draw [SOLVED]
« Reply #1 on: December 20, 2013, 07:14:15 pm »
You should always convert from window coordinates to world coordinates, using the mapPixelToCoords function of the window.
Laurent Gomila - SFML developer

 

anything