TextureResource.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "Resource.h"
  26. #include "PixelFormat.h"
  27. #include "Texture.h"
  28. #include "Bundle.h"
  29. #include "Allocator.h"
  30. #include "File.h"
  31. namespace crown
  32. {
  33. // Bump the version whenever a change in the header is made
  34. const uint32_t TEXTURE_VERSION = 1;
  35. struct TextureHeader
  36. {
  37. uint32_t version; // Texture file version
  38. uint32_t format; // Format of the pixels
  39. uint32_t width; // Width in pixels
  40. uint32_t height; // Height in pixels
  41. };
  42. class TextureResource
  43. {
  44. public:
  45. //-----------------------------------------------------------------------------
  46. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  47. {
  48. File* file = bundle.open(id);
  49. TextureResource* resource = (TextureResource*)allocator.allocate(sizeof(TextureResource));
  50. file->read(&resource->m_header, sizeof(TextureHeader));
  51. size_t size = resource->width() * resource->height() * Pixel::bytes_per_pixel(resource->format());
  52. resource->m_data = (uint8_t*)allocator.allocate(sizeof(uint8_t) * size);
  53. file->read(resource->m_data, size);
  54. bundle.close(file);
  55. return resource;
  56. }
  57. //-----------------------------------------------------------------------------
  58. static void online(void* resource)
  59. {
  60. CE_ASSERT(resource != NULL, "Resource not loaded");
  61. }
  62. //-----------------------------------------------------------------------------
  63. static void unload(Allocator& allocator, void* resource)
  64. {
  65. CE_ASSERT(resource != NULL, "Resource not loaded");
  66. allocator.deallocate(((TextureResource*)resource)->m_data);
  67. allocator.deallocate(resource);
  68. }
  69. //-----------------------------------------------------------------------------
  70. static void offline(void* /*resource*/)
  71. {
  72. }
  73. public:
  74. PixelFormat format() const
  75. {
  76. return (PixelFormat) m_header.format;
  77. }
  78. uint32_t width() const
  79. {
  80. return m_header.width;
  81. }
  82. uint32_t height() const
  83. {
  84. return m_header.height;
  85. }
  86. const uint8_t* data() const
  87. {
  88. return m_data;
  89. }
  90. private:
  91. TextureHeader m_header;
  92. uint8_t* m_data;
  93. };
  94. } // namespace crown