奇怪的SFML行为 [英] Strange SFML behavior

查看:198
本文介绍了奇怪的SFML行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试写一个按钮类。它需要2个纹理, m_texture m_onHover 和它的标题,应该自动居中。 update()负责选择正确的纹理。

 类按钮:public sf :: Drawable 
{
private:
const sf :: Texture * m_texture;
const sf :: Texture * m_onHover;
sf :: Sprite m_sprite;

public:
button();

sf :: Text m_caption; // public允许容易形成,参见centerCaption()

bool mouseIsOver()const;
void update();

void setPosition(sf :: Vector2f position);
void setPosition(float x,float y);

void centerCaption();

//访问函数
void setTexture(const sf :: Texture& texture){m_texture =& m_sprite.setTexture(* m_texture); }
void setonHoverTexture(const sf :: Texture& texture){m_onHover =& texture; }
void setCaption(sf :: String text){m_caption.setString(text); centerCaption(); }
void setFontSize(unsigned int size){m_caption.setCharacterSize(size); centerCaption(); }
void setFont(sf :: Font& font){m_caption.setFont(font); }

private:
virtual void draw(sf :: RenderTarget& target,sf :: RenderStates states)const;
};

bool button :: mouseIsOver()const
{
if(m_sprite.getGlobalBounds()。contains(sf :: Vector2f(sf :: Mouse :: getPosition ))//为contains()创建一个float向量,因为getPosition赋给int向量
{
return true;
}
else
{
return false;
}
}

一切似乎都工作, mouseIsOver()返回true似乎在sprite上移动了40个像素。 $ getGlobalBounds()在rect中的值似乎是正确的。

解决方案

光标位置应转换为正确的坐标系。基本上你需要使用 sf :: RenderTarget :: mapPixelToCoords (通过继承在 sf :: Renderwindow 中可用)。有关详情,请参阅文档和§Coordinates转化

还有,你可能想考虑让你的button类继承自 sf :: Transformable ,所以你不必管理position / rotation / scale / ...自己。查看创建SFML类似实体


I'm currently trying to write a button class. It takes 2 textures, m_texture and m_onHover, and its caption, which should be automatically centered. The function update() takes care of selecting the correct texture.

class button : public sf::Drawable
{
private:
    const sf::Texture *m_texture;
    const sf::Texture *m_onHover;
    sf::Sprite m_sprite;

public:
    button(); 

    sf::Text m_caption; // public to allow easy formating, see centerCaption()

    bool mouseIsOver() const;
    void update();

    void setPosition(sf::Vector2f position);
    void setPosition(float x, float y);

    void centerCaption();

    // Access functions
    void setTexture(const sf::Texture &texture) { m_texture = &texture;     m_sprite.setTexture(*m_texture); }
    void setonHoverTexture(const sf::Texture &texture) { m_onHover = &texture; }
    void setCaption(sf::String text)    { m_caption.setString(text);        centerCaption(); }
    void setFontSize(unsigned int size) { m_caption.setCharacterSize(size); centerCaption(); }
    void setFont(sf::Font& font) { m_caption.setFont(font); }

private:
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};

bool button::mouseIsOver() const
{
    if (m_sprite.getGlobalBounds().contains(sf::Vector2f(sf::Mouse::getPosition())))    // creating a float vector for contains() because getPosition gives int vector
    {
        return true;
    }
    else
    {
        return false;
    }
}

Everything seems to be working, but the mouse position at which mouseIsOver() returns true seems to be moved 40 pixels above the sprite. The values in the rect from getGlobalBounds() seem to be correct when printed in the console.

Unluckily I dont have enough reputation to post a screenshot.

解决方案

The cursor position should be translated to the proper coordinate system. Basically you need to use sf::RenderTarget::mapPixelToCoords (available in sf::Renderwindow by inheritance). For more details, have a look at the documentation and §Coordinates conversions of the official tutorial.

Also, you might want to consider making your button class inherit from sf::Transformable so that you don't have to manage the position/rotation/scale/... yourself. Have a look at Creating a SFML-like entity

这篇关于奇怪的SFML行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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