SoundSystem.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "SoundSystem.h"
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <windows.h>
  5. SoundSystem::SoundSystem():
  6. device(nullptr),
  7. context(nullptr)
  8. {
  9. device = alcOpenDevice(nullptr);
  10. if (!device)
  11. return;
  12. context = alcCreateContext(device, nullptr);
  13. if (!context)
  14. return;
  15. alcMakeContextCurrent(context);
  16. }
  17. SoundSystem::~SoundSystem()
  18. {
  19. for (auto sound : sounds)
  20. delete sound;
  21. alcMakeContextCurrent(nullptr);
  22. alcDestroyContext(context);
  23. alcCloseDevice(device);
  24. }
  25. void SoundSystem::SetListener(const Vec3f& inPos, const Vec3f& inVel, const Vec3f& inOri)
  26. {
  27. ALfloat orientation[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 };
  28. alListenerfv(AL_POSITION, inPos.v);
  29. alListenerfv(AL_VELOCITY, inVel.v);
  30. alListenerfv(AL_ORIENTATION, orientation);
  31. }
  32. unsigned int SoundSystem::LoadSound(const char* inWavPath, bool inLooping)
  33. {
  34. Sound* sound = new Sound(inWavPath, inLooping);
  35. sounds.push_back(sound);
  36. return sounds.size() - 1;
  37. }
  38. Sound* SoundSystem::GetSound(unsigned int inID)
  39. {
  40. if (inID > sounds.size())
  41. return nullptr;
  42. return sounds[inID];
  43. }