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

Author Topic: Efficient environment sprites?  (Read 2260 times)

0 Members and 1 Guest are viewing this topic.

nimn

  • Newbie
  • *
  • Posts: 13
    • View Profile
Efficient environment sprites?
« on: November 19, 2016, 09:53:15 pm »
Howdy, I am new to SFML and C++ to some degree as well.
My issue is that when I display my character sprite and a large background sprite I get a massive drop in frame rate. My window is only 512 x 448 and I am aiming for 60fps, but only getting 18-19 with a monolithic background sprite. I imagine that this will only get worse as I add bad dudes and such. I tried toying around with leaving the background sprite out of the loop and adding a function to redraw the background directly over just where the character was and removing the clear.window, but there seems like a much easier way should be out there but after a lot of googling I wasn't able to find it or not skillful enough to recognize/implement it.

Any help would be much appreciated

int main() {
        setup();
        sf::RenderWindow window(sf::VideoMode(512, 448), "Zelda");
        window.setFramerateLimit(60);
        sf::Clock clock;

       

                while (window.isOpen()) {
                        sf::Event event;
                        while (window.pollEvent(event)) {
                                switch (event.type) {
                                case sf::Event::Closed:
                                        window.close();
                                        break;
                                }
                        }

                        window.clear();
                        input();
                        environmentcontrol();     //supplies sprite name, coords on spritesheet etc
                       
                        ///////Environment//////
                        if (spriteenv != "null") {
                                sf::Texture texture;
                                if (!texture.loadFromFile(spriteenv)) {
                                        cout << "Didnt Load Sprite" << endl;
                                }
                                sf::Sprite sprite(texture);
                                sprite.setPosition(envx, envy);
                                sprite.setTextureRect(sf::IntRect(envx1, envy1, envx2, envy2));
                                window.draw(sprite);
                        }
                        spritecontrol();     //supplies character sprite file name, sheet location and window position

                        ////////SPRITE 1////////
                        if (sprite1 != "null") {
                                sf::Texture texture;
                                if (!texture.loadFromFile(sprite1)) {
                                        cout << "Didnt Load Sprite" << endl;
                                }
                                sf::Sprite sprite(texture);
                                sprite.setPosition(sp1x, sp1y);
                                sprite.setTextureRect(sf::IntRect(sp1x1, sp1y1, sp1x2, sp1y2));
                                sprite.setColor(sf::Color(255, 255, 255, 255));
                                window.draw(sprite);
                        }
                        sf::Time time = clock.getElapsedTime();
                        cout << 1.0 / time.asSeconds() << endl;
                        clock.restart().asSeconds();
                        window.display();
                }
        }
 

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Efficient environment sprites?
« Reply #1 on: November 19, 2016, 10:26:05 pm »
Your problem is you're recreating all your assets every iteration, which is what kills your framerate.

You should only reload stuff when it really changes. For example, rather than determining whether "spriteenv" is set, also check whether it's been changed.

Does this help you for a start or do you need more?

nimn

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Efficient environment sprites?
« Reply #2 on: November 19, 2016, 10:56:11 pm »
thanks for the help

I see where your going, but the window.clear() is nuking the window and the assets only show up for a single frame unless they move. How would I get them drawn, or keep them from being cleared if they are unchanged?

thanks thanks!

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Efficient environment sprites?
« Reply #3 on: November 20, 2016, 10:09:29 am »
Look at the SFML examples. Basically you'd do something like this:
sf::RenderWindow window(/*params...*/);

sf::Texture myTexture;
sf::Sprite mySprite;
// Setup the sprite accordingly

while(window.isOpen()) {
    sf::Event event;
    while(window.pollEvent(event)) {
        // Handle your events here
    }
    // Update sprites and other objects here (unless you did in event handling already)

    window.clear();
    window.draw(mySprite);
    window.display();
}

nimn

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Efficient environment sprites?
« Reply #4 on: November 21, 2016, 12:07:49 am »
well that was a dumb mistake - but wow what a difference
Just needed a new, unique texture object (terminology?) and pull the texture definition out of the gameloop.

Got it working! as high as 6000fps!
thanks a bunch for all the help!

 

anything