Thomas_Vcheck
Thomas_Vcheck

Reputation: 192

Declaration of IntRect SFML

I'm working on a project in SFMLand I would like to set the position of a rectangle on my sprite sheet using a std::pair. In my .hpp I have this attribut. sf::IntRect _size; which is use to set the width, height, x, y of the rectangle. In my .cppfile, I've done this.

void Character::setCharacter(const std::string& texture_p, std::pair<float, float> rec, std::pair<float, float> recc)
{
    _size = {rec.first, rec.second, recc.first, recc.second};

    if (!_texture->loadFromFile(texture_p))
        throw CharacterError(ERR_TEXTURE, __FILE__, __LINE__);
    _sprite->setTexture(*_texture);
    _sprite->setTextureRect(_size);
}

but during the compilation, I have this error and I don't know its meaning.

Element '1': conversion from '_Ty1' requires a narrowing conversion
Element '2': conversion from '_Ty2' requires a narrowing conversion
Element '3': conversion from '_Ty1' requires a narrowing conversion
Element '4': conversion from '_Ty2' requires a narrowing conversion

Upvotes: 0

Views: 259

Answers (1)

flogram_dev
flogram_dev

Reputation: 42858

The error means you're implicitly converting from float to int which can cause a loss of precision.

Explicitly convert the elements from float to int:

_size = {
    static_cast<int>(rec.first),
    static_cast<int>(rec.second),
    static_cast<int>(recc.first),
    static_cast<int>(recc.second)
};

Or change rec and recc to be std::pair<int, int> in the first place.

Upvotes: 1

Related Questions