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

Author Topic: Making sf::Texture object a static member of a class.  (Read 3845 times)

0 Members and 1 Guest are viewing this topic.

Shivank17

  • Newbie
  • *
  • Posts: 2
    • View Profile
Making sf::Texture object a static member of a class.
« on: March 07, 2016, 03:20:00 pm »
Hi everyone!!

I am making a simple 2D game in which i require to create many graphical entities.
These entities have sf::Sprite and sf::Texture as their data members. I want all the objects of this entity class to share same texture so as to increase game speed :-\ . I was thinking of making texture a static variable but i don't know what to initialize the texture with.
Please help!! :)

(Pls pardon my English it's not my first language :P)

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Making sf::Texture object a static member of a class.
« Reply #1 on: March 07, 2016, 03:55:25 pm »
I'd recommend instead to use a resource manager (have a look at e.g. Thor, or the many things on the wiki) and let the objects share the same resource "key". This way you have a good generalisation of the problem that can be applied to any class and offers even more flexibility by making a clear separation of tasks (e.g. your entities are not responsible for resource management).
SFML / OS X developer

ka0s420

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
    • Email
Re: Making sf::Texture object a static member of a class.
« Reply #2 on: March 08, 2016, 08:22:55 am »
here's some code for a simple resourceHolder that im using atm, largely based off one that's in the book sfml essentials, main difference between them is that in the book they make the class a singleton. In this case i dont bother, because im lazy and i know that only my game class should hold the resourceHolder, other classes that need a texture get handed a pointer to it. Anyways, here's some code:

header:
class resourceHolder
{
public:
        resourceHolder();
        sf::Texture& getTexture(std::string path);
        sf::Font& getFont(std::string path);
        sf::SoundBuffer& getSound(std::string path);
private:
        std::map<std::string, sf::Texture> textures;
        std::map<std::string, sf::Font> fonts;
        std::map<std::string, sf::SoundBuffer> sounds;
};
 
cpp file:
#include "resourceHolder.h"



resourceHolder::resourceHolder()
        : textures()
{
}

sf::Texture & resourceHolder::getTexture(std::string path)
{
        auto& pairFound = textures.find(path);
        if (pairFound != textures.end()) {

                return pairFound->second;
        }
        else
        {
                auto& texture = textures[path];
                texture.loadFromFile(path);
                return texture;
        }
}

sf::Font & resourceHolder::getFont(std::string path)
{
        auto& pairFound = fonts.find(path);
        if (pairFound != fonts.end()) {

                return pairFound->second;
        }
        else
        {
                auto& font = fonts[path];
                font.loadFromFile(path);
                return font;
        }
}

sf::SoundBuffer & resourceHolder::getSound(std::string path)
{
        auto pairFound = sounds.find(path);
        if (pairFound != sounds.end())
        {
                return pairFound->second;
        }
        else
        {
               
                auto& sBuffer = sounds[path];
                sBuffer.loadFromFile(path);
                return sBuffer;
        }
}
 

basically, you just use this class to store your textures sounds whatever, and they stay there without being destroyed until the program exits. For each type of resource there is a map object that stores the resources filepath as its id and a resource of the desired type. It checks the map to see if that resource already exists - if it does, it returns it, if it doesn't, it adds it then returns it. To simplify the file paths, I wrote them as const strings in a header file behind a namespace and include that in the project too. So for example, instead of writing "graphics/player.png" i can instead type something like Sprites::player.

Hope this helps and isn't too awful a way of doing it.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Making sf::Texture object a static member of a class.
« Reply #3 on: March 08, 2016, 10:58:42 am »
Here's a link to the Thor Resources tutorial, in case you're interested. It's pretty similar to the ResourceHolder in our book SFML Game Development, but it comes with quite a few improvements and additional features.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Shivank17

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Making sf::Texture object a static member of a class.
« Reply #4 on: March 08, 2016, 11:07:51 am »
Making a resource manager is a great idea! :D
The resource tutorial was very helpful .

Thanks everyone for your support!! :)