MaterialResource.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "TextureResource.h"
  26. #include "Resource.h"
  27. #include "Vector3.h"
  28. #include "Color4.h"
  29. #include "Bundle.h"
  30. #include "Allocator.h"
  31. namespace crown
  32. {
  33. struct MaterialHeader
  34. {
  35. uint32_t num_texture_layers;
  36. uint32_t texture_layers_offset;
  37. };
  38. /// A material describes the visual properties of a surface.
  39. /// It is primarly intended for rendering purposes but can
  40. /// also be used to drive other types of systems such as sounds or physics.
  41. struct MaterialResource
  42. {
  43. public:
  44. //-----------------------------------------------------------------------------
  45. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  46. {
  47. File* file = bundle.open(id);
  48. const size_t file_size = file->size();
  49. void* res = allocator.allocate(file_size);
  50. file->read(res, file_size);
  51. bundle.close(file);
  52. return res;
  53. }
  54. //-----------------------------------------------------------------------------
  55. static void online(void* /*resource*/)
  56. {
  57. }
  58. //-----------------------------------------------------------------------------
  59. static void unload(Allocator& a, void* res)
  60. {
  61. CE_ASSERT_NOT_NULL(res);
  62. a.deallocate(res);
  63. }
  64. //-----------------------------------------------------------------------------
  65. static void offline(void* /*resource*/)
  66. {
  67. }
  68. //-----------------------------------------------------------------------------
  69. uint32_t num_texture_layers() const
  70. {
  71. return ((MaterialHeader*) this)->num_texture_layers;
  72. }
  73. //-----------------------------------------------------------------------------
  74. ResourceId get_texture_layer(uint32_t i) const
  75. {
  76. MaterialHeader* h = (MaterialHeader*) this;
  77. ResourceId* begin = (ResourceId*) (((char*) this) + h->texture_layers_offset);
  78. return begin[i];
  79. }
  80. private:
  81. // Disable construction
  82. MaterialResource();
  83. };
  84. } // namespace crown