ResourceManager.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Types.h"
  23. #include "ResourceManager.h"
  24. #include "ResourceLoader.h"
  25. #include "String.h"
  26. #include "Hash.h"
  27. #include "Path.h"
  28. #include <algorithm>
  29. namespace crown
  30. {
  31. //-----------------------------------------------------------------------------
  32. ResourceManager::ResourceManager(Filesystem* filesystem) :
  33. m_resource_loader(this, filesystem),
  34. m_resources(m_allocator)
  35. {
  36. }
  37. //-----------------------------------------------------------------------------
  38. ResourceManager::~ResourceManager()
  39. {
  40. }
  41. //-----------------------------------------------------------------------------
  42. ResourceId ResourceManager::load(const char* name)
  43. {
  44. char basename[512];
  45. char extension[512];
  46. path::filename_without_extension(name, basename, 512);
  47. path::extension(name, extension, 512);
  48. uint32_t name_hash = hash::fnv1a_32(basename, string::strlen(basename));
  49. uint32_t type_hash = hash::fnv1a_32(extension, string::strlen(extension));
  50. return load(name_hash, type_hash);
  51. }
  52. //-----------------------------------------------------------------------------
  53. ResourceId ResourceManager::load(uint32_t name, uint32_t type)
  54. {
  55. // Search for an already existent resource
  56. ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), name);
  57. // If resource not found, create a new one
  58. if (entry == m_resources.end())
  59. {
  60. ResourceId id;
  61. id.name = name;
  62. id.type = type;
  63. id.index = m_resources.size();
  64. ResourceEntry entry;
  65. entry.id = id;
  66. entry.state = RS_UNLOADED;
  67. entry.references = 1;
  68. entry.resource = NULL;
  69. m_resources.push_back(entry);
  70. m_resource_loader.load(id);
  71. return id;
  72. }
  73. // Else, increment its reference count
  74. entry->references++;
  75. return entry->id;
  76. }
  77. //-----------------------------------------------------------------------------
  78. void ResourceManager::unload(ResourceId name)
  79. {
  80. assert(has(name));
  81. ResourceEntry& entry = m_resources[name.index];
  82. entry.references--;
  83. if (entry.references == 0)
  84. {
  85. entry.state = RS_UNLOADED;
  86. entry.resource = NULL;
  87. m_resource_loader.unload(name);
  88. }
  89. }
  90. //-----------------------------------------------------------------------------
  91. void ResourceManager::reload(ResourceId name)
  92. {
  93. assert(has(name));
  94. ResourceEntry& entry = m_resources[name.index];
  95. if (entry.state == RS_LOADED)
  96. {
  97. // FIXME
  98. }
  99. }
  100. //-----------------------------------------------------------------------------
  101. bool ResourceManager::has(ResourceId name)
  102. {
  103. if (m_resources.size() > name.index)
  104. {
  105. return (m_resources[name.index].id.name == name.name);
  106. }
  107. return false;
  108. }
  109. //-----------------------------------------------------------------------------
  110. void* ResourceManager::data(ResourceId name)
  111. {
  112. assert(has(name));
  113. return m_resources[name.index].resource;
  114. }
  115. //-----------------------------------------------------------------------------
  116. bool ResourceManager::is_loaded(ResourceId name)
  117. {
  118. assert(has(name));
  119. return m_resources[name.index].state == RS_LOADED;
  120. }
  121. //-----------------------------------------------------------------------------
  122. void ResourceManager::loading(ResourceId name)
  123. {
  124. assert(has(name));
  125. m_resources[name.index].state = RS_LOADING;
  126. }
  127. //-----------------------------------------------------------------------------
  128. void ResourceManager::online(ResourceId name, void* resource)
  129. {
  130. assert(has(name));
  131. ResourceEntry& entry = m_resources[name.index];
  132. entry.resource = resource;
  133. entry.state = RS_LOADED;
  134. }
  135. } // namespace crown