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

Author Topic: Is this going to cause performance issues?  (Read 2597 times)

0 Members and 1 Guest are viewing this topic.

ILostMyAccount

  • Newbie
  • *
  • Posts: 22
    • View Profile
Is this going to cause performance issues?
« 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?

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Is this going to cause performance issues?
« Reply #1 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.

ILostMyAccount

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Is this going to cause performance issues?
« Reply #2 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!

ILostMyAccount

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Is this going to cause performance issues?
« Reply #3 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++;
                }
        }
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Is this going to cause performance issues?
« Reply #4 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 ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is this going to cause performance issues?
« Reply #5 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.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Is this going to cause performance issues?
« Reply #6 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything