1
\$\begingroup\$

Currently I am working on a framework for a game utilising the SFML graphics library and in one of the classes representing a renderable object in the framework there is an sf::Sprite that is used to represent and draw the object. The issue is that the object instance of class 1 that is represented by the sf::Sprite (which is a private member variable of class 1), is in turn a private member variable of class 2 of which an instance is in turn a private member variable of class 3 from which the sprites belonging to the instances of class 1 and 2 must be retrieved to be rendered in the renderer class. In classes 1, 2 and 3 there are appropriate getter functions like so:

class One
{
     public:

     //Functions
     One();                                         
    ~One();

     sf::Sprite getOne(){ return oneRep;};
    animate();

     private:
     //One variables
     sf::Texture oneTexture;
     sf::Sprite oneRep;

};

class Two
{
     public:

     //Functions
     Two();                                         
    ~Two();                                                                                             
     One getCOneInst(){ return cOneInst;};

    private:

    //Class 1 private member variable
    One cOneInst;
};

class Three
{
    public:

    //Functions
    Three();                                        
    ~Three();

    initTestSprite();                                                                                           
    One getCTwoInst(){ return cTwoInst;};

private:

    //Class 2 private member variable
    Two cTwoInst;
};

class Renderer
{
    public:

    //Functions
    Renderer();                                     
    ~Renderer();                                                                                        

    private:
    //Class 3 private member variable
    Three cThreeInst;

    //Test Class One private member variables
    One testCOneInst;
    sf::Texture oneTexture;
    sf::vector2f position

};

Which are used to access the sprites representing the instances of class 1 and 2 from the instance of class 3 so that the renderer class may render them to the screen of the game. The issue is that: if an instance of class 1 (represented by the sf::Sprite) is constructed in the renderer class like so:

initTestSprite()
{
    testCOneInst.setTexture(oneTexture);                                                    
    testCOneInst.setTextureRect(textRect);                                  
    testCOneInst.setColor(sf::Color(255, 255, 255, 255));                                                               
    testCOneInst.setPosition(position);
}

And should the getter function be called on the instance of classOne to retrieve the sprite representing the instance of classOne for rendering in the render class

window.draw(testCOneInst.getOne()); 

The render class through the function above will render the sprite representing the instance of class One: testCOneInst correctly. The problem and the question is: When the sprite representing the instance of class One must be retrieved from the instance of class Two in which the class One instance is a private member variable – the functions that should be able to be called on the class One sprite to manipulate the texture being applied to it, for example setting the IntRect texture to a new location in order to animate the sprite representing the instance of class One does not function correctly – the original texture is still applied to the sprite representing the instance of class One, if the sprite is accessed by the renderer class from the instance of class 2 and in turn the instance of class 3 like so:

cThreeInst.getCTwoInst().getCOneInst().animate();

Before rendering the sprite with the updated texture changes to the window like so:

window.draw(cThreeInst.getCTwoInst().getCOneInst().getOne());

The changes to the IntRect of the sf::Sprite instance representing class 1 will not be show when the sprite is rendered – and the question is why?

\$\endgroup\$
1
  • \$\begingroup\$ Tried to up vote it - apparently need 15 reputation, other components of the framework need to be implemented first so haven't yet tried what you recommended - but when I do (and it may be a couple of months or so from now) i'll give the answer you provided a tick \$\endgroup\$
    – Enchanter
    Jun 24 '15 at 19:13
1
\$\begingroup\$

Your getters (getCTwoInst, getCOneInst and getOne) return copies so you're calling the animate function on a copy of a copy of your sprite. This modified copy is then left unused.

Return references instead:

Two& getCTwoInst() {return cTwoInst;}
\$\endgroup\$
2
  • \$\begingroup\$ Ended up using pointers as the sprites were ultimately stored in a vector but your answer definitely helped. \$\endgroup\$
    – Enchanter
    Aug 14 '15 at 18:17
  • \$\begingroup\$ Good to know you managed to fix it! \$\endgroup\$
    – Alayric
    Aug 15 '15 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .