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])