PhysicsResource.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. namespace crown
  30. {
  31. namespace physics_resource
  32. {
  33. static uint32_t shape_type_to_enum(const char* type)
  34. {
  35. const StringId32 th = hash::murmur2_32(type, string::strlen(type));
  36. if (th == hash::HASH32("sphere", 0x2eaa6850)) return PhysicsShapeType::SPHERE;
  37. else if (th == hash::HASH32("capsule", 0xefe0dead)) return PhysicsShapeType::CAPSULE;
  38. else if (th == hash::HASH32("box", 0x5af3a067)) return PhysicsShapeType::BOX;
  39. else if (th == hash::HASH32("plane", 0xfb96769a)) return PhysicsShapeType::PLANE;
  40. CE_FATAL("Bad shape type");
  41. }
  42. //-----------------------------------------------------------------------------
  43. void parse_controller(JSONElement e, PhysicsController& controller)
  44. {
  45. JSONElement name = e.key("name");
  46. JSONElement height = e.key("height");
  47. JSONElement radius = e.key("radius");
  48. JSONElement slope_limit = e.key("slope_limit");
  49. JSONElement step_offset = e.key("step_offset");
  50. JSONElement contact_offset = e.key("contact_offset");
  51. controller.name = hash::murmur2_32(name.string_value(), name.size());
  52. controller.height = height.float_value();
  53. controller.radius = radius.float_value();
  54. controller.slope_limit = slope_limit.float_value();
  55. controller.step_offset = step_offset.float_value();
  56. controller.contact_offset = contact_offset.float_value();
  57. }
  58. //-----------------------------------------------------------------------------
  59. void parse_shape(JSONElement e, PhysicsShape& shape)
  60. {
  61. JSONElement name = e.key("name");
  62. JSONElement type = e.key("type");
  63. shape.name = hash::murmur2_32(name.string_value(), name.size());
  64. shape.type = shape_type_to_enum(type.string_value());
  65. }
  66. //-----------------------------------------------------------------------------
  67. void parse_actor(JSONElement e, PhysicsActor& actor, List<PhysicsShape>& actor_shapes)
  68. {
  69. JSONElement name = e.key("name");
  70. JSONElement node = e.key("node");
  71. JSONElement shapes = e.key("shapes");
  72. actor.name = hash::murmur2_32(name.string_value(), name.size());
  73. actor.node = hash::murmur2_32(node.string_value(), node.size());
  74. actor.num_shapes = shapes.size();
  75. for (uint32_t i = 0; i < actor.num_shapes; i++)
  76. {
  77. PhysicsShape ps;
  78. parse_shape(shapes[i], ps);
  79. actor_shapes.push_back(ps);
  80. }
  81. }
  82. //-----------------------------------------------------------------------------
  83. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  84. {
  85. File* file = fs.open(resource_path, FOM_READ);
  86. char* buf = (char*)default_allocator().allocate(file->size());
  87. file->read(buf, file->size());
  88. JSONParser json(buf);
  89. JSONElement root = json.root();
  90. bool m_has_controller = false;
  91. PhysicsController m_controller;
  92. // Read controller
  93. JSONElement controller = root.key_or_nil("controller");
  94. if (controller.is_nil())
  95. {
  96. m_has_controller = false;
  97. }
  98. else
  99. {
  100. parse_controller(controller, m_controller);
  101. m_has_controller = true;
  102. }
  103. // Read actors
  104. List<PhysicsActor> m_actors(default_allocator());
  105. List<uint32_t> m_shape_index(default_allocator());
  106. List<PhysicsShape> m_shapes(default_allocator());
  107. JSONElement actors = root.key_or_nil("actors");
  108. if (!actors.is_nil())
  109. {
  110. for (uint32_t i = 0; i < actors.size(); i++)
  111. {
  112. if (m_shapes.size() == 0)
  113. {
  114. m_shape_index.push_back(0);
  115. }
  116. else
  117. {
  118. m_shape_index.push_back(m_shapes.size() - 1);
  119. }
  120. PhysicsActor a;
  121. parse_actor(actors[i], a, m_shapes);
  122. m_actors.push_back(a);
  123. }
  124. }
  125. fs.close(file);
  126. default_allocator().deallocate(buf);
  127. PhysicsHeader h;
  128. h.version = 1;
  129. h.num_controllers = m_has_controller ? 1 : 0;
  130. h.num_actors = m_actors.size();
  131. uint32_t offt = sizeof(PhysicsHeader);
  132. h.controller_offset = offt; offt += sizeof(PhysicsController) * h.num_controllers;
  133. h.actors_offset = offt;
  134. out_file->write((char*) &h, sizeof(PhysicsHeader));
  135. if (m_has_controller)
  136. {
  137. out_file->write((char*) &m_controller, sizeof(PhysicsController));
  138. }
  139. if (m_actors.size())
  140. {
  141. out_file->write((char*) m_actors.begin(), sizeof(PhysicsActor) * m_actors.size());
  142. }
  143. }
  144. } // namespace physics_resource
  145. } // namespace crown