TextureResource.h 3.2 KB

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