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

Author Topic: Better way to handle sprites  (Read 3826 times)

0 Members and 1 Guest are viewing this topic.

BackgammonFan

  • Newbie
  • *
  • Posts: 2
    • View Profile
Better way to handle sprites
« on: July 17, 2024, 10:14:33 pm »
Hi! I am working on making a backgammon game, and am currently making my sprites like this
sf::Texture boardTexture;
sf::Sprite board;
board.setTexture(boardTexture);
board.setScale(3,3);
board.setPosition(100,200);

This is not that bad with one sprite, but with a game like backgammon I will need way more!
There must be a better way to do this, right?

vokazoo

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Better way to handle sprites
« Reply #1 on: July 17, 2024, 11:27:59 pm »
If I understood your problem, you're looking for a way to organise sprites and textures instead of creating them one by one? If that's not correct, please expand on this.

For a very simple game, the way you're doing it should be fine, but if you want more organisation, you can sort sprites by putting them in arrays like std::array<sf::Sprite, 15> player1Pieces. Or, for more functionality, create class Piece and put sf::Sprite (or better sf::VertexArray) as a member field, something like this:
class Piece : public sf::Transformable, public sf::Drawable
{
    sf::Sprite sprite;
    bool captured = false;

    void draw(sf::RenderTarget& target, sf::RenderStates states) const override
    {
        if (captured)
            return;
        states.transform *= getTransform();
        target.draw(sprite, states);
    }
public:
    void setTexture(const sf::Texture& texture) { sprite.setTexture(texture); }
    void capture() { captured = true; }
    // add extra functionality here as needed
};

Keep textures outside of class unless there's only one sprite that uses it. If you need to manage huge amount of textures it may be a good idea to make some kind of texture manager, otherwise an array that holds all textures is fine. Keep in mind that Textures must not get out of scope as long as any sprite is using them. So at the end it should look something like this:
int main()
{
    enum TextureID { Board, RedPiece, BlackPiece, Dice, Count };
    std::array<sf::Texture, TextureID::Count> textures;
    textures[Board]     .loadFromFile("Board.png");
    textures[RedPiece]  .loadFromFile("RedPiece.png");
    textures[BlackPiece].loadFromFile("BlackPiece.png");
    textures[Dice]      .loadFromFile("Dice.png");

    sf::Sprite board;
    board.setTexture(textures[Board]);

    sf::Sprite dice;
    dice.setTexture(textures[Dice]);

    std::array<Piece, 15> player1Pieces;
    std::array<Piece, 15> player2Pieces;
    for (Piece& piece : player1Pieces)
        piece.setTexture(textures[RedPiece]);
    for (Piece& piece : player2Pieces)
        piece.setTexture(textures[BlackPiece]);
    // set starting positions for pieces in a loop

    bool gameRunning = true;
    while (gameRunning)
    {
        // game loop
        // update game logic
        // draw previously created sprites
    }
}

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Better way to handle sprites
« Reply #2 on: July 18, 2024, 04:29:33 am »
Also you can keep in mind that its not always necessary to have several sprites. You can draw the same sprite with different values if you change those values and draw before one loop has completed.

For example:

while(window.isOpen())
{
    //Event stuff
   
    window.clear();
    for(int a=0;a<20;a++)
    {
         sprite.setTexture(texture[cardID]);
         sprite.setPosition(position[cardID]);
         window.draw(sprite);
    }
    window.display();
}


The example code separates position and texture values to their own entities while only one sprite is ever used.

BackgammonFan

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Better way to handle sprites
« Reply #3 on: July 19, 2024, 05:04:29 pm »
Thank you so much for the help  ;D

 

anything