unit_resource.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 "physics_types.h"
  30. #include "matrix4x4.h"
  31. #include "camera.h"
  32. namespace crown
  33. {
  34. // All offsets are absolute
  35. struct UnitHeader
  36. {
  37. ResourceId physics_resource;
  38. StringId64 sprite_animation;
  39. uint32_t num_renderables;
  40. uint32_t renderables_offset;
  41. uint32_t num_materials;
  42. uint32_t materials_offset;
  43. uint32_t num_cameras;
  44. uint32_t cameras_offset;
  45. uint32_t num_scene_graph_nodes;
  46. uint32_t scene_graph_nodes_offset;
  47. uint32_t num_keys;
  48. uint32_t keys_offset;
  49. uint32_t values_size;
  50. uint32_t values_offset;
  51. };
  52. struct UnitRenderable
  53. {
  54. enum { MESH, SPRITE } type;
  55. ResourceId resource;
  56. StringId32 name;
  57. int32_t node;
  58. bool visible;
  59. };
  60. struct UnitMaterial
  61. {
  62. StringId64 id;
  63. };
  64. struct UnitCamera
  65. {
  66. uint32_t name;
  67. int32_t node;
  68. ProjectionType::Enum type;
  69. float fov;
  70. float near;
  71. float far;
  72. };
  73. struct UnitNode
  74. {
  75. StringId32 name;
  76. Matrix4x4 pose;
  77. int32_t parent;
  78. };
  79. struct ValueType
  80. {
  81. enum Enum
  82. {
  83. BOOL,
  84. FLOAT,
  85. STRING,
  86. VECTOR3
  87. };
  88. };
  89. struct Key
  90. {
  91. StringId32 name;
  92. uint32_t type;
  93. uint32_t offset;
  94. };
  95. struct UnitResource
  96. {
  97. //-----------------------------------------------------------------------------
  98. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  99. {
  100. File* file = bundle.open(id);
  101. const size_t file_size = file->size();
  102. void* res = allocator.allocate(file_size);
  103. file->read(res, file_size);
  104. bundle.close(file);
  105. return res;
  106. }
  107. //-----------------------------------------------------------------------------
  108. static void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  109. {
  110. }
  111. static void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  112. {
  113. }
  114. //-----------------------------------------------------------------------------
  115. static void unload(Allocator& allocator, void* resource)
  116. {
  117. allocator.deallocate(resource);
  118. }
  119. ResourceId sprite_animation() const
  120. {
  121. ResourceId id;
  122. id.type = SPRITE_ANIMATION_TYPE;
  123. id.name = ((UnitHeader*) this)->sprite_animation;
  124. return id;
  125. }
  126. //-----------------------------------------------------------------------------
  127. ResourceId physics_resource() const
  128. {
  129. return ((UnitHeader*) this)->physics_resource;
  130. }
  131. //-----------------------------------------------------------------------------
  132. uint32_t num_renderables() const
  133. {
  134. return ((UnitHeader*) this)->num_renderables;
  135. }
  136. //-----------------------------------------------------------------------------
  137. UnitRenderable get_renderable(uint32_t i) const
  138. {
  139. CE_ASSERT(i < num_renderables(), "Index out of bounds");
  140. UnitHeader* h = (UnitHeader*) this;
  141. UnitRenderable* begin = (UnitRenderable*) (((char*) this) + h->renderables_offset);
  142. return begin[i];
  143. }
  144. //-----------------------------------------------------------------------------
  145. uint32_t num_materials() const
  146. {
  147. return ((UnitHeader*) this)->num_materials;
  148. }
  149. //-----------------------------------------------------------------------------
  150. UnitMaterial get_material(uint32_t i) const
  151. {
  152. CE_ASSERT(i < num_materials(), "Index out of bounds");
  153. UnitHeader* h = (UnitHeader*) this;
  154. UnitMaterial* begin = (UnitMaterial*) (((char*) this) + h->materials_offset);
  155. return begin[i];
  156. }
  157. //-----------------------------------------------------------------------------
  158. uint32_t num_cameras() const
  159. {
  160. return ((UnitHeader*) this)->num_cameras;
  161. }
  162. //-----------------------------------------------------------------------------
  163. UnitCamera get_camera(uint32_t i) const
  164. {
  165. CE_ASSERT(i < num_cameras(), "Index out of bounds");
  166. UnitHeader* h = (UnitHeader*) this;
  167. UnitCamera* begin = (UnitCamera*) (((char*) this) + h->cameras_offset);
  168. return begin[i];
  169. }
  170. //-----------------------------------------------------------------------------
  171. uint32_t num_scene_graph_nodes() const
  172. {
  173. return ((UnitHeader*) this)->num_scene_graph_nodes;
  174. }
  175. //-----------------------------------------------------------------------------
  176. UnitNode* scene_graph_nodes() const
  177. {
  178. UnitHeader* h = (UnitHeader*) this;
  179. return (UnitNode*) (((char*) this) + h->scene_graph_nodes_offset);
  180. }
  181. //-----------------------------------------------------------------------------
  182. uint32_t num_keys() const
  183. {
  184. return ((UnitHeader*) this)->num_keys;
  185. }
  186. //-----------------------------------------------------------------------------
  187. bool has_key(const char* k) const
  188. {
  189. UnitHeader* h = (UnitHeader*) this;
  190. const uint32_t nk = num_keys();
  191. Key* begin = (Key*) (((char*) this) + h->keys_offset);
  192. for (uint32_t i = 0; i < nk; i++)
  193. {
  194. if (begin[i].name == string::murmur2_32(k, string::strlen(k)))
  195. {
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. //-----------------------------------------------------------------------------
  202. bool get_key(const char* k, Key& out_k) const
  203. {
  204. UnitHeader* h = (UnitHeader*) this;
  205. const uint32_t nk = num_keys();
  206. Key* begin = (Key*) (((char*) this) + h->keys_offset);
  207. for (uint32_t i = 0; i < nk; i++)
  208. {
  209. if (begin[i].name == string::murmur2_32(k, string::strlen(k)))
  210. {
  211. out_k = begin[i];
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217. //-----------------------------------------------------------------------------
  218. uint32_t values_size() const
  219. {
  220. return ((UnitHeader*) this)->values_size;
  221. }
  222. //-----------------------------------------------------------------------------
  223. const char* values() const
  224. {
  225. UnitHeader* h = (UnitHeader*) this;
  226. return ((char*) this) + h->values_offset;
  227. }
  228. private:
  229. // Disable construction
  230. UnitResource();
  231. };
  232. } // namespace crown