Recently, I've tried to exercise SFML with textures. However, I've encountered some problems. SFML website tells me that the most efficient way to use textures is to update sprites. Otherwise, our program may consume a lot of memory. I have one base class and two other classes derived from it. How may I initialize my texture
variable so derived classes could use it with their sprites? I've tried initializing texture in Base
class constructor and call it in derived classes, but then, I realized that my problem wasn't solved, because calling one constructor twice is a nonsense. This problem is so important for me, because I'm using the State Pattern which creates a lot of dynamically allocated objects.
#include <iostream>
class Base
{
protected:
sf::Texture texture; //How to initialize this?
sf::Sprite sprite;
public:
Base();
};
class Derived1 : public Base
{
public:
Derived1()
{
sprite.setTexture(texture);
}
};
class Derived2 : public Base
{
public:
Derived2()
{
sprite.setTexture(texture);
}
};