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

Author Topic: A little problem with SetSubRect  (Read 3927 times)

0 Members and 1 Guest are viewing this topic.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
A little problem with SetSubRect
« on: March 31, 2011, 03:45:31 pm »
Apologies if I've missed the obvious.

I'm using SetSubRect to point at 64x64 images in a sheet and have noticed that when moving the sprites, I get part of the adjacent image 'creeping' in.

In this example, which is basically the tutorial, you can notice it as a white line at the edge of the sprite. Be patient when moving it around, sometimes it takes a little while to happen.

I suppose a workaround would be to leave a blank pixel on every edge, but I'd prefer to understand it better.

Thanks!

The test image is here:




Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    sf::Image Image;
    if (!Image.LoadFromFile("spritesheet.png"))
        return EXIT_FAILURE;

Image.SetSmooth(false);

    sf::Sprite Sprite(Image);

    Sprite.SetColor(sf::Color(255, 255, 255, 255));
    Sprite.SetPosition(200.f, 100.f);
    Sprite.SetScale(.5, .5);
Sprite.SetSubRect(sf::IntRect(64, 64, 128, 128));

float xpos = 200;
float ypos = 100;

    // Start game loop
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Get elapsed time
        float ElapsedTime = App.GetFrameTime();

        // Move the sprite
        if (App.GetInput().IsKeyDown(sf::Key::Left))  xpos -= 10 * ElapsedTime;
        if (App.GetInput().IsKeyDown(sf::Key::Right)) xpos += 10 * ElapsedTime;
        if (App.GetInput().IsKeyDown(sf::Key::Up))    ypos -= 10 * ElapsedTime;
        if (App.GetInput().IsKeyDown(sf::Key::Down))  ypos += 10 * ElapsedTime;

Sprite.SetPosition(xpos, ypos);
       
        App.Clear();
        App.Draw(Sprite);
        App.Display();
    }

    return EXIT_SUCCESS;
}
{much better code}

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
A little problem with SetSubRect
« Reply #1 on: April 01, 2011, 02:57:50 am »
Are you using SFML 1.6 or 2? I had this issue with 1.6 and it was fixed by migrating to SFML 2 after I was told there was no way to fix it in 1.6

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
A little problem with SetSubRect
« Reply #2 on: April 01, 2011, 04:10:44 am »
A Minecraft fan? Yay.
The third and fourth parameters in sf::IntRect are width and height, not right and bottom (at least in SFML2, which I am using). This is resulting in a large amount of whitespace to the bottom and left.
As for your problem, I am seeing it in SFML2. The only way I can think of to fix this is to ensure that the sprite is only drawn at integer coordinates.
I use the latest build of SFML2

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
A little problem with SetSubRect
« Reply #3 on: April 01, 2011, 09:45:35 am »
I'm using SFML 1.6, so if changing to 2 would clear this up I don't need to worry about it too much for now?

I see it worked for you JAssange, but not for Oni.

@Oni - Width & Height make more sense, but in 1.6 they appear to be right/bottom.
It does help if drawn at integer coordinates, but if the screen is resized it can happen again.

So I guess switching to SFML 2 before my project gets too big would be a good idea?

Oni - Yes, I've put the hours in on Minecraft  :wink:
{much better code}

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
A little problem with SetSubRect
« Reply #4 on: April 03, 2011, 04:42:04 am »
Quote from: "Jove"
So I guess switching to SFML 2 before my project gets too big would be a good idea?
Yes, especially since SFML1.x isn't maintained anymore.
I use the latest build of SFML2