-
I'm loading the textures like this:
sf::Texture texture[16];
int count = 0;
for (int line = 0; line <= 64; line += 64)
{
for (int column = 0; column < 512; column += 64)
{
texture[count].loadFromFile("Resources/Sprites/SpriteSheet.png",sf::IntRect(column,line,64,64));
count++;
}
}
Am I going to take any advantage from the fact that they are on the same image file? Or is this just going to make things worse?
-
Why do you want to create a texture for each rect?
Just load the whole image into your texture and draw the needed rect with a sprite.
See setSubRect(...) in the sf::Sprite class.
-
Why do you want to create a texture for each rect?
Just load the whole image into your texture and draw the needed rect with a sprite.
See setSubRect(...) in the sf::Sprite class.
Ok, thank you!
-
Now it's like this
sf::Texture texture;
texture.loadFromFile("Resources/Sprites/SpriteSheet.png");
sf::Sprite sprite[16];
int count = 0;
for (int line = 0; line <= 64; line += 64)
{
for (int column = 0; column < 512; column += 64)
{
sprite[count].setTexture(texture);
sprite[count].setTextureRect(sf::IntRect(column, line, 64, 64));
count++;
}
}
-
How about using a std::vector instead of an array?
Also, don't forget to check to make sure that the texture loading was success before proceeding.
Here's one possible rewrite:
sf::Texture texture;
if (!texture.loadFromFile("Resources/Sprites/SpriteSheet.png"))
return EXIT_FAILURE;
const sf::Vector2i spriteSize(64, 64);
std::vector<sf::Sprite> sprites(16, sf::Sprite(texture));
for (unsigned int line = 0; line < 2; ++line)
{
for (unsigned int column = 0; column < 8; ++column)
sprites[line * 8 + column].setTextureRect(sf::IntRect(column * spriteSize.x, line * spriteSize.y, spriteSize.x, spriteSize.y));
}
This post is just a suggestion ;)
-
How about using a std::vector instead of an array?
If the size is fixed there's absolutely no reason. I can't see what your code improves compared to the raw array.
-
True, in this case. It was just another option.
I would generally use vector as standard and sometimes forget that people really like to use arrays for some reason ;D
One thing I prefered in my version was the ability to use the sprite's constructor during the creation of the vector to assign the textures all there at once.
Also, testing the result of the texture loading was missing, although I might output some error with it too.
Just for kicks, it also reduces the need for the excess int (count) outside of the scope that it's required.