random_racer/src/ResourceManager.cpp

Go to the documentation of this file.
00001 #ifdef __linux__
00002     #define __USE_BSD
00003     #define __USE_POSIX
00004 #endif
00005 
00006 #include <sys/stat.h>
00007 #include <sys/types.h>
00008 
00009 #include <vrs/image/image.h>
00010 #include <vrs/io/objectloader.h>
00011 #include <vrs/opengl/imagecubemaptexturegl.h>
00012 #include <vrs/opengl/imagetexture2dgl.h>
00013 #include <vrs/sg/scenething.h>
00014 #include <vrs/io/threedsreader.h>
00015 
00016 #include "ResourceManager.h"
00017 
00018 
00019 using namespace VRS;
00020 
00021 namespace random_racer
00022 {
00023 
00024 SO<ResourceManager> ResourceManager::s_instance = NULL;
00025 
00026 ResourceManager::ResourceManager()
00027 {
00028     // TODO: Windows build
00029     // initialize the home dir path
00030     char* home = getenv("HOME");
00031     VRS_Error(home != NULL, "There is no Home Variabel");
00032     
00033     m_homeDir = std::string(home);
00034 #ifdef __APPLE__
00035     m_homeDir += "/Library/Application Support/Random Racer";
00036 #elif defined(__linux__)
00037     m_homeDir += "/.random_racer";
00038 #endif
00039     
00040     struct stat statusBuffer;
00041     if(lstat(m_homeDir.c_str(), &statusBuffer) < 0)
00042     {
00043         // if there is no dir, create one
00044         VRS_Error( mkdir(m_homeDir.c_str(), 0755) == 0, "Cant create Dir");
00045     }
00046     else
00047     {
00048         VRS_Error((statusBuffer.st_mode & S_IFDIR) == S_IFDIR, "Is not a Dir");
00049     }
00050     
00051     // initialize the data path
00052     m_fileDir = "";
00053 }
00054 
00055 ResourceManager::~ResourceManager()
00056 {
00057 }
00058 
00059 void
00060 ResourceManager::setArgv(const std::string& p_argv)
00061 {
00062     if(p_argv.rfind("RandomRacer.app/Contents/MacOS") != std::string::npos)
00063         m_fileDir = p_argv.substr(0,p_argv.rfind("MacOS")) + "Resources/";
00064     else
00065     {
00066         struct stat statusBuffer;
00067         if(stat("data", &statusBuffer) < 0)
00068         {
00069             VRS_Error(stat("../data", &statusBuffer) == 0,"Cant find data Dir");
00070             m_fileDir = "../data/";
00071         }
00072         else
00073             m_fileDir = "data/";
00074     }
00075 }
00076 
00077 SO<ResourceManager>
00078 ResourceManager::get()
00079 {
00080     if(s_instance == NULL)
00081         s_instance = new ResourceManager();
00082 
00083     return s_instance;
00084 }
00085 
00086 SO<Image>
00087 ResourceManager::loadImage(const std::string& p_imageName)
00088 {
00089     SO<Image> image;
00090     ResourceContainer containerObject;
00091 
00092     if(m_resourceRegistry.contains(p_imageName))
00093     {
00094         VRS_Error(m_resourceRegistry.getValue(p_imageName).type == ImageType,
00095                     "Key(Image) already exists");
00096         
00097         containerObject = m_resourceRegistry.getValue(p_imageName);
00098         image = VRS_GuardedCast(Image, containerObject.sharedObject);
00099     }
00100     else
00101     {
00102         image = VRS_GuardedLoadObject(Image, m_fileDir + p_imageName);
00103 
00104         containerObject.type = ImageType;
00105         containerObject.sharedObject = image;
00106 
00107         m_resourceRegistry.insert(p_imageName, containerObject);
00108     }
00109     return image;
00110 }
00111 
00112 SO<ImageTexture2DGL>
00113 ResourceManager::loadTexture2D(const std::string& p_imageName, 
00114                                bool p_pow2, bool p_noException)
00115 {
00116     SO<ImageTexture2DGL> texture;
00117     ResourceContainer containerObject;
00118 
00119     if(m_resourceRegistry.contains(p_imageName))
00120     {
00121         VRS_Error(m_resourceRegistry.getValue(p_imageName).type == TextureType,
00122                     "Key(Texture) already exists");
00123 
00124         containerObject = m_resourceRegistry.getValue(p_imageName);
00125         texture = VRS_GuardedCast(ImageTexture2DGL,
00126                                     containerObject.sharedObject);
00127     }
00128     else
00129     {
00130         SO<Image> image;
00131         
00132         if(p_noException)
00133             image = VRS_LOAD_OBJECT(Image, m_fileDir + p_imageName); 
00134         else
00135             image = VRS_GuardedLoadObject(Image, m_fileDir + p_imageName);
00136 
00137         if(image != NULL)
00138         {
00139             if(p_pow2)
00140                 texture = new ImageTexture2DGL(image->copyToPow2());
00141             else
00142                 texture = new ImageTexture2DGL(image);        
00143 
00144             containerObject.type = TextureType;
00145             containerObject.sharedObject = texture;
00146 
00147             m_resourceRegistry.insert(p_imageName, containerObject);
00148         }
00149         else
00150             texture = NULL;
00151     }
00152     return texture;
00153 }
00154 
00155 SO<Font>
00156 ResourceManager::loadFont(const std::string& p_fontName, Font::FontType 
00157                                                                     p_fontType)
00158 {
00159     SO<Font> font;
00160     ResourceContainer containerObject;
00161 
00162     if(m_resourceRegistry.contains(p_fontName))
00163     {
00164         VRS_Error(m_resourceRegistry.getValue(p_fontName).type == FontType,
00165                     "Key(Font) already exists");
00166 
00167         containerObject = m_resourceRegistry.getValue(p_fontName);
00168         font = VRS_GuardedCast(Font, containerObject.sharedObject);
00169     }
00170     else
00171     {
00172         font = Font::createFont(m_fileDir + p_fontName, p_fontType);
00173         
00174         containerObject.type = FontType;
00175         containerObject.sharedObject = font;
00176 
00177         m_resourceRegistry.insert(p_fontName, containerObject);
00178     }
00179     return font;
00180 }
00181 
00182 SO<ImageCubeMapTextureGL> 
00183 ResourceManager::loadCubeTexture(const std::string& p_imageName, const 
00184                                                         std::string& p_format)
00185 {
00186     SO<ImageCubeMapTextureGL> cubeTexture;
00187     ResourceContainer containerObject;
00188     
00189     if(m_resourceRegistry.contains(p_imageName))
00190     {
00191         VRS_Error(m_resourceRegistry.getValue(p_imageName).type == CubeType,
00192                     "Key(Cube) already exists");
00193 
00194         containerObject = m_resourceRegistry.getValue(p_imageName);
00195         cubeTexture = VRS_GuardedCast(ImageCubeMapTextureGL,
00196                                                 containerObject.sharedObject);
00197     }
00198     else
00199     {
00200         SO<Image> front, back, left, right, bottom, top;
00201         
00202         front = VRS_GuardedLoadObject(Image, m_fileDir + p_imageName + 
00203                                                         "_front" + p_format);
00204         back = VRS_GuardedLoadObject(Image, m_fileDir + p_imageName + 
00205                                                         "_back" + p_format);
00206         left = VRS_GuardedLoadObject(Image, m_fileDir + p_imageName + 
00207                                                         "_left" + p_format);
00208         right = VRS_GuardedLoadObject(Image, m_fileDir + p_imageName + 
00209                                                         "_right" + p_format);
00210         bottom = VRS_GuardedLoadObject(Image, m_fileDir + p_imageName + 
00211                                                         "_bottom" + p_format);
00212         top = VRS_GuardedLoadObject(Image, m_fileDir + p_imageName + 
00213                                                         "_top" + p_format);
00214         
00215         cubeTexture = new ImageCubeMapTextureGL(right, left, top, bottom, front, 
00216                                                                         back);
00217 
00218         containerObject.type = CubeType;
00219         containerObject.sharedObject = cubeTexture;
00220 
00221         m_resourceRegistry.insert(p_imageName, containerObject);
00222     }
00223     return cubeTexture;
00224 }
00225 
00226 std::string
00227 ResourceManager::getPathOfFile(const std::string& p_fileName)
00228 {  
00229     return m_homeDir +"/"+ p_fileName;
00230 }
00231 
00232 SO<SceneThing> 
00233 ResourceManager::load3ds(const std::string& p_fileName)
00234 {
00235     ThreeDSReader::setTextureMode(ThreeDSReader::TRY_TEXTURES);
00236     ThreeDSReader::setReadMode(ThreeDSReader::READ_SCENE);
00237     ThreeDSReader::setOptimizations(ThreeDSReader::NO_OPTIMIZATIONS);
00238     ThreeDSReader::setMetaInfo(true);
00239     
00240     SO<SceneThing> modelThing = new SceneThing();
00241     
00242     modelThing = VRS_LOAD_OBJECT(SceneThing, m_fileDir + p_fileName);
00243      
00244     return modelThing;
00245 }
00246 
00247 bool
00248 ResourceManager::freeResource(const std::string& p_name)
00249 {
00250     if(m_resourceRegistry.contains(p_name))
00251         return m_resourceRegistry.erase(p_name);
00252     else
00253         return false;
00254 }
00255 
00256 void
00257 ResourceManager::freeAllResources()
00258 {
00259     m_resourceRegistry.clear();
00260 }
00261 
00262 } // namespace random_racer

Generated on Fri May 11 21:01:58 2007 for Random Racer by  doxygen 1.5.1