UnitResource.h 6.1 KB

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