#define OLC_PGE_APPLICATION #include "olcPixelGameEngine.h" #define OLC_SOUNDWAVE #include "olcSoundWaveEngine.h" using namespace olc; enum SoundID { SONG, HERO, SIGG }; class CustomSound : public olc::PixelGameEngine { public: CustomSound() { sAppName = "Custom GameSound System based on olcSoundWaveEngine"; } sound::WaveEngine swe; sound::Wave song,hero,sigg; struct GameSound { sound::PlayingWave sound; SoundID id; double duration; float playTime; bool paused = false; }; std::vectorplayingSounds; void PlayWaveform(SoundID id, sound::Wave* pWave, bool bLoop=false, double dSpeed=1.0) { sound::PlayingWave playing_sound = swe.PlayWaveform(pWave, bLoop, dSpeed); playingSounds.push_back({ playing_sound,id,playing_sound->dDuration }); } void PauseSound(GameSound& sound) { sound.paused = true; sound::WaveInstance& wi = *sound.sound; wi.bPaused = true; } void UnpauseSound(GameSound& sound) { sound.paused = false; sound::WaveInstance& wi = *sound.sound; wi.bPaused = false; } void StopSound(SoundID id) { for (std::vector::iterator it = playingSounds.begin(); it != playingSounds.end();) { GameSound& sound = *it; if (sound.id == id) { swe.StopWaveform(sound.sound); it=playingSounds.erase(it); if (it == playingSounds.end()) { break; } else { continue; } } ++it; } } public: bool OnUserCreate() override { // Called once at the start, so create things here //engine = &swe; swe.InitialiseAudio(); song = sound::Wave("WeatherChannel.wav"); hero = sound::Wave("Hero.wav"); sigg = sound::Wave("Sigg.wav"); return true; } bool OnUserUpdate(float fElapsedTime) override { if (GetKey(A).bPressed) { StopSound(SONG); PlayWaveform(SONG,&song,true); } if (GetKey(S).bPressed) { PlayWaveform(HERO, &hero, false); } if (GetKey(D).bPressed) { PlayWaveform(SIGG, &sigg, false); } if (GetKey(SPACE).bPressed) { StopSound(HERO); StopSound(SIGG); } if (GetKey(P).bPressed) { for (std::vector::iterator it = playingSounds.begin(); it != playingSounds.end(); ++it) { GameSound& sound = *it; PauseSound(sound); } } if (GetKey(U).bPressed) { for (std::vector::iterator it = playingSounds.begin(); it != playingSounds.end(); ++it) { GameSound& sound = *it; UnpauseSound(sound); } } for (std::vector::iterator it = playingSounds.begin(); it != playingSounds.end(); ++it) { GameSound& sound = *it; if (!sound.paused) { sound.playTime += fElapsedTime; if (sound.playTime > sound.duration) { it = playingSounds.erase(it); if (it == playingSounds.end()) { break; } } } } // called once per frame return true; } }; int main() { CustomSound demo; if (demo.Construct(256, 240, 4, 4)) demo.Start(); return 0; }