Skip to main content
added 95 characters in body
Source Link
BOOM
  • 11
  • 2

I currently have a Renderer class, which as in the example code, looks like this:

Class Renderer()
{
private:
    std::vector<sf::Sprite*> sprites;

public:
    
    void pushSprite(sf::Sprite& sprite)
    {
        sprites.push_back(&sprite);
    }

    void update()
    {
        std::sort(sprites.begin(), sprites.end(), sort_y);
    }

    void render(sf::RenderWindow window)
    {
        for (/**/)
        {
            window.draw(*sprites[i])
        }
    }
}

The problem is the following, I have classes like Projectile which holds it's own sprite, and I have vectors in my Game class, to hold instances of classes like this, but as these vectors increase in size, they may change their position in memory, therefore making all the pointers in the Renderer invalid.

I have a few questions:

Having a Renderer class structured like that is the usual? If not, how should I do it? If yes, how would I solve the problem?

I currently have a Renderer class, which as in the example code, looks like this:

Class Renderer()
{
private:
    std::vector<sf::Sprite*> sprites;

public:
    
    void pushSprite(sf::Sprite& sprite)
    {
        sprites.push_back(&sprite);
    }

    void render(sf::RenderWindow window)
    {
        for (/**/)
        {
            window.draw(*sprites[i])
        }
    }
}

The problem is the following, I have classes like Projectile which holds it's own sprite, and I have vectors in my Game class, to hold instances of classes like this, but as these vectors increase in size, they may change their position in memory, therefore making all the pointers in the Renderer invalid.

I have a few questions:

Having a Renderer class structured like that is the usual? If not, how should I do it? If yes, how would I solve the problem?

I currently have a Renderer class, which as in the example code, looks like this:

Class Renderer()
{
private:
    std::vector<sf::Sprite*> sprites;

public:
    
    void pushSprite(sf::Sprite& sprite)
    {
        sprites.push_back(&sprite);
    }

    void update()
    {
        std::sort(sprites.begin(), sprites.end(), sort_y);
    }

    void render(sf::RenderWindow window)
    {
        for (/**/)
        {
            window.draw(*sprites[i])
        }
    }
}

The problem is the following, I have classes like Projectile which holds it's own sprite, and I have vectors in my Game class, to hold instances of classes like this, but as these vectors increase in size, they may change their position in memory, therefore making all the pointers in the Renderer invalid.

I have a few questions:

Having a Renderer class structured like that is the usual? If not, how should I do it? If yes, how would I solve the problem?

Source Link
BOOM
  • 11
  • 2

Creating a Renderer class using SFML

I currently have a Renderer class, which as in the example code, looks like this:

Class Renderer()
{
private:
    std::vector<sf::Sprite*> sprites;

public:
    
    void pushSprite(sf::Sprite& sprite)
    {
        sprites.push_back(&sprite);
    }

    void render(sf::RenderWindow window)
    {
        for (/**/)
        {
            window.draw(*sprites[i])
        }
    }
}

The problem is the following, I have classes like Projectile which holds it's own sprite, and I have vectors in my Game class, to hold instances of classes like this, but as these vectors increase in size, they may change their position in memory, therefore making all the pointers in the Renderer invalid.

I have a few questions:

Having a Renderer class structured like that is the usual? If not, how should I do it? If yes, how would I solve the problem?