Просмотр исходного кода

Replace ComponentList with separated fixed-size array of components

Daniele Bartolini 12 лет назад
Родитель
Сommit
7ca57bb306
2 измененных файлов с 143 добавлено и 160 удалено
  1. 112 37
      engine/Unit.cpp
  2. 31 123
      engine/Unit.h

+ 112 - 37
engine/Unit.cpp

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "IdTable.h"
 #include "World.h"
 #include "Allocator.h"
+#include "Log.h"
 
 namespace crown
 {
@@ -35,115 +36,189 @@ namespace crown
 typedef Id CameraId;
 
 Unit::Unit()
-	: m_creator(NULL)
-	, m_resource(NULL)
-	, m_scene_graph(NULL)
-	, m_component(NULL)
+	: m_world(NULL)
+	, m_num_cameras(0)
+	, m_num_meshes(0)
 {
 }
 
 //-----------------------------------------------------------------------------
-void Unit::create(World& creator, SceneGraph& graph, ComponentList& components, UnitId id, const Vector3& pos, const Quaternion& rot)
+void Unit::create(World& world, UnitId id, const Vector3& pos, const Quaternion& rot)
 {
-	m_root_node = graph.create_node(-1, pos, rot);
-
-	m_scene_graph = &graph;
-	m_component = &components;
-	m_creator = &creator;
+	m_root_node = m_scene_graph.create_node(-1, pos, rot);
+	m_world = &world;
 	m_id = id;
 }
 
 //-----------------------------------------------------------------------------
 void Unit::destroy()
 {
+}
 
+//-----------------------------------------------------------------------------
+Vector3 Unit::local_position(int32_t node) const
+{
+	return m_scene_graph.local_position(node);
 }
 
 //-----------------------------------------------------------------------------
-void Unit::load(UnitResource* ur)
+Quaternion Unit::local_rotation(int32_t node) const
 {
-	m_resource = ur;
+	return m_scene_graph.local_rotation(node);
 }
 
 //-----------------------------------------------------------------------------
-void Unit::unload()
+Matrix4x4 Unit::local_pose(int32_t node) const
 {
+	return m_scene_graph.local_pose(node);
 }
 
 //-----------------------------------------------------------------------------
-void Unit::reload(UnitResource* new_ur)
+Vector3 Unit::world_position(int32_t node) const
 {
-	(void)new_ur;
+	return m_scene_graph.world_position(node);
 }
 
 //-----------------------------------------------------------------------------
-Vector3 Unit::local_position(int32_t node) const
+Quaternion Unit::world_rotation(int32_t node) const
 {
-	return m_scene_graph->local_position(node);
+	return m_scene_graph.world_rotation(node);
 }
 
 //-----------------------------------------------------------------------------
-Quaternion Unit::local_rotation(int32_t node) const
+Matrix4x4 Unit::world_pose(int32_t node) const
 {
-	return m_scene_graph->local_rotation(node);
+	return m_scene_graph.world_pose(node);
 }
 
 //-----------------------------------------------------------------------------
-Matrix4x4 Unit::local_pose(int32_t node) const
+void Unit::set_local_position(const Vector3& pos, int32_t node)
 {
-	return m_scene_graph->local_pose(node);
+	m_scene_graph.set_local_position(node, pos);
 }
 
 //-----------------------------------------------------------------------------
-Vector3 Unit::world_position(int32_t node) const
+void Unit::set_local_rotation(const Quaternion& rot, int32_t node)
 {
-	return m_scene_graph->world_position(node);
+	m_scene_graph.set_local_rotation(node, rot);
 }
 
 //-----------------------------------------------------------------------------
-Quaternion Unit::world_rotation(int32_t node) const
+void Unit::set_local_pose(const Matrix4x4& pose, int32_t node)
 {
-	return m_scene_graph->world_rotation(node);
+	m_scene_graph.set_local_pose(node, pose);
 }
 
 //-----------------------------------------------------------------------------
-Matrix4x4 Unit::world_pose(int32_t node) const
+void Unit::link_node(int32_t child, int32_t parent)
 {
-	return m_scene_graph->world_pose(node);
+	m_scene_graph.link(child, parent);
 }
 
 //-----------------------------------------------------------------------------
-void Unit::set_local_position(const Vector3& pos, int32_t node)
+void Unit::unlink_node(int32_t child)
 {
-	m_scene_graph->set_local_position(node, pos);
+	m_scene_graph.unlink(child);
 }
 
 //-----------------------------------------------------------------------------
-void Unit::set_local_rotation(const Quaternion& rot, int32_t node)
+void Unit::add_component(const char* name, Id component, uint32_t& size, Component* array)
 {
-	m_scene_graph->set_local_rotation(node, rot);
+	Component comp;
+	comp.name = hash::murmur2_32(name, string::strlen(name), 0);
+	comp.component = component;
+
+	array[size] = comp;
+	size++;
 }
 
 //-----------------------------------------------------------------------------
-void Unit::set_local_pose(const Matrix4x4& pose, int32_t node)
+Id Unit::find_component(const char* name, uint32_t size, Component* array)
+{
+	uint32_t name_hash = hash::murmur2_32(name, string::strlen(name), 0);
+
+	Id comp;
+	comp.id = INVALID_ID;
+
+	for (uint32_t i = 0; i < size; i++)
+	{
+		if (name_hash == array[i].name)
+		{
+			comp = array[i].component;
+		}
+	}
+
+	return comp;
+}
+
+//-----------------------------------------------------------------------------
+Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
+{
+	Id comp;
+	comp.id = INVALID_ID;
+
+	if (index < size)
+	{
+		comp = array[index].component;
+	}
+
+	return comp;
+}
+
+//-----------------------------------------------------------------------------
+void Unit::add_camera(const char* name, CameraId camera)
 {
-	m_scene_graph->set_local_pose(node, pose);
+	CE_ASSERT(m_num_cameras < MAX_CAMERA_COMPONENTS, "Max camera number reached");
+
+	add_component(name, camera, m_num_cameras, m_cameras);
+}
+
+//-----------------------------------------------------------------------------
+void Unit::add_mesh(const char* name, MeshId mesh)
+{
+	CE_ASSERT(m_num_meshes < MAX_MESH_COMPONENTS, "Max mesh number reached");
+
+	add_component(name, mesh, m_num_meshes, m_meshes);
 }
 
 //-----------------------------------------------------------------------------
 Camera* Unit::camera(const char* name)
 {
-	Component* c = m_component->get_component(name);
+	CameraId cam = find_component(name, m_num_cameras, m_cameras);
 
-	return m_creator->lookup_camera(c->component);
+	CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
+
+	return m_world->lookup_camera(cam);
+}
+
+//-----------------------------------------------------------------------------
+Camera* Unit::camera(uint32_t i)
+{
+	CameraId cam = find_component(i, m_num_cameras, m_cameras);
+
+	CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
+
+	return m_world->lookup_camera(cam);
 }
 
 //-----------------------------------------------------------------------------
 Mesh* Unit::mesh(const char* name)
 {
-	Component* c = m_component->get_component(name);
+	MeshId mesh = find_component(name, m_num_meshes, m_meshes);
+
+	CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
+
+	return m_world->lookup_mesh(mesh);
+}
+
+//-----------------------------------------------------------------------------
+Mesh* Unit::mesh(uint32_t i)
+{
+	MeshId mesh = find_component(i, m_num_meshes, m_meshes);
+
+	CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
 
-	return m_creator->lookup_mesh(c->component);
+	return m_world->lookup_mesh(mesh);
 }
 
 } // namespace crown

+ 31 - 123
engine/Unit.h

@@ -37,135 +37,30 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-typedef	Id ComponentId;
-typedef Id UnitId;
-
-//-----------------------------------------------------------------------------
-struct ComponentType
-{
-	enum Enum
-	{
-		UNKNOWN,
-		CAMERA,
-		MESH,
-		SOUND
-	};
-};
+typedef Id CameraId;
 
-//-----------------------------------------------------------------------------
 struct Component
 {
-	Id32				id;
-	ComponentType::Enum type;
-	ComponentId			component;
+	uint32_t name;
+	Id component;
 };
 
-struct ComponentList
-{
-	ComponentList()
-		: m_components(default_allocator())
-	{
-	}
-
-	//-----------------------------------------------------------------------------
-	bool next_free_component(uint32_t& index)
-	{
-		uint32_t i;
-		for (i = 0; i < m_components.size(); i++)
-		{
-			// id == 0 means free slot
-			if (m_components[i].id == 0)
-			{
-				index = i;
-				return true;
-			}
-		}
-
-		return false;
-	}
-
-	//-----------------------------------------------------------------------------
-	void add_component(const char* name, uint32_t type, ComponentId component)
-	{
-		CE_ASSERT_NOT_NULL(name);
-
-		uint32_t key = hash::murmur2_32(name, string::strlen(name), 0);
-
-		uint32_t free_index;
-
-		if (next_free_component(free_index))
-		{
-			m_components[free_index].id.key = key;
-			m_components[free_index].type = (ComponentType::Enum)type;
-			m_components[free_index].component = component;
-		}
-
-		Component comp;
-
-		comp.id.key = key;
-		comp.type = (ComponentType::Enum)type;
-		comp.component = component;
-
-		m_components.push_back(comp);
-	}
-
-	//-----------------------------------------------------------------------------
-	void remove_component(const char* name)
-	{
-		uint32_t hashed_name = hash::murmur2_32(name, string::strlen(name), 0);
-
-		for (uint32_t i = 0; i < m_components.size(); i++)
-		{
-			if (m_components[i].id == hashed_name)
-			{
-				m_components[i].id = 0;
-				return;
-			}
-		}
-
-		CE_FATAL("Component not found!");
-	}
-
-	//-----------------------------------------------------------------------------
-	Component* get_component(const char* name)
-	{
-		uint32_t hashed_name = hash::murmur2_32(name, string::strlen(name), 0);
-
-		for (uint32_t i = 0; i < m_components.size(); i++)
-		{
-			if (m_components[i].id == hashed_name)
-			{
-				return &m_components[i];
-			}
-		}
-
-		CE_FATAL("Component not found!");
-	}
-
-public:
-
-	List<Component> m_components;
-};
-
-//-----------------------------------------------------------------------------
-struct UnitResource
-{
-};
+typedef Id UnitId;
+typedef Id MeshId;
 
 class Camera;
 class Mesh;
 class World;
 
+#define MAX_CAMERA_COMPONENTS 8
+#define MAX_MESH_COMPONENTS 8
+
 struct Unit
 {
 					Unit();
-	void			create(World& creator, SceneGraph& graph, ComponentList& components, UnitId id, const Vector3& pos, const Quaternion& rot);
+	void			create(World& world, UnitId id, const Vector3& pos, const Quaternion& rot);
 	void			destroy();
 
-	void			load(UnitResource* ur);
-	void			unload();
-	void			reload(UnitResource* new_ur);
-
 	Vector3			local_position(int32_t node = 0) const;
 	Quaternion		local_rotation(int32_t node = 0) const;
 	Matrix4x4		local_pose(int32_t node = 0) const;
@@ -178,22 +73,35 @@ struct Unit
 	void			set_local_rotation(const Quaternion& rot, int32_t node = 0);
 	void			set_local_pose(const Matrix4x4& pose, int32_t node = 0);
 
+	void			link_node(int32_t child, int32_t parent);
+	void			unlink_node(int32_t child);
+
+	void			add_component(const char* name, Id component, uint32_t& size, Component* array);
+	Id				find_component(const char* name, uint32_t size, Component* array);
+	Id				find_component(uint32_t index, uint32_t size, Component* array);
+
+	void			add_camera(const char* name, CameraId camera);
+	void			add_mesh(const char* name, MeshId mesh);
+
 	Camera*			camera(const char* name);
+	Camera*			camera(uint32_t i);
+
 	Mesh*			mesh(const char* name);
+	Mesh*			mesh(uint32_t i);
 
-private:
+public:
 
-	bool			next_free_component(uint32_t& index);
+	World*			m_world;
+	UnitId			m_id;
 
-public:
+	int32_t			m_root_node;
+	SceneGraph		m_scene_graph;
 
-	World*				m_creator;
-	UnitResource*		m_resource;
-	UnitId				m_id;
+	uint32_t		m_num_cameras;
+	Component		m_cameras[MAX_CAMERA_COMPONENTS];
 
-	int32_t				m_root_node;
-	SceneGraph*			m_scene_graph;
-	ComponentList*		m_component;
+	uint32_t		m_num_meshes;
+	Component		m_meshes[MAX_MESH_COMPONENTS];
 };
 
 } // namespace crown