Explorar o código

rename Sound to Source

mikymod %!s(int64=12) %!d(string=hai) anos
pai
achega
5a78a451c4
Modificáronse 2 ficheiros con 110 adicións e 92 borrados
  1. 75 50
      engine/renderers/al/ALRenderer.cpp
  2. 35 42
      engine/renderers/al/ALRenderer.h

+ 75 - 50
engine/renderers/al/ALRenderer.cpp

@@ -57,7 +57,8 @@ static const char* al_error_to_string(ALenum error)
 
 //-----------------------------------------------------------------------------
 ALRenderer::ALRenderer() :
-	m_sounds_id_table(m_allocator, MAX_SOUNDS)
+	m_buffers_id_table(m_allocator, MAX_BUFFERS),
+	m_sources_id_table(m_allocator, MAX_SOURCES)
 {
 
 }
@@ -80,13 +81,6 @@ void ALRenderer::init()
 	}
 
 	AL_CHECK(alcMakeContextCurrent(m_context));
-
-	ALfloat dir[] = {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f};
-
-	AL_CHECK(alListener3f(AL_POSITION, 0, 0, 1.0f));
-	AL_CHECK(alListener3f(AL_VELOCITY, 0, 0, 0));
-	AL_CHECK(alListenerfv(AL_ORIENTATION, dir));
-
 }
 
 //-----------------------------------------------------------------------------
@@ -97,14 +91,32 @@ void ALRenderer::shutdown()
 }
 
 //-----------------------------------------------------------------------------
-SoundId ALRenderer::create_sound(const void* data, uint32_t size, uint32_t sample_rate, uint32_t channels, uint32_t bxs)
+SoundListener create_listener(float gain, Vec3 position, Vec3 velocity, Vec3 orientation_up, Vec3 orientation_at)
+{
+	AL_CHECK(alListener3f(AL_POSITION, position.x, position.y, position.z));
+	AL_CHECK(alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z));
+
+	ALfloat orientation[] = {	
+								orientation_up.x, 
+								orientation_up.y, 
+								orientation_up.z,
+								orientation_at.x, 
+								orientation_at.y, 
+								orientation_at.z
+							};
+
+	AL_CHECK(alListenerfv(AL_ORIENTATION, orientation));
+}
+
+//-----------------------------------------------------------------------------
+SoundBufferId ALRenderer::create_buffer(const void* data, uint32_t size, uint32_t sample_rate, uint32_t channels, uint32_t bxs)
 {
-	SoundId id = m_sounds_id_table.create();
+	SoundBufferId id = m_buffers_id_table.create();
 
-	Sound& al_sound = m_sounds[id.index];
+	SoundBuffer& al_buffer = m_buffers[id.index];
 
 	// Generates AL buffer
-	AL_CHECK(alGenBuffers(1, &al_sound.bufferid));
+	AL_CHECK(alGenBuffers(1, &al_buffer.id));
 
 	bool stereo = (channels > 1);
 
@@ -115,11 +127,11 @@ SoundId ALRenderer::create_sound(const void* data, uint32_t size, uint32_t sampl
 		{
 			if (stereo)
 			{
-				al_sound.format = AL_FORMAT_STEREO8;
+				al_buffer.format = AL_FORMAT_STEREO8;
 			}
 			else
 			{
-				al_sound.format = AL_FORMAT_MONO8;
+				al_buffer.format = AL_FORMAT_MONO8;
 			}
 
 			break;
@@ -129,11 +141,11 @@ SoundId ALRenderer::create_sound(const void* data, uint32_t size, uint32_t sampl
 		{
 			if (stereo)
 			{
-				al_sound.format = AL_FORMAT_STEREO16;
+				al_buffer.format = AL_FORMAT_STEREO16;
 			}
 			else
 			{
-				al_sound.format = AL_FORMAT_MONO16;
+				al_buffer.format = AL_FORMAT_MONO16;
 			}
 
 			break;
@@ -147,84 +159,97 @@ SoundId ALRenderer::create_sound(const void* data, uint32_t size, uint32_t sampl
 	}
 
 	// Sets sound's size
-	al_sound.size = size;
+	al_buffer.size = size;
 
 	// Sets sound's frequency
-	al_sound.freq = sample_rate;
+	al_buffer.freq = sample_rate;
 
 	// Fills AL buffer
-	AL_CHECK(alBufferData(al_sound.bufferid, al_sound.format, data, al_sound.size, al_sound.freq));
+	AL_CHECK(alBufferData(al_buffer.id, al_buffer.format, data, al_buffer.size, al_buffer.freq));
+}
+
+//-----------------------------------------------------------------------------
+void ALRenderer::destroy_buffer(SoundBufferId id)
+{
+	CE_ASSERT(m_buffers_id_table.has(id), "SoundBuffer does not exist");
+
+	SoundBuffer& al_buffer = m_buffers[id.index];
+
+	AL_CHECK(alDeleteBuffers(1, &al_buffer.id));
+}
+
+//-----------------------------------------------------------------------------
+SoundSourceId ALRenderer::create_source(Vec3 position, Vec3 velocity, Vec3 direction)
+{
+	SoundSourceId id = m_sources_id_table.create();
+
+	SoundSource& al_source = m_sources[id.index];
 
 	// Creates AL source
-	AL_CHECK(alGenSources(1, &al_sound.sourceid));
+	AL_CHECK(alGenSources(1, &al_source.id));
 
-	// Sets tmp source's properties
-    AL_CHECK(alSourcef(al_sound.sourceid, AL_PITCH, 1));
+    AL_CHECK(alSourcef(al_source.id, AL_GAIN, 1));
 
-    AL_CHECK(alSourcef(al_sound.sourceid, AL_GAIN, 1));
+    AL_CHECK(alSourcef(al_source.id, AL_MIN_GAIN, 0.0f));
 
-	AL_CHECK(alSource3f(al_sound.sourceid, AL_POSITION, 0, 0, 0));
+    AL_CHECK(alSourcef(al_source.id, AL_MAX_GAIN, 1.0f));
 
-	AL_CHECK(alSourcei(al_sound.sourceid, AL_LOOPING, AL_FALSE));
+	AL_CHECK(alSource3f(al_source.id, AL_POSITION, position.x, position.y, position.z));
 
+	AL_CHECK(alSource3f(al_source.id, AL_VELOCITY, velocity.x, velocity.y, velocity.z));
 
-	// Binds buffer to sources
-	AL_CHECK(alSourcei(al_sound.sourceid, AL_BUFFER, al_sound.bufferid));
+	AL_CHECK(alSource3f(al_source.id, AL_DIRECTION, direction.x, direction.y, direction.z));
 
+	AL_CHECK(alSourcei(al_source.id, AL_LOOPING, AL_FALSE));
 
 	return id;
 }
 
 //-----------------------------------------------------------------------------
-void ALRenderer::play_sound(SoundId id)
+void ALRenderer::play_source(SoundSourceId id)
 {
-	CE_ASSERT(m_sounds_id_table.has(id), "Sound does not exist");
+	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
 
-	Sound& al_sound = m_sounds[id.index];
+	SoundSource& al_source = m_sources[id.index];
 
-	AL_CHECK(alSourcePlay(al_sound.sourceid));
+	AL_CHECK(alSourcePlay(al_source.id));
 }
 
 //-----------------------------------------------------------------------------
-void ALRenderer::pause_sound(SoundId id)
+void ALRenderer::pause_source(SoundSourceId id)
 {
-	CE_ASSERT(m_sounds_id_table.has(id), "Sound does not exist");
+	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
 
-	Sound& al_sound = m_sounds[id.index];
+	SoundSource& al_source = m_sources[id.index];
 
-	if (is_sound_playing(id))
+	if (is_source_playing(id))
 	{
-		AL_CHECK(alSourcePause(al_sound.sourceid));
+		AL_CHECK(alSourcePause(al_source.id));
 	}
 }
 
 //-----------------------------------------------------------------------------
-void ALRenderer::destroy_sound(SoundId id)
+void ALRenderer::destroy_source(SoundSourceId id)
 {
-	CE_ASSERT(m_sounds_id_table.has(id), "Sound does not exist");
+	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
 
-	Sound& al_sound = m_sounds[id.index];
+	SoundSource& al_source = m_sources[id.index];
 
-	// alDeleteSources(1, al_sound.sourceid);
-	// alDeleteBuffers(1, al_sound.bufferid);
-
-	
-
-	// Must be implemented
+	alDeleteSources(1, &al_source.id);
 }
 
+
 //-----------------------------------------------------------------------------
-bool ALRenderer::is_sound_playing(SoundId id)
+bool ALRenderer::is_source_playing(SoundSourceId id)
 {
-	CE_ASSERT(m_sounds_id_table.has(id), "Sound does not exist");
+	CE_ASSERT(m_sources_id_table.has(id), "SoundSource does not exist");
 
-	Sound& al_sound = m_sounds[id.index];
+	SoundSource& al_source = m_sources[id.index];
 
 	ALint source_state;
-	alGetSourcei(al_sound.sourceid, AL_SOURCE_STATE, &source_state);
+	alGetSourcei(al_source.id, AL_SOURCE_STATE, &source_state);
 
 	return source_state == AL_PLAYING;
 }
 
-
 } // namespace crown

+ 35 - 42
engine/renderers/al/ALRenderer.h

@@ -39,9 +39,11 @@ namespace crown
 {
 
 typedef Id SoundId;
+typedef Id SoundBufferId;
+typedef Id SoundSourceId;
 
 //-----------------------------------------------------------------------------
-struct Listener
+struct SoundListener
 {
 	ALfloat gain;
 	ALfloat position[3];
@@ -50,76 +52,67 @@ struct Listener
 };
 
 //-----------------------------------------------------------------------------
-struct Buffer
+struct SoundBuffer
 {
-	ALuint 		bufferid;
+	ALuint 		id;
+	
 	ALenum 		format;
 	ALsizei		size;
 	ALsizei		freq;
 };
 
-
 //-----------------------------------------------------------------------------
-struct Source
+struct SoundSource
 {
 	ALuint		id;
-};
 
-//-----------------------------------------------------------------------------
-struct Sound
-{
-	Source 		source;
-	Buffer 		buffer;		// Should be an array of buffer
+	ALfloat		pos[3];
+	ALfloat		vel[3];
+	ALfloat		dir[3];
 };
 
+
+
 //-----------------------------------------------------------------------------
 class ALRenderer
 {
 public:
 
-					ALRenderer();
-
-	void			init();
-	void			shutdown();
-
-	// FIXME: public APIs, They should be placed in AudioSystem
-	SoundId			create_sound(const void* data, uint32_t size, uint32_t sample_rate, uint32_t channels, uint32_t bxs);
-	void 			play_sound(SoundId id);
-	void			pause_sound(SoundId id);
-	void 			destroy_sound(SoundId id);
-
-	bool			is_sound_playing(SoundId id);
+	static const uint32_t	MAX_SOURCES = 128;
+	static const uint32_t	MAX_BUFFERS_PER_SOURCES = 16;
+	static const uint32_t 	MAX_BUFFERS = MAX_SOURCES * MAX_BUFFERS_PER_SOURCES;
 
 public:
 
-	static const uint32_t MAX_SOUNDS = 128;
-	// END
+							ALRenderer();
 
-private:
+	void					init();
+	void					shutdown();
+
+	SoundListener			create_listener(float gain, Vec3 position, Vec3 velocity, Vec3 orientation_up, Vec3 orientation_at);
 
-	// FIXME: the following methods are the real wrapper of needed OpenAL functions
-	Buffer			create_buffer(const void* data, uint32_t size, uint32_t sample_rate, uint32_t channels, uint32_t bxs);
-	void			destroy_buffer(Buffer b);
+	SoundBufferId			create_buffer(const void* data, uint32_t size, uint32_t sample_rate, uint32_t channels, uint32_t bxs);
+	void					destroy_buffer(SoundBufferId id);
 
-	Source			create_source();
-	void			play_source(Source s);
-	void			pause_source(Source s);
-	void			destroy_source(Source s);
-	bool			is_source_playing(Source s);
+	SoundSourceId			create_source(Vec3 position, Vec3 velocity, Vec3 direction);
+	void 					play_source(SoundSourceId id);
+	void					pause_source(SoundSourceId id);
+	void 					destroy_source(SoundSourceId id);
 
-	Listener		create_listener();
-	void			destroy_listener(Listener);
-	// END
+	bool					is_source_playing(SoundSourceId id);
 
 private:
 
-	HeapAllocator 	m_allocator;
+	HeapAllocator 			m_allocator;
+
+	ALCdevice*				m_device;
+	ALCcontext*				m_context;
 
-	ALCdevice*		m_device;
-	ALCcontext*		m_context;
+	IdTable 				m_buffers_id_table;
+	SoundBuffer 			m_buffers[MAX_BUFFERS];
 
-	IdTable			m_sounds_id_table;
-	Sound 			m_sounds[MAX_SOUNDS];
+	IdTable 				m_sources_id_table;
+	SoundSource 			m_sources[MAX_SOURCES];
 };
 
 } // namespace crown