Archived

This topic is now archived and is closed to further replies.

What is a singleton class?

This topic is 6021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I''m at the point in my engine where I need to code a texture manager and mesh manager.The way it would work is,the managers load up all the textures and meshes once and keeps them in memory and the actor classes would just have a reference to the mesh.When it''s time to render each actor,the engine will take the ref number from the actor class and draw the appropriate mesh with texture.Now,what I want to know is,how do I make these manager classes global to the ENTIRE engine? I was told that I should use a singleton class but I can''t seem to find more information about this.Any ideas? Or maybe I''m going about the whole problem the wrong way.If you guys have any suggestions,I''d love to hear them.Thanx!

Share this post


Link to post
Share on other sites
  
class CTextureManager
{
public:
CTextureManager* GetTextureManager()
{
if(s_Singleton)
{
cRef=1;
return s_Singleton;
}
else
{
cRef++;
s_Singleton = new CTextureManager;
return s_Singleton;
}
}
int ReleaseTextureManager()
{
cRef--;
if(cRef==0)
delete s_Singleton;
}
protected:
CTextureManager();
~CTextureManager();
static CTextureManager* s_Singleton;
static cRef;
};


This ensures that only one texture manager exist, and anyone can ask for a reference to it. The object is reference counted so you know when it is (and isn''t) safe to delete the singleton.

The texture manager should be accessed by GetTextureManager/ReleaseTextureManager pairs. You can change the way this works to suit your needs.

Share this post


Link to post
Share on other sites
Hey thanx! Why didn''t I think of that!
Now before I go on implementing this,is my approach in solving this texture/mesh manager class a good or bad idea? Or is there anything else that I should be aware of?

Thanx again for the help!

Share this post


Link to post
Share on other sites
I think the mesh manager / texture manager class idea is a fantastic idea (but then I would because thats how my own engine design works ) but why do they need to be globally available? The only things that need to see them are the Renderer and the classes that make up your game tokens. In my own design, the game tokens recieve pointers to a base TextureManager and ModelManager classes when the Scene graph is attached to the Renderer class. This means that you could add multiple API support (OpenGL & Direct3D) without screwing up the engine, and even switch between the two on the fly.

Share this post


Link to post
Share on other sites