2
0

ResourcePackage.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "ResourceManager.h"
  26. #include "PackageResource.h"
  27. #include "Resource.h"
  28. #include "Log.h"
  29. namespace crown
  30. {
  31. /// Collection of resources to load in a batch.
  32. class ResourcePackage
  33. {
  34. public:
  35. //-----------------------------------------------------------------------------
  36. ResourcePackage(ResourceManager& resman, const ResourceId id, const PackageResource* package)
  37. : m_resource_manager(&resman), m_package_id(id), m_package(package), m_has_loaded(false)
  38. {
  39. CE_ASSERT_NOT_NULL(package);
  40. }
  41. /// Loads all the resources in the package.
  42. /// @note
  43. /// The resources are not immediately available after the call is made,
  44. /// instead, you have to poll for completion with has_loaded()
  45. void load()
  46. {
  47. for (uint32_t i = 0; i < m_package->num_textures(); i++)
  48. {
  49. m_resource_manager->load(TEXTURE_TYPE, m_package->get_texture_id(i));
  50. }
  51. for (uint32_t i = 0; i < m_package->num_scripts(); i++)
  52. {
  53. m_resource_manager->load(LUA_TYPE, m_package->get_script_id(i));
  54. }
  55. for (uint32_t i = 0; i < m_package->num_sounds(); i++)
  56. {
  57. m_resource_manager->load(SOUND_TYPE, m_package->get_sound_id(i));
  58. }
  59. for (uint32_t i = 0; i < m_package->num_meshes(); i++)
  60. {
  61. m_resource_manager->load(MESH_TYPE, m_package->get_mesh_id(i));
  62. }
  63. }
  64. /// Unloads all the resources in the package.
  65. void unload()
  66. {
  67. for (uint32_t i = 0; i < m_package->num_meshes(); i++)
  68. {
  69. m_resource_manager->unload(m_package->get_mesh_id(i));
  70. }
  71. for (uint32_t i = 0; i < m_package->num_sounds(); i++)
  72. {
  73. m_resource_manager->unload(m_package->get_sound_id(i));
  74. }
  75. for (uint32_t i = 0; i < m_package->num_scripts(); i++)
  76. {
  77. m_resource_manager->unload(m_package->get_script_id(i));
  78. }
  79. for (uint32_t i = 0; i < m_package->num_textures(); i++)
  80. {
  81. m_resource_manager->unload(m_package->get_texture_id(i));
  82. }
  83. }
  84. /// Waits until the package has been loaded.
  85. void flush()
  86. {
  87. m_resource_manager->flush();
  88. m_has_loaded = true;
  89. }
  90. /// Returns whether the package has been loaded.
  91. bool has_loaded() const
  92. {
  93. return m_has_loaded;
  94. }
  95. /// Returns the resource id of the package.
  96. ResourceId resource_id() const
  97. {
  98. return m_package_id;
  99. }
  100. private:
  101. ResourceManager* m_resource_manager;
  102. const ResourceId m_package_id;
  103. const PackageResource* m_package;
  104. bool m_has_loaded;
  105. };
  106. } // namespace crown