sfxALProvider.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "core/strings/stringFunctions.h"
  27. #include "console/console.h"
  28. #include "core/module.h"
  29. class SFXALProvider : public SFXProvider
  30. {
  31. public:
  32. SFXALProvider()
  33. : SFXProvider( "OpenAL" ) { mALDL = NULL; }
  34. virtual ~SFXALProvider();
  35. protected:
  36. OPENALFNTABLE mOpenAL;
  37. ALDeviceList *mALDL;
  38. struct ALDeviceInfo : SFXDeviceInfo
  39. {
  40. };
  41. void init();
  42. public:
  43. SFXDevice *createDevice( const String& deviceName, bool useHardware, S32 maxBuffers );
  44. };
  45. MODULE_BEGIN( OpenAL )
  46. MODULE_INIT_BEFORE( SFX )
  47. MODULE_SHUTDOWN_AFTER( SFX )
  48. SFXALProvider* mProvider;
  49. MODULE_INIT
  50. {
  51. mProvider = new SFXALProvider;
  52. }
  53. MODULE_SHUTDOWN
  54. {
  55. delete mProvider;
  56. }
  57. MODULE_END;
  58. void SFXALProvider::init()
  59. {
  60. if( LoadOAL10Library( NULL, &mOpenAL ) != AL_TRUE )
  61. {
  62. Con::printf( "SFXALProvider - OpenAL not available." );
  63. return;
  64. }
  65. mALDL = new ALDeviceList( mOpenAL );
  66. // Did we get any devices?
  67. if ( mALDL->GetNumDevices() < 1 )
  68. {
  69. Con::printf( "SFXALProvider - No valid devices found!" );
  70. return;
  71. }
  72. // Cool, loop through them, and caps em
  73. const char *deviceFormat = "OpenAL v%d.%d %s";
  74. char temp[256];
  75. for( S32 i = 0; i < mALDL->GetNumDevices(); i++ )
  76. {
  77. ALDeviceInfo* info = new ALDeviceInfo;
  78. info->name = String( mALDL->GetDeviceName( i ) );
  79. S32 major, minor, eax = 0;
  80. mALDL->GetDeviceVersion( i, &major, &minor );
  81. // Apologies for the blatent enum hack -patw
  82. for( S32 j = SFXALEAX2; j < SFXALEAXRAM; j++ )
  83. eax += (int)mALDL->IsExtensionSupported( i, (SFXALCaps)j );
  84. if( eax > 0 )
  85. {
  86. eax += 2; // EAX support starts at 2.0
  87. dSprintf( temp, sizeof( temp ), "[EAX %d.0] %s", eax, ( mALDL->IsExtensionSupported( i, SFXALEAXRAM ) ? "EAX-RAM" : "" ) );
  88. }
  89. else
  90. dStrcpy( temp, "" );
  91. info->driver = String::ToString( deviceFormat, major, minor, temp );
  92. info->hasHardware = eax > 0;
  93. info->maxBuffers = mALDL->GetMaxNumSources( i );
  94. mDeviceInfo.push_back( info );
  95. }
  96. regProvider( this );
  97. }
  98. SFXALProvider::~SFXALProvider()
  99. {
  100. UnloadOAL10Library();
  101. if (mALDL)
  102. delete mALDL;
  103. }
  104. SFXDevice *SFXALProvider::createDevice( const String& deviceName, bool useHardware, S32 maxBuffers )
  105. {
  106. ALDeviceInfo *info = dynamic_cast< ALDeviceInfo* >
  107. ( _findDeviceInfo( deviceName) );
  108. // Do we find one to create?
  109. if ( info )
  110. return new SFXALDevice( this, mOpenAL, info->name, useHardware, maxBuffers );
  111. return NULL;
  112. }