-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b286acf
commit c9fba23
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |