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

Author Topic: Array of Sprites  (Read 1258 times)

0 Members and 1 Guest are viewing this topic.

palace

  • Newbie
  • *
  • Posts: 2
    • View Profile
Array of Sprites
« on: February 02, 2025, 04:03:15 am »
Hi All. I decided to dig out an old SFML 2 project from way way back and update to play around with SFML 3. Pretty much everything converted ok following the migration guide. One thing I'm stuck on is my array of Sprites. I've got a header file where I setup an array of sprites.

sf::Sprite Patterns[23];

Then in main I load in a texture and each sprite is a part of that texture.

    PiecePatterns.loadFromFile(PatternFile);
    for (int patterns = 0; patterns < 23; patterns++)
    {
        Patterns[patterns].setTexture(PiecePatterns);
        Patterns[patterns].setTextureRect(sf::IntRect(patterns * PATTERN_WIDTH, 0, PATTERN_WIDTH, PATTERN_HEIGHT));
    }

Now probably not the most elegant but worked back in the day. Now there is no default constructor I'm wondering what might be the best way to handle this in SFML 3?

Cheers
Peter

palace

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Array of Sprites
« Reply #1 on: February 02, 2025, 05:02:56 am »
Ok. Solved it by rethinking the old code. Given it was one texture like a spritesheet, I stored the piece of texture I needed for each of the 23 patterns in and array of sf:IntRect. Then when I need a particular pattern I just do a setTexture for the sprite.

//draw top pattern
top = PieceTop[piece] >> 5;
Patterns.setTextureRect(PatternData[top]);
Patterns.setPosition(sf::Vector2f(X, Y));
Window.draw(Patterns);

//draw right
right = PieceRight[piece] >> 10;
Patterns.setTextureRect(PatternData
);
Patterns.setRotation(sf::degrees(90));
Patterns.setPosition(sf::Vector2f(X + sprite_size, Y));
Window.draw(Patterns);
Patterns.setRotation(sf::degrees(0));
.
.
.


Hapax

  • Hero Member
  • *****
  • Posts: 3415
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Array of Sprites
« Reply #2 on: February 04, 2025, 05:36:46 am »
The removal of default constructors for "lightweight" objects have thrown me too, to be honest.

However, it seems that you are using sprites to draw multiple shapes that would really be one big shape (likely some sort of tile map or similar?)

For this, I'd highly recommend learning how to use a vertex array; it allows you to draw multiple quads (as a sprite is) - or tiles - as one object. Once you grasp them, you'll likely never use anything else!

With that said, if you find them too daunting:
1) ask for help ;),
2) use a tile map object*.



*such as my very own Cheese Map, which is designed to simplify this sort of thing!
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*