Search code examples
c++cachingsfml

Should I cache this object in c++ (SFML)


(I'm completely new to c++, but I have expierence in c# and java)

Hi, I'm creating a chess game in c++ (SFML) and I have a Piece class which has draw method. Right now I'm doing it like this:

void Piece::draw(sf::RenderWindow& p_window, bool p_selected) {
    if (p_selected) {
        sf::RectangleShape shape;
        shape.setFillColor(sf::Color(190,235,127));
        shape.setOutlineColor(sf::Color(221,237,142));
        shape.setOutlineThickness(3);
        shape.setSize(sf::Vector2f(80,80));
        shape.setPosition(sprite.getPosition());
        p_window.draw(shape);
    }

    p_window.draw(sprite);
}

If the piece is selected, I create RectangleShape (to let player know what piece is selected) , set its properties and then I draw it.

is this a good way of doing this? Or should I cache the RectangleShape with setted properties and just update its position to the selected piece position?

Let me know if I missed something important, thanks for any answer.


Solution

  • Realistically, the performance impact of caching vs not caching an sf::RectangleShape is tiny, especially for a relatively simple program such as a chess game. Generally though, it is a good idea to cache variables that are used repeatedly each frame rather than creating them over and over again. I decided to write a small test case using the code you provided to measure the difference in performance over 10,000 draw calls. The result was a roughly 23% performance increase (1518ms vs 1030ms.) Here is the code I used so that you can test it yourself:

    #include <SFML/Graphics.hpp>
    #include <iostream>
    
    class Test {
    public:
        static sf::RectangleShape rect;
    
        Test() {
            rect.setFillColor(sf::Color(190, 235, 127));
            rect.setOutlineColor(sf::Color(221, 237, 142));
            rect.setOutlineThickness(3);
            rect.setSize(sf::Vector2f(80, 80));
            rect.setPosition(0, 0);
        }
    
        void drawNoCache(sf::RenderWindow& p_window) {
            sf::RectangleShape shape;
            shape.setFillColor(sf::Color(190, 235, 127));
            shape.setOutlineColor(sf::Color(221, 237, 142));
            shape.setOutlineThickness(3);
            shape.setSize(sf::Vector2f(80, 80));
            shape.setPosition(0, 0);
            p_window.draw(shape);
        }
    
    
        void drawWithCache(sf::RenderWindow& p_window) {
            p_window.draw(rect);
        }
    };
    
    sf::RectangleShape Test::rect;
    
    int main() {
        sf::RenderWindow window(sf::VideoMode(80, 80), "SFML");
        Test t;
        sf::Clock clock;
        clock.restart();
        for (int i = 0; i < 10000; i++) {
            window.clear(sf::Color(0, 0, 0));
            t.drawNoCache(window);
            window.display();
        }
        std::cout << clock.restart().asMilliseconds() << std::endl;
        for (int i = 0; i < 10000; i++) {
            window.clear(sf::Color(0, 0, 0));
            t.drawWithCache(window);
            window.display();
        }
        std::cout << clock.restart().asMilliseconds() << std::endl;
    }