PhysicsResource.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include "Allocator.h"
  24. #include "Filesystem.h"
  25. #include "Hash.h"
  26. #include "JSONParser.h"
  27. #include "PhysicsResource.h"
  28. #include "StringUtils.h"
  29. #include "DynamicString.h"
  30. namespace crown
  31. {
  32. namespace physics_resource
  33. {
  34. static uint32_t actor_type_to_enum(const char* type)
  35. {
  36. if (string::strcmp("static", type) == 0) return PhysicsActorType::STATIC;
  37. else if (string::strcmp("dynamic_physical", type) == 0) return PhysicsActorType::DYNAMIC_PHYSICAL;
  38. else if (string::strcmp("dynamic_kinematic", type) == 0) return PhysicsActorType::DYNAMIC_KINEMATIC;
  39. CE_FATAL("Bad actor type");
  40. }
  41. static uint32_t shape_type_to_enum(const char* type)
  42. {
  43. const StringId32 th = hash::murmur2_32(type, string::strlen(type));
  44. if (string::strcmp("sphere", type) == 0) return PhysicsShapeType::SPHERE;
  45. else if (string::strcmp("capsule", type) == 0) return PhysicsShapeType::CAPSULE;
  46. else if (string::strcmp("box", type) == 0) return PhysicsShapeType::BOX;
  47. else if (string::strcmp("plane", type) == 0) return PhysicsShapeType::PLANE;
  48. CE_FATAL("Bad shape type");
  49. }
  50. //-----------------------------------------------------------------------------
  51. void parse_controller(JSONElement e, PhysicsController& controller)
  52. {
  53. JSONElement name = e.key("name");
  54. JSONElement height = e.key("height");
  55. JSONElement radius = e.key("radius");
  56. JSONElement slope_limit = e.key("slope_limit");
  57. JSONElement step_offset = e.key("step_offset");
  58. JSONElement contact_offset = e.key("contact_offset");
  59. DynamicString contr_name;
  60. name.string_value(contr_name);
  61. controller.name = hash::murmur2_32(contr_name.c_str(), contr_name.length());
  62. controller.height = height.float_value();
  63. controller.radius = radius.float_value();
  64. controller.slope_limit = slope_limit.float_value();
  65. controller.step_offset = step_offset.float_value();
  66. controller.contact_offset = contact_offset.float_value();
  67. }
  68. //-----------------------------------------------------------------------------
  69. void parse_shape(JSONElement e, PhysicsShape& shape)
  70. {
  71. JSONElement name = e.key("name");
  72. JSONElement type = e.key("type");
  73. JSONElement x = e.key("x");
  74. JSONElement y = e.key("y");
  75. JSONElement z = e.key("z");
  76. JSONElement w = e.key("w");
  77. DynamicString shape_name;
  78. DynamicString shape_type;
  79. name.string_value(shape_name);
  80. type.string_value(shape_type);
  81. shape.name = hash::murmur2_32(shape_name.c_str(), shape_name.length());
  82. shape.type = shape_type_to_enum(shape_type.c_str());
  83. shape.x = x.float_value();
  84. shape.y = y.float_value();
  85. shape.z = z.float_value();
  86. shape.w = w.float_value();
  87. }
  88. //-----------------------------------------------------------------------------
  89. void parse_actor(JSONElement e, PhysicsActor& actor, List<PhysicsShape>& actor_shapes)
  90. {
  91. JSONElement name = e.key("name");
  92. JSONElement node = e.key("node");
  93. JSONElement type = e.key("type");
  94. JSONElement shapes = e.key("shapes");
  95. DynamicString actor_name;
  96. DynamicString actor_node;
  97. DynamicString actor_type;
  98. name.string_value(actor_name);
  99. node.string_value(actor_node);
  100. type.string_value(actor_type);
  101. actor.name = hash::murmur2_32(actor_name.c_str(), actor_name.length());
  102. actor.node = hash::murmur2_32(actor_node.c_str(), actor_node.length());
  103. actor.type = actor_type_to_enum(actor_type.c_str());
  104. actor.num_shapes = shapes.size();
  105. for (uint32_t i = 0; i < actor.num_shapes; i++)
  106. {
  107. PhysicsShape ps;
  108. parse_shape(shapes[i], ps);
  109. actor_shapes.push_back(ps);
  110. }
  111. }
  112. //-----------------------------------------------------------------------------
  113. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  114. {
  115. File* file = fs.open(resource_path, FOM_READ);
  116. char* buf = (char*)default_allocator().allocate(file->size());
  117. file->read(buf, file->size());
  118. JSONParser json(buf);
  119. JSONElement root = json.root();
  120. bool m_has_controller = false;
  121. PhysicsController m_controller;
  122. // Read controller
  123. JSONElement controller = root.key_or_nil("controller");
  124. if (controller.is_nil())
  125. {
  126. m_has_controller = false;
  127. }
  128. else
  129. {
  130. parse_controller(controller, m_controller);
  131. m_has_controller = true;
  132. }
  133. // Read actors
  134. List<PhysicsActor> m_actors(default_allocator());
  135. List<uint32_t> m_shapes_indices(default_allocator());
  136. List<PhysicsShape> m_shapes(default_allocator());
  137. JSONElement actors = root.key_or_nil("actors");
  138. if (!actors.is_nil())
  139. {
  140. for (uint32_t i = 0; i < actors.size(); i++)
  141. {
  142. if (m_shapes.size() == 0)
  143. {
  144. m_shapes_indices.push_back(0);
  145. }
  146. else
  147. {
  148. m_shapes_indices.push_back(m_shapes.size());
  149. }
  150. PhysicsActor a;
  151. parse_actor(actors[i], a, m_shapes);
  152. m_actors.push_back(a);
  153. }
  154. }
  155. fs.close(file);
  156. default_allocator().deallocate(buf);
  157. PhysicsHeader h;
  158. h.version = 1;
  159. h.num_controllers = m_has_controller ? 1 : 0;
  160. h.num_actors = m_actors.size();
  161. h.num_shapes_indices = m_shapes_indices.size();
  162. h.num_shapes = m_shapes.size();
  163. uint32_t offt = sizeof(PhysicsHeader);
  164. h.controller_offset = offt; offt += sizeof(PhysicsController) * h.num_controllers;
  165. h.actors_offset = offt; offt += sizeof(PhysicsActor) * h.num_actors;
  166. h.shapes_indices_offset = offt; offt += sizeof(uint32_t) * h.num_shapes_indices;
  167. h.shapes_offset = offt;
  168. out_file->write((char*) &h, sizeof(PhysicsHeader));
  169. if (m_has_controller)
  170. {
  171. out_file->write((char*) &m_controller, sizeof(PhysicsController));
  172. }
  173. if (m_actors.size())
  174. {
  175. out_file->write((char*) m_actors.begin(), sizeof(PhysicsActor) * m_actors.size());
  176. }
  177. if (m_shapes_indices.size())
  178. {
  179. out_file->write((char*) m_shapes_indices.begin(), sizeof(uint32_t) * m_shapes_indices.size());
  180. }
  181. if (m_shapes.size())
  182. {
  183. out_file->write((char*) m_shapes.begin(), sizeof(PhysicsShape) * m_shapes.size());
  184. }
  185. }
  186. } // namespace physics_resource
  187. } // namespace crown