0

So I animated a sprite sheet for a game I'm making, but for some reason It's lagging my game a lot.

The sprite sheet is a 1248x120 .png file, each sprite is 156x120 pixels in size.

Code I'm using to animate:

    void Entity::AnimateHorizontal(sf::Texture tex, int width, int hight)
    {
    sprite.setTextureRect(sf::IntRect(source.x * width, 0, width, hight));
    frameCounter += frameSpeed * frameTimer.restart().asSeconds();
    if (frameCounter >= switchFrame)
        {
            frameCounter = 0;
            source.x++;
            if (source.x * width >= tex.getSize().x) source.x = 0;
        }
}

And:

Object.AnimateHorizontal(tex_Mech1Feet,156,120);

In my game loop.

FPS just loading the sheet, no animation: Click me!

FPS with animating the sheet: Click me!

Also note that this is currently the only thing I'm actually doing in my game, so it definitely shouldn't be lagging this bad.

I'm still a complete novice when it comes to programming with C++, so please bear with me.

That being said, I appreciate any and all advice, thanks in advance!

nvoigt
  • 75,013
  • 26
  • 93
  • 142

1 Answers1

3

void Entity::AnimateHorizontal(sf::Texture tex, int width, int hight)

You're copying the texture each time. This is probably the performance bottleneck. Use a (const) reference instead.

My general advice: use a profiler to know how much time your program spend in which function. This way you know directly where to look at.

Hiura
  • 3,500
  • 2
  • 20
  • 39
  • That doesn't seem to fix the problem, seeing as the `sf::Texture tex` is only used to get the sprite sheet's size to check whether or not it has reached the end and should loop, I don't see how using const to reference it would do anything. If anything is copying the texture it would be the `sprite.setTextureRect` command (but afaik _setTextureRect_ doesn't even set the texture. I'm using `(!tex.loadFromFile("Data/graphics/sprites/spritesheet.png")) return;` for that, _setTextureRect_ should only be setting which part of the texture is used, shouldn't it?). What should I do instead? – user2558804 Oct 18 '13 at 22:45
  • Also, if I make the sprite sheet smaller, I get close to 60 fps again, could it be that my sprite sheet is too big or my processor is too slow? (Hard to believe though, running a i5 2500k @ 4.2ghz) – user2558804 Oct 18 '13 at 22:47
  • 1
    No, the Texture is copied when you call `AnimateHorizontal()`. Also, even if you use only `getSize()` the compiler **has to** copy the whole texture. Remember also that the texture is stored on your graphical memory. Your processor power has pretty much nothing to do here. (There's a lot of technical details I won't go into.) – Hiura Oct 19 '13 at 08:33
  • BTW, if you're still a beginner I highly recommend one of these books. It will make your life so much easier. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Hiura Oct 19 '13 at 08:34
  • If it's my GPU that's under strain, why does my TaskManager say my CPU is using 40% on my App? Also, thanks for the book recommendations, I will definitely check those out. – user2558804 Oct 19 '13 at 18:10