Daniele Bartolini 11 лет назад
Родитель
Сommit
6ea23094ac

+ 1 - 1
engine/core/json/json_parser.h

@@ -121,7 +121,7 @@ public:
 	Matrix4x4 to_matrix4x4(const Matrix4x4& def = matrix4x4::IDENTITY) const;
 
 	/// Returns the string id value hashed to murmur32() of the element.
-	StringId32 to_string_id(const StringId32 def = 0) const;
+	StringId32 to_string_id(const StringId32 def = StringId32(uint32_t(0))) const;
 
 	/// Returns the resource id value of the element.
 	/// If @a type is NULL then the string element is assumed to already contain extension.

+ 33 - 0
engine/core/string_id.cpp

@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "types.h"
+#include "murmur.h"
+#include "string_utils.h"
+
+namespace crown
+{
+
+StringId32::StringId32(const char* str)
+	: _id(murmur32(str, strlen(str)))
+{
+}
+
+StringId32::StringId32(const char* str, uint32_t len)
+	: _id(murmur32(str, len))
+{
+}
+
+StringId64::StringId64(const char* str)
+	: _id(murmur64(str, strlen(str)))
+{
+}
+
+StringId64::StringId64(const char* str, uint32_t len)
+	: _id(murmur64(str, len))
+{
+}
+
+} // namespace crown

+ 2 - 3
engine/core/strings/dynamic_string.h

@@ -10,7 +10,6 @@
 #include "string_utils.h"
 #include "array.h"
 #include "string_utils.h"
-#include "murmur.h"
 #include <algorithm>
 #include <cstring>
 
@@ -58,7 +57,7 @@ public:
 	/// Returns wheterh the string ends with the given @a s string.
 	bool ends_with(const char* s) const;
 
-	/// Returns the string hashed to murmur32.
+	/// Returns the StringId32 of the string.
 	StringId32 to_string_id() const;
 
 	///
@@ -203,7 +202,7 @@ inline bool DynamicString::ends_with(const char* s) const
 
 inline StringId32 DynamicString::to_string_id() const
 {
-	return murmur32(c_str(), length());
+	return StringId32(c_str());
 }
 
 inline const char* DynamicString::c_str() const

+ 36 - 3
engine/core/types.h

@@ -6,12 +6,43 @@
 #pragma once
 
 #define NOMINMAX
+#include "config.h"
 #include <cstddef>
 #include <stdint.h>
-#include "config.h"
+#include <stdio.h>
+
+namespace crown
+{
+
+struct StringId32
+{
+	uint32_t _id;
 
-typedef uint32_t StringId32;
-typedef uint64_t StringId64;
+	StringId32() : _id(0) {}
+	explicit StringId32(uint32_t idx) : _id(idx) {}
+	explicit StringId32(const char* str);
+	explicit StringId32(const char* str, uint32_t len);
+	StringId32 operator=(StringId32 a) { _id = a._id; return *this; }
+	bool operator==(StringId32 a) const { return _id == a._id; }
+	bool operator!=(StringId32 a) const { return _id != a._id; }
+	bool operator<(StringId32 a) const { return _id < a._id; }
+	uint32_t id() const { return _id; }
+};
+
+struct StringId64
+{
+	uint64_t _id;
+
+	StringId64() : _id(0) {}
+	explicit StringId64(uint64_t idx) : _id(idx) {}
+	explicit StringId64(const char* str);
+	explicit StringId64(const char* str, uint32_t len);
+	StringId64 operator=(StringId64 a) { _id = a._id; return *this; }
+	bool operator==(StringId64 a) const { return _id == a._id; }
+	bool operator!=(StringId64 a) const { return _id != a._id; }
+	bool operator<(StringId64 a) const { return _id < a._id; }
+	uint64_t id() const { return _id; }
+};
 
 #define INVALID_ID 65535
 
@@ -41,3 +72,5 @@ struct Id
 		return id != other.id || index != other.index;
 	}
 };
+
+} // namespace crown

+ 2 - 2
engine/crown.h

@@ -34,8 +34,8 @@ namespace crown
 			, do_continue(false)
 			, parent_window(0)
 			, console_port(10001)
-			, boot_package(0)
-			, boot_script(0)
+			, boot_package(uint64_t(0))
+			, boot_script(uint64_t(0))
 			, window_width(CROWN_DEFAULT_WINDOW_WIDTH)
 			, window_height(CROWN_DEFAULT_WINDOW_HEIGHT)
 		{

+ 10 - 0
engine/lua/lua_stack.h

@@ -142,6 +142,11 @@ struct LuaStack
 		lua_pushinteger(L, value);
 	}
 
+	void push_string_id(StringId32 value)
+	{
+		lua_pushinteger(L, value.id());
+	}
+
 	void push_float(float value)
 	{
 		lua_pushnumber(L, value);
@@ -175,6 +180,11 @@ struct LuaStack
 		return CHECKINTEGER(L, index);
 	}
 
+	StringId32 get_string_id(int32_t index)
+	{
+		return StringId32(uint32_t(CHECKINTEGER(L, index)));
+	}
+
 	float get_float(int32_t index)
 	{
 		return (float) CHECKNUMBER(L, index);

+ 1 - 1
engine/lua/lua_unit.cpp

@@ -235,7 +235,7 @@ static int unit_get_key(lua_State* L)
 		{
 			StringId32 val;
 			unit->get_key(key, val);
-			stack.push_uint32(val);
+			stack.push_string_id(val);
 			return 1;
 		}
 		case ValueType::VECTOR3:

+ 1 - 1
engine/physics/actor.cpp

@@ -273,7 +273,7 @@ void Actor::disable_collision()
 
 void Actor::set_collision_filter(const char* name)
 {
-	set_collision_filter(murmur32(name, strlen(name)));
+	set_collision_filter(StringId32(name));
 }
 
 void Actor::set_collision_filter(StringId32 filter)

+ 10 - 0
engine/resource/level_resource.cpp

@@ -102,6 +102,16 @@ template <> inline BinaryReader& operator&(BinaryReader& br, uint64_t& v)
 	return br;
 }
 
+template <> inline BinaryWriter& operator&(BinaryWriter& br, StringId64& id)
+{
+	return br & id._id;
+}
+
+template <> inline BinaryReader& operator&(BinaryReader& br, StringId64& id)
+{
+	return br & id._id;
+}
+
 template <> inline BinaryReader& operator&(BinaryReader& br, ResourceId& id)
 {
 	return br & id.type & id.name;

+ 1 - 1
engine/resource/physics_resource.h

@@ -64,7 +64,7 @@ struct ShapeResource
 {
 	StringId32 name;			// Name of the shape
 	StringId32 shape_class;		// Shape class from global.physics_config
-	StringId32 type;			// Type of the shape
+	uint32_t type;				// Type of the shape
 	StringId32 material;		// Material from global.physics_config
 	Vector3 position;			// In actor space
 	Quaternion rotation;		// In actor space

+ 12 - 11
engine/resource/resource_id.h

@@ -7,6 +7,7 @@
 
 #include "string_utils.h"
 #include "murmur.h"
+#include "path.h"
 #include <inttypes.h>
 
 namespace crown
@@ -15,26 +16,26 @@ namespace crown
 struct ResourceId
 {
 	ResourceId()
-		: type(0)
-		, name(0)
+		: type(uint64_t(0))
+		, name(uint64_t(0))
 	{
 	}
 
-	ResourceId(const char* type, const char* name)
-		: type(murmur64(type, strlen(type), SEED))
-		, name(murmur64(name, strlen(name), SEED))
+	ResourceId(StringId64 type, StringId64 name)
+		: type(type)
+		, name(name)
 	{
 	}
 
-	ResourceId(uint64_t type, uint64_t name)
-		: type(type)
-		, name(name)
+	ResourceId(const char* type, const char* name)
+		: type(murmur64(type, strlen(type), 0))
+		, name(murmur64(name, strlen(name), SEED))
 	{
 	}
 
 	const char* to_string(char out[64])
 	{
-		snprintf(out, 64, "%.16" PRIx64 "-%.16" PRIx64, type, name);
+		snprintf(out, 64, "%.16" PRIx64 "-%.16" PRIx64, type.id(), name.id());
 		return out;
 	}
 
@@ -50,8 +51,8 @@ struct ResourceId
 
 	static const uint64_t SEED = 0;
 
-	uint64_t type;
-	uint64_t name;
+	StringId64 type;
+	StringId64 name;
 };
 
 } // namespace crown

+ 12 - 10
engine/resource/resource_registry.cpp

@@ -43,7 +43,7 @@ typedef void  (*ResourceUnloadCallback)(Allocator& a, void* resource);
 
 struct ResourceCallback
 {
-	uint64_t type;
+	StringId64 type;
 	ResourceCompileCallback on_compile;
 	ResourceLoadCallback on_load;
 	ResourceUnloadCallback on_unload;
@@ -51,6 +51,8 @@ struct ResourceCallback
 	ResourceOfflineCallback on_offline;
 };
 
+#define NULL_RESOURCE_TYPE StringId64(uint64_t(0))
+
 static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
 {
 	{ LUA_TYPE,              lur::compile, lur::load, lur::unload, lur::online, lur::offline },
@@ -67,43 +69,43 @@ static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
 	{ LEVEL_TYPE,            lvr::compile, lvr::load, lvr::unload, lvr::online, lvr::offline },
 	{ SHADER_TYPE,           shr::compile, shr::load, shr::unload, shr::online, shr::offline },
 	{ SPRITE_ANIMATION_TYPE, sar::compile, sar::load, sar::unload, sar::online, sar::offline },
-	{ 0,                     NULL,         NULL,      NULL,        NULL,        NULL         }
+	{ NULL_RESOURCE_TYPE,    NULL,         NULL,      NULL,        NULL,        NULL         }
 };
 
-static const ResourceCallback* find_callback(uint64_t type)
+static const ResourceCallback* find_callback(StringId64 type)
 {
 	const ResourceCallback* c = RESOURCE_CALLBACK_REGISTRY;
 
-	while (c->type != 0 && c->type != type)
+	while (c->type != NULL_RESOURCE_TYPE && c->type != type)
 	{
 		c++;
 	}
 
-	CE_ASSERT(c->type != 0, "Compiler not found");
+	CE_ASSERT(c->type != NULL_RESOURCE_TYPE, "Compiler not found");
 	return c;
 }
 
-void resource_on_compile(uint64_t type, const char* path, CompileOptions& opts)
+void resource_on_compile(StringId64 type, const char* path, CompileOptions& opts)
 {
 	return find_callback(type)->on_compile(path, opts);
 }
 
-void* resource_on_load(uint64_t type, File& file, Allocator& a)
+void* resource_on_load(StringId64 type, File& file, Allocator& a)
 {
 	return find_callback(type)->on_load(file, a);
 }
 
-void resource_on_unload(uint64_t type, Allocator& allocator, void* resource)
+void resource_on_unload(StringId64 type, Allocator& allocator, void* resource)
 {
 	return find_callback(type)->on_unload(allocator, resource);
 }
 
-void resource_on_online(uint64_t type, StringId64 id, ResourceManager& rm)
+void resource_on_online(StringId64 type, StringId64 id, ResourceManager& rm)
 {
 	return find_callback(type)->on_online(id, rm);
 }
 
-void resource_on_offline(uint64_t type, StringId64 id, ResourceManager& rm)
+void resource_on_offline(StringId64 type, StringId64 id, ResourceManager& rm)
 {
 	return find_callback(type)->on_offline(id, rm);
 }

+ 5 - 5
engine/resource/resource_registry.h

@@ -14,10 +14,10 @@
 namespace crown
 {
 
-void resource_on_compile(uint64_t type, const char* path, CompileOptions& opts);
-void* resource_on_load(uint64_t type, File& file, Allocator& a);
-void resource_on_online(uint64_t type, StringId64 id, ResourceManager& rm);
-void resource_on_offline(uint64_t type, StringId64 id, ResourceManager& rm);
-void resource_on_unload(uint64_t type, Allocator& allocator, void* resource);
+void resource_on_compile(StringId64 type, const char* path, CompileOptions& opts);
+void* resource_on_load(StringId64 type, File& file, Allocator& a);
+void resource_on_online(StringId64 type, StringId64 id, ResourceManager& rm);
+void resource_on_offline(StringId64 type, StringId64 id, ResourceManager& rm);
+void resource_on_unload(StringId64 type, Allocator& allocator, void* resource);
 
 } // namespace crown

+ 14 - 14
engine/resource/resource_types.h

@@ -20,20 +20,20 @@
 #define TEXTURE_EXTENSION          "texture"
 #define UNIT_EXTENSION             "unit"
 
-#define FONT_TYPE                  uint64_t(0x9efe0a916aae7880)
-#define LEVEL_TYPE                 uint64_t(0x2a690fd348fe9ac5)
-#define LUA_TYPE                   uint64_t(0xa14e8dfa2cd117e2)
-#define MATERIAL_TYPE              uint64_t(0xeac0b497876adedf)
-#define MESH_TYPE                  uint64_t(0x48ff313713a997a1)
-#define PACKAGE_TYPE               uint64_t(0xad9c6d9ed1e5e77a)
-#define PHYSICS_CONFIG_TYPE        uint64_t(0x72e3cc03787a11a1)
-#define PHYSICS_TYPE               uint64_t(0x5f7203c8f280dab8)
-#define SHADER_TYPE                uint64_t(0xcce8d5b5f5ae333f)
-#define SOUND_TYPE                 uint64_t(0x90641b51c98b7aac)
-#define SPRITE_ANIMATION_TYPE      uint64_t(0x487e78e3f87f238d)
-#define SPRITE_TYPE                uint64_t(0x8d5871f9ebdb651c)
-#define TEXTURE_TYPE               uint64_t(0xcd4238c6a0c69e32)
-#define UNIT_TYPE                  uint64_t(0xe0a48d0be9a7453f)
+#define FONT_TYPE                  StringId64(0x9efe0a916aae7880)
+#define LEVEL_TYPE                 StringId64(0x2a690fd348fe9ac5)
+#define LUA_TYPE                   StringId64(0xa14e8dfa2cd117e2)
+#define MATERIAL_TYPE              StringId64(0xeac0b497876adedf)
+#define MESH_TYPE                  StringId64(0x48ff313713a997a1)
+#define PACKAGE_TYPE               StringId64(0xad9c6d9ed1e5e77a)
+#define PHYSICS_CONFIG_TYPE        StringId64(0x72e3cc03787a11a1)
+#define PHYSICS_TYPE               StringId64(0x5f7203c8f280dab8)
+#define SHADER_TYPE                StringId64(0xcce8d5b5f5ae333f)
+#define SOUND_TYPE                 StringId64(0x90641b51c98b7aac)
+#define SPRITE_ANIMATION_TYPE      StringId64(0x487e78e3f87f238d)
+#define SPRITE_TYPE                StringId64(0x8d5871f9ebdb651c)
+#define TEXTURE_TYPE               StringId64(0xcd4238c6a0c69e32)
+#define UNIT_TYPE                  StringId64(0xe0a48d0be9a7453f)
 
 // #define FONT_VERSION               uint32_t(1)
 // #define LEVEL_VERSION              uint32_t(1)

+ 13 - 13
engine/resource/unit_resource.cpp

@@ -48,7 +48,7 @@ namespace unit_resource
 		return (ProjectionType::Enum)0;
 	}
 
-	const StringId32 NO_PARENT = 0xFFFFFFFF;
+	const StringId32 NO_PARENT(0xFFFFFFFF);
 
 	struct GraphNode
 	{
@@ -130,14 +130,14 @@ namespace unit_resource
 			JSONElement node = e.key(node_name);
 
 			GraphNode gn;
-			gn.name = murmur32(node_name, strlen(node_name));
+			gn.name = StringId32(node_name);
 			gn.parent = NO_PARENT;
 
 			if (!node.key("parent").is_nil())
 			{
 				DynamicString parent_name;
 				node.key("parent").to_string(parent_name);
-				gn.parent = murmur32(parent_name.c_str(), parent_name.length(), 0);
+				gn.parent = parent_name.to_string_id();
 			}
 
 			JSONElement pos = node.key("position");
@@ -172,10 +172,10 @@ namespace unit_resource
 			DynamicString camera_type;
 			type.to_string(camera_type);
 
-			StringId32 node_name_hash = murmur32(node_name.c_str(), node_name.length());
+			StringId32 node_name_hash = node_name.to_string_id();
 
 			UnitCamera cn;
-			cn.name = murmur32(camera_name, strlen(camera_name));
+			cn.name = StringId32(camera_name);
 			cn.node = find_node_index(node_name_hash, node_depths);
 			cn.type = projection_name_to_enum(camera_type.c_str());
 			cn.fov =  camera.key_or_nil("fov").to_float(16.0f / 9.0f);
@@ -197,10 +197,10 @@ namespace unit_resource
 			JSONElement renderable = e.key(renderable_name);
 
 			DynamicString node_name; renderable.key("node").to_string(node_name);
-			StringId32 node_name_hash = murmur32(node_name.c_str(), node_name.length(), 0);
+			StringId32 node_name_hash = node_name.to_string_id();
 
 			UnitRenderable rn;
-			rn.name = murmur32(renderable_name, strlen(renderable_name), 0);
+			rn.name = StringId32(renderable_name);
 			rn.node = find_node_index(node_name_hash, node_depths);
 			rn.visible = renderable.key("visible").to_bool();
 
@@ -237,7 +237,7 @@ namespace unit_resource
 			JSONElement value = e.key(key);
 
 			Key out_key;
-			out_key.name = murmur32(key, strlen(key));
+			out_key.name = StringId32(key);
 			out_key.offset = array::size(values);
 
 			if (value.is_bool()) out_key.type = ValueType::BOOL;
@@ -266,7 +266,7 @@ namespace unit_resource
 				{
 					DynamicString val;
 					value.to_string(val);
-					StringId32 val_hash = murmur32(val.c_str(), val.length());
+					StringId32 val_hash = val.to_string_id();
 					array::push(values, (char*) &val_hash, sizeof(StringId32));
 					break;
 				}
@@ -346,8 +346,6 @@ namespace unit_resource
 		}
 
 		ResourceId sprite_anim;
-		sprite_anim.type = 0;
-		sprite_anim.name = 0;
 		if (root.has_key("sprite_animation"))
 			sprite_anim = root.key("sprite_animation").to_resource_id("sprite_animation");
 
@@ -539,11 +537,12 @@ namespace unit_resource
 	bool has_key(const UnitResource* ur, const char* k)
 	{
 		const uint32_t nk = num_keys(ur);
+		const StringId32 key_hash(k);
 		Key* begin = (Key*) ((char*)ur + ur->keys_offset);
 
 		for (uint32_t i = 0; i < nk; i++)
 		{
-			if (begin[i].name == murmur32(k, strlen(k)))
+			if (begin[i].name == key_hash)
 			{
 				return true;
 			}
@@ -555,11 +554,12 @@ namespace unit_resource
 	bool get_key(const UnitResource* ur, const char* k, Key& out_k)
 	{
 		const uint32_t nk = num_keys(ur);
+		const StringId32 key_hash(k);
 		Key* begin = (Key*) ((char*)ur + ur->keys_offset);
 
 		for (uint32_t i = 0; i < nk; i++)
 		{
-			if (begin[i].name == murmur32(k, strlen(k)))
+			if (begin[i].name == key_hash)
 			{
 				out_k = begin[i];
 				return true;

+ 1 - 1
engine/resource/unit_resource.h

@@ -55,7 +55,7 @@ struct UnitMaterial
 
 struct UnitCamera
 {
-	uint32_t name;
+	StringId32 name;
 	int32_t node;
 	uint32_t type; // ProjectionType::Enum
 	float fov;

+ 2 - 3
engine/world/scene_graph.cpp

@@ -9,7 +9,6 @@
 #include "matrix4x4.h"
 #include "allocator.h"
 #include "string_utils.h"
-#include "murmur.h"
 #include <string.h>
 
 #define CLEAN		0
@@ -74,7 +73,7 @@ void SceneGraph::destroy()
 
 int32_t SceneGraph::node(const char* name) const
 {
-	return node(murmur32(name, strlen(name)));
+	return node(StringId32(name));
 }
 
 int32_t SceneGraph::node(StringId32 name) const
@@ -93,7 +92,7 @@ int32_t SceneGraph::node(StringId32 name) const
 
 bool SceneGraph::has_node(const char* name) const
 {
-	StringId32 name_hash = murmur32(name, strlen(name), 0);
+	const StringId32 name_hash(name);
 
 	for (uint32_t i = 0; i < m_num_nodes; i++)
 	{

+ 6 - 7
engine/world/unit.cpp

@@ -75,7 +75,7 @@ void Unit::create_objects(const Matrix4x4& pose)
 	memcpy(m_values, values(m_resource), values_size(m_resource));
 
 	StringId64 anim_id = sprite_animation(m_resource);
-	if (anim_id != 0)
+	if (anim_id.id() != 0)
 	{
 		m_sprite_animation = m_world.sprite_animation_player()->create_sprite_animation((SpriteAnimationResource*) device()->resource_manager()->get(SPRITE_ANIMATION_TYPE, anim_id));
 	}
@@ -144,7 +144,7 @@ void Unit::create_renderable_objects()
 	for (uint32_t i = 0; i < num_materials(m_resource); i++)
 	{
 		const UnitMaterial* mat = get_material(m_resource, i);
-		add_material(murmur32("default", strlen("default"), 0), material_manager::get()->create_material(mat->id));
+		add_material(StringId32("default"), material_manager::get()->create_material(mat->id));
 	}
 
 	// Create renderables
@@ -174,7 +174,7 @@ void Unit::create_physics_objects()
 {
 	using namespace unit_resource;
 	using namespace physics_resource;
-	if (unit_resource::physics_resource(m_resource) != 0)
+	if (unit_resource::physics_resource(m_resource).id() != 0)
 	{
 		const PhysicsResource* pr = (PhysicsResource*) device()->resource_manager()->get(PHYSICS_TYPE, unit_resource::physics_resource(m_resource));
 
@@ -313,8 +313,7 @@ void Unit::add_component(StringId32 name, Id component, uint32_t& size, Componen
 
 Id Unit::find_component(const char* name, uint32_t size, Component* array)
 {
-	const uint32_t name_hash = murmur32(name, strlen(name), 0);
-	return find_component_by_name(name_hash, size, array);
+	return find_component_by_name(StringId32(name), size, array);
 }
 
 Id Unit::find_component_by_index(uint32_t index, uint32_t size, Component* array)
@@ -475,7 +474,7 @@ bool Unit::is_a(const char* name)
 void Unit::play_sprite_animation(const char* name, bool loop)
 {
 	if (m_sprite_animation)
-		m_sprite_animation->play(murmur32(name, strlen(name), 0), loop);
+		m_sprite_animation->play(StringId32(name), loop);
 }
 
 void Unit::stop_sprite_animation()
@@ -555,7 +554,7 @@ void Unit::set_key(const char* k, const char* v)
 	using namespace unit_resource;
 	Key key;
 	unit_resource::get_key(m_resource, k, key);
-	*(StringId32*)(m_values + key.offset) = murmur32(v, strlen(v));
+	*(StringId32*)(m_values + key.offset) = StringId32(v);
 }
 
 void Unit::set_key(const char* k, const Vector3& v)

+ 1 - 1
engine/world/unit.h

@@ -37,7 +37,7 @@ typedef Id MaterialId;
 
 struct Component
 {
-	uint32_t name;
+	StringId32 name;
 	ComponentId component;
 };