Improve playsoundsafe

This commit is contained in:
Miloslav Číž 2020-04-01 12:20:18 +02:00
parent d0f87a65fb
commit 8006a924c5
1 changed files with 10 additions and 19 deletions

25
main.c
View File

@ -259,9 +259,10 @@ struct
uint8_t currentRandom; ///< for RNG
uint8_t spriteAnimationFrame;
uint8_t explosionSoundPlayed; /**< Prevents playing too many explosion sounds
at once */
uint8_t monsterSoundPlayed;
uint8_t soundsPlayedThisFrame; /**< Each bit says whether given sound was
played this frame, prevents playing too many
sounds at once. */
RCL_RayConstraints rayConstraints;
uint8_t keyStates[SFG_KEY_COUNT]; /**< Pressed states of keys, each value
@ -443,22 +444,13 @@ uint8_t SFG_random()
void SFG_playSoundSafe(uint8_t soundIndex, uint8_t volume)
{
if (soundIndex == 2) // explosion?
{
if (!SFG_game.explosionSoundPlayed)
SFG_playSound(soundIndex,volume);
uint8_t mask = 0x01 << soundIndex;
SFG_game.explosionSoundPlayed = 1;
}
else if (soundIndex == 5) // monster sound
if (!(SFG_game.soundsPlayedThisFrame & mask))
{
if (!SFG_game.monsterSoundPlayed)
SFG_playSound(soundIndex,volume);
SFG_game.monsterSoundPlayed = 1;
SFG_game.soundsPlayedThisFrame |= mask;
}
else
SFG_playSound(soundIndex,volume);
}
/**
@ -3160,8 +3152,7 @@ void SFG_gameStepMenu()
*/
void SFG_gameStep()
{
SFG_game.explosionSoundPlayed = 0;
SFG_game.monsterSoundPlayed = 0;
SFG_game.soundsPlayedThisFrame = 0;
for (uint8_t i = 0; i < SFG_KEY_COUNT; ++i)
if (!SFG_keyPressed(i))