PolyResourceManager.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyEventDispatcher.h"
  22. #include <vector>
  23. #define RESOURCE_CHECK_INTERVAL 2000
  24. namespace Polycode {
  25. class Resource;
  26. class PolycodeShaderModule;
  27. class String;
  28. class _PolyExport ResourcePool : public EventDispatcher {
  29. public:
  30. ResourcePool(const String &name, ResourcePool *fallbackPool);
  31. ~ResourcePool();
  32. void setFallbackPool(ResourcePool *pool);
  33. void addResource(Resource *resource);
  34. void removeResource(Resource *resource);
  35. bool hasResource(Resource *resource);
  36. Resource *getResource(int resourceType, const String& resourceName) const;
  37. String getName();
  38. void setName(const String &name);
  39. Resource *getResourceByPath(const String& resourcePath) const;
  40. void Update(int elapsed);
  41. std::vector<Resource *> getResources(int resourceType);
  42. void checkForChangedFiles();
  43. bool reloadResourcesOnModify;
  44. bool dispatchChangeEvents;
  45. int resourceSubscribers;
  46. bool deleteOnUnsubscribe;
  47. static bool defaultReloadResourcesOnModify;
  48. private:
  49. ResourcePool *fallbackPool;
  50. String name;
  51. int ticksSinceCheck;
  52. std::vector <Resource*> resources;
  53. };
  54. /**
  55. * Manages loading and unloading of resources from directories and archives. Should only be accessed via the CoreServices singleton.
  56. */
  57. class _PolyExport ResourceManager : public EventDispatcher {
  58. public:
  59. ResourceManager();
  60. ~ResourceManager();
  61. /**
  62. * Loads resources from a directory.
  63. * @param dirPath Path to directory to load resources from.
  64. * @param recursive If true, will recurse into subdirectories.
  65. */
  66. void addDirResource(const String& dirPath, bool recursive=true);
  67. /**
  68. * Adds a zip or folder as a readable source. This doesn't actually load resources from it, just mounts it as a readable source, so you can call addDirResource on the folders inside of it like you would on regular folders. Most other disk IO in the engine (loading images, etc.) will actually check mounted archive files as well.
  69. */
  70. void addArchive(const String& path);
  71. /**
  72. * Removes a zip or folder as a readable source.
  73. */
  74. void removeArchive(const String& path);
  75. void parseTexturesIntoPool(ResourcePool *pool, const String& dirPath, bool recursive, const String& basePath);
  76. void parseMaterialsIntoPool(ResourcePool *pool, const String& dirPath, bool recursive);
  77. void parseShadersIntoPool(ResourcePool *pool, const String& dirPath, bool recursive);
  78. void parseProgramsIntoPool(ResourcePool *pool, const String& dirPath, bool recursive);
  79. void parseCubemapsIntoPool(ResourcePool *pool, const String& dirPath, bool recursive);
  80. void parseOtherIntoPool(ResourcePool *pool, const String& dirPath, bool recursive);
  81. ResourcePool *getGlobalPool();
  82. ResourcePool *getResourcePoolByName(const String &name);
  83. void addResourcePool(ResourcePool *pool);
  84. void removeResourcePool(ResourcePool *pool);
  85. std::vector<Resource*> getResources(int resourceType);
  86. void removeResource(Resource *resource);
  87. void subscribeToResourcePool(ResourcePool *pool);
  88. void unsubscibeFromResourcePool(ResourcePool *pool);
  89. void Update(int elapsed);
  90. void handleEvent(Event *event);
  91. private:
  92. ResourcePool *globalPool;
  93. std::vector <ResourcePool*> pools;
  94. };
  95. }