Daniele Bartolini 11 роки тому
батько
коміт
da3b042507

+ 2 - 2
engine/audio/sound_world.h

@@ -26,7 +26,7 @@ public:
 
 	/// Plays the sound @a sr at the given @a volume [0 .. 1].
 	/// If loop is true the sound will be played looping.
-	virtual SoundInstanceId play(SoundResource* sr, bool loop, float volume, const Vector3& pos) = 0;
+	virtual SoundInstanceId play(const SoundResource* sr, bool loop, float volume, const Vector3& pos) = 0;
 
 	/// Stops the sound with the given @a id.
 	/// After this call, the instance will be destroyed.
@@ -53,7 +53,7 @@ public:
 	/// Sets the @a volumes of @a num sound instances @a ids.
 	virtual void set_sound_volumes(uint32_t num, const SoundInstanceId* ids, const float* volumes) = 0;
 
-	virtual void reload_sounds(SoundResource* old_sr, SoundResource* new_sr) = 0;
+	virtual void reload_sounds(const SoundResource* old_sr, const SoundResource* new_sr) = 0;
 
 	/// Sets the @a pose of the listener in world space.
 	virtual void set_listener_pose(const Matrix4x4& pose) = 0;

+ 6 - 6
engine/audio/sound_world_al.cpp

@@ -77,7 +77,7 @@ namespace audio_globals
 
 struct SoundInstance
 {
-	void create(SoundResource* sr, const Vector3& pos)
+	void create(const SoundResource* sr, const Vector3& pos)
 	{
 		using namespace sound_resource;
 
@@ -113,7 +113,7 @@ struct SoundInstance
 		AL_CHECK(alDeleteSources(1, &_source));
 	}
 
-	void reload(SoundResource* new_sr)
+	void reload(const SoundResource* new_sr)
 	{
 		destroy();
 		create(new_sr, position());
@@ -187,7 +187,7 @@ struct SoundInstance
 		AL_CHECK(alSourcef(_source, AL_GAIN, volume));
 	}
 
-	SoundResource* resource()
+	const SoundResource* resource()
 	{
 		return _resource;
 	}
@@ -195,7 +195,7 @@ struct SoundInstance
 public:
 
 	SoundInstanceId _id;
-	SoundResource* _resource;
+	const SoundResource* _resource;
 	ALuint _buffer;
 	ALuint _source;
 };
@@ -213,7 +213,7 @@ public:
 	{
 	}
 
-	virtual SoundInstanceId play(SoundResource* sr, bool loop, float volume, const Vector3& pos)
+	virtual SoundInstanceId play(const SoundResource* sr, bool loop, float volume, const Vector3& pos)
 	{
 		SoundInstance instance;
 		instance.create(sr, pos);
@@ -283,7 +283,7 @@ public:
 		}
 	}
 
-	virtual void reload_sounds(SoundResource* old_sr, SoundResource* new_sr)
+	virtual void reload_sounds(const SoundResource* old_sr, const SoundResource* new_sr)
 	{
 		for (uint32_t i = 0; i < id_array::size(_playing_sounds); i++)
 		{

+ 6 - 6
engine/audio/sound_world_sles.cpp

@@ -123,7 +123,7 @@ namespace sles_sound_world
 
 struct SoundInstance
 {
-	void create(SLEngineItf engine, SLObjectItf output_mix, SoundInstanceId id, SoundResource* sr)
+	void create(SLEngineItf engine, SLObjectItf output_mix, SoundInstanceId id, const SoundResource* sr)
 	{
 		using namespace sound_resource;
 
@@ -238,7 +238,7 @@ struct SoundInstance
 		(*_player)->Destroy(_player);
 	}
 
-	void reload(SoundResource* new_sr)
+	void reload(const SoundResource* new_sr)
 	{
 	}
 
@@ -315,7 +315,7 @@ struct SoundInstance
 		// }
 	}
 
-	SoundResource* resource()
+	const SoundResource* resource()
 	{
 		return _resource;
 	}
@@ -330,7 +330,7 @@ struct SoundInstance
 public:
 
 	SoundInstanceId _id;
-	SoundResource* _resource;
+	const SoundResource* _resource;
 
 	SLObjectItf _player;
 	SLAndroidSimpleBufferQueueItf _player_bufferqueue;
@@ -353,7 +353,7 @@ public:
 		sles_sound_world::shutdown();
 	}
 
-	virtual SoundInstanceId play(SoundResource* sr, bool loop, float volume, const Vector3& /*pos*/)
+	virtual SoundInstanceId play(const SoundResource* sr, bool loop, float volume, const Vector3& /*pos*/)
 	{
 		SoundInstance dummy;
 		SoundInstanceId id = id_array::create(_playing_sounds, dummy);
@@ -424,7 +424,7 @@ public:
 		}
 	}
 
-	virtual void reload_sounds(SoundResource* old_sr, SoundResource* new_sr)
+	virtual void reload_sounds(const SoundResource* old_sr, const SoundResource* new_sr)
 	{
 		for (uint32_t i = 0; i < id_array::size(_playing_sounds); i++)
 		{

+ 5 - 0
engine/lua/lua_stack.h

@@ -232,6 +232,11 @@ struct LuaStack
 		return lua_next(L, i);
 	}
 
+	StringId64 get_resource_id(int index)
+	{
+		return StringId64(CHECKSTRING(L, index));
+	}
+
 	void push_resource_package(ResourcePackage* package)
 	{
 		ResourcePackage** p = (ResourcePackage**) lua_newuserdata(L, sizeof(ResourcePackage*));

+ 1 - 1
engine/lua/lua_unit.cpp

@@ -183,7 +183,7 @@ static int unit_controller(lua_State* L)
 static int unit_is_a(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(stack.get_unit(1)->is_a(stack.get_string(2)));
+	stack.push_bool(stack.get_unit(1)->is_a(stack.get_resource_id(2)));
 	return 1;
 }
 

+ 10 - 6
engine/lua/lua_world.cpp

@@ -20,11 +20,12 @@ static int world_spawn_unit(lua_State* L)
 {
 	LuaStack stack(L);
 	World* world = stack.get_world(1);
-	const char* name = stack.get_string(2);
+	const StringId64 name = stack.get_resource_id(2);
 	const Vector3& pos = stack.num_args() > 2 ? stack.get_vector3(3) : vector3::ZERO;
 	const Quaternion& rot = stack.num_args() > 3 ? stack.get_quaternion(4) : quaternion::IDENTITY;
 
-	LUA_ASSERT(device()->resource_manager()->can_get(UNIT_EXTENSION, name), stack, "Unit '%s' not found", name);
+	LUA_ASSERT(device()->resource_manager()->can_get(UNIT_TYPE, name), stack, "Unit not found");
+
 	UnitId unit = world->spawn_unit(name, pos, rot);
 	stack.push_unit(world->get_unit(unit));
 	return 1;
@@ -89,14 +90,15 @@ static int world_play_sound(lua_State* L)
 {
 	LuaStack stack(L);
 	World* world = stack.get_world(1);
-	const char* name = stack.get_string(2);
-	int32_t nargs = stack.num_args();
+	const StringId64 name = stack.get_resource_id(2);
+	const int32_t nargs = stack.num_args();
 	const bool loop = nargs > 2 ? stack.get_bool(3) : false;
 	const float volume = nargs > 3 ? stack.get_float(4) : 1.0f;
 	const Vector3& pos = nargs > 4 ? stack.get_vector3(5) : vector3::ZERO;
 	const float range = nargs > 5 ? stack.get_float(6) : 1000.0f;
 
-	LUA_ASSERT(device()->resource_manager()->can_get(SOUND_EXTENSION, name), stack, "Sound '%s' not found", name);
+	LUA_ASSERT(device()->resource_manager()->can_get(SOUND_TYPE, name), stack, "Sound not found");
+
 	stack.push_sound_instance_id(world->play_sound(name, loop, volume, pos, range));
 	return 1;
 }
@@ -181,7 +183,9 @@ static int world_destroy_debug_line(lua_State* L)
 static int world_load_level(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.get_world(1)->load_level(stack.get_string(2));
+	StringId64 name = stack.get_resource_id(2);
+	LUA_ASSERT(device()->resource_manager()->can_get(LEVEL_TYPE, name), stack, "Level not found");
+	stack.get_world(1)->load_level(name);
 	return 0;
 }
 

+ 2 - 0
engine/resource/unit_resource.cpp

@@ -351,6 +351,7 @@ namespace unit_resource
 
 		UnitResource ur;
 		ur.version = VERSION;
+		ur.name = StringId64(unit_name.c_str());
 		ur.physics_resource = m_physics_resource.name;
 		ur.sprite_animation = sprite_anim.name;
 		ur.num_renderables = array::size(m_renderables);
@@ -370,6 +371,7 @@ namespace unit_resource
 
 		opts.write(ur.version);
 		opts.write(ur._pad);
+		opts.write(ur.name);
 		opts.write(ur.physics_resource);
 		opts.write(ur.sprite_animation);
 		opts.write(ur.num_renderables);

+ 1 - 0
engine/resource/unit_resource.h

@@ -19,6 +19,7 @@ struct UnitResource
 {
 	uint32_t version;
 	uint32_t _pad;
+	StringId64 name;
 	StringId64 physics_resource;
 	StringId64 sprite_animation;
 	uint32_t num_renderables;

+ 3 - 4
engine/world/unit.cpp

@@ -22,11 +22,10 @@ namespace crown
 
 using namespace unit_resource;
 
-Unit::Unit(World& w, UnitId unit_id, StringId64 resid, const UnitResource* ur, const Matrix4x4& pose)
+Unit::Unit(World& w, UnitId unit_id, const UnitResource* ur, const Matrix4x4& pose)
 	: m_world(w)
 	, m_scene_graph(*w.scene_graph_manager()->create_scene_graph())
 	, m_sprite_animation(NULL)
-	, m_resource_id(resid)
 	, m_resource(ur)
 	, m_id(unit_id)
 	, m_num_cameras(0)
@@ -466,9 +465,9 @@ Material* Unit::material(uint32_t i)
 	return material_manager::get()->lookup_material(material);
 }
 
-bool Unit::is_a(const char* name)
+bool Unit::is_a(StringId64 name)
 {
-	return m_resource_id == ResourceId("unit", name).name;
+	return m_resource->name == name;
 }
 
 void Unit::play_sprite_animation(const char* name, bool loop)

+ 2 - 4
engine/world/unit.h

@@ -41,7 +41,6 @@ struct Component
 	ComponentId component;
 };
 
-
 class SceneGraphManager;
 class World;
 struct Actor;
@@ -56,7 +55,7 @@ struct Material;
 /// @ingroup World
 struct Unit
 {
-	Unit(World& w, UnitId unit_id, StringId64 resid, const UnitResource* ur, const Matrix4x4& pose);
+	Unit(World& w, UnitId unit_id, const UnitResource* ur, const Matrix4x4& pose);
 	~Unit();
 
 	void set_id(const UnitId id);
@@ -137,7 +136,7 @@ struct Unit
 
 	Controller* controller();
 
-	bool is_a(const char* name);
+	bool is_a(StringId64 name);
 
 	void play_sprite_animation(const char* name, bool loop);
 	void stop_sprite_animation();
@@ -167,7 +166,6 @@ public:
 	World& m_world;
 	SceneGraph& m_scene_graph;
 	SpriteAnimation* m_sprite_animation;
-	const StringId64 m_resource_id;
 	const UnitResource*	m_resource;
 	UnitId m_id;
 

+ 51 - 0
engine/world/unit_manager.h

@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "types.h"
+#include "array.h"
+
+namespace crown
+{
+
+#define UNIT_INDEX_BITS 22
+#define UNIT_INDEX_MASK 0x003fffff
+#define UNIT_ID_BITS    8
+#define UNIT_ID_MASK    0x3fc00000
+
+struct UnitId
+{
+	uint32_t id;
+
+	uint32_t index()
+	{
+		return id & UNIT_INDEX_MASK;
+	}
+
+	uint32_t id()
+	{
+		return (id >> UNIT_INDEX_BITS) & UNIT_ID_MASK;
+	}
+};
+
+struct UnitManager
+{
+	Array<uint8_t> identifiers;
+
+	UnitId create()
+	{
+	}
+
+	void destroy(UnitId id)
+	{
+
+	}
+
+	bool has(UnitId id)
+	{
+		return identifiers[id.index()] == id.id();
+	}
+};
+
+} // namespace crown

+ 18 - 21
engine/world/world.cpp

@@ -57,24 +57,22 @@ World::~World()
 	CE_DELETE(default_allocator(), _scenegraph_manager);
 }
 
-UnitId World::spawn_unit(const char* name, const Vector3& pos, const Quaternion& rot)
+UnitId World::spawn_unit(const UnitResource* ur, const Vector3& pos, const Quaternion& rot)
 {
-	const ResourceId id(UNIT_EXTENSION, name);
-	return spawn_unit(id.name, pos, rot);
-}
-
-UnitId World::spawn_unit(StringId64 name, const Vector3& pos, const Quaternion& rot)
-{
-	UnitResource* ur = (UnitResource*)_resource_manager->get(UNIT_TYPE, name);
-
 	Unit* u = (Unit*) m_unit_pool.allocate(sizeof(Unit), CE_ALIGNOF(Unit));
 	const UnitId unit_id = id_array::create(m_units, u);
-	new (u) Unit(*this, unit_id, name, ur, Matrix4x4(rot, pos));
+	new (u) Unit(*this, unit_id, ur, Matrix4x4(rot, pos));
 
 	post_unit_spawned_event(unit_id);
 	return unit_id;
 }
 
+UnitId World::spawn_unit(StringId64 name, const Vector3& pos, const Quaternion& rot)
+{
+	UnitResource* ur = (UnitResource*)_resource_manager->get(UNIT_TYPE, name);
+	return spawn_unit(ur, pos, rot);
+}
+
 void World::destroy_unit(UnitId id)
 {
 	CE_DELETE(m_unit_pool, id_array::get(m_units, id));
@@ -174,16 +172,15 @@ void World::destroy_camera(CameraId id)
 	id_array::destroy(m_cameras, id);
 }
 
-SoundInstanceId World::play_sound(const char* name, const bool loop, const float volume, const Vector3& pos, const float range)
+SoundInstanceId World::play_sound(const SoundResource* sr, const bool loop, const float volume, const Vector3& pos, const float range)
 {
-	ResourceId id(SOUND_EXTENSION, name);
-	return play_sound(id.name, loop, volume, pos, range);
+	return _sound_world->play(sr, loop, volume, pos);
 }
 
 SoundInstanceId World::play_sound(StringId64 name, const bool loop, const float volume, const Vector3& pos, const float range)
 {
-	SoundResource* sr = (SoundResource*)_resource_manager->get(SOUND_TYPE, name);
-	return _sound_world->play(sr, loop, volume, pos);
+	const SoundResource* sr = (const SoundResource*)_resource_manager->get(SOUND_TYPE, name);
+	play_sound(sr, loop, volume, pos, range);
 }
 
 void World::stop_sound(SoundInstanceId id)
@@ -240,12 +237,6 @@ void World::destroy_debug_line(DebugLine* line)
 	CE_DELETE(default_allocator(), line);
 }
 
-void World::load_level(const char* name)
-{
-	const LevelResource* lr = (LevelResource*) _resource_manager->get(LEVEL_EXTENSION, name);
-	load_level(lr);
-}
-
 void World::load_level(const LevelResource* lr)
 {
 	using namespace level_resource;
@@ -267,6 +258,12 @@ void World::load_level(const LevelResource* lr)
 	post_level_loaded_event();
 }
 
+void World::load_level(StringId64 name)
+{
+	const LevelResource* lr = (LevelResource*) _resource_manager->get(LEVEL_TYPE, name);
+	load_level(lr);
+}
+
 SceneGraphManager* World::scene_graph_manager()
 {
 	return _scenegraph_manager;

+ 3 - 3
engine/world/world.h

@@ -41,7 +41,7 @@ public:
 	~World();
 
 	/// Spawns a new instance of the unit @a name at the given @a position and @a rotation.
-	UnitId spawn_unit(const char* name, const Vector3& position = vector3::ZERO, const Quaternion& rotation = quaternion::IDENTITY);
+	UnitId spawn_unit(const UnitResource* ur, const Vector3& position = vector3::ZERO, const Quaternion& rotation = quaternion::IDENTITY);
 	UnitId spawn_unit(StringId64 name, const Vector3& pos, const Quaternion& rot);
 
 	/// Destroys the unit with the given @a id.
@@ -88,7 +88,7 @@ public:
 
 	/// Plays the sound with the given @a name at the given @a position, with the given
 	/// @a volume and @a range. @a loop controls whether the sound must loop or not.
-	SoundInstanceId play_sound(const char* name, bool loop = false, float volume = 1.0f, const Vector3& position = vector3::ZERO, float range = 50.0f);
+	SoundInstanceId play_sound(const SoundResource* sr, bool loop = false, float volume = 1.0f, const Vector3& position = vector3::ZERO, float range = 50.0f);
 	SoundInstanceId play_sound(StringId64 name, const bool loop, const float volume, const Vector3& pos, const float range);
 
 	/// Stops the sound with the given @a id.
@@ -127,8 +127,8 @@ public:
 	void destroy_debug_line(DebugLine* line);
 
 	/// Loads the level @a name into the world.
-	void load_level(const char* name);
 	void load_level(const LevelResource* lr);
+	void load_level(StringId64 name);
 
 	SceneGraphManager* scene_graph_manager();
 	SpriteAnimationPlayer* sprite_animation_player();