niedziela, 10 października 2010

Simple resource manager

Hi!

Today I want to show you my simple resource manager. I never earlier have been writing similar mechanisms.

What is it?

A resource manager let us use frequently the same resource. But what can be a resource ? A lot of things can be a resource. The best example is file (model file, texture file, etc.). We can have a lot of object which refer to the same texture/model but we really have only one resource of texture/model. Below drawing show it :


My resource class look like below:

class IResource{
private:
    uint4 countLock;
    float lastUseTime;
    STATE_RES state;
    string name;
    string group;
protected:
    //Wywoływane podczas ładowania
    virtual void OnLoad() {}
    //Wywoływane podczas odładowywania
    virtual void OnUnload() {}
public:
    IResource(){}
    IResource(const string &Name, const string &Group = "");
    virtual ~IResource();
    void Load();
    void Unload();
    void Lock();
    void Unlock();
    const string& GetName() { return name; }
    const string& GetGroup() { return group; }
    STATE_RES GetState();
};

Each resource have own name. By this name we get a pointer to resource. Optionally each resource may have a group name. For example all files of texture may have name group : "texture" etc.

My resource manager is writing as Singleton. He provides functions to get a resource with the given name. This function is template, so we can get ever resource any type. And that's it. Manager haves a few function more, but I won' t describe it, you can download all source code below.

ATTENTION: You should create resource objects  in some function, but no as global objects because probably the program will return a mistake.

Use of my manager

Below example shows use of my manager :

class Texture : public library::IResource{
//some data like pixels
public: Texture(const string& name, const string& group = "") : library:IResource(name, group) {}
}

void LoadAllResources(){
   Texture texture1("myTexture.bmp, "Texture");
 }

int main(){
   BindTexture( ResManager::GetResource("myTexture.bmp);
}
library:: this is my namespace where we can find all class of resource manager.
I strongly believe that this isn't hard.

Here you can download a source code.

Greetings.

1 komentarz:

  1. Właśnie próbuje coś podobnego zrobić. Teraz wiem jak mniej więcej coś takiego uzyskać. Zerknę jeszcze twój kod który i zapewne pomoże w ukończeniu menadżera zasobów którego robię i robię

    OdpowiedzUsuń