|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| A Singleton Texture Manager for OpenGL |
|
![]() mother Member since: 10/3/2000 From: Canada |
||||
|
|
||||
| Great article. One suggestion, though: To decrease the chance that a singleton class is used incorrectly, the constructor(s) could be made private. That way, an error is generated at compile time if an instance of the singleton is instantiated. For example: class Singleton { public: static Singleton &getInstance() { return _instance; } private: Singleton(); ~Singleton(); static Singleton &_instance; }; Singleton &Singleton::_instance; void main() { // Compile-time error, since the ctor is private. Singleton s; // Compile-time error, since the ctor is private. Singleton *s = new Singleton; } |
||||
|
||||
![]() Deifante Member since: 3/8/2000 From: Calgary, Canada |
||||
|
|
||||
| Great.. but how then is one to get the one instance that is needed for the program? |
||||
|
||||
![]() mother Member since: 10/3/2000 From: Canada |
||||
|
|
||||
| void main() { Singleton &s = Singleton::getInstance(); } |
||||
|
||||
![]() mother Member since: 10/3/2000 From: Canada |
||||
|
|
||||
| void main() { Singleton &s = Singleton::getInstance(); } |
||||
|
||||
![]() Sly Member since: 7/28/2000 From: Brisbane, Australia |
||||
|
|
||||
| What is the advantage to using a singleton class in this case? If you only want one instance of it, then why do you need a class at all? Public stuff goes in the header...
Private stuff goes in the source file. This is a lot simpler in my opinion. No classes, no references, guaranteed no multiple instances, just good old C. Steve 'Sly' Williams Code Monkey Krome Studios |
||||
|
||||
![]() mother Member since: 10/3/2000 From: Canada |
||||
|
|
||||
| You're absolutely right. Doing it in straight C will give the same effect. There are advantages to making a singleton a class, though. For instance, if requirements change (as they often do), and multiple instances are needed, you simply need to remove the getInstance() method, and make the constructor(s) public. In other words, it's trivial to convert a singleton class into a regular class. |
||||
|
||||
![]() Deifante Member since: 3/8/2000 From: Calgary, Canada |
||||
|
|
||||
| I don't get it. If you just refernce the static pointer you still don't have an actual instaniation of the class. Un less when you define the static memeber you explicitly make the class (if i'm makeing sence here); |
||||
|
||||
![]() Outworlder Member since: 3/3/2000 From: Fortaleza, Brazil |
||||
|
|
||||
Another advantage of the singletons is that all functions that deal with a particular subject are contained in a class. Ive done some projects that had a lot of functions. It was just a pain in the a**, and the clarity of the code suffered a lot. (Imagine that):
It always get messy. No matter how you organize it. It is still an organized mess. Gaiomard Dragon -===(UDIC)===- Edited by - Outworlder on July 19, 2001 9:31:36 PM |
||||
|
||||
![]() Marvin Member since: 7/20/2001 From: United Kingdom |
||||
|
|
||||
| oki, so ive loaded my texture into the texture manager, how do i bind it? examples please, thanks |
||||
|
||||
![]() CodeSmith Member since: 6/28/2001 From: San Antonio, USA |
||||
|
|
||||
It all depends on HOW you loaded your texture with the class, if YOU want to deal with the OpenGL texture ID's then you would do it as normal AKAOr if you want THE CLASS to handle the texture IDs I belive what your looking for is in the return value. hope that helps -The guy that wrote the tutorial CodeSmith the Pixel Pusher www.cs.trinity.edu/~csmith8 |
||||
|
||||
![]() Gyzmo Member since: 9/18/1999 From: Tiberia |
||||
|
|
||||
The way the singleton is written now can create problems if there is another singleton which requires the use of the texture singleton as static initialisation is currently not necessarily in-order, a better (imho) solution would be to initialize the object on the first call to getInstance() i.e.:
This way you can be sure the object is instantiated the first time getInstance is called. Gyzmo ================================================ two wrongs don't make a right, three lefts do |
||||
|
||||
![]() WarMage Member since: 6/8/2001 From: GNU is not Eunuchs!! |
||||
|
|
||||
| Of course a single texture manager is going to make sense These "middle managers" are just pretty (inter)faces to help classify your texture lists. By this example, CglModelTexManager::BindTexture("ModelXSkin.tga") would simply call the superclass CglTexManager::BindTexture("ModelXSkin.tga", nTex_MODEL), where nTex_MODEL is then returned to CGlModelTexManager, who then manages it's own list when it comes time to do CglModelTexManager::DeleteAll(). Sweet, huh? <rant> The only thing that bugs me is the word "Singleton". Why does an instance of a GLOBAL STATIC CLASS get its own nifty name? Capitalised, even? do you capitalize Internet? I hope not, since it isn't a proper noun. Frankly if somebody says to me "I built a Singleton component to do X.." and I got guys in my shop that have been doing (godlike) code for 20 years going "What the hell is the Singleton Class?", then that says to me this is just another fancy name they give so that the kids with degrees can show off in front of the experienced old fogies... </rant> ------------ -WarMage ...ummmm, yeah. that one. Edited by - WarMage on July 22, 2001 8:09:52 PM |
||||
|
||||
![]() CodeSmith Member since: 6/28/2001 From: San Antonio, USA |
||||
|
|
||||
quote: your 100% right, that IS a better way to do it. However if you just leave it as a static member you have absolutley no control over initialization and deconstruction. So yeah, it is MUCH MUCH MUCH cleaner but with the sacrifice of alittle control CodeSmith the Pixel Pusher www.cs.trinity.edu/~csmith8 |
||||
|
||||
![]() Vetinari Moderator Emeritus Member since: 1/22/2000 From: USA |
||||
|
|
||||
quote: I believe that in Gyzmo's code, it is initialized at the first call to getInstance. If your singleton class relies on some other subsytem to be initialized, you probably are not doing things in the best way. Mike |
||||
|
||||
![]() Nil_z Member since: 1/17/2001 From: Japan |
||||
|
|
||||
| I am not quite clear about singleton class, maybe someone here will kindly answer my question. Since I want the class to have only one instance, why can't I declare all those member parameters that need to be unique as static member? Can't that do the same job as "singleton class"? Is there any difference between the two? Thanks a lot. |
||||
|
||||
![]() Vetinari Moderator Emeritus Member since: 1/22/2000 From: USA |
||||
|
|
||||
quote: The best way to implement a singleton class will guarentee that there can not be more than one instance of a class no matter how incompetant the users of the API may be. It will also be initialized lazily so that it is only initialized if it is used. Gyzmo's code above is the best way I know to implement a singleton. Mike |
||||
|
||||
![]() lonestarr Member since: 11/19/2002 From: France |
||||
|
|
||||
| Hi, there is a bug in CTextureManager::LoadBitmapFile : pImage = new UBYTE [BitmapInfoHeader.biSizeImage]; should be replaced by : DWORD trueSize = BitmapInfoHeader.biSizeImage; if (trueSize==0) trueSize = BitmapInfoHeader.biWidth * BitmapInfoHeader.biHeight * 3; pImage = new UBYTE [trueSize]; Just because in MSDN: biSizeImage = Specifies the size, in bytes, of the image. This may be set to zero for BI_RGB bitmaps. -lonestarr |
||||
|
||||
![]() lonestarr Member since: 11/19/2002 From: France |
||||
|
|
||||
| I forgot : you should also replace any further BitmapInfoHeader.biSizeImage by trueSize -lonestarr |
||||
|
||||
![]() Cybrid Member since: 6/21/2005 From: Bilbao, Spain |
||||
|
|
||||
| Well, I supouse I'm posting a bit late ^_^ , but what if I have another classes to handle the way to obtain data from texture files?. I'm using FreeImage to do it. So at first I have created an abstract class and a set of sub-classes to handle each type of texture file (png,jpg,bmp). Would it be ok if I just instantiate png class in the textureManager class?, or is it impossible due to singleton structure?. Thanks in advance (if someone replies ;) ) |
||||
|
||||
![]() shurcool Member since: 5/6/2001 From: Toronto, Canada |
||||
|
|
||||
| There's a minor bug/inconsistency in the DESTROY_TEXMANAGER call. I don't think it's very important because GL textures should get cleaned up anyway when you destroy the GL context, but... In the header file it mentions the following. Quote: But DESTROY_TEXMANAGER doesn't actually call FreeAll like it says. To fix it, insert one line in the CTextureManager::Destroy() function. void CTextureManager::Destroy (void) {
if (m_Singleton) {
m_Singleton->FreeAll(); // <-- This line was missing
delete [] m_Singleton->nTexIDs;
m_Singleton->nTexIDs = 0;
delete m_Singleton;
m_Singleton = 0;
}
} |
||||
|
||||
![]() smr GDNet+ Member since: 7/16/2004 From: Pekin, IL, United States |
||||
|
|
||||
| I recommend that you forget the singleton and implement this as a normal class. You can then pass the manager object to the constructor of any classes who need it, that way their dependency on the texture manager is clearly and explicitly documented. |
||||
|
||||
![]() shurcool Member since: 5/6/2001 From: Toronto, Canada |
||||
|
|
||||
| You certainly have a point there. That's exactly how I would do it if I were to do this from scratch today. But don't forget this article is 7 years old. ;) Singletons were something exotic and poorly understood, and hence a FOTM design pattern at the time. |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|