Skip to main content
GameDev.net gamedev.net
🔒 Locked

What is a singleton class?

Started by TheGecko Jun 24, 2001 at 9:07 PM 2 replies 400+ views
Original Post
TheGecko
TheGecko
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!
Shannon Barber
Shannon Barber
  
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.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
TheGecko
TheGecko
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!

Sandman
Sandman
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.