SFML community forums

Help => General => Topic started by: ILostMyAccount on January 28, 2015, 11:12:58 am

Title: Is this going to cause performance issues?
Post by: ILostMyAccount on January 28, 2015, 11:12:58 am
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?
Title: Re: Is this going to cause performance issues?
Post by: Rhimlock on January 28, 2015, 11:20:27 am
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.
Title: Re: Is this going to cause performance issues?
Post by: ILostMyAccount on January 28, 2015, 11:21:26 am
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!
Title: Re: Is this going to cause performance issues?
Post by: ILostMyAccount on January 28, 2015, 11:27:21 am
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++;
                }
        }
 
Title: Re: Is this going to cause performance issues?
Post by: Hapax on January 29, 2015, 02:17:13 am
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 ;)
Title: Re: Is this going to cause performance issues?
Post by: Laurent on January 29, 2015, 08:03:42 am
Quote
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.
Title: Re: Is this going to cause performance issues?
Post by: Hapax on January 29, 2015, 04:18:29 pm
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.