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

Author Topic: Sharing sprites between instances as Static data?  (Read 1964 times)

0 Members and 1 Guest are viewing this topic.

Pixel_Outlaw

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Sharing sprites between instances as Static data?
« on: February 01, 2011, 07:18:57 am »
Here is a sample of my C++ code to attempt to share a single sprite between instances of a class. I want to avoid the redundancy of having each object own a copy of the same image.


Code: [Select]

class Rock
{
    public:
        Rock();
        int x;
        int y;

        Rock(int new_x, int new_y, sf::RenderWindow * the_window);
        ~Rock();

        void update();

        // hold a static sprite for all rocks
        static sf::Image my_image;
        static sf::Sprite my_sprite;

        // hold the window to draw to
        sf::RenderWindow* my_window;
};

// define static variables

????????????????????????



My problem is that sprites must be loaded when created and the static implementation  wants one created on the fly. The rocks need to have an image associated with them an also that image as the sprite's image. I'm a bit new to the concept of static data so forgive me of my code is just off the wall. I'm working from a tutorial that used simple ints. The image for the sprite exists in "resources/sprites/block.png".

From my understanding of static data defined outside the class to which it belongs.

type Class::member_data = value
...
sf::Image Rock::my_image = ??? <- What goes here to assign a new image?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sharing sprites between instances as Static data?
« Reply #1 on: February 01, 2011, 07:54:48 am »
You don't need to share a single sprite for all your instances. This is a really lightweight class. Moreover, I guess that all your rocks won't be displayed at the same position, right? This is exactly what sf::Sprite is for, displaying the same image but with different transformations and rendering settings.

For the image:
Code: [Select]
Rock.hpp
--------

class Rock
{
public:

    Rock();

private: // this should really not be public :)

    static sf::Image our_image;
    static bool our_image_loaded;

    sf::Sprite my_sprite;
    sf::RenderWindow* my_window;
};


Code: [Select]
Rock.cpp
--------

sf::Image Rock::our_image;
bool Rock::our_image_loaded = false;

Rock::Rock()
{
    if (!our_image_loaded)
    {
        our_image.LoadFromFile(...);
        our_image_loaded = true;
    }

    my_sprite.SetImage(our_image);
}


You could also use an image manager (see the wiki), if this is not the only image that you need to share in your code.
Laurent Gomila - SFML developer

Pixel_Outlaw

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Sharing sprites between instances as Static data?
« Reply #2 on: February 01, 2011, 08:13:10 am »
Thanks again Laurent.

Also thank you for fixing my code. I'm still learning C++ for this school project. I wanted to show my programming class that coding can be something much more than just playing inside a black console window. So I'm going to recommended your library to the class through my project. Most of my fellow students don't enjoy programming simply because they have nothing to do with it. This should provide them a simple example of what you can do with C++ and a good cross platform library.

 

anything