UnitResource.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "Bundle.h"
  27. #include "Allocator.h"
  28. #include "File.h"
  29. namespace crown
  30. {
  31. // All offsets are absolute
  32. struct UnitHeader
  33. {
  34. uint32_t num_renderables;
  35. uint32_t renderables_offset;
  36. uint32_t num_cameras;
  37. uint32_t cameras_offset;
  38. };
  39. struct UnitRenderable
  40. {
  41. enum { MESH, SPRITE } type;
  42. ResourceId resource;
  43. uint32_t name;
  44. bool visible;
  45. };
  46. struct UnitCamera
  47. {
  48. uint32_t name;
  49. };
  50. class UnitResource
  51. {
  52. public:
  53. //-----------------------------------------------------------------------------
  54. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  55. {
  56. File* file = bundle.open(id);
  57. const size_t file_size = file->size() - 12;
  58. UnitResource* res = (UnitResource*) allocator.allocate(sizeof(UnitResource));
  59. res->m_data = (char*) allocator.allocate(file_size);
  60. file->read(res->m_data, file_size);
  61. bundle.close(file);
  62. return res;
  63. }
  64. //-----------------------------------------------------------------------------
  65. static void online(void* /*resource*/)
  66. {
  67. }
  68. //-----------------------------------------------------------------------------
  69. static void unload(Allocator& allocator, void* resource)
  70. {
  71. CE_ASSERT_NOT_NULL(resource);
  72. allocator.deallocate(((UnitResource*)resource)->m_data);
  73. allocator.deallocate(resource);
  74. }
  75. //-----------------------------------------------------------------------------
  76. static void offline(void* /*resource*/)
  77. {
  78. }
  79. public:
  80. //-----------------------------------------------------------------------------
  81. uint32_t num_renderables() const
  82. {
  83. CE_ASSERT_NOT_NULL(m_data);
  84. return ((UnitHeader*)m_data)->num_renderables;
  85. }
  86. //-----------------------------------------------------------------------------
  87. const UnitRenderable& get_renderable(uint32_t i) const
  88. {
  89. CE_ASSERT(i < num_renderables(), "Index out of bounds");
  90. UnitRenderable* begin = (UnitRenderable*) (m_data + ((UnitHeader*)m_data)->renderables_offset);
  91. return begin[i];
  92. }
  93. //-----------------------------------------------------------------------------
  94. uint32_t num_cameras() const
  95. {
  96. CE_ASSERT_NOT_NULL(m_data);
  97. return ((UnitHeader*)m_data)->num_cameras;
  98. }
  99. //-----------------------------------------------------------------------------
  100. const UnitCamera& get_camera(uint32_t i) const
  101. {
  102. CE_ASSERT(i < num_cameras(), "Index out of bounds");
  103. UnitCamera* begin = (UnitCamera*) (m_data + ((UnitHeader*)m_data)->cameras_offset);
  104. return begin[i];
  105. }
  106. private:
  107. char* m_data;
  108. };
  109. } // namespace crown