PolySoundManager.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * PolySoundManager.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 10/12/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolySoundManager.h"
  10. using namespace Polycode;
  11. SoundManager::SoundManager() {
  12. initAL();
  13. }
  14. void SoundManager::initAL() {
  15. alGetError();
  16. if(alcGetCurrentContext() == NULL) {
  17. Logger::log("AL already initialized\n");
  18. }
  19. device = alcOpenDevice(NULL);
  20. if(device == NULL) {
  21. Logger::log("InitializeAL: Cannot open preferred device\n");
  22. return;
  23. }
  24. if (alcGetError(device) != ALC_NO_ERROR) {
  25. alcCloseDevice(device);
  26. // PCCE_THROW("InitializeAL: Could not open device (alc error)");
  27. }
  28. context = alcCreateContext(device, NULL);
  29. if (context == NULL) {
  30. alcCloseDevice(device);
  31. // PCCE_THROW("InitializeAL: Could not create context");
  32. }
  33. if (alcGetError(device) != ALC_NO_ERROR) {
  34. alcDestroyContext(context);
  35. alcCloseDevice(device);
  36. // PCCE_THROW("InitializeAL: Could not open device (alc error)");
  37. }
  38. if (alcMakeContextCurrent(context) != ALC_TRUE) {
  39. alcDestroyContext(context);
  40. alcCloseDevice(device);
  41. // PCCE_THROW("InitializeAL: Could not make context current");
  42. }
  43. if (alcGetError(device) != ALC_NO_ERROR) {
  44. alcMakeContextCurrent(NULL);
  45. alcDestroyContext(context);
  46. alcCloseDevice(device);
  47. // PCCE_THROW("InitializeAL: Could not make context current (alc error)");
  48. }
  49. alGetError();
  50. ALfloat listenerPos[] = { 0.0, 0.0, 0.0 };
  51. ALfloat listenerVel[] = { 0.0, 0.0, 0.0 };
  52. ALfloat listenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 };
  53. alListenerfv(AL_POSITION, listenerPos);
  54. alListenerfv(AL_VELOCITY, listenerVel);
  55. alListenerfv(AL_ORIENTATION, listenerOri);
  56. if (alGetError() != AL_NO_ERROR) {
  57. // ShutdownAL();
  58. // PCCE_THROW("InitializeAL: Could not set listener position");
  59. }
  60. Logger::log("OpenAL initialized...\n");
  61. }
  62. SoundManager::~SoundManager() {
  63. alcSuspendContext(context);
  64. alcDestroyContext(context);
  65. if (device != NULL) {
  66. alcCloseDevice(device);
  67. }
  68. }