winOpenAL.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platformWin32/platformWin32.h"
  23. #include "console/console.h"
  24. #include "al/altypes.h"
  25. #include "al/alctypes.h"
  26. #define INITGUID
  27. #include "al/eaxtypes.h"
  28. // Define the OpenAL and Extension Stub functions
  29. #define AL_FUNCTION(fn_return, fn_name, fn_args, fn_value) fn_return stub_##fn_name fn_args{ fn_value }
  30. #include "al/al_func.h"
  31. #include "al/alc_func.h"
  32. #include "al/eax_func.h"
  33. #undef AL_FUNCTION
  34. // Declare the OpenAL and Extension Function pointers
  35. // And initialize them to the stub functions
  36. #define AL_FUNCTION(fn_return,fn_name,fn_args, fn_value) fn_return (*fn_name)fn_args = stub_##fn_name;
  37. #include "al/al_func.h"
  38. #include "al/alc_func.h"
  39. #include "al/eax_func.h"
  40. #undef AL_FUNCTION
  41. /*! Get a function address from the OpenAL DLL and bind it to the
  42. * function pointer
  43. */
  44. static bool bindFunction( void *&fnAddress, const char *name )
  45. {
  46. // JMQ: MinGW gcc 3.2 needs the cast to void*
  47. fnAddress = (void*)(GetProcAddress( winState.hinstOpenAL, name ));
  48. if( !fnAddress )
  49. Con::errorf(ConsoleLogEntry::General, " Missing OpenAL function '%s'", name);
  50. return (fnAddress != NULL);
  51. }
  52. /*! Get a function address for an OpenAL extension function and bind it
  53. * to it's function pointer
  54. */
  55. static bool bindExtensionFunction( void *&fnAddress, const char *name )
  56. {
  57. fnAddress = alGetProcAddress( (ALubyte*)name );
  58. if( !fnAddress )
  59. Con::errorf(ConsoleLogEntry::General, " Missing OpenAL Extension function '%s'", name);
  60. return (fnAddress != NULL);
  61. }
  62. /*! Bind the functions in the OpenAL DLL to the al interface functions
  63. */
  64. static bool bindOpenALFunctions()
  65. {
  66. bool result = true;
  67. #define AL_FUNCTION(fn_return, fn_name, fn_args, fn_value) result &= bindFunction( *(void**)&fn_name, #fn_name);
  68. #include "al/al_func.h"
  69. #include "al/alc_func.h"
  70. #undef AL_FUNCTION
  71. return result;
  72. }
  73. /*! Bind the stub functions to the al interface functions
  74. */
  75. static void unbindOpenALFunctions()
  76. {
  77. #define AL_FUNCTION(fn_return, fn_name, fn_args, fn_value) fn_name = stub_##fn_name;
  78. #include "al/al_func.h"
  79. #include "al/alc_func.h"
  80. #include "al/eax_func.h"
  81. #undef AL_FUNCTION
  82. }
  83. /*! Bind the EAX Extension functions to the EAX interface functions
  84. */
  85. static bool bindEAXFunctions()
  86. {
  87. bool result = true;
  88. #define AL_FUNCTION(fn_return, fn_name, fn_args, fn_value) result &= bindExtensionFunction( *(void**)&fn_name, #fn_name);
  89. #include "al/eax_func.h"
  90. #undef AL_FUNCTION
  91. return result;
  92. }
  93. namespace Audio
  94. {
  95. /*! Shutdown and Unload the OpenAL DLL
  96. */
  97. void OpenALDLLShutdown()
  98. {
  99. if (winState.hinstOpenAL)
  100. FreeLibrary(winState.hinstOpenAL);
  101. winState.hinstOpenAL = NULL;
  102. unbindOpenALFunctions();
  103. }
  104. /*! Dynamically Loads the OpenAL DLL if present and binds all the functions.
  105. * If there is no DLL or an unexpected error occurs binding functions the
  106. * stub functions are automatically bound.
  107. */
  108. bool OpenALDLLInit()
  109. {
  110. OpenALDLLShutdown();
  111. winState.hinstOpenAL = LoadLibrary( dT("OpenAl32.dll") );
  112. if(winState.hinstOpenAL != NULL)
  113. {
  114. // if the DLL loaded bind the OpenAL function pointers
  115. if(bindOpenALFunctions())
  116. {
  117. // if EAX is available bind it's function pointers
  118. //if (alIsExtensionPresent((ALubyte*)"EAX" ))
  119. bindEAXFunctions();
  120. return(true);
  121. }
  122. // an error occured, shutdown
  123. OpenALDLLShutdown();
  124. }
  125. return(false);
  126. }
  127. } // end namespace Audio