UnitResource.h 6.3 KB

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