mikymod 12 лет назад
Родитель
Сommit
02f2983ce0

+ 1 - 0
engine/CMakeLists.txt

@@ -337,6 +337,7 @@ set (LUA_SRC
 	lua/LuaResourcePackage.cpp
 	lua/LuaResourcePackage.cpp
 	lua/LuaWorld.cpp
 	lua/LuaWorld.cpp
 	lua/LuaUnit.cpp
 	lua/LuaUnit.cpp
+	lua/LuaSound.cpp
 )
 )
 
 
 set (LUA_HEADERS
 set (LUA_HEADERS

+ 60 - 3
engine/SoundWorld.cpp

@@ -34,8 +34,20 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 namespace crown
 {
 {
 
 
+// //-----------------------------------------------------------------------------
+// void SoundWorld::init()
+// {
+
+// }
+
+// //-----------------------------------------------------------------------------
+// void SoundWorld::shutdown()
+// {
+
+// }
+
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-SoundInstanceId SoundWorld::create_sound(const char* name)
+SoundInstanceId SoundWorld::play_sound(const char* name, const bool loop, const Vec3& pos)
 {
 {
 	SoundInstanceId id = m_sound_table.create();
 	SoundInstanceId id = m_sound_table.create();
 
 
@@ -43,15 +55,60 @@ SoundInstanceId SoundWorld::create_sound(const char* name)
 
 
 	m_sound[id.index].m_sound = sound->m_id;
 	m_sound[id.index].m_sound = sound->m_id;
 
 
+	device()->sound_renderer()->set_sound_loop(m_sound[id.index].m_sound, loop);
+
+	device()->sound_renderer()->set_sound_position(m_sound[id.index].m_sound, pos);
+
+	device()->sound_renderer()->play_sound(m_sound[id.index].m_sound);
+
 	return id;
 	return id;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void SoundWorld::destroy_sound(SoundInstanceId sound)
+void SoundWorld::pause_sound(SoundInstanceId sound)
+{
+	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+	device()->sound_renderer()->pause_sound(sound);
+}
+
+// //-----------------------------------------------------------------------------
+// void SoundWorld::link_sound(SoundInstanceId sound, UnitId unit)
+// {
+// 	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+// 	// Must be implemented
+// }
+
+//-----------------------------------------------------------------------------
+void SoundWorld::set_listener(const Vec3& pos, const Vec3& vel, const Vec3& or_up, const Vec3& or_at)
+{
+	device()->sound_renderer()->set_listener(pos, vel, or_up, or_at);
+}
+
+//-----------------------------------------------------------------------------
+void SoundWorld::set_sound_position(SoundInstanceId sound, const Vec3& pos)
+{
+	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+	device()->sound_renderer()->set_sound_position(sound, pos);
+}
+
+//-----------------------------------------------------------------------------
+void SoundWorld::set_sound_range(SoundInstanceId sound, const float range)
+{
+	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+	device()->sound_renderer()->set_sound_max_distance(sound, range);
+}
+
+//-----------------------------------------------------------------------------
+void SoundWorld::set_sound_volume(SoundInstanceId sound, const float vol)
 {
 {
 	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
 	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
 
 
-	// Stub
+	device()->sound_renderer()->set_sound_gain(sound, vol);
 }
 }
 
 
+
 } // namespace crown
 } // namespace crown

+ 15 - 2
engine/SoundWorld.h

@@ -28,6 +28,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 
 #include "IdTable.h"
 #include "IdTable.h"
 #include "SoundRenderer.h"
 #include "SoundRenderer.h"
+#include "Vec3.h"
+#include "Mat4.h"
 
 
 namespace crown
 namespace crown
 {
 {
@@ -41,9 +43,20 @@ struct SoundInstance
 
 
 class SoundWorld
 class SoundWorld
 {
 {
+public:
+	// void						init();
+	// void						shutdown();
 
 
-	SoundInstanceId				create_sound(const char* name);
-	void						destroy_sound(SoundInstanceId sound);
+	SoundInstanceId				play_sound(const char* name, const bool loop = false, const Vec3& pos = Vec3::ZERO);
+	void						pause_sound(SoundInstanceId sound);
+
+	// void 						link_sound(SoundInstanceId sound, UnitId unit);
+
+	void						set_listener(const Vec3& pos, const Vec3& vel, const Vec3& or_up, const Vec3& or_at);
+
+	void						set_sound_position(SoundInstanceId sound, const Vec3& pos);
+	void						set_sound_range(SoundInstanceId sound, const float range);
+	void						set_sound_volume(SoundInstanceId sound, const float vol);
 
 
 private:
 private:
 
 

+ 8 - 6
engine/World.cpp

@@ -35,23 +35,18 @@ namespace crown
 World::World()
 World::World()
 	: m_allocator(default_allocator(), 1048576)
 	: m_allocator(default_allocator(), 1048576)
 	, m_is_init(false)
 	, m_is_init(false)
-	, m_sound_world(NULL)
 {
 {
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void World::init()
 void World::init()
 {
 {
-	m_sound_world = CE_NEW(m_allocator, SoundWorld);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void World::shutdown()
 void World::shutdown()
 {
 {
-	if (m_sound_world)
-	{
-		CE_DELETE(m_allocator, m_sound_world);
-	}
+
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -110,4 +105,11 @@ void World::update(float /*dt*/)
 {
 {
 }
 }
 
 
+//-----------------------------------------------------------------------------
+SoundWorld&	World::sound_world()
+{
+	return m_sound_world;
+}
+
+
 } // namespace crown
 } // namespace crown

+ 12 - 10
engine/World.h

@@ -44,20 +44,22 @@ class Quat;
 class World
 class World
 {
 {
 public:
 public:
-					World();
+						World();
 
 
-	void			init();
-	void			shutdown();
+	void				init();
+	void				shutdown();
 
 
-	UnitId			spawn_unit(const char* name, const Vec3& pos = Vec3::ZERO, const Quat& rot = Quat(Vec3(0, 1, 0), 0.0f));
-	void			kill_unit(UnitId unit);
+	UnitId				spawn_unit(const char* name, const Vec3& pos = Vec3::ZERO, const Quat& rot = Quat(Vec3(0, 1, 0), 0.0f));
+	void				kill_unit(UnitId unit);
 
 
-	void			link_unit(UnitId child, UnitId parent);
-	void			unlink_unit(UnitId child, UnitId parent);
+	void				link_unit(UnitId child, UnitId parent);
+	void				unlink_unit(UnitId child, UnitId parent);
 
 
-	Unit*			unit(UnitId unit);
+	Unit*				unit(UnitId unit);
 
 
-	void			update(float dt);
+	void				update(float dt);
+
+	SoundWorld&			sound_world();
 
 
 private:
 private:
 
 
@@ -68,7 +70,7 @@ private:
 	IdTable<MAX_UNITS> 	m_unit_table;
 	IdTable<MAX_UNITS> 	m_unit_table;
 	Unit				m_units[MAX_UNITS];
 	Unit				m_units[MAX_UNITS];
 
 
-	SoundWorld*			m_sound_world;
+	SoundWorld			m_sound_world;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 42 - 2
engine/compilers/package/PackageCompiler.cpp

@@ -38,7 +38,12 @@ namespace crown
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 PackageCompiler::PackageCompiler()
 PackageCompiler::PackageCompiler()
-	: m_has_texture(false), m_has_lua(false), m_textures(default_allocator()), m_scripts(default_allocator())
+	: m_has_texture(false)
+	, m_has_lua(false)
+	, m_has_sound(false)
+	, m_textures(default_allocator())
+	, m_scripts(default_allocator())
+	, m_sounds(default_allocator())
 {
 {
 }
 }
 
 
@@ -105,9 +110,37 @@ size_t PackageCompiler::compile_impl(Filesystem& fs, const char* resource_path)
 		}
 		}
 	}
 	}
 
 
+	// Check for sounds
+	if (root.has_key("sound"))
+	{
+		JSONElement sound_array = root.key("sound");
+		uint32_t sound_array_size = sound_array.size();
+
+		for (uint32_t i = 0; i < sound_array_size; i++)
+		{
+			TempAllocator256 alloc;
+			DynamicString sound_name(alloc);
+			sound_name += sound_array[i].string_value();
+			sound_name += ".sound";
+
+			Log::i("file: %s", sound_name.c_str());
+
+			if (!fs.is_file(sound_name.c_str()))
+			{
+				Log::e("Sound '%s' does not exist.", sound_name.c_str());
+				return 0;
+			}
+
+			ResourceId id;
+			id.id = hash::murmur2_64(sound_name.c_str(), string::strlen(sound_name.c_str()), 0);
+			m_sounds.push_back(id);
+		}
+	}
+
 	return sizeof(PackageHeader) +
 	return sizeof(PackageHeader) +
 			m_textures.size() * sizeof(ResourceId) +
 			m_textures.size() * sizeof(ResourceId) +
-			m_scripts.size() * sizeof(ResourceId);
+			m_scripts.size() * sizeof(ResourceId) +
+			m_sounds.size() * sizeof(ResourceId);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -116,9 +149,11 @@ void PackageCompiler::write_impl(File* out_file)
 	PackageHeader header;
 	PackageHeader header;
 	header.num_textures = m_textures.size();
 	header.num_textures = m_textures.size();
 	header.num_scripts = m_scripts.size();
 	header.num_scripts = m_scripts.size();
+	header.num_sounds = m_sounds.size();
 
 
 	header.textures_offset = sizeof(PackageHeader);
 	header.textures_offset = sizeof(PackageHeader);
 	header.scripts_offset  = header.textures_offset + sizeof(ResourceId) * header.num_textures;
 	header.scripts_offset  = header.textures_offset + sizeof(ResourceId) * header.num_textures;
+	header.sounds_offset = header.scripts_offset + sizeof(ResourceId) * header.num_scripts;
 
 
 	out_file->write((char*) &header, sizeof(PackageHeader));
 	out_file->write((char*) &header, sizeof(PackageHeader));
 
 
@@ -130,10 +165,15 @@ void PackageCompiler::write_impl(File* out_file)
 	{
 	{
 		out_file->write((char*) m_scripts.begin(), sizeof(ResourceId) * header.num_scripts);
 		out_file->write((char*) m_scripts.begin(), sizeof(ResourceId) * header.num_scripts);
 	}
 	}
+	if (m_sounds.size() > 0)
+	{
+		out_file->write((char*) m_sounds.begin(), sizeof(ResourceId) * header.num_sounds);
+	}
 
 
 	// Cleanup
 	// Cleanup
 	m_textures.clear();
 	m_textures.clear();
 	m_scripts.clear();
 	m_scripts.clear();
+	m_sounds.clear();
 }
 }
 
 
 } // namespace crown
 } // namespace crown

+ 2 - 0
engine/compilers/package/PackageCompiler.h

@@ -46,9 +46,11 @@ private:
 
 
 	bool m_has_texture;
 	bool m_has_texture;
 	bool m_has_lua;
 	bool m_has_lua;
+	bool m_has_sound;
 
 
 	List<ResourceId> m_textures;
 	List<ResourceId> m_textures;
 	List<ResourceId> m_scripts;
 	List<ResourceId> m_scripts;
+	List<ResourceId> m_sounds;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 6 - 2
engine/core/containers/IdTable.h

@@ -37,8 +37,12 @@ namespace crown
 
 
 struct Id
 struct Id
 {
 {
-	uint16_t id;
-	uint16_t index;
+	union
+	{
+		uint16_t id;
+		uint16_t index;
+		uint32_t value;
+	};
 };
 };
 
 
 /// Table of Ids.
 /// Table of Ids.

+ 2 - 0
engine/lua/LuaEnvironment.cpp

@@ -57,8 +57,10 @@ CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
 	load_accelerometer(*env);
 	load_accelerometer(*env);
 	load_device(*env);
 	load_device(*env);
 	load_resource_package(*env);
 	load_resource_package(*env);
+
 	load_unit(*env);
 	load_unit(*env);
 	load_world(*env);
 	load_world(*env);
+	load_sound(*env);
 
 
 	return 1;
 	return 1;
 }
 }

+ 1 - 0
engine/lua/LuaEnvironment.h

@@ -111,6 +111,7 @@ void load_resource_package(LuaEnvironment& env);
 void load_unit(LuaEnvironment& env);
 void load_unit(LuaEnvironment& env);
 void load_camera(LuaEnvironment& env);
 void load_camera(LuaEnvironment& env);
 void load_world(LuaEnvironment& env);
 void load_world(LuaEnvironment& env);
+void load_sound(LuaEnvironment& env);
 
 
 CE_EXPORT int32_t luaopen_libcrown(lua_State* L);
 CE_EXPORT int32_t luaopen_libcrown(lua_State* L);
 
 

+ 128 - 0
engine/lua/LuaSound.cpp

@@ -0,0 +1,128 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+#include "LuaStack.h"
+#include "LuaEnvironment.h"
+#include "Device.h"
+#include "World.h"
+#include "SoundWorld.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int sound_world_play_sound(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+	const char* name = stack.get_string(2);
+
+	const bool loop = stack.num_args() > 2 ? stack.get_bool(3) : false;
+	const Vec3& pos = stack.num_args() > 3 ? stack.get_vec3(4) : Vec3::ZERO;
+
+	SoundInstanceId id = world->sound_world().play_sound(name, loop, pos);
+
+	stack.push_int32(id.value);
+
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int sound_world_pause_sound(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+
+	SoundInstanceId id = {0};
+	id.value = stack.get_int(2);
+
+	world->sound_world().pause_sound(id);
+
+	return 0;
+}
+
+// //-----------------------------------------------------------------------------
+// CE_EXPORT int sound_world_link_sound(lua_State* L)
+// {
+
+// }
+
+// //-----------------------------------------------------------------------------
+// CE_EXPORT int sound_world_set_listener(lua_State* L)
+// {
+
+// }
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int sound_world_set_sound_position(lua_State* /*L*/)
+{
+	// LuaStack stack
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int sound_world_set_sound_range(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+
+	SoundInstanceId id = {0};
+	id.value = stack.get_int(2);
+	float range = stack.get_float(3);
+
+	world->sound_world().set_sound_range(id, range);
+
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int sound_world_set_sound_volume(lua_State* L)
+{
+	LuaStack stack(L);
+
+	World* world = (World*) stack.get_lightdata(1);
+
+	SoundInstanceId id = {0};
+	id.value = stack.get_int(2);
+	float vol = stack.get_float(2);
+
+	world->sound_world().set_sound_volume(id, vol);
+
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+void load_sound(LuaEnvironment& env)
+{
+	env.load_module_function("Sound", "play_sound",	sound_world_play_sound);
+	env.load_module_function("Sound", "pause_sound", sound_world_pause_sound);
+	env.load_module_function("Sound", "set_sound_range", sound_world_set_sound_range);
+	env.load_module_function("Sound", "set_sound_volume", sound_world_set_sound_volume);
+}
+
+} // namespace crown

+ 19 - 0
engine/resource/PackageResource.h

@@ -42,6 +42,8 @@ struct PackageHeader
 	uint32_t textures_offset;
 	uint32_t textures_offset;
 	uint32_t num_scripts;
 	uint32_t num_scripts;
 	uint32_t scripts_offset;
 	uint32_t scripts_offset;
+	uint32_t num_sounds;
+	uint32_t sounds_offset;
 };
 };
 
 
 class PackageResource
 class PackageResource
@@ -96,6 +98,14 @@ public:
 		return ((PackageHeader*)m_data)->num_scripts;
 		return ((PackageHeader*)m_data)->num_scripts;
 	}
 	}
 
 
+	//-----------------------------------------------------------------------------
+	uint32_t num_sounds() const
+	{
+		CE_ASSERT_NOT_NULL(m_data);
+
+		return ((PackageHeader*)m_data)->num_textures;
+	}
+
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	ResourceId get_texture_id(uint32_t i) const
 	ResourceId get_texture_id(uint32_t i) const
 	{
 	{
@@ -114,6 +124,15 @@ public:
 		return begin[i];
 		return begin[i];
 	}
 	}
 
 
+	//-----------------------------------------------------------------------------
+	ResourceId get_sound_id(uint32_t i) const
+	{
+		CE_ASSERT(i < num_scripts(), "Index out of bounds");
+
+		ResourceId* begin = (ResourceId*) (m_data + ((PackageHeader*)m_data)->sounds_offset);
+		return begin[i];
+	}
+
 private:
 private:
 
 
 	char* m_data;
 	char* m_data;

+ 12 - 1
engine/resource/ResourcePackage.h

@@ -30,6 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "ResourceManager.h"
 #include "ResourceManager.h"
 #include "PackageResource.h"
 #include "PackageResource.h"
 #include "Resource.h"
 #include "Resource.h"
+#include "Log.h"
 
 
 namespace crown
 namespace crown
 {
 {
@@ -61,6 +62,11 @@ public:
 		{
 		{
 			m_resource_manager->load(LUA_TYPE, m_package->get_script_id(i));
 			m_resource_manager->load(LUA_TYPE, m_package->get_script_id(i));
 		}
 		}
+
+		for (uint32_t i = 0; i < m_package->num_sounds(); i++)
+		{
+			m_resource_manager->load(SOUND_TYPE, m_package->get_sound_id(i));
+		}
 	}
 	}
 
 
 	/// Unloads all the resources in the package.
 	/// Unloads all the resources in the package.
@@ -74,7 +80,12 @@ public:
 		for (uint32_t i = 0; i < m_package->num_scripts(); i++)
 		for (uint32_t i = 0; i < m_package->num_scripts(); i++)
 		{
 		{
 			m_resource_manager->unload(m_package->get_script_id(i));
 			m_resource_manager->unload(m_package->get_script_id(i));
-		}		
+		}
+
+		for (uint32_t i = 0; i < m_package->num_sounds(); i++)
+		{
+			m_resource_manager->unload(m_package->get_sound_id(i));
+		}	
 	}
 	}
 
 
 	/// Waits until the package has been loaded. 
 	/// Waits until the package has been loaded. 

+ 3 - 3
engine/tests/CMakeLists.txt

@@ -9,7 +9,7 @@ project(crown-tests)
 #add_executable(paths paths.cpp)
 #add_executable(paths paths.cpp)
 #add_executable(dynamic-strings dynamic-strings.cpp)
 #add_executable(dynamic-strings dynamic-strings.cpp)
 #add_executable(json json.cpp)
 #add_executable(json json.cpp)
-add_executable(events events.cpp)
+#add_executable(events events.cpp)
 
 
 
 
 #target_link_libraries(allocators crown)
 #target_link_libraries(allocators crown)
@@ -19,7 +19,7 @@ add_executable(events events.cpp)
 #target_link_libraries(paths crown)
 #target_link_libraries(paths crown)
 #target_link_libraries(dynamic-strings crown)
 #target_link_libraries(dynamic-strings crown)
 #target_link_libraries(json crown)
 #target_link_libraries(json crown)
-target_link_libraries(events crown)
+#target_link_libraries(events crown)
 
 
 
 
 #install (TARGETS allocators DESTINATION test)
 #install (TARGETS allocators DESTINATION test)
@@ -29,5 +29,5 @@ target_link_libraries(events crown)
 #install (TARGETS paths DESTINATION test)
 #install (TARGETS paths DESTINATION test)
 #install (TARGETS dynamic-strings DESTINATION test)
 #install (TARGETS dynamic-strings DESTINATION test)
 #install (TARGETS json DESTINATION test)
 #install (TARGETS json DESTINATION test)
-install (TARGETS events DESTINATION test)
+#install (TARGETS events DESTINATION test)