Переглянути джерело

improve PhysicsResource with shape_index and shapes

mikymod 12 роки тому
батько
коміт
43d1a091af
2 змінених файлів з 57 додано та 5 видалено
  1. 21 5
      engine/resource/PhysicsResource.cpp
  2. 36 0
      engine/resource/PhysicsResource.h

+ 21 - 5
engine/resource/PhysicsResource.cpp

@@ -95,6 +95,7 @@ void parse_shape(JSONElement e, PhysicsShape& shape)
 
 	shape.name = hash::murmur2_32(shape_name.c_str(), shape_name.length());
 	shape.type = shape_type_to_enum(shape_type.c_str());
+
 	shape.x = x.float_value();
 	shape.y = y.float_value();
 	shape.z = z.float_value();
@@ -119,6 +120,7 @@ void parse_actor(JSONElement e, PhysicsActor& actor, List<PhysicsShape>& actor_s
 	actor.name = hash::murmur2_32(actor_name.c_str(), actor_name.length());
 	actor.node = hash::murmur2_32(actor_node.c_str(), actor_node.length());
 	actor.type = actor_type_to_enum(actor_type.c_str());
+
 	actor.num_shapes = shapes.size();
 
 	for (uint32_t i = 0; i < actor.num_shapes; i++)
@@ -156,7 +158,7 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 
 	// Read actors
 	List<PhysicsActor> m_actors(default_allocator());
-	List<uint32_t> m_shape_index(default_allocator());
+	List<uint32_t> m_shapes_indices(default_allocator());
 	List<PhysicsShape> m_shapes(default_allocator());
 	JSONElement actors = root.key_or_nil("actors");
 	if (!actors.is_nil())
@@ -165,13 +167,13 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 		{
 			if (m_shapes.size() == 0)
 			{
-				m_shape_index.push_back(0);
+				m_shapes_indices.push_back(0);
 			}
 			else
 			{
-				m_shape_index.push_back(m_shapes.size() - 1);
+				m_shapes_indices.push_back(m_shapes.size());
 			}
-			
+
 			PhysicsActor a;
 			parse_actor(actors[i], a, m_shapes);
 			m_actors.push_back(a);
@@ -185,10 +187,14 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	h.version = 1;
 	h.num_controllers = m_has_controller ? 1 : 0;
 	h.num_actors = m_actors.size();
+	h.num_shapes_indices = m_shapes_indices.size();
+	h.num_shapes = m_shapes.size();
 
 	uint32_t offt = sizeof(PhysicsHeader);
 	h.controller_offset = offt; offt += sizeof(PhysicsController) * h.num_controllers;
-	h.actors_offset = offt;
+	h.actors_offset = offt; offt += sizeof(PhysicsActor) * h.num_actors;
+	h.shapes_indices_offset = offt; offt += sizeof(uint32_t) * h.num_shapes_indices;
+	h.shapes_offset = offt;
 
 	out_file->write((char*) &h, sizeof(PhysicsHeader));
 
@@ -201,6 +207,16 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	{
 		out_file->write((char*) m_actors.begin(), sizeof(PhysicsActor) * m_actors.size());
 	}
+
+	if (m_shapes_indices.size())
+	{
+		out_file->write((char*) m_shapes_indices.begin(), sizeof(uint32_t) * m_shapes_indices.size());
+	}
+
+	if (m_shapes.size())
+	{
+		out_file->write((char*) m_shapes.begin(), sizeof(PhysicsShape) * m_shapes.size());
+	}
 }
 
 } // namespace physics_resource

+ 36 - 0
engine/resource/PhysicsResource.h

@@ -42,6 +42,10 @@ struct PhysicsHeader
 	uint32_t controller_offset;
 	uint32_t num_actors;
 	uint32_t actors_offset;
+	uint32_t num_shapes_indices;
+	uint32_t shapes_indices_offset;
+	uint32_t num_shapes;
+	uint32_t shapes_offset;
 };
 
 struct PhysicsController
@@ -157,6 +161,38 @@ struct PhysicsResource
 		return actor[i];
 	}
 
+	//-----------------------------------------------------------------------------
+	uint32_t num_shapes_indices() const
+	{
+		return ((PhysicsHeader*) this)->num_shapes_indices;
+	}
+
+	//-----------------------------------------------------------------------------
+	uint32_t shape_index(uint32_t i) const
+	{
+		CE_ASSERT(i < num_shapes_indices(), "Index out of bounds");
+
+		const PhysicsHeader* ph = (PhysicsHeader*) this;
+		uint32_t* index = (uint32_t*) (((char*) this) + ph->shapes_indices_offset);
+		return index[i];
+	}
+
+	//-----------------------------------------------------------------------------
+	uint32_t num_shapes() const
+	{
+		return ((PhysicsHeader*) this)->num_shapes;
+	}
+
+	//-----------------------------------------------------------------------------
+	PhysicsShape shape(uint32_t i) const
+	{
+		CE_ASSERT(i < num_shapes(), "Index out of bounds");
+
+		const PhysicsHeader* ph = (PhysicsHeader*) this;
+		PhysicsShape* shape = (PhysicsShape*) (((char*) this) + ph->shapes_offset);
+		return shape[i];
+	}
+
 private:
 
 	// Disable construction