Added support for panning sound.

CorrectiveAction
sigonasr2 1 year ago
parent 7dcab92739
commit 578eac6811
  1. 26
      olcCodeJam2023Entry/VirusAttack.cpp
  2. 4
      olcCodeJam2023Entry/VirusAttack.h
  3. 17
      olcCodeJam2023Entry/olcPGEX_AudioListener.h
  4. 11
      olcCodeJam2023Entry/olcPGEX_AudioSource.h

@ -51,8 +51,7 @@ bool VirusAttack::OnUserCreate(){
IMAGES[MATRIX]->Sprite()->SetSampleMode(Sprite::PERIODIC); IMAGES[MATRIX]->Sprite()->SetSampleMode(Sprite::PERIODIC);
AL.AudioSystemInit(); AL.AudioSystemInit();
AS_Test.AL = &AL; InitializeSounds();
AS_Test.LoadAudioSample(0, "./assets/test.wav");
units.push_back(std::make_unique<LeftShifter>(vf2d{128,128},IMAGES,true)); units.push_back(std::make_unique<LeftShifter>(vf2d{128,128},IMAGES,true));
units.push_back(std::make_unique<RightShifter>(vf2d{129,129},IMAGES,true)); units.push_back(std::make_unique<RightShifter>(vf2d{129,129},IMAGES,true));
@ -76,6 +75,21 @@ bool VirusAttack::OnUserCreate(){
return true; return true;
} }
void VirusAttack::InitializeSounds(){
int soundIndex=0;
auto LoadSound=[&](Audio&audio,std::string soundFilename){
audio.AL=&AL;
audio.LoadAudioSample(soundIndex,std::string("./assets/"+soundFilename).c_str());
soundIndex++;
};
AS_Test.SetDefaults(5,1,0,1,false);
AS_Test.fPlaySpeed=5;
LoadSound(AS_Test,"test.wav");
LoadSound(explosion,"SampleA.wav");
}
void VirusAttack::HandleDraggingSelection(){ void VirusAttack::HandleDraggingSelection(){
auto NotClickingOnMinimap=[&](){return !(GetMouseX()>=ScreenWidth()-64&&GetMouseY()>=ScreenHeight()-64);}; auto NotClickingOnMinimap=[&](){return !(GetMouseX()>=ScreenWidth()-64&&GetMouseY()>=ScreenHeight()-64);};
if(GetMouse(0).bPressed){ if(GetMouse(0).bPressed){
@ -312,9 +326,11 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
HandleRightClickMove(); HandleRightClickMove();
HandlePanAndZoom(fElapsedTime); HandlePanAndZoom(fElapsedTime);
HandleMinimapClick(); HandleMinimapClick();
AL.vecPos=game.GetWorldOffset()+GetScreenSize()/2;
if (GetKey(olc::Key::P).bPressed) AL.OnUserUpdate(fElapsedTime);
AS_Test.Play(); if (GetKey(olc::Key::P).bPressed){
AS_Test.Play(1,1);
}
for(auto&tile:TileManager::visibleTiles){ for(auto&tile:TileManager::visibleTiles){
tile.second-=fElapsedTime; tile.second-=fElapsedTime;

@ -35,7 +35,8 @@ private:
std::map<Image,std::unique_ptr<Renderable>>IMAGES; std::map<Image,std::unique_ptr<Renderable>>IMAGES;
olcPGEX_AudioListener AL; olcPGEX_AudioListener AL;
olcPGEX_AudioSource AS_Test; Audio AS_Test,explosion;
Audio*bgm;
TileTransformedView game; TileTransformedView game;
@ -58,6 +59,7 @@ private:
void UpdateMatrixTexture(float fElapsedTime); void UpdateMatrixTexture(float fElapsedTime);
void RenderCollectionPoints(CollectionPoint*cp); void RenderCollectionPoints(CollectionPoint*cp);
void RenderFogOfWar(); void RenderFogOfWar();
void InitializeSounds();
public: public:
VirusAttack(); VirusAttack();

@ -170,6 +170,12 @@ Justin Richards
#include "soloud.h" #include "soloud.h"
#include "soloud_wav.h" #include "soloud_wav.h"
struct PlayingInstance{
vf2d pos;
float vol;
int handle;
};
class olcPGEX_AudioListener : public olc::PGEX class olcPGEX_AudioListener : public olc::PGEX
{ {
public: public:
@ -201,6 +207,7 @@ public:
// Vector of Audio Samples // Vector of Audio Samples
std::vector<sAudioSample> audioSamples; std::vector<sAudioSample> audioSamples;
std::vector<PlayingInstance> handles;
std::list<SoLoud::Wav> wavs; std::list<SoLoud::Wav> wavs;
@ -220,6 +227,8 @@ public:
// Calculate distance between listener and source // Calculate distance between listener and source
float GetDistance(olc::vf2d sourcePos, bool returnRoot = true); float GetDistance(olc::vf2d sourcePos, bool returnRoot = true);
void OnUserUpdate(float fElapsedTime);
}; };
#ifdef AUDIO_LISTENER_IMPLEMENTATION #ifdef AUDIO_LISTENER_IMPLEMENTATION
@ -280,6 +289,14 @@ float olcPGEX_AudioListener::GetDistance(olc::vf2d sourcePos, bool returnRoot)
} }
void olcPGEX_AudioListener::OnUserUpdate(float fElapsedTime){
std::erase_if(handles,[&](PlayingInstance&handle){return !soloud.isValidVoiceHandle(handle.handle);});
for(PlayingInstance&handle:handles){
soloud.setPan(handle.handle,std::clamp((handle.pos.x-vecPos.x)/1024,-1.f,1.f));
soloud.setVolume(handle.handle,handle.vol*std::max(0.f,abs(1-std::min(1.0f,(GetDistance(handle.pos)/1024.f)))));
}
}
#endif // AUDIO_LISTENER_IMPLEMENTATION #endif // AUDIO_LISTENER_IMPLEMENTATION
#endif #endif

@ -101,7 +101,6 @@ public:
olc::vf2d pos = { 0.0f, 0.0f }; olc::vf2d pos = { 0.0f, 0.0f };
// Instruct Audio Listener to load this sound (if not loaded already) // Instruct Audio Listener to load this sound (if not loaded already)
void LoadAudioSample(int ID, const char* fileName); void LoadAudioSample(int ID, const char* fileName);
@ -141,17 +140,21 @@ void olcPGEX_AudioSource::Play(float speed, float vol, bool looping, bool paused
{ {
// Set parameters // Set parameters
fPlaySpeed = speed; fPlaySpeed = speed;
fVolume = vol; fVolume = vol*std::max(0.f,abs(1-std::min(1.0f,(AL->GetDistance(pos)/1024.f))));
bLooping = looping; bLooping = looping;
bPaused = paused; bPaused = paused;
// Assign a handle to this instance of the sound we are about to play // Assign a handle to this instance of the sound we are about to play
handle = AL->soloud.play(*AL->GetAudioSampleByID(nID)->wav, fVolume, 0.0f, bPaused); handle = AL->soloud.play(*AL->GetAudioSampleByID(nID)->wav, fVolume, 0.0f, bPaused);
AL->handles.push_back({pos,vol,handle});
// Set speed and looping // Set speed and looping
AL->soloud.setRelativePlaySpeed(handle, fPlaySpeed); AL->soloud.setRelativePlaySpeed(handle, fPlaySpeed);
AL->soloud.setLooping(handle, looping); AL->soloud.setLooping(handle, looping);
AL->soloud.setPan(handle,std::clamp((pos.x-AL->vecPos.x)/1024,-1.f,1.f));
// Update Play status // Update Play status
bIsPlaying = true; bIsPlaying = true;
} }
@ -221,4 +224,6 @@ void olcPGEX_AudioSource::SetDefaults(float speed, float vol, float minVol, floa
} }
#endif // AUDIO_SOURCE_IMPLEMENTATION #endif // AUDIO_SOURCE_IMPLEMENTATION
#endif #endif
typedef olcPGEX_AudioSource Audio;
Loading…
Cancel
Save