Add sounds

This commit is contained in:
2024-02-10 15:03:39 +01:00
parent 6eddf11cfa
commit 20af3967a8
22 changed files with 88 additions and 14 deletions

View File

@@ -4,8 +4,10 @@ void soundsApplyVolume(SoundState *sounds, f32 master, f32 music, f32 sound) {
SetMasterVolume(master);
if (sounds->musicLoaded)
SetMusicVolume(sounds->music, music);
if (IsSoundPlaying(sounds->playing))
SetSoundVolume(sounds->playing, sound);
for (i32 i = 0; i < SOUND_MAX_PLAYING; i++) {
if (IsSoundPlaying(sounds->playing[i]))
SetSoundVolume(sounds->playing[i], sound);
}
sounds->masterVolume = master;
sounds->musicVolume = music;
@@ -16,11 +18,33 @@ void soundsLoad(SoundState *sounds, SoundType type, f32 interval, const char *pa
Sound newSound = LoadSound(path);
sounds->sounds[type] = newSound;
sounds->soundInterval[type] = interval;
sounds->playing = newSound;
}
void soundsPlay(SoundState *sounds, SoundType type) {
PlaySound(sounds->playing);
SetSoundVolume(sounds->playing, sounds->soundVolume);
bool soundsPlay(SoundState *sounds, SoundType type) {
f32 time = GetTime();
f32 lastPlayedTime = sounds->soundLastPlayed[type];
if (time - lastPlayedTime < sounds->soundInterval[type])
return false;
i32 freeSlot = -1;
for (i32 i = 0; i < SOUND_MAX_PLAYING; i++) {
if (IsSoundPlaying(sounds->playing[i])) {
freeSlot = i;
break;
}
}
if (freeSlot == -1) {
return false;
}
PlaySound(sounds->playing[freeSlot]);
SetSoundVolume(sounds->playing[freeSlot], sounds->soundVolume);
sounds->soundLastPlayed[type] = time;
return true;
}
bool soundsPosPlay(SoundState *sounds, Vector2 position, SoundType type) {
if (!CheckCollisionPointRec(position, sounds->cameraBounds))
return false;
return soundsPlay(sounds, type);
}
void soundsUnloadAll(SoundState *sounds) {
@@ -29,10 +53,11 @@ void soundsUnloadAll(SoundState *sounds) {
}
}
void soundsUpdate(SoundState *sounds) {
void soundsUpdate(SoundState *sounds, Rectangle cameraBounds) {
if (sounds->musicLoaded == true) {
UpdateMusicStream(sounds->music);
}
sounds->cameraBounds = cameraBounds;
}
void soundsLoadMusicStream(SoundState *sounds, const char *path) {
soundsUnloadMusicStream(sounds);