UnitResource.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include "PhysicsTypes.h"
  30. #include "Matrix4x4.h"
  31. namespace crown
  32. {
  33. // All offsets are absolute
  34. struct UnitHeader
  35. {
  36. ResourceId physics_resource;
  37. ResourceId material_resource;
  38. uint32_t num_renderables;
  39. uint32_t renderables_offset;
  40. uint32_t num_cameras;
  41. uint32_t cameras_offset;
  42. uint32_t num_scene_graph_nodes;
  43. uint32_t scene_graph_names_offset;
  44. uint32_t scene_graph_poses_offset;
  45. uint32_t scene_graph_parents_offset;
  46. };
  47. struct UnitRenderable
  48. {
  49. enum { MESH, SPRITE } type;
  50. ResourceId resource;
  51. StringId32 name;
  52. int32_t node;
  53. bool visible;
  54. };
  55. struct UnitCamera
  56. {
  57. uint32_t name;
  58. int32_t node;
  59. };
  60. struct UnitResource
  61. {
  62. //-----------------------------------------------------------------------------
  63. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  64. {
  65. File* file = bundle.open(id);
  66. const size_t file_size = file->size();
  67. void* res = allocator.allocate(file_size);
  68. file->read(res, file_size);
  69. bundle.close(file);
  70. return res;
  71. }
  72. //-----------------------------------------------------------------------------
  73. static void online(void* /*resource*/)
  74. {
  75. }
  76. //-----------------------------------------------------------------------------
  77. static void unload(Allocator& allocator, void* resource)
  78. {
  79. CE_ASSERT_NOT_NULL(resource);
  80. allocator.deallocate(resource);
  81. }
  82. //-----------------------------------------------------------------------------
  83. static void offline(void* /*resource*/)
  84. {
  85. }
  86. //-----------------------------------------------------------------------------
  87. ResourceId physics_resource() const
  88. {
  89. return ((UnitHeader*) this)->physics_resource;
  90. }
  91. //-----------------------------------------------------------------------------
  92. ResourceId material_resource() const
  93. {
  94. return ((UnitHeader*) this)->material_resource;
  95. }
  96. //-----------------------------------------------------------------------------
  97. uint32_t num_renderables() const
  98. {
  99. return ((UnitHeader*) this)->num_renderables;
  100. }
  101. //-----------------------------------------------------------------------------
  102. UnitRenderable get_renderable(uint32_t i) const
  103. {
  104. CE_ASSERT(i < num_renderables(), "Index out of bounds");
  105. UnitHeader* h = (UnitHeader*) this;
  106. UnitRenderable* begin = (UnitRenderable*) (((char*) this) + h->renderables_offset);
  107. return begin[i];
  108. }
  109. //-----------------------------------------------------------------------------
  110. uint32_t num_cameras() const
  111. {
  112. return ((UnitHeader*) this)->num_cameras;
  113. }
  114. //-----------------------------------------------------------------------------
  115. UnitCamera get_camera(uint32_t i) const
  116. {
  117. CE_ASSERT(i < num_cameras(), "Index out of bounds");
  118. UnitHeader* h = (UnitHeader*) this;
  119. UnitCamera* begin = (UnitCamera*) (((char*) this) + h->cameras_offset);
  120. return begin[i];
  121. }
  122. //-----------------------------------------------------------------------------
  123. uint32_t num_scene_graph_nodes() const
  124. {
  125. return ((UnitHeader*) this)->num_scene_graph_nodes;
  126. }
  127. //-----------------------------------------------------------------------------
  128. StringId32* scene_graph_names() const
  129. {
  130. UnitHeader* h = (UnitHeader*) this;
  131. return (StringId32*) (((char*) this) + h->scene_graph_names_offset);
  132. }
  133. //-----------------------------------------------------------------------------
  134. Matrix4x4* scene_graph_poses() const
  135. {
  136. UnitHeader* h = (UnitHeader*) this;
  137. return (Matrix4x4*) (((char*) this) + h->scene_graph_poses_offset);
  138. }
  139. //-----------------------------------------------------------------------------
  140. int32_t* scene_graph_parents() const
  141. {
  142. UnitHeader* h = (UnitHeader*) this;
  143. return (int32_t*) (((char*) this) + h->scene_graph_parents_offset);
  144. }
  145. //-----------------------------------------------------------------------------
  146. StringId32 scene_graph_name(uint32_t i) const
  147. {
  148. UnitHeader* h = (UnitHeader*) this;
  149. StringId32* begin = (StringId32*) (((char*) this) + h->scene_graph_names_offset);
  150. return begin[i];
  151. }
  152. //-----------------------------------------------------------------------------
  153. Matrix4x4 scene_graph_pose(uint32_t i) const
  154. {
  155. UnitHeader* h = (UnitHeader*) this;
  156. Matrix4x4* begin = (Matrix4x4*) (((char*) this) + h->scene_graph_poses_offset);
  157. return begin[i];
  158. }
  159. //-----------------------------------------------------------------------------
  160. int32_t scene_graph_parent(uint32_t i) const
  161. {
  162. UnitHeader* h = (UnitHeader*) this;
  163. int32_t* begin = (int32_t*) (((char*) this) + h->scene_graph_parents_offset);
  164. return begin[i];
  165. }
  166. private:
  167. // Disable construction
  168. UnitResource();
  169. };
  170. } // namespace crown