00001 /* -*- c++ -*- */ 00002 ///////////////////////////////////////////////////////////////////////////// 00003 // 00004 // TextureManager.h -- Copyright (c) 2006 David Henry 00005 // last modification: feb. 21, 2006 00006 // 00007 // This code is licenced under the MIT license. 00008 // 00009 // This software is provided "as is" without express or implied 00010 // warranties. You may freely copy and compile this source into 00011 // applications you distribute provided that the copyright text 00012 // below is included in the resulting source code. 00013 // 00014 // Definitions of a texture manager class. 00015 // 00016 ///////////////////////////////////////////////////////////////////////////// 00017 00018 #ifndef __TEXTUREMANAGER_H__ 00019 #define __TEXTUREMANAGER_H__ 00020 00021 #include "DataManager.h" 00022 #include "Texture.h" 00023 00024 00025 ///////////////////////////////////////////////////////////////////////////// 00026 // 00027 // class Texture2DManager -- a texture manager which can register/unregister 00028 // Texture2D objects. Destroy all registred textures at death. 00029 // 00030 // The texture manager is a singleton. 00031 // 00032 ///////////////////////////////////////////////////////////////////////////// 00033 00034 class Texture2DManager : 00035 public DataManager<Texture2D, Texture2DManager> 00036 { 00037 friend class DataManager<Texture2D, Texture2DManager>; 00038 00039 public: 00040 // Public interface 00041 00042 // Load and register a texture. If the texture has already been 00043 // loaded previously, return it instead of loading it. 00044 Texture2D *load (const string &filename) 00045 { 00046 // Look for the texture in the registry 00047 Texture2D *tex = request (filename); 00048 00049 // If not found, load the texture 00050 if (tex == NULL) 00051 { 00052 tex = new Texture2D (filename); 00053 00054 // If the texture creation failed, delete the 00055 // unusable object and return NULL 00056 if (tex->fail ()) 00057 { 00058 delete tex; 00059 tex = NULL; 00060 } 00061 else 00062 { 00063 // The texture has been successfully loaded, 00064 // register it. 00065 registerObject (tex->name (), tex); 00066 } 00067 } 00068 00069 return tex; 00070 } 00071 }; 00072 00073 #endif // __TEXTUREMANAGER_H__