sfxALProvider.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "sfx/sfxProvider.h"
  24. #include "sfx/openal/sfxALDevice.h"
  25. #include "sfx/openal/aldlist.h"
  26. #include "sfx/openal/LoadOAL.h"
  27. #include "core/strings/stringFunctions.h"
  28. #include "console/console.h"
  29. #include "core/module.h"
  30. class SFXALProvider : public SFXProvider
  31. {
  32. public:
  33. SFXALProvider()
  34. : SFXProvider( "OpenAL" ) { mALDL = NULL; }
  35. virtual ~SFXALProvider();
  36. protected:
  37. OPENALFNTABLE mOpenAL;
  38. ALDeviceList *mALDL;
  39. struct ALDeviceInfo : SFXDeviceInfo
  40. {
  41. };
  42. void init();
  43. public:
  44. SFXDevice *createDevice( const String& deviceName, bool useHardware, S32 maxBuffers );
  45. };
  46. MODULE_BEGIN( OpenAL )
  47. MODULE_INIT_BEFORE( SFX )
  48. MODULE_SHUTDOWN_AFTER( SFX )
  49. SFXALProvider* mProvider;
  50. MODULE_INIT
  51. {
  52. mProvider = new SFXALProvider;
  53. }
  54. MODULE_SHUTDOWN
  55. {
  56. delete mProvider;
  57. }
  58. MODULE_END;
  59. void SFXALProvider::init()
  60. {
  61. if( LoadOAL10Library( NULL, &mOpenAL ) != AL_TRUE )
  62. {
  63. Con::printf( "SFXALProvider - OpenAL not available." );
  64. return;
  65. }
  66. mALDL = new ALDeviceList( mOpenAL );
  67. // Did we get any devices?
  68. if ( mALDL->GetNumDevices() < 1 )
  69. {
  70. Con::printf( "SFXALProvider - No valid devices found!" );
  71. return;
  72. }
  73. // Cool, loop through them, and caps em
  74. const char *deviceFormat = "OpenAL v%d.%d %s";
  75. char temp[256];
  76. for( S32 i = 0; i < mALDL->GetNumDevices(); i++ )
  77. {
  78. ALDeviceInfo* info = new ALDeviceInfo;
  79. info->name = String( mALDL->GetDeviceName( i ) );
  80. S32 major, minor, eax = 0;
  81. mALDL->GetDeviceVersion( i, &major, &minor );
  82. // Apologies for the blatent enum hack -patw
  83. for( S32 j = SFXALEAX2; j < SFXALEAXRAM; j++ )
  84. eax += (int)mALDL->IsExtensionSupported( i, (SFXALCaps)j );
  85. if( eax > 0 )
  86. {
  87. eax += 2; // EAX support starts at 2.0
  88. dSprintf( temp, sizeof( temp ), "[EAX %d.0] %s", eax, ( mALDL->IsExtensionSupported( i, SFXALEAXRAM ) ? "EAX-RAM" : "" ) );
  89. }
  90. else
  91. dStrcpy( temp, "" );
  92. info->driver = String::ToString( deviceFormat, major, minor, temp );
  93. info->hasHardware = eax > 0;
  94. info->maxBuffers = mALDL->GetMaxNumSources( i );
  95. mDeviceInfo.push_back( info );
  96. }
  97. regProvider( this );
  98. }
  99. SFXALProvider::~SFXALProvider()
  100. {
  101. UnloadOAL10Library();
  102. if (mALDL)
  103. delete mALDL;
  104. }
  105. SFXDevice *SFXALProvider::createDevice( const String& deviceName, bool useHardware, S32 maxBuffers )
  106. {
  107. ALDeviceInfo *info = dynamic_cast< ALDeviceInfo* >
  108. ( _findDeviceInfo( deviceName) );
  109. // Do we find one to create?
  110. if ( info )
  111. return new SFXALDevice( this, mOpenAL, info->name, useHardware, maxBuffers );
  112. return NULL;
  113. }