Procházet zdrojové kódy

Add SoundWorld::is_playing()

Daniele Bartolini před 12 roky
rodič
revize
6dcfe417bd

+ 3 - 0
engine/audio/SoundWorld.h

@@ -64,6 +64,9 @@ public:
 	/// After this call, the instance will be destroyed.
 	virtual void stop(SoundInstanceId id) = 0;
 
+	/// Returns wheter the sound @a id is playing.
+	virtual bool is_playing(SoundInstanceId id) = 0;
+
 	/// Stops all the sounds in the world.
 	virtual void stop_all() = 0;
 

+ 12 - 0
engine/audio/backend/ALSoundWorld.cpp

@@ -172,6 +172,13 @@ struct SoundInstance
 		}
 	}
 
+	bool is_playing()
+	{
+		ALint state;
+		AL_CHECK(alGetSourcei(m_source, AL_SOURCE_STATE, &state));
+		return state == AL_PLAYING;
+	}
+
 	bool finished()
 	{
 		ALint state;
@@ -249,6 +256,11 @@ public:
 		m_playing_sounds.destroy(id);
 	}
 
+	virtual bool is_playing(SoundInstanceId id)
+	{
+		return m_playing_sounds.has(id) && m_playing_sounds.lookup(id).is_playing();
+	}
+
 	virtual void stop_all()
 	{
 		for (uint32_t i = 0; i < m_playing_sounds.size(); i++)

+ 12 - 0
engine/audio/backend/SLESSoundWorld.cpp

@@ -287,6 +287,13 @@ struct SoundInstance
 		SL_CHECK((*play_itf())->SetPlayState(play_itf(), SL_PLAYSTATE_STOPPED));
 	}
 
+	bool is_playing()
+	{
+		SLuint32 state;
+		SL_CHECK((*play_itf())->GetPlayState(play_itf(), &state));
+		return state == SL_PLAYSTATE_PLAYING;	
+	}
+
 	bool finished()
 	{
 		return m_finished;
@@ -393,6 +400,11 @@ public:
 		m_playing_sounds.destroy(id);
 	}
 
+	virtual bool is_playing(SoundInstanceId id)
+	{
+		return m_playing_sounds.has(id) && m_playing_sounds.lookup(id).is_playing();
+	}
+
 	virtual void stop_all()
 	{
 		for (uint32_t i = 0; i < m_playing_sounds.size(); i++)

+ 13 - 0
engine/lua/LuaSoundWorld.cpp

@@ -51,6 +51,7 @@ CE_EXPORT int sound_world_pause_all(lua_State* L)
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
 CE_EXPORT int sound_world_resume_all(lua_State* L)
 {
 	LuaStack stack(L);
@@ -60,12 +61,24 @@ CE_EXPORT int sound_world_resume_all(lua_State* L)
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
+CE_EXPORT int sound_world_is_playing(lua_State* L)
+{
+	LuaStack stack(L);
+
+	SoundWorld* sw = stack.get_sound_world(1);
+	SoundInstanceId id = stack.get_sound_instance_id(2);
+	stack.push_bool(sw->is_playing(id));
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 void load_sound_world(LuaEnvironment& env)
 {
 	env.load_module_function("SoundWorld", "stop_all",    sound_world_stop_all);
 	env.load_module_function("SoundWorld", "pause_all",   sound_world_pause_all);
 	env.load_module_function("SoundWorld", "resume_all",  sound_world_resume_all);
+	env.load_module_function("SoundWorld", "is_playing",  sound_world_is_playing);
 }
 
 } // namespace crown