MCPcopy Create free account
hub / github.com/defold/defold / CompSoundUpdate

Function CompSoundUpdate

engine/gamesys/src/gamesys/components/comp_sound.cpp:220–279  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

218 }
219
220 static dmGameObject::UpdateResult CompSoundUpdate(const dmGameObject::ComponentsUpdateParams& params, dmGameObject::ComponentsUpdateResult&)
221 {
222 dmGameObject::UpdateResult update_result = dmGameObject::UPDATE_RESULT_OK;
223 SoundWorld* world = (SoundWorld*)params.m_World;
224 DM_PROPERTY_ADD_U32(rmtp_Sound, world->m_Components.Size());
225 for (uint32_t i = 0; i < world->m_Entries.Size(); ++i)
226 {
227 PlayEntry& entry = world->m_Entries[i];
228 if (!entry.m_SoundInstance)
229 continue;
230
231 DM_PROPERTY_ADD_U32(rmtp_SoundPlaying, 1);
232 float prev_delay = entry.m_Delay;
233 entry.m_Delay -= params.m_UpdateContext->m_DT;
234
235 if (entry.m_Delay < 0.0f)
236 {
237 if (prev_delay >= 0.0f)
238 {
239 dmSound::Result r = dmSound::Play(entry.m_SoundInstance);
240 if (r != dmSound::RESULT_OK)
241 {
242 dmLogError("Error playing sound: (%d)", r);
243 update_result = dmGameObject::UPDATE_RESULT_UNKNOWN_ERROR;
244 // IsPlaying will hopefully and eventually be true
245 // so that the instance can be removed
246 }
247 }
248 else if (!dmSound::IsPlaying(entry.m_SoundInstance) && !(entry.m_PauseRequested || entry.m_Paused))
249 {
250 update_result = HandleEntryFinishedPlaying(world, entry, i);
251 }
252 else if (entry.m_PauseRequested)
253 {
254 entry.m_PauseRequested = 0;
255 dmSound::Result r = dmSound::Pause(entry.m_SoundInstance, (bool)entry.m_Paused);
256 if (r != dmSound::RESULT_OK)
257 {
258 dmLogError("Error pausing sound: (%d)", r);
259 update_result = dmGameObject::UPDATE_RESULT_UNKNOWN_ERROR;
260 }
261 }
262 else if (entry.m_StopRequested)
263 {
264 dmSound::Result r = dmSound::Stop(entry.m_SoundInstance);
265 if (r != dmSound::RESULT_OK)
266 {
267 dmLogError("Error deleting sound: (%d)", r);
268 update_result = dmGameObject::UPDATE_RESULT_UNKNOWN_ERROR;
269 }
270 }
271 }
272 else if (entry.m_StopRequested)
273 {
274 // If a stop was requested before we started playing, we can remove it immediately and dispatch the callback
275 update_result = HandleEntryFinishedPlaying(world, entry, i);
276 }
277 }

Callers

nothing calls this directly

Calls 6

PlayFunction · 0.50
IsPlayingFunction · 0.50
PauseFunction · 0.50
StopFunction · 0.50
SizeMethod · 0.45

Tested by

no test coverage detected