Using SFML 2.1, Code::Blocks 12.11, minGW gcc-4.7.1-tdm, "ThinkPad Display 1366x768" monitor
My issue is how a certain sprite looks by default when generated on the screen.
Here's the necessary code:
const int WINDOW_WIDTH = 1024;
const int WINDOW_HEIGHT = 576;
sf::RenderWindow Window;
Window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "azertyqwerty", sf::Style::Close);
sf::Texture playerTexture;
sf::Sprite playerSprite;
if (!playerTexture.loadFromFile("player.png"))
std::cout << "Error: Could not load player image" << std::endl;
playerSprite.setTexture(playerTexture);
playerSprite.setOrigin(playerSprite.getLocalBounds().width/2, playerSprite.getLocalBounds().height/2);
playerSprite.setPosition(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
while (Window.isOpen())
{
Window.clear();
Window.draw(playerSprite);
Window.display();
}
Here's the original image file used: http://i1093.photobucket.com/albums/i434/GanadoFO/player.png
(http://i1093.photobucket.com/albums/i434/GanadoFO/player.png)
It is a 5x5 square image. The border is red and the center pixel is a golden color.
Here's what the sprite looks like when displayed, no scaling or other transformations: (http://i1093.photobucket.com/albums/i434/GanadoFO/sprite5x5_01.png)
Here's the same picture enlarged (externally in Paint, not via the scale function) to clearly show the error:
(http://i1093.photobucket.com/albums/i434/GanadoFO/sprite5x5_01_enlarged.png)
You can see the weird displacement of the pixels compared to the original image. This is the problem.
If I use the scale function to blow-up the image in program run-time, it becomes the intended look, albeit very big:
(http://i1093.photobucket.com/albums/i434/GanadoFO/sprite5x5_scaled.png)
So, my questions:
Why does it not correctly display the image file the way it is? Why does it displace the border pixels, and how can I fix it?
Is it due to SFML itself or my monitor/gpu?
If someone can test my image to see if it does the same thing on their program, I would appreciate it, thanks.
That does appear to be the problem, but could you explain why? Considering 1024 and 576 are both divisible by 2, why does it still make a round-off error? If I make the initial position, for example,
playerSprite.setPosition(512, 288);
it does display the sprite correctly. Sorry if that's a more basic C++ question as opposed to SFML.
Anyway, so I changed it to
playerSprite.setPosition(int(WINDOW_WIDTH/2 + 0.5), int(WINDOW_HEIGHT/2 + 0.5));
and it also displays the sprite correctly, thanks.
EDIT:
Also, obviously the round-off issue applies to the origin as well.
playerSprite.setOrigin(playerSprite.getLocalBounds().width/2, playerSprite.getLocalBounds().height/2);
If I comment this line, the sprite shows up correctly, but if I keep it active, then it's the old problem again. Why does the origin position affect what my sprite looks like when it is displayed?