在SFML中滚动屏幕的鼠标位置 [英] Mouse position with screen scrolling in SFML

查看:207
本文介绍了在SFML中滚动屏幕的鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个与网格对齐的块放置。一切正常,在我脑子里,这应该更新鼠标相对于每个窗口的窗口的位置吗?

  sf: :Vector2i position = sf :: Mouse :: getPosition(window.mywindow); 

//获得位置
int xSnap =(position.x / gridWidth)* gridWidth;
int ySnap =(position.y / gridHeight)* gridHeight;

但我也有屏幕滚动使用



<$如果(player.playerSprite.getPosition()。x + 16> screenDimensions.x / 2)
position.x = player.playerSprite.getPosition()。x + 16(p $ p> ;
else
position.x = screenDimensions.x / 2;

// Y
if(player.playerSprite.getPosition()。y + 16> screenDimensions.y / 2)
position.y = player.playerSprite.getPosition( ).y + 16;
else
position.y = screenDimensions.y / 2;

当屏幕从原始位置滚动时,它不更新鼠标位置,例如,可以说窗口大小是800x600,在800x600窗口内,位置工作正常,但可以说我的精灵向右移动了200px(它在精灵到达屏幕中间时开始滚动)超过原始位置,使用此对象进行放置代码然后出现在鼠标左侧200px。



我希望有道理

解决方案

您需要调用 window.mapPixelToCoords()来将像素位置转换为视图的坐标系。



< pre-class =lang-cpp prettyprint-override> sf :: Vector2i pixel_pos = sf :: Mouse :: getPosition(window.mywindow);
sf :: Vector2f coord_pos = window.mywindow.mapPixelToCoords(pixel_pos);

作为一般建议:不要使用公共类成员 - mywindow 和 playerSprite 不能从外部访问。


Im trying to create a block placement with snap to grid. Everything is working, and in my head this should update the position of the mouse relative to the window each frame right?

sf::Vector2i position = sf::Mouse::getPosition(window.mywindow);

//get position
int xSnap = (position.x / gridWidth) * gridWidth;
int ySnap = (position.y / gridHeight) * gridHeight;

But i also have screen scrolling using

if (player.playerSprite.getPosition().x + 16 > screenDimensions.x / 2)
        position.x = player.playerSprite.getPosition().x + 16;
    else
        position.x = screenDimensions.x / 2;

    //Y
    if (player.playerSprite.getPosition().y + 16 > screenDimensions.y / 2)
        position.y = player.playerSprite.getPosition().y + 16;
    else
        position.y = screenDimensions.y / 2;

When the screen scrolls either way from its original position it doesnt update the mouse position, so for example, lets say the window size is 800x600, within that 800x600 window the position works fine, but lets say my sprite move 200px right (it starts scrolling when the sprite reaches the middle of the screen) past the original position, the object im placing using this code then appears 200px to the left of the mouse. Same happens with the y axis.

I hope that make sense

解决方案

You need to call window.mapPixelToCoords() to transform your pixel position to the coordinate system of your view.

sf::Vector2i pixel_pos = sf::Mouse::getPosition(window.mywindow);
sf::Vector2f coord_pos = window.mywindow.mapPixelToCoords(pixel_pos);

And as a general advice: Don't use public class members - mywindow and playerSprite should not be accessible from the outside.

这篇关于在SFML中滚动屏幕的鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆