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

Author Topic: Title bar, mouse position, and mapPixelToCoords  (Read 2413 times)

0 Members and 1 Guest are viewing this topic.

rycode

  • Newbie
  • *
  • Posts: 2
    • View Profile
Title bar, mouse position, and mapPixelToCoords
« on: October 12, 2017, 07:45:33 am »
hi --

I'm running into an issue where the using mapPixelToCoords is returning oddly offset/scaled values when being run in a window with a title bar.

It is as though the effective viewport for the drawing area extends under the title bar, but something in the rest of the code is not accounting for that and giving me odd results. I use sf::Mouse::getPosition(m_Window) and m_Window.mapPixelToCoords(mouse_pos) to draw a simple quad at the coords of the mouse cursor, but the quad drifts off the mouse position depending on where in the window it is.

I put together a very simple test program that works nicely with a borderless window (rect sticks to cursor) but changes behavior when you add the title bar to it.

I would expect the behavior to be consistent regardless of title bar presence. I am on Windows 10 and using SFML 2.4.2

Could someone advise me if I'm doing something wrong, or if this is indeed unexpected?
Gif is attached, hopefully that works.

Thanks!


test code is:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window/Mouse.hpp>
#include <stdio.h>

int main(int argc, char** argv)
{
  sf::VideoMode vm(200, 200);
  sf::RenderWindow m_Window;
  m_Window.create(vm, "title", sf::Style::Default);

  while(m_Window.isOpen())
  {
    sf::Vector2i mouse_pos = sf::Mouse::getPosition(m_Window);

    printf("%d %d\n", mouse_pos.x, mouse_pos.y);

    m_Window.clear(sf::Color::Blue);

    sf::Vector2f world_pos = m_Window.mapPixelToCoords(mouse_pos);

    sf::RectangleShape shape;
    shape.setFillColor(sf::Color::White);
    shape.setPosition(world_pos.x, world_pos.y);  
    shape.setSize( sf::Vector2f(100, 100) );
    m_Window.draw(shape);

    m_Window.display();
 
  }
}


 


rycode

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Title bar, mouse position, and mapPixelToCoords
« Reply #1 on: October 15, 2017, 07:05:33 am »

A bit of an update. I tried the exact same exe on a different computer (Nvidia drivers) and it worked as expected -- the rectangle stuck to the mouse cursor, even with the title bar.

My primary dev machine is a Dell laptop with both Intel integrated graphics and an Nvidia chip. I forced it to use the Nvidia chip and everything works well. Switch back to Intel integrated and the scaling offset is there again.

There must be some sort of bug / oddity in the Intel drivers where things don't work well for some funky reason. I've been building SFML from source and tracing through the code when the app is behaving oddly and can't see anything in there that seems to relate to the behavior.

Am going to try to update my Intel drivers and see if that helps.

And, just for specifics, in this case the values returned by mapPixelToCoords are (as expected) the same as the values passed in, since I'm using the default view. There's no odd values coming back from there, the oddness seems to be in the final image generation on the window.







FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Title bar, mouse position, and mapPixelToCoords
« Reply #2 on: October 15, 2017, 06:34:27 pm »
It works correctly for me when launched on my integrated Intel, no idea what it could be right now, especially since it works on another machine. Maybe try reset the sf::View to same one or anything really, maybe the view is somehow not initialized right. It's really unusual.
« Last Edit: October 15, 2017, 06:36:13 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Title bar, mouse position, and mapPixelToCoords
« Reply #3 on: October 16, 2017, 11:24:20 am »
It appears to be mapping the position in the client area to the actual (outer, including title bar) size of the window (the edges are considered thicker than is visible on Windows 10), which, obviously, it should not.

I wonder if this is a Windows 10-only problem. Was the other test computer running Windows 10?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Title bar, mouse position, and mapPixelToCoords
« Reply #4 on: October 16, 2017, 12:02:50 pm »
I'm on Windows 10 and it works correctly on the latest SFML master.
Back to C++ gamedev with SFML in May 2023