ResourceLoader.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "Thread.h"
  26. #include "Resource.h"
  27. #include "HeapAllocator.h"
  28. #include "Queue.h"
  29. #include "List.h"
  30. #include "Mutex.h"
  31. #include "Cond.h"
  32. namespace crown
  33. {
  34. struct LoadedResource
  35. {
  36. LoadedResource(ResourceId r, void* d) : resource(r), data(d) {}
  37. ResourceId resource;
  38. void* data;
  39. };
  40. class Bundle;
  41. /// Loads resources in a background thread.
  42. class ResourceLoader : public Thread
  43. {
  44. public:
  45. /// Reads the resource data from the given @a bundle.
  46. ResourceLoader(Bundle& bundle);
  47. /// Loads the @a resource in a background thread.
  48. void load(ResourceId resource);
  49. /// Unloads the given @a resource @a data
  50. void unload(ResourceId resource, void* data);
  51. /// Returns the number of resources still in the loading queue.
  52. uint32_t remaining() const;
  53. /// Returns the number of resources already loaded.
  54. uint32_t num_loaded() const;
  55. /// Returns a list of the last loaded resources.
  56. void get_loaded(List<LoadedResource>& l);
  57. // Loads resources in the loading queue.
  58. int32_t run();
  59. private:
  60. void* load_by_type(ResourceId name);
  61. void unload_by_type(ResourceId name, void* data);
  62. private:
  63. // Used to strore resource memory
  64. HeapAllocator m_allocator;
  65. HeapAllocator m_resource_allocator;
  66. // Whether to look for resources
  67. Bundle& m_bundle;
  68. Queue<ResourceId> m_load_queue;
  69. List<LoadedResource> m_done_queue;
  70. Mutex m_load_mutex;
  71. Mutex m_done_mutex;
  72. Cond m_load_requests;
  73. };
  74. } // namespace crown