Resource.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #pragma once
  23. namespace Crown
  24. {
  25. /**
  26. Enumerates the loading state of a resource.
  27. */
  28. enum LoadState
  29. {
  30. LS_UNLOADED = 0, //!< The resouce is not loaded
  31. LS_LOADING = 1, //!< The resource is loading
  32. LS_LOADED = 2, //!< The resource is loaded
  33. LS_UNLOADING = 3 //!< The resource is unloading
  34. };
  35. class ResourceManager;
  36. /**
  37. Abstracts a generic resource.
  38. Each resource is uniquely identified by its name.
  39. The name of a resource is simply the path to the
  40. corresponding file on the disk relative to the root path.
  41. More info about the root path in Filesystem.
  42. "textures/grass.tga" is a texture resource located at
  43. "ROOT_PATH/textures/grass.tga".
  44. Paths to resources follow the same conventions used
  45. for referring to files on disk covered in Filesystem.
  46. In order to save space and keep high performances, resource
  47. names are stored as 64-bit long sting hashes.
  48. */
  49. class Resource
  50. {
  51. public:
  52. /**
  53. Constructor.
  54. */
  55. Resource() : mIsReloadable(false), mLoadState(LS_UNLOADED), mSize(0) {}
  56. /**
  57. Destructor.
  58. */
  59. virtual ~Resource() { }
  60. /**
  61. Loads the resource.
  62. */
  63. virtual void Load(const char* name) { (void)name; }
  64. /**
  65. Unloads the resource and then optionally reloads it.
  66. @param reload
  67. Whether to reload after unloading
  68. */
  69. virtual void Unload(const char* name, bool reload) { (void)name; (void)reload; }
  70. /**
  71. Returns whether the resource is reloadable.
  72. @return Whether is reloadable
  73. */
  74. inline bool IsReloadable() { return mIsReloadable; }
  75. /**
  76. Returns the loading state of the resource.
  77. @return The loading state
  78. */
  79. inline LoadState GetLoadState() { return mLoadState; }
  80. /**
  81. Returns the resource's size in bytes.
  82. @return The size in bytes
  83. */
  84. inline size_t GetSize() { return mSize; }
  85. protected:
  86. bool mIsReloadable; //!< Whether is reloadable
  87. LoadState mLoadState; //!< The loading state
  88. size_t mSize; //!< The size in bytes
  89. };
  90. } // namespace Crown