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

Author Topic: Trouble with window resizing  (Read 4212 times)

0 Members and 1 Guest are viewing this topic.

Amber Crawford

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Trouble with window resizing
« on: September 09, 2021, 09:11:46 am »
I've started playing with sfml to learn c++ but I just had a problem with my code. atm I'm just trying to get a pink square to follow the mouse, but when I resize the window (just by dragging the edge of it) the square gets messed up with positions by moving the square more than I do with my mouse (but only in the opposite direction of where I enlarged, for example: if I enlarge the window upwards then it will be all messed up in the down direction but everything horizontal will just be fine). Along with this if I fullscreen it with f11 or the button at the top of the window the square just dissapears. pastebin for game.cpp: https://pastebin.com/7kDejF2e main.cpp runs update() and render()

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Trouble with window resizing
« Reply #1 on: September 09, 2021, 09:49:56 am »
If your sf::View moves or isn't the same size as the window, then the pixel position of your mouse doesn't match anymore with the coordinates of entities inside your world.
You should use mapPixelToCoords to convert the position of your mouse to world coordinates.
(you may think you don't use any view, but there's always a default view)
« Last Edit: September 09, 2021, 09:52:08 am by G. »

Amber Crawford

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Trouble with window resizing
« Reply #2 on: September 10, 2021, 03:09:16 am »
If your sf::View moves or isn't the same size as the window, then the pixel position of your mouse doesn't match anymore with the coordinates of entities inside your world.
You should use mapPixelToCoords to convert the position of your mouse to world coordinates.
(you may think you don't use any view, but there's always a default view)

I tried this but just kept getting errors that I needed an & symbol??? how exactly would I use this, thanks.

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Trouble with window resizing
« Reply #3 on: September 10, 2021, 05:53:48 am »
Not sure what you mean. The function takes a reference to a point. (references are an essential C++ concept)
sf::Vector2i mousePosition = sf::Mouse::getPosition(*this->window);
sf::Vector2f mouseCoords = this->window->mapPixelToCoords(mousePosition);
Then use the mouse coords to set the position of your player.

 

anything