resmanager.h
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:4k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // resmanager.h
  2. //
  3. // Copyright (C) 2001 Chris Laurel <claurel@shatters.net>
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. #ifndef _CELUTIL_RESMANAGER_H_
  10. #define _CELUTIL_RESMANAGER_H_
  11. #include <string>
  12. #include <vector>
  13. #include <map>
  14. #include <celutil/reshandle.h>
  15. enum ResourceState {
  16.     ResourceNotLoaded     = 0,
  17.     ResourceLoaded        = 1,
  18.     ResourceLoadingFailed = 2,
  19. };
  20. template<class T> class ResourceInfo
  21. {
  22.  public:
  23.     ResourceInfo() : state(ResourceNotLoaded), resource(NULL) {};
  24.     virtual ~ResourceInfo() {};
  25.     virtual std::string resolve(const std::string&) = 0;
  26.     virtual T* load(const std::string&) = 0;
  27.     typedef T ResourceType;
  28.     ResourceState state;
  29.     std::string resolvedName;
  30.     T* resource;
  31. };
  32. template<class T> class ResourceManager
  33. {
  34.  private:
  35.     std::string baseDir;
  36.  public:
  37.     ResourceManager();
  38.     ResourceManager(std::string _baseDir) : baseDir(_baseDir) {};
  39.     ~ResourceManager();
  40.     typedef typename T::ResourceType ResourceType;
  41.  private:
  42.     typedef std::vector<T> ResourceTable;
  43.     typedef std::map<T, ResourceHandle> ResourceHandleMap;
  44.     typedef std::map<std::string, ResourceType*> NameMap;
  45.     typedef typename ResourceHandleMap::value_type ResourceHandleMapValue;
  46.     typedef typename NameMap::value_type NameMapValue;
  47.     ResourceTable resources;
  48.     ResourceHandleMap handles;
  49.     NameMap loadedResources;
  50.  public:
  51.     ResourceHandle getHandle(const T& info)
  52.     {
  53.         typename ResourceHandleMap::iterator iter = handles.find(info);
  54.         if (iter != handles.end())
  55.         {
  56.             return iter->second;
  57.         }
  58.         else
  59.         {
  60.             ResourceHandle h = handles.size();
  61.             resources.insert(resources.end(), info);
  62.             handles.insert(ResourceHandleMapValue(info, h));
  63.             return h;
  64.         }
  65.     }
  66.     ResourceType* find(ResourceHandle h)
  67.     {
  68.         if (h >= (int) handles.size() || h < 0)
  69.         {
  70.             return NULL;
  71.         }
  72.         else
  73.         {
  74.             if (resources[h].state == ResourceNotLoaded)
  75.             {
  76.                 resources[h].resolvedName = resources[h].resolve(baseDir);
  77.                 typename NameMap::iterator iter =
  78.                     loadedResources.find(resources[h].resolvedName);
  79.                 if (iter != loadedResources.end())
  80.                 {
  81.                     resources[h].resource = iter->second;
  82.                     resources[h].state = ResourceLoaded;
  83.                 }
  84.                 else
  85.                 {
  86.                     resources[h].resource = resources[h].load(resources[h].resolvedName);
  87.                     if (resources[h].resource == NULL)
  88.                     {
  89.                         resources[h].state = ResourceLoadingFailed;
  90.                     }
  91.                     else
  92.                     {
  93.                         resources[h].state = ResourceLoaded;
  94.                         loadedResources.insert(NameMapValue(resources[h].resolvedName, resources[h].resource));
  95.                     }
  96.                 }
  97.             }
  98.             if (resources[h].state == ResourceLoaded)
  99.                 return resources[h].resource;
  100.             else
  101.                 return NULL;
  102.         }
  103.     }
  104.     const T* getResourceInfo(ResourceHandle h)
  105.     {
  106.         if (h >= (int) handles.size() || h < 0)
  107.             return NULL;
  108.         else
  109.             return &resources[h];
  110.     }
  111. };
  112. #endif // _CELUTIL_RESMANAGER_H_