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

Author Topic: Declaring array of sprites with identical texture.  (Read 6261 times)

0 Members and 1 Guest are viewing this topic.

MingDynasty

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Declaring array of sprites with identical texture.
« on: November 27, 2016, 11:39:52 pm »
Hey all,

Pretty new to SFML and C++ in general. I'm wondering how you guys typically declare an array of sf::Sprites. I'm currently working on an Asteroids clone and I'm working on how best to implement a bullet system. I'm currently trying something like below:
sf::Texture * bulletTexture = new sf::Texture;
bulletTexture->loadFromFile(pathToBulletPNG);
sf::Sprite * sprites[100];
for (int i = 0; i < 100; ++i)
{
      sprites[i] = new sf::Sprite(*bulletTexture);
}
 

My issue is that when I go to iterate through and draw all of the active bullets, the last bullet to fire is the only one that gets drawn. I have a feeling it's related to how I'm creating the sprites and assigning them to my array. Is it possible that because I'm creating all of my sprites with the same texture that it's messing up the drawing?

Also, is this the correct way to approach an array of objects like this? Would like to hear if there's a better/more efficient way of handling arrays in C++. Thanks!

TCVM

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Declaring array of sprites with identical texture.
« Reply #1 on: November 28, 2016, 01:00:29 am »
What's your draw code?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Declaring array of sprites with identical texture.
« Reply #2 on: November 29, 2016, 02:06:20 am »
Hey all,
Hi! :)

Also, is this the correct way to approach an array of objects like this? Would like to hear if there's a better/more efficient way of handling arrays in C++. Thanks!
A good default container for storing multiple objects would be a vector (std::vector). Although there is also other types (including std::array), a vector is a good place to start and then only use a different one if you have good reason. One useful thing about vectors (as opposed to arrays) is that they can have their size changed (elements can be removed).

Using raw pointers in this (i.e. "sf::Sprite*" with "new sf::Sprite") can be dangerous. Memory control is very weak here; it would be wise to look into so-called "smart pointers". This short article on RAII explains this quite plainly.



As for your storage...

If you use a vector, you can simply erase the ones that are no longer in use. Then, draw all of the elements in the vector.

However, if you use an array, you need to keep track of which ones are currently in use and therefore which one to draw. One way is to create a custom struct/class that would contain the sprite and whether or not it should be drawn or is "alive". Another would be to "nullify" the pointer (e.g. sprites[0] = nullptr) and only draw the ones that aren't null if (sprites[0] != nullptr).

p.s. Technically, nullptr can be NULL for regular pointers and the draw test could be just if (sprites[0])
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

andariel97

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Declaring array of sprites with identical texture.
« Reply #3 on: November 29, 2016, 09:03:27 am »
When you call sprites = new sf::Sprite(*bulletTexture); you dont take the data from the texture and copy it into the sprite, you only create a link between the two. So if you lose the data in the texture,you cant draw the sprite. Because your pointers are local you lose them when you exit the function.make them global