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

Author Topic: Optimizing Texture and Sprite management  (Read 3455 times)

0 Members and 1 Guest are viewing this topic.

MattF

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Optimizing Texture and Sprite management
« on: November 08, 2012, 01:59:09 pm »
Hi!

I have a multi-part question regarding the proper way of loading, storing and accessing textures and sprites.

Currently i am working on a 2d side-scrolling brawler. Three major game elements, which use textures and sprites are hero, enemies and background/map. I'd like to implement parallax scrolling later on.

Anyway, i wanted to ask about a proper way to load and store gfx elements for each of those instances.

For hero class, i keep all textures and sprites as members, and load textures in the constructor. Since there is only one hero, and he is loaded with the map, it should just work fine. Here's relevant parts:


Hero::Hero (b2World *aWorld, float posX, float posY) : world(aWorld)
        {
                hero_run.loadFromFile("HeroTextures/run.png");
                hero_turn.loadFromFile("HeroTextures/turn.png");
                hero_jump.loadFromFile("HeroTextures/jump.png");
                hero_crouch.loadFromFile("HeroTextures/crouch.png");
                hero_roll.loadFromFile("HeroTextures/roll.png");
                hero_stop.loadFromFile("HeroTextures/stop.png");
                hero_stand.loadFromFile("HeroTextures/stand.png");
                hero_attack.loadFromFile("HeroTextures/attack.png");
                m_sprite.setPosition(posX, posY);
                m_sprite.setOrigin(250,250);
                frame = 0;
                set_b2_body(); // Box2d stuff
               
 }

void Hero::display(State state)
{
        switch(state)
        {
        case running:
                if( direction == right)
                m_sprite.setScale(1.0f,1.0f);
                else
                m_sprite.setScale(-1.0f,1.0f);

                m_sprite.setTexture(hero_run);
                m_sprite.setTextureRect(hero_run_clips[frame]);
                frame++;
                if (frame > 20 )
                        frame = 0;
                break;
}

 

That's the gist of it. I imagine, for enemy class, i'll have to load textures somewhere else, because there will be mutliple enemies constructed from the same texture. Other than that, setTexture seems the way to go.

So regarding this part, my question is: is this the optimal way of handling graphics for animated entities, or could i store it, or access it in more optimized way?


Second part of my post pertains to the background. After preparing elements of the map i just set em the standard way:


TestLevel::TestLevel(b2World *aWorld) : world(aWorld)

{
        backgTexture1.loadFromFile("background/testlevel1.jpg");
        backgTexture2.loadFromFile("background/testlevel2.jpg");
        backgTexture3.loadFromFile("background/testlevel3.jpg");
        background1.setTexture(backgTexture1);
        background2.setTexture(backgTexture2);
        background3.setTexture(backgTexture3);
        background1.setPosition(0,0);
        background2.setPosition(5000,0);
        background3.setPosition(10000,0);
        set_ground_bodies(); // box2d stuff
}

 

It runs nice and all, but loading time is now good couple seconds, and it's only a small part of an actual planned level. Those textures are pretty big though (5000x5000).

So my last question is: how to improve loading time?

Edit: only part of my post showed the 1st time, edited for full version.
« Last Edit: November 08, 2012, 02:23:46 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Optimizing Texture and Sprite management
« Reply #1 on: November 08, 2012, 02:28:18 pm »
Quote
So regarding this part, my question is: is this the optimal way of handling graphics for animated entities, or could i store it, or access it in more optimized way?
It would be even more optimized with all the animations put into the same texture, but otherwise it's ok.

Quote
So my last question is: how to improve loading time?
You can't. The only thing that you can do is to load textures on the fly in a thread, if possible.
Laurent Gomila - SFML developer

MattF

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Optimizing Texture and Sprite management
« Reply #2 on: November 08, 2012, 02:43:22 pm »
Ok, thanks.

Since this game won't be a resource hog, i guess it won't be a problem to load 20% of the map, and load rest on the fly.

Brodal

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: Optimizing Texture and Sprite management
« Reply #3 on: November 10, 2012, 01:34:57 pm »
One way to handle textures that is to my liking is to use a texture manager of sorts. In my case it is usually a singleton that loads textures for me when i need them ( singletons are kinda frowned upon so someones probably gonna argue with me there ). I use an std::map to store each texture with a string as key value to it, and when i want a texture i call the manager and ask for a texture, if the texture doesn't exist in the map i load it, if it does exist i just return a reference to it. There are many ways to do it, this is just one :)

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Optimizing Texture and Sprite management
« Reply #4 on: November 11, 2012, 09:52:30 pm »
1) Don´t load files in the object constructor. File loading may fail, and you can have a "half constructed" object (Put the loading code in a loading function).
2 ) Put all the character animations into 1 texture file. This should be better for your graphics driver, and even better for your hard drive
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Halsys

  • Jr. Member
  • **
  • Posts: 66
  • Halsys like Hal.sys
    • View Profile
    • Email
Re: Optimizing Texture and Sprite management
« Reply #5 on: November 11, 2012, 10:46:06 pm »
This is mostly used for 3d games but try researching Mipmaps...
That way, progressively, your loading the sprites...
This is also great for when you want to zoom out and will not be able to see the whites of there eyes to begin with.

Ya its stupid, Mipmaps for 2D games? Here's my offer, Why not?
If you notice I put "....", in my sentences way too much... I'm sorry.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Optimizing Texture and Sprite management
« Reply #6 on: November 11, 2012, 10:48:49 pm »
1) Don´t load files in the object constructor. File loading may fail, and you can have a "half constructed" object (Put the loading code in a loading function).
He doesn't load files manually and it's perfectly safe to call loadFromFile on a sf::Texture in the constructor. If the image file doesn't exist you'll just be left with an white square.

If you load files on your own, you better handle the exception correctly, than just putting somewhere else. But depending on the logic it might be nicer to put it somewhere else. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Optimizing Texture and Sprite management
« Reply #7 on: November 12, 2012, 04:28:14 pm »
It seems we will never agree in something  ;D
(just kidding)
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!