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

Author Topic: Texture loadFromFile every frame.  (Read 1278 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Texture loadFromFile every frame.
« on: December 04, 2020, 10:03:12 pm »
Hello,

So I'm getting more and more familiar with sf::RenderTexture. It has REALLY helped with this current project since it requires drawing millions of pixels. I finally got it to run smooth switching to it. What I am concerned about is the texture loading the sf::image being drawn to every frame.

It's running great, as I said, but I'm just wondering if this is 'okay' to do and if not, what would be the appropriate solution? Is loading from the image 'okay', but loading up a new texture from a file different? I'm just curious about the future and if I had more stuff going on it would cause problems.

Thanks

void Worms_Part1::onDraw() {

        for (int y = worldMin.y; y <= worldMax.y; ++y) {
                for (int x = worldMin.x; x <= worldMax.x; ++x) {
                        const sf::Vector2i p(x, y);
                        if (m_map.isInBounds(p)) {

                                m_image.setPixel(x, y, m_map(p).isLand ? sf::Color(58, 110, 45) : sf::Color(45, 198, 198));
                        }
                }
        }

        m_renderTexture.clear();
       
        m_texture.loadFromImage(m_image);
        m_renderTexture.draw(sf::Sprite(m_texture));
        m_renderTexture.display();
       
        m_renderSprite.setTexture(m_renderTexture.getTexture());
        window.draw(m_renderSprite);
}

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Texture loadFromFile every frame.
« Reply #1 on: December 07, 2020, 03:08:18 pm »
An sf::Image has its data stored in RAM already, so when you do loadFromImage() the graphics data is copied from RAM to video memory which, as you've noticed, is reasonably fast - fast enough for your use case at least. Loading an image from a file is slower because it has to read from storage rather than RAM (although SSDs appear to be rapidly closing that gap), be decoded if it's a compressed format such as png or jpg, then finally get copied into a section of RAM ready to be uploaded to the GPU.

I probably wouldn't recommend trying to load from file every frame, but as a more general rule I tend to not worry about these things too much until they present a measurable problem, in which case I'll investigate more and look for ways to improve performance.