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

Author Topic: Sprite is showing outside texture rect  (Read 179 times)

0 Members and 1 Guest are viewing this topic.

isaiah0311

  • Newbie
  • *
  • Posts: 3
    • View Profile
Sprite is showing outside texture rect
« on: August 13, 2020, 10:16:03 pm »
void Graphics::render(unsigned char id, float x, float y) {
        sf::Sprite sprite;
        sprite.setTexture(*tileset);

        sf::IntRect rect;
        rect.left = id % 16 * 16;
        rect.top = id / 16 * 24;
        rect.width = 16;
        rect.height = 24;

        sprite.setTextureRect(rect);
        sprite.setScale(sf::Vector2f(2.0f, 2.0f));
        sprite.setPosition(sf::Vector2f(x, y));

        window->draw(sprite);
}

There is one texture for the game, it's a 16 x 16 tile sheet with each tile having a size of 16 x 24px. When I display a tile, it will show a thin line of the tile below it. I can't figure out why it would display more than the 16 x 24. Please help.

Stauricus

  • Full Member
  • ***
  • Posts: 191
    • View Profile
    • Email
Re: Sprite is showing outside texture rect
« Reply #1 on: August 13, 2020, 11:45:30 pm »
did you set texture smooth to be true?

isaiah0311

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite is showing outside texture rect
« Reply #2 on: August 14, 2020, 12:00:58 am »
No, should I?

isaiah0311

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite is showing outside texture rect
« Reply #3 on: August 14, 2020, 12:37:51 am »
Solved the issue. I had anti-aliasing set to 16. By setting to 8 or lower it removes this issue. I had not had this problem before when working with 16 x 16px tiles, only 16 x 24px.

Hapax

  • Hero Member
  • *****
  • Posts: 3020
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Sprite is showing outside texture rect
« Reply #4 on: August 15, 2020, 11:24:20 pm »
This is an issue that can occur if all co-ordinates are not integers; if you have, for example, fractional positions or not pixel perfect scaling, when choosing a colour to use for a pixel, it can be given a choice and may choose as you did not intend.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

 

anything