At first it was just raw pointers but then someone told me and pointed me to resources which said that i should never use raw pointers in C++ if i have the option of using smart pointers. So i made it with weak_ptr/shared_ptr. I spent quite a lot of time on it(learning about smart pointers mostly) so even though I don't plan on using it I still want to know what i did wrong.
You should not use raw pointers
with ownership - i.e. don't manage memory manually using
new and
delete. Here,
std::unique_ptr is the alternative. Raw pointers are no problem as long as they don't own resources.
std::shared_ptr is a complex and expensive smart pointer, it should never be the default approach. It can be taken when ownership has to be shared among multiple owners, and it's not clear which owner is responsible of destruction.
std::weak_ptr is always used together with
std::shared_ptr; it allows to have weak references (i.e. smart pointers that don't keep the resource alive).
Also is there anything wrong with how I've made the sprite cache?
I don't see why you have a sprite manager and shared pointers to sprites.
sf::Sprite is a very lightweight class, don't be afraid of copying it. So, simply have a
std::vector<sf::Sprite> without any managers, custom sprite classes and smart pointers. These indirections make code very complicated, but don't really add an advantage. Your class
ActualSprite is broken anyway, since it doesn't consider the Rule Of Three.
What I'd like the most is to have either a specialized class(a functor maybe?) or just a function which would be used to unify(synchronize?) the logic elements with sprites.
You could simply have a function that draws a tile.
void Renderer::draw(const Tile& tile, int x, int y)
{
sf::Sprite sprite;
sprite.setTexture(getTileTexture(tile));
sprite.setTextureRect(getTileTextureRect(tile));
sprite.setPosition(Tile::Size * x, Tile::Size * y);
...
mWindow.draw(sprite);
}