Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]One sprite for all my objects  (Read 2281 times)

0 Members and 1 Guest are viewing this topic.


  • Jr. Member
  • **
  • Posts: 55
    • View Profile
[SOLVED]One sprite for all my objects
« on: June 24, 2017, 05:17:49 am »
SFML forum,
 I'm using SFML but this might be a C++ OOP question.

 I want to use a sprite from a class in all my objects:

class Sprite
    Sprite() {};

    sf::Texture MySprite;
    sf::Sprite sprite;

    void Load_Sprite()
        { cout << " Texture Loading Error" << endl; system("pause"); }

 But I have go use one of the objects to load the sprite so now it
 belongs to that object.  I would like to make the sprite static so
 I only need one and use it in every object.

 Any suggestions on how to do that?
« Last Edit: June 26, 2017, 05:17:48 am by jerryd »


  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: One sprite for all my objects
« Reply #1 on: June 24, 2017, 06:34:07 am »
Don't do that, just use one sprite per object. There's no problem in having many sprites, that's what they are for.

You should do that with your textures, though, because textures use more resources. Try to use only one texture for all your sprites, and to achieve that don't have a texture inside your class, but rather create your textures outside and just pass them to your "Load_Sprite()" method.

Something like this:

class Object {

        sf::Sprite mySprite;

        void load_sprite( sf::Texture & externalTexture ) {

            // It receives the texture externally as a parameter.
            // Don't forget to use the "&" symbol, meaning that
            // you are passing the texture "by reference".
            // (If you don't know what this is,
            // I strongly recommend reading about it).

            mySprite.setTexture( externalTexture );



int main() {

    // You load your textures externally, NOT inside your class.

    sf::Texture myTexture;

    // You make instances of your class,
    // and you pass your textures as necessary.

    Object myObject;
    myObject.load_sprite( myTexture );

    Object anotherObject;
    anotherObject.load_sprite( myTexture );

    Object thirdObject;
    thirdObject.load_sprite( myTexture );


« Last Edit: June 24, 2017, 06:46:49 am by AFS »


  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: One sprite for all my objects
« Reply #2 on: June 24, 2017, 12:28:53 pm »
In the case of special requirements:
- you need any kind of texture manager
- manager can also hold sprite assigned to texture
- game objects can hold pointers to these sprites

The problem starts when you want to change the parameters of sprite, like scale. In such case you need individual sprites for each object, reset scale values or make sprite copies on-the-fly.