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

Author Topic: Using Texture::loadFromFile(filename. IntRect) to load a spritesheet  (Read 1577 times)

0 Members and 1 Guest are viewing this topic.

Daniel Schreiber Mendes

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Hey my friends I have a really big spritesheet and wanted to ask about the performance difference between loading a lot of textures from parts of it and loading the full texture one time and setting the IntRect when giving it to a sprite. 
Like this pseudocode:
    loadFromFile(spritesheetPath, IntRect(184, 108, 13, 16));
   
    loadFromFile(spritesheetPath, IntRect(168, 108, 14, 18));
   
    loadFromFile(spritesheetPath, IntRect(634, 24, 122, 25));
   // and so on
 
vs.
    loadFromFile(spritesheet);
    sprite1.setTexture(spritesheet, IntRect(184, 108, 13, 16))
    sprite2.setTexture(spritesheet, IntRect(168, 108, 14, 18))
    sprite3.setTexture(spritesheet, IntRect(634, 24, 122, 25))
    // and so on
 
The second method would be very inconvenient because of the way my Resource Manager is structured but I don't want do loose too much performance when loading lots of textures.

Thanks for your Answers :)

Daniel

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Using Texture::loadFromFile(filename. IntRect) to load a spritesheet
« Reply #1 on: October 19, 2019, 08:09:33 pm »
in the first case you are loading the same texture over and over again. it's bad at loading time and probably at drawing time too (I'm saying "probably" because I don't know if internally SFML keeps copies of the textures or just many references to the same texture while loading a texture with IntRect)

use the second case, where you just load it once and point where each sprite should use it.

edit: why exactly would it be inconvenient for you? remember you can change a sprite's texture rect anytime with
sprite.setTextureRect(IntRect(184, 108, 13, 16))
« Last Edit: October 19, 2019, 08:15:06 pm by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Using Texture::loadFromFile(filename. IntRect) to load a spritesheet
« Reply #2 on: October 19, 2019, 09:24:25 pm »
Quote
in the first case you are loading the same texture over and over again. it's bad at loading time and probably at drawing time too
It's "bad" (depends on texture size and number of spritesheets) at loading time but you can easily avoid loading the same image from disk again and again, by loading it into a sf::Image and then loading all parts into sf::Textures.

At drawing time, I don't think it really makes a difference. Everyone will tell you to use a single texture but in the end it won't make a huge performance difference.
Laurent Gomila - SFML developer

Daniel Schreiber Mendes

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Using Texture::loadFromFile(filename. IntRect) to load a spritesheet
« Reply #3 on: October 20, 2019, 09:43:31 am »
Alright thanks you two I implemented it and it works fine. Wasn't that much effort. :)

Daniel Schreiber Mendes

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Using Texture::loadFromFile(filename. IntRect) to load a spritesheet
« Reply #4 on: October 20, 2019, 10:01:30 am »
It actually increased my loading performance 4  x times when loading the image one time and getting the textures from it.