106
Graphics / 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:
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;
}