Skip to content

Commit

Permalink
add sound_queue.h
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Mar 16, 2024
1 parent b286acf commit c9fba23
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions sound/sound_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef SOUND_SOUND_QUEUE_H
#define SOUND_SOUND_QUEUE_H

struct SoundToPlay {
const char* filename_;
Effect* effect_;
int selection_;

SoundToPlay() :filename_(nullptr), effect_(nullptr) {}
explicit SoundToPlay(const char* file) : filename_(file){ }
SoundToPlay(Effect* effect, int selection = -1) : filename_(nullptr), effect_(effect), selection_(selection) {}
bool Play(BufferedWavPlayer* player) {
if (filename_) return player->PlayInCurrentDir(filename_);
effect_->Select(selection_);
player->PlayOnce(effect_);
return true;
}
bool isSet() {
return filename_ != nullptr || effect_ != nullptr;
}
};

template<int QueueLength>
class SoundQueue {
public:
bool Play(SoundToPlay p) {
if (sounds_ < QueueLength) {
queue_[sounds_++] = p;
return true;
}
return false;
}
bool Play(const char* p) {
return Play(SoundToPlay(p));
}
// Called from Loop()
void PollSoundQueue(RefPtr<BufferedWavPlayer>& player) {
if (sounds_ && (!player || !player->isPlaying())) {
if (!player) {
player = GetFreeWavPlayer();
if (!player) return;
player->set_volume_now(1.0f);
}
queue_[0].Play(player.get());
sounds_--;
for (int i = 0; i < sounds_; i++) queue_[i] = queue_[i+1];
}
}
private:
int sounds_;
SoundToPlay queue_[QueueLength];
};

#endif // SOUND_SOUND_QUEUE_H

0 comments on commit c9fba23

Please sign in to comment.