Bladeren bron

add SoundWorld and make some changes to ResourceManager

mikymod 12 jaren geleden
bovenliggende
commit
57c04a305a

+ 3 - 1
engine/CMakeLists.txt

@@ -76,6 +76,7 @@ set (SRC
 	World.cpp
 	Unit.cpp
 	SceneGraph.cpp
+	SoundWorld.cpp
 )
 
 set (HEADERS
@@ -85,7 +86,8 @@ set (HEADERS
 	Device.h
 	World.h
 	Unit.h
-	SceneGraph.h	
+	SceneGraph.h
+	SoundWorld.h
 )
 
 set (CORE_SRC

+ 57 - 0
engine/SoundWorld.cpp

@@ -0,0 +1,57 @@
+/*
+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 "SoundWorld.h"
+#include "Device.h"
+#include "ResourceManager.h"
+#include "SoundRenderer.h"
+#include "SoundResource.h"
+#include "Assert.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+SoundInstanceId SoundWorld::create_sound(const char* name)
+{
+	SoundInstanceId id = m_sound_table.create();
+
+	SoundResource* sound = (SoundResource*)device()->resource_manager()->lookup("sounds", name);
+
+	m_sound[id.index].m_sound = sound->m_id;
+
+	return id;
+}
+
+//-----------------------------------------------------------------------------
+void SoundWorld::destroy_sound(SoundInstanceId sound)
+{
+	CE_ASSERT(m_sound_table.has(sound), "SoundInstance does not exists");
+
+	// Stub
+}
+
+} // namespace crown

+ 54 - 0
engine/SoundWorld.h

@@ -0,0 +1,54 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "IdTable.h"
+#include "SoundRenderer.h"
+
+namespace crown
+{
+
+typedef Id SoundInstanceId;
+
+struct SoundInstance
+{
+	SoundId m_sound;
+};
+
+class SoundWorld
+{
+
+	SoundInstanceId				create_sound(const char* name);
+	void						destroy_sound(SoundInstanceId sound);
+
+private:
+
+	IdTable<MAX_SOUNDS> 		m_sound_table;
+	SoundInstance				m_sound[MAX_SOUNDS];
+};
+
+} // namespace crown

+ 20 - 0
engine/World.cpp

@@ -26,21 +26,41 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Assert.h"
 #include "World.h"
+#include "Allocator.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
 World::World()
+	: m_allocator(default_allocator(), 1048576)
+	, m_is_init(false)
+	, m_sound_world(NULL)
 {
 }
 
+//-----------------------------------------------------------------------------
+void World::init()
+{
+	m_sound_world = CE_NEW(m_allocator, SoundWorld);
+}
+
+//-----------------------------------------------------------------------------
+void World::shutdown()
+{
+	if (m_sound_world)
+	{
+		CE_DELETE(m_allocator, m_sound_world);
+	}
+}
+
 //-----------------------------------------------------------------------------
 UnitId World::spawn_unit(const char* /*name*/, const Vec3& pos, const Quat& rot)
 {
 	const UnitId unit = m_unit_table.create();
 
 	m_units[unit.index].create(pos, rot);
+
 	// m_units[unit.index].load(ur);
 
 	return unit;

+ 11 - 1
engine/World.h

@@ -30,6 +30,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "HeapAllocator.h"
 #include "IdTable.h"
 #include "Unit.h"
+#include "SoundWorld.h"
+#include "LinearAllocator.h"
 
 namespace crown
 {
@@ -42,9 +44,11 @@ class Quat;
 class World
 {
 public:
-	
 					World();
 
+	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);
 
@@ -57,8 +61,14 @@ public:
 
 private:
 
+	LinearAllocator		m_allocator;
+
+	bool				m_is_init :1;
+
 	IdTable<MAX_UNITS> 	m_unit_table;
 	Unit				m_units[MAX_UNITS];
+
+	SoundWorld*			m_sound_world;
 };
 
 } // namespace crown

+ 1 - 1
engine/audio/SoundRenderer.h

@@ -39,7 +39,7 @@ typedef 	Id 		SoundId;
 
 //-----------------------------------------------------------------------------
 class SoundRendererImpl;
-	class SoundResource;
+class SoundResource;
 class Vec3;
 
 //-----------------------------------------------------------------------------

+ 11 - 0
engine/resource/ResourceManager.cpp

@@ -71,6 +71,7 @@ void ResourceManager::unload(ResourceId name)
 	
 	if (entry->references == 0)
 	{
+		resource_on_offline(entry->type, entry->resource);
 		resource_on_unload(entry->type, m_resource_heap, entry->resource);
 
 		// Swap with last
@@ -221,4 +222,14 @@ void ResourceManager::online(ResourceId name, void* resource)
 	entry->resource = resource;
 }
 
+//-----------------------------------------------------------------------------
+void ResourceManager::offline(ResourceId name, void* resource)
+{
+	ResourceEntry* entry = find(name);
+	resource_on_offline(entry->type, resource);
+
+	entry->resource = resource;
+}
+
+
 } // namespace crown

+ 2 - 0
engine/resource/ResourceManager.h

@@ -114,6 +114,8 @@ private:
 	ResourceId				load(uint32_t type, ResourceId name);
 	void					online(ResourceId name, void* resource);
 
+	void					offline(ResourceId name, void* resource);
+
 private:
 
 	ProxyAllocator			m_resource_heap;

+ 15 - 4
engine/resource/SoundResource.h

@@ -31,6 +31,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Bundle.h"
 #include "Allocator.h"
 #include "File.h"
+#include "Device.h"
+#include "SoundRenderer.h"
 
 namespace crown
 {
@@ -89,6 +91,12 @@ public:
 	static void online(void* resource)
 	{
 		CE_ASSERT(resource != NULL, "Resource not loaded");
+
+		SoundResource* sound = (SoundResource*)resource;
+
+		SoundId id = device()->sound_renderer()->create_sound(sound);
+
+		sound->m_id = id;
 	}
 
 	//-----------------------------------------------------------------------------
@@ -101,9 +109,13 @@ public:
 	}
 
 	//-----------------------------------------------------------------------------
-	static void offline(void* /*resource*/)
+	static void offline(void* resource)
 	{
+		CE_ASSERT(resource != NULL, "Resource not loaded");
 
+		SoundResource* sound = (SoundResource*)resource;
+
+		device()->sound_renderer()->destroy_sound(sound->m_id);
 	}
 
 public:
@@ -114,16 +126,15 @@ public:
 	uint32_t			channels() const { return m_header.channels; }
 	uint16_t			block_size() const { return m_header.block_size; }
 	uint16_t			bits_ps() const { return m_header.bits_ps; }
-
 	uint8_t				sound_type() const { return m_header.sound_type; }
 
 	const uint8_t*		data() const { return m_data; }
 
-private:
+public:
 
 	SoundHeader		m_header;
 	uint8_t*		m_data;
-
+	SoundId			m_id;
 };
 
 } // namespace crown