From 642ce2eb9c9feaeb2edcfa78e0fbbdf56890c73d Mon Sep 17 00:00:00 2001 From: Nicolas Hake Date: Fri, 1 Aug 2014 21:05:55 +0200 Subject: [PATCH] Merge all of the audio provider switches into one There's no point in splitting the audio library selection into multiple CPP macros, since there can always only be one anyway. Merge all of them into a single macro AUDIO_TK (for "toolkit") and have CMake select one for the user, instead of making him choose (and potentially failing). --- CMakeLists.txt | 66 ++------------- cmake/FindAudio.cmake | 143 ++++++++++++++++++++++++++++++++ config.h.cmake | 16 ++-- src/gui/C4StartupAboutDlg.cpp | 2 +- src/platform/C4MusicFile.cpp | 6 +- src/platform/C4MusicFile.h | 10 +-- src/platform/C4MusicSystem.cpp | 39 +++++---- src/platform/C4MusicSystem.h | 13 +-- src/platform/C4SoundLoaders.cpp | 20 +++-- src/platform/C4SoundLoaders.h | 19 +++-- src/platform/C4SoundSystem.cpp | 54 +++++------- src/platform/C4SoundSystem.h | 12 +-- 12 files changed, 238 insertions(+), 162 deletions(-) create mode 100644 cmake/FindAudio.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f6bfc024..ef7d46489 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,12 +48,6 @@ CMAKE_DEPENDENT_OPTION(USE_GTK "Use GTK for the developer mode" ON "USE_X11" OFF CMAKE_DEPENDENT_OPTION(USE_GTK3 "Use GTK3 instead of GTK2" OFF "USE_GTK" OFF) CMAKE_DEPENDENT_OPTION(USE_COCOA "Use Apple Cocoa for the developer mode and the windows." ON "APPLE" OFF) -if(APPLE) - SET(INITIAL_USE_OPEN_AL ON) -else() - SET(INITIAL_USE_OPEN_AL OFF) -endif() -option(USE_OPEN_AL "Use OpenAL to play sounds" ${INITIAL_USE_OPEN_AL}) option(WITH_AUTOMATIC_UPDATE "Automatic updates are downloaded from the project website." OFF) # We link Boost statically because that makes it easier for us to distribute @@ -867,21 +861,8 @@ if(MAPE_GTK_FOUND) link_directories(${MAPE_GTK_LIBRARY_DIRS}) endif() -if(USE_OPEN_AL) - if(MSVC) - if(${FIND_LIBRARY_USE_LIB64_PATHS}) - FINDLIB(OPENAL_LIBRARY NAMES OpenAL64) - else() - FINDLIB(OPENAL_LIBRARY NAMES OpenAL32) - endif() - endif() - FINDLIB(OGG_LIBRARY NAMES libogg_static libogg ogg) - FINDLIB(VORBIS_LIBRARY NAMES libvorbis_static libvorbis vorbis) - FINDLIB(VORBISFILE_LIBRARY NAMES libvorbisfile_static libvorbisfile vorbisfile) - if(NOT APPLE) - FINDLIB(ALUT_LIBRARY NAMES alut_static alut) - endif() -endif() +# Select an audio library +find_package("Audio") ############################################################################ # Precompiled header support, part 1 (pre-target) @@ -1061,14 +1042,9 @@ target_link_libraries(openclonk libmisc ) -if(USE_OPEN_AL) - target_link_libraries(openclonk - ${OPENAL_LIBRARY} - ${ALUT_LIBRARY} - ${VORBIS_LIBRARY} - ${VORBISFILE_LIBRARY} - ${OGG_LIBRARY} - ) +if(Audio_FOUND) + target_link_libraries(openclonk ${Audio_LIBRARIES}) + include_directories(${Audio_INCLUDE_DIRS}) endif() if(MAPE_GTK_FOUND) @@ -1320,7 +1296,7 @@ if(USE_X11) ) endif() if(USE_COCOA) - TARGET_LINK_LIBRARIES(openclonk "-framework Cocoa -framework AppKit -framework Quartz -framework OpenAL -framework AudioToolBox") + TARGET_LINK_LIBRARIES(openclonk "-framework Cocoa -framework AppKit -framework Quartz") endif() if (WIN32) # CMake is too incompetent to check whether these libraries can be linked to @@ -1342,36 +1318,6 @@ if (WIN32) if(MAPE_GTK_FOUND) target_link_libraries(mape winmm) endif() - - if(NOT USE_OPEN_AL) - find_package(FMod) - if(FMOD_FOUND) - set(HAVE_FMOD TRUE) - target_link_libraries(openclonk - ${FMOD_LIBRARIES} - ) - include_directories(${FMOD_INCLUDE_DIR}) - else() - set(HAVE_FMOD FALSE) - endif() - endif() -else() - SET(HAVE_FMOD FALSE) -endif() -if(NOT HAVE_FMOD AND NOT USE_OPEN_AL OR USE_SDL_MAINLOOP AND NOT USE_OPEN_AL) - include(FindSDL) - SET(HAVE_SDL ${SDL_FOUND}) - if(SDL_FOUND) - include_directories(${SDL_INCLUDE_DIR}) - FINDLIB(SDLMIXER_LIBRARIES SDL_mixer) - if(SDLMIXER_LIBRARIES) - SET(HAVE_LIBSDL_MIXER ON) - endif() - target_link_libraries(openclonk - ${SDL_LIBRARY} - ${SDLMIXER_LIBRARIES} - ) - endif() endif() if(HAVE_UPNP) diff --git a/cmake/FindAudio.cmake b/cmake/FindAudio.cmake new file mode 100644 index 000000000..dac30181f --- /dev/null +++ b/cmake/FindAudio.cmake @@ -0,0 +1,143 @@ +# OpenClonk, http://www.openclonk.org +# +# Copyright (c) 2009-2014, The OpenClonk Team and contributors +# +# Distributed under the terms of the ISC license; see accompanying file +# "COPYING" for details. +# +# "Clonk" is a registered trademark of Matthes Bender, used with permission. +# See accompanying file "TRADEMARK" for details. +# +# To redistribute this file separately, substitute the full license texts +# for the above references. + +# This module chooses an audio provider for use in OpenClonk. + +macro(__FINDAUDIO_FINDOPENAL) + find_package(PkgConfig QUIET) + if(PKG_CONFIG_FOUND) + pkg_check_modules(OpenAL "openal>=1.15.1") + pkg_check_modules(Alut "freealut>=1.1.0") + pkg_check_modules(OggVorbis "vorbisfile>=1.3.2" "vorbis>=1.3.2" "ogg>=1.3.0") + else() + if(MSVC OR APPLE) + find_path(OpenAL_INCLUDE_DIRS al.h PATH_SUFFIXES include/AL include/OpenAL include) + find_path(Vorbis_INCLUDE_DIRS vorbis/codec.h vorbis/vorbisfile.h PATH_SUFFIXES include) + find_library(Ogg_LIBRARY NAMES libogg_static libogg ogg) + find_library(Vorbis_LIBRARY NAMES libvorbis_static libvorbis vorbis) + find_library(Vorbisfile_LIBRARY NAMES libvorbisfile_static libvorbisfile vorbisfile) + if(OpenAL_INCLUDE_DIRS) + set(OpenAL_FOUND TRUE) + endif() + if(Vorbis_INCLUDE_DIRS AND Ogg_LIBRARY AND Vorbis_LIBRARY AND Vorbisfile_LIBRARY) + set(OggVorbis_FOUND TRUE) + set(OggVorbis_LIBRARIES ${Vorbisfile_LIBRARY} ${Vorbis_LIBRARY} ${Ogg_LIBRARY}) + set(OggVorbis_INCLUDE_DIRS(${Vorbis_INCLUDE_DIRS})) + endif() + endif() + if(MSVC) + find_path(Alut_INCLUDE_DIRS alut.h PATH_SUFFIXES include/AL include/OpenAL include) + find_library(Alut_LIBRARY NAMES alut_static alut) + if(${FIND_LIBRARY_USE_LIB64_PATHS}) + find_library(OpenAL_LIBRARY NAMES OpenAL64) + else() + find_library(OpenAL_LIBRARY NAMES OpenAL32) + endif() + if(NOT OpenAL_LIBRARY) + set(OpenAL_FOUND FALSE) + endif() + if(Alut_INCLUDE_DIRS AND Alut_LIBRARY) + set(Alut_FOUND TRUE) + set(Alut_LIBRARIES ${Alut_LIBRARY}) + endif() + endif() + endif() +endmacro() + +macro(__FINDAUDIO_FINDSDLMIXER) + find_package(PkgConfig QUIET) + if(PKG_CONFIG_FOUND) + pkg_check_modules(SDLMixer "SDL_mixer>=1.2.12") + endif() +endmacro() + +if(Audio_TK) + # Already chosen, don't do anything +elseif(USE_CONSOLE) + # Dedicated servers don't need audio output + set(Audio_TK "none") + set(Audio_FOUND TRUE) +else() + # Test for OpenAL + __FINDAUDIO_FINDOPENAL() + __FINDAUDIO_FINDSDLMIXER() + find_package("FMod") + + if(OpenAL_FOUND AND (APPLE OR Alut_FOUND) AND OggVorbis_FOUND) + # Prefer OpenAL + set(Audio_TK "OpenAL") + elseif(SDLMixer_FOUND) + set(Audio_TK "SDL_Mixer") + elseif(FMOD_FOUND) + set(Audio_TK "FMod") + endif() +endif() + +# Search for the libraries again. If the provider was selected automatically, this will be +# answered from cache; otherwise (because the user manually selected a provider) it will +# make sure the provider is available. +if(Audio_TK STREQUAL "OpenAL") + __FINDAUDIO_FINDOPENAL() + if(OpenAL_FOUND AND (APPLE OR Alut_FOUND) AND OggVorbis_FOUND) + set(Audio_FOUND TRUE) + set(Audio_LIBRARIES ${OpenAL_LIBRARIES} ${OggVorbis_LIBRARIES}) + set(Audio_INCLUDE_DIRS ${OpenAL_INCLUDE_DIRS} ${OggVorbis_INCLUDE_DIRS}) + if (NOT APPLE) + # Apple doesn't need freealut + set(Audio_LIBRARIES ${Audio_LIBRARIES} ${Alut_LIBRARIES}) + set(Audio_INCLUDE_DIRS ${Audio_INCLUDE_DIRS} ${Alut_INCLUDE_DIRS}) + else() + # but it uses the AudioToolBox framework + set(Audio_LIBRARIES ${Audio_LIBRARIES} "-framework OpenAL -framework AudioToolBox") + endif() + endif() +elseif(Audio_TK STREQUAL "SDL_Mixer") + __FINDAUDIO_FINDSDLMIXER() + if(SDLMixer_FOUND) + set(Audio_FOUND TRUE) + set(Audio_LIBRARIES ${SDLMixer_LIBRARIES}) + set(Audio_INCLUDE_DIRS ${SDLMixer_INCLUDE_DIRS}) + endif() +elseif(Audio_TK STREQUAL "FMod") + find_package("FMod") + if(FMOD_FOUND) + set(Audio_FOUND TRUE) + set(Audio_LIBRARIES ${FMOD_LIBRARIES}) + set(Audio_INCLUDE_DIRS ${FMOD_INCLUDE_DIR}) + endif() +elseif(Audio_TK STREQUAL "none") + set(Audio_FOUND TRUE) + set(Audio_LIBRARIES "") + set(Audio_INCLUDE_DIRS "") +endif() + +if(Audio_FOUND) + string(TOUPPER "${Audio_TK}" Audio_TK_UPPER) + if(NOT Audio_FIND_QUIETLY) + message(STATUS "Using Audio toolkit: ${Audio_TK}") + endif() +elseif(Audio_TK) + message(FATAL_ERROR "User-requested audio provider not available.") +else() + set(Audio_FOUND FALSE) + set(Audio_TK "none") + string(TOUPPER "${Audio_TK}" Audio_TK_UPPER) + set(Audio_LIBRARIES "") + set(Audio_INCLUDE_DIRS "") + + if(Audio_FIND_REQUIRED) + message(FATAL_ERROR "No audio provider was found") + else() + message(STATUS "Not enabling audio output.") + endif() +endif() diff --git a/config.h.cmake b/config.h.cmake index 35f5f208b..2c40f049e 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -7,9 +7,6 @@ /* The backtrace function is declared in execinfo.h and works */ #cmakedefine HAVE_EXECINFO_H 1 -/* Whether FMOD shall be used */ -#cmakedefine HAVE_FMOD 1 - /* Define to 1 if you have the header file. */ #cmakedefine HAVE_HISTORY_H 1 @@ -28,9 +25,6 @@ /* Define if you have a readline compatible library */ #cmakedefine HAVE_LIBREADLINE 1 -/* Define to 1 if you have SDL_mixer. */ -#cmakedefine HAVE_LIBSDL_MIXER 1 - /* Define to 1 if you have the header file. */ #cmakedefine HAVE_LOCALE_H 1 @@ -160,9 +154,6 @@ /* MP3 music */ #cmakedefine USE_MP3 1 -/* OpenAL sound */ -#cmakedefine USE_OPEN_AL 1 - /* Define to 1 if SDL is used for the main loop */ #cmakedefine USE_SDL_MAINLOOP 1 @@ -192,3 +183,10 @@ /* Use Apple Cocoa for the UI */ #cmakedefine USE_COCOA 1 + +/* Select an audio provider */ +#define AUDIO_TK_NONE 0 +#define AUDIO_TK_OPENAL 1 +#define AUDIO_TK_FMOD 2 +#define AUDIO_TK_SDL_MIXER 3 +#define AUDIO_TK AUDIO_TK_${Audio_TK_UPPER} diff --git a/src/gui/C4StartupAboutDlg.cpp b/src/gui/C4StartupAboutDlg.cpp index d4468de90..b87d619ef 100644 --- a/src/gui/C4StartupAboutDlg.cpp +++ b/src/gui/C4StartupAboutDlg.cpp @@ -38,7 +38,7 @@ C4StartupAboutDlg::C4StartupAboutDlg() : C4StartupDlg(LoadResStr("IDS_DLG_ABOUT" C4GUI::ComponentAligner caMain(rcClient, 0,0, true); C4GUI::ComponentAligner caButtons(caMain.GetFromBottom(caMain.GetHeight()*1/8), 0,0, false); C4GUI::CallbackButton *btn; -#ifdef HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD AddElement(new C4GUI::Label("Using FMOD Sound System, copyright (c) Firelight Technologies Pty, Ltd., 1994-2010.", caMain.GetFromBottom(rUseFont.GetLineHeight()))); #endif diff --git a/src/platform/C4MusicFile.cpp b/src/platform/C4MusicFile.cpp index 0e03ac0fd..e5a87fcc2 100644 --- a/src/platform/C4MusicFile.cpp +++ b/src/platform/C4MusicFile.cpp @@ -21,7 +21,7 @@ #include #include -#ifdef HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD #include #endif @@ -53,7 +53,7 @@ bool C4MusicFile::Init(const char *szFile) return true; } -#if defined HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD bool C4MusicFileMID::Play(bool loop) { // check existance @@ -292,7 +292,7 @@ void C4MusicFileOgg::SetVolume(int iLevel) FSOUND_SetVolume(Channel, (int) ((iLevel * 255) / 100)); } -#elif defined HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER C4MusicFileSDL::C4MusicFileSDL(): Data(NULL), Music(NULL) diff --git a/src/platform/C4MusicFile.h b/src/platform/C4MusicFile.h index c637ad5bf..b7fdfb771 100644 --- a/src/platform/C4MusicFile.h +++ b/src/platform/C4MusicFile.h @@ -18,9 +18,9 @@ #ifndef INC_C4MusicFile #define INC_C4MusicFile -#if defined HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD #include -#elif defined HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER #define USE_RWOPS #include #undef USE_RWOPS @@ -55,7 +55,7 @@ protected: bool SongExtracted; }; -#if defined HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD class C4MusicFileMID : public C4MusicFile { public: @@ -123,7 +123,7 @@ protected: bool Playing; }; -#elif defined HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER typedef struct _Mix_Music Mix_Music; class C4MusicFileSDL : public C4MusicFile { @@ -138,6 +138,6 @@ protected: char *Data; Mix_Music * Music; }; -#endif // HAVE_LIBSDL_MIXER +#endif #endif diff --git a/src/platform/C4MusicSystem.cpp b/src/platform/C4MusicSystem.cpp index 5b5f546ad..584c9d19e 100644 --- a/src/platform/C4MusicSystem.cpp +++ b/src/platform/C4MusicSystem.cpp @@ -28,14 +28,17 @@ #include #include -#if defined HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD #include -#elif defined HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER #include +#elif AUDIO_TK == AUDIO_TK_OPENAL && !defined(APPLE) +#ifdef _WIN32 +// This is an ugly hack to make FreeALUT not dllimport everything. +#define _XBOX #endif - -#if defined(USE_OPEN_AL) && !defined(__APPLE__) -#include +#include +#undef _XBOX #endif C4MusicSystem::C4MusicSystem(): @@ -43,7 +46,7 @@ C4MusicSystem::C4MusicSystem(): SongCount(0), PlayMusicFile(NULL), Volume(100) -#ifdef USE_OPEN_AL +#if AUDIO_TK == AUDIO_TK_OPENAL , alcDevice(NULL), alcContext(NULL) #endif { @@ -54,7 +57,7 @@ C4MusicSystem::~C4MusicSystem() Clear(); } -#ifdef USE_OPEN_AL +#if AUDIO_TK == AUDIO_TK_OPENAL void C4MusicSystem::SelectContext() { alcMakeContextCurrent(alcContext); @@ -63,7 +66,7 @@ void C4MusicSystem::SelectContext() bool C4MusicSystem::InitializeMOD() { -#if defined HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD #ifdef _WIN32 // Debug code switch (Config.Sound.FMMode) @@ -97,7 +100,7 @@ bool C4MusicSystem::InitializeMOD() // ok MODInitialized = true; return true; -#elif defined HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER SDL_version compile_version; const SDL_version * link_version; MIX_VERSION(&compile_version); @@ -118,7 +121,7 @@ bool C4MusicSystem::InitializeMOD() } MODInitialized = true; return true; -#elif defined(USE_OPEN_AL) +#elif AUDIO_TK == AUDIO_TK_OPENAL alcDevice = alcOpenDevice(NULL); if (!alcDevice) { @@ -146,16 +149,16 @@ bool C4MusicSystem::InitializeMOD() void C4MusicSystem::DeinitializeMOD() { -#if defined HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD FSOUND_StopSound(FSOUND_ALL); /* to prevent some hangs in FMOD */ #ifdef DEBUG Sleep(0); #endif FSOUND_Close(); -#elif defined HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER Mix_CloseAudio(); SDL_Quit(); -#elif defined(USE_OPEN_AL) +#elif AUDIO_TK == AUDIO_TK_OPENAL #ifndef __APPLE__ alutExit(); #endif @@ -237,7 +240,7 @@ void C4MusicSystem::Load(const char *szFile) if (!szFile || !*szFile) return; C4MusicFile *NewSong=NULL; // get extension -#if defined HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD const char *szExt = GetExtension(szFile); // get type switch (GetMusicFileTypeByExtension(GetExtension(szFile))) @@ -258,7 +261,7 @@ void C4MusicSystem::Load(const char *szFile) break; default: return; // safety } -#elif defined HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER if (GetMusicFileTypeByExtension(GetExtension(szFile)) == MUSICTYPE_UNKNOWN) return; NewSong = new C4MusicFileSDL; #endif @@ -382,7 +385,7 @@ void C4MusicSystem::ClearSongs() void C4MusicSystem::Clear() { -#ifdef HAVE_LIBSDL_MIXER +#if AUDIO_TK == AUDIO_TK_SDL_MIXER // Stop a fadeout Mix_HaltMusic(); #endif @@ -392,7 +395,7 @@ void C4MusicSystem::Clear() void C4MusicSystem::Execute() { -#ifndef HAVE_LIBSDL_MIXER +#if AUDIO_TK != AUDIO_TK_SDL_MIXER if (!::Game.iTick35) #endif { @@ -513,7 +516,7 @@ MusicType GetMusicFileTypeByExtension(const char* ext) { if (SEqualNoCase(ext, "mid")) return MUSICTYPE_MID; -#if defined HAVE_FMOD || defined HAVE_LIBSDL_MIXER +#if AUDIO_TK == AUDIO_TK_FMOD || AUDIO_TK == AUDIO_TK_SDL_MIXER else if (SEqualNoCase(ext, "xm") || SEqualNoCase(ext, "it") || SEqualNoCase(ext, "s3m") || SEqualNoCase(ext, "mod")) return MUSICTYPE_MOD; #ifdef USE_MP3 diff --git a/src/platform/C4MusicSystem.h b/src/platform/C4MusicSystem.h index e066e26d2..a1125f114 100644 --- a/src/platform/C4MusicSystem.h +++ b/src/platform/C4MusicSystem.h @@ -22,14 +22,9 @@ #include -#if defined(USE_OPEN_AL) -#ifdef __APPLE__ -#import -#import -#else -#include -#include -#endif +#if AUDIO_TK == AUDIO_TK_OPENAL +#include +#include #endif class C4MusicFileInfoNode; @@ -77,7 +72,7 @@ protected: bool MODInitialized; bool InitializeMOD(); void DeinitializeMOD(); -#ifdef USE_OPEN_AL +#if AUDIO_TK == AUDIO_TK_OPENAL private: ALCdevice* alcDevice; ALCcontext* alcContext; diff --git a/src/platform/C4SoundLoaders.cpp b/src/platform/C4SoundLoaders.cpp index 4b7706dc3..387f94b79 100644 --- a/src/platform/C4SoundLoaders.cpp +++ b/src/platform/C4SoundLoaders.cpp @@ -19,12 +19,17 @@ #include -#if defined(USE_OPEN_AL) +#if AUDIO_TK == AUDIO_TK_OPENAL #if defined(__APPLE__) #import #import #else -#include +#ifdef _WIN32 +// This is an ugly hack to make FreeALUT not dllimport everything. +#define _XBOX +#endif +#include +#undef _XBOX #endif #endif @@ -33,7 +38,7 @@ using namespace C4SoundLoaders; SoundLoader* SoundLoader::first_loader(NULL); -#if defined(USE_OPEN_AL) && defined(__APPLE__) +#if AUDIO_TK == AUDIO_TK_OPENAL && defined(__APPLE__) namespace { static OSStatus AudioToolBoxReadCallback(void* context, SInt64 inPosition, UInt32 requestCount, void* buffer, UInt32* actualCount) @@ -111,8 +116,7 @@ bool AppleSoundLoader::ReadInfo(SoundInfo* result, BYTE* data, size_t data_lengt AppleSoundLoader AppleSoundLoader::singleton; #endif -#ifdef USE_OPEN_AL - +#if AUDIO_TK == AUDIO_TK_OPENAL size_t VorbisLoader::read_func(void* ptr, size_t byte_size, size_t size_to_read, void* datasource) { size_t spaceToEOF; @@ -254,9 +258,8 @@ bool WavLoader::ReadInfo(SoundInfo* result, BYTE* data, size_t data_length, uint WavLoader WavLoader::singleton; #endif -#endif -#ifdef HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER #define USE_RWOPS #include @@ -278,9 +281,8 @@ bool SDLMixerSoundLoader::ReadInfo(SoundInfo* result, BYTE* data, size_t data_le } SDLMixerSoundLoader SDLMixerSoundLoader::singleton; -#endif -#ifdef HAVE_FMOD +#elif AUDIO_TK == AUDIO_TK_FMOD bool FMODSoundLoader::ReadInfo(SoundInfo* result, BYTE* data, size_t data_length, uint32_t options) { int32_t iOptions = FSOUND_NORMAL | FSOUND_2D | FSOUND_LOADMEMORY; diff --git a/src/platform/C4SoundLoaders.h b/src/platform/C4SoundLoaders.h index 46bc67703..63a569329 100644 --- a/src/platform/C4SoundLoaders.h +++ b/src/platform/C4SoundLoaders.h @@ -13,7 +13,10 @@ * for the above references. */ -#ifdef USE_OPEN_AL +#ifndef INC_C4SoundLoaders +#define INC_C4SoundLoaders + +#if AUDIO_TK == AUDIO_TK_OPENAL #include #include #endif @@ -29,7 +32,7 @@ namespace C4SoundLoaders double sample_length; uint32_t sample_rate; std::vector sound_data; -#ifdef USE_OPEN_AL +#if AUDIO_TK == AUDIO_TK_OPENAL ALenum format; #endif C4SoundHandle final_handle; @@ -53,7 +56,7 @@ namespace C4SoundLoaders virtual bool ReadInfo(SoundInfo* info, BYTE* data, size_t data_length, uint32_t options = 0) = 0; }; -#if defined(USE_OPEN_AL) && defined(__APPLE__) +#if AUDIO_TK == AUDIO_TK_OPENAL && defined(__APPLE__) class AppleSoundLoader: public SoundLoader { public: @@ -64,7 +67,7 @@ namespace C4SoundLoaders }; #endif -#ifdef USE_OPEN_AL +#if AUDIO_TK == AUDIO_TK_OPENAL class VorbisLoader: public SoundLoader { private: @@ -96,18 +99,16 @@ namespace C4SoundLoaders static WavLoader singleton; }; #endif // apple -#endif // openal -#ifdef HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER class SDLMixerSoundLoader: public SoundLoader { public: static SDLMixerSoundLoader singleton; virtual bool ReadInfo(SoundInfo* result, BYTE* data, size_t data_length, uint32_t); }; -#endif -#ifdef HAVE_FMOD +#elif AUDIO_TK == AUDIO_TK_FMOD class FMODSoundLoader: public SoundLoader { public: @@ -116,3 +117,5 @@ namespace C4SoundLoaders }; #endif } + +#endif diff --git a/src/platform/C4SoundSystem.cpp b/src/platform/C4SoundSystem.cpp index bbf8c3a12..14f87de7d 100644 --- a/src/platform/C4SoundSystem.cpp +++ b/src/platform/C4SoundSystem.cpp @@ -28,7 +28,7 @@ #include #include -#ifdef HAVE_LIBSDL_MIXER +#if AUDIO_TK == AUDIO_TK_SDL_MIXER #define USE_RWOPS #include #endif @@ -52,13 +52,11 @@ C4SoundEffect::~C4SoundEffect() void C4SoundEffect::Clear() { while (FirstInst) RemoveInst(FirstInst); -#ifdef HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD if (pSample) FSOUND_Sample_Free(pSample); -#endif -#ifdef HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER if (pSample) Mix_FreeChunk(pSample); -#endif -#ifdef USE_OPEN_AL +#elif AUDIO_TK == AUDIO_TK_OPENAL if (pSample) alDeleteBuffers(1, &pSample); #endif pSample = 0; @@ -98,7 +96,7 @@ bool C4SoundEffect::Load(BYTE *pData, size_t iDataLen, bool fRaw) } else { -#ifdef USE_OPEN_AL +#if AUDIO_TK == AUDIO_TK_OPENAL Application.MusicSystem.SelectContext(); alGenBuffers(1, &pSample); alBufferData(pSample, info.format, &info.sound_data[0], info.sound_data.size(), info.sample_rate); @@ -253,7 +251,7 @@ bool C4SoundInstance::CheckStart() bool C4SoundInstance::Start() { -#ifdef HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD // Start if ((iChannel = FSOUND_PlaySound(FSOUND_FREE, pEffect->pSample)) == -1) return false; @@ -266,12 +264,12 @@ bool C4SoundInstance::Start() int32_t iTime = (C4TimeMilliseconds::Now() - tStarted) % pEffect->Length; FSOUND_SetCurrentPosition(iChannel, iTime / 10 * pEffect->SampleRate / 100); } -#elif defined HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER // Be paranoid about SDL_Mixer initialisation if (!Application.MusicSystem.MODInitialized) return false; if ((iChannel = Mix_PlayChannel(-1, pEffect->pSample, fLooping? -1 : 0)) == -1) return false; -#elif defined(USE_OPEN_AL) +#elif AUDIO_TK == AUDIO_TK_OPENAL Application.MusicSystem.SelectContext(); alGenSources(1, (ALuint*)&iChannel); alSourcei(iChannel, AL_BUFFER, pEffect->pSample); @@ -292,16 +290,14 @@ bool C4SoundInstance::Stop() if (!pEffect) return false; // Stop sound bool fRet = true; -#ifdef HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD if (Playing()) fRet = !! FSOUND_StopSound(iChannel); -#endif -#ifdef HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER // iChannel == -1 will halt all channels. Is that right? if (Playing()) Mix_HaltChannel(iChannel); -#endif -#ifdef USE_OPEN_AL +#elif AUDIO_TK == AUDIO_TK_OPENAL if (iChannel != -1) { if (Playing()) alSourceStop(iChannel); @@ -318,15 +314,13 @@ bool C4SoundInstance::Stop() bool C4SoundInstance::Playing() { if (!pEffect) return false; -#ifdef HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD if (fLooping) return true; return isStarted() ? FSOUND_GetCurrentSample(iChannel) == pEffect->pSample : C4TimeMilliseconds::Now() < tStarted + pEffect->Length; -#endif -#ifdef HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER return Application.MusicSystem.MODInitialized && (iChannel != -1) && Mix_Playing(iChannel); -#endif -#ifdef USE_OPEN_AL +#elif AUDIO_TK == AUDIO_TK_OPENAL if (iChannel == -1) return false; else @@ -363,13 +357,11 @@ void C4SoundInstance::Execute() // stop, if started if (isStarted()) { -#ifdef HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD FSOUND_StopSound(iChannel); -#endif -#ifdef HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER Mix_HaltChannel(iChannel); -#endif -#ifdef USE_OPEN_AL +#elif AUDIO_TK == AUDIO_TK_OPENAL alDeleteSources(1, (ALuint*)&iChannel); #endif iChannel = -1; @@ -382,16 +374,14 @@ void C4SoundInstance::Execute() if (!CheckStart()) return; // set volume & panning -#ifdef HAVE_FMOD +#if AUDIO_TK == AUDIO_TK_FMOD FSOUND_SetVolume(iChannel, BoundBy(iVol / 100, 0, 255)); FSOUND_SetPan(iChannel, BoundBy(256*(iPan+100)/200,0,255)); -#endif -#ifdef HAVE_LIBSDL_MIXER +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER Mix_Volume(iChannel, (iVol * MIX_MAX_VOLUME) / (100 * 256)); //Mix_SetPanning(iChannel, ((100 + iPan) * 256) / 200, ((100 - iPan) * 256) / 200); Mix_SetPanning(iChannel, BoundBy((100 - iPan) * 256 / 100, 0, 255), BoundBy((100 + iPan) * 256 / 100, 0, 255)); -#endif -#ifdef USE_OPEN_AL +#elif AUDIO_TK == AUDIO_TK_OPENAL alSource3f(iChannel, AL_POSITION, 0, 0, 0); // FIXME alSourcef(iChannel, AL_GAIN, float(iVol) / (100.0f*256.0f)); #endif @@ -448,7 +438,7 @@ bool C4SoundSystem::Init() if (!Reloc.Open(SoundFile, C4CFN_Sound)) return false; // Load static sound from Sound.ocg LoadEffects(SoundFile); -#ifdef HAVE_LIBSDL_MIXER +#if AUDIO_TK == AUDIO_TK_SDL_MIXER Mix_AllocateChannels(C4MaxSoundInstances); #endif return true; @@ -475,7 +465,7 @@ void C4SoundSystem::ClearEffects() void C4SoundSystem::Execute() { -#ifdef USE_OPEN_AL +#if AUDIO_TK == AUDIO_TK_OPENAL Application.MusicSystem.SelectContext(); #endif C4SoundEffect *csfx; diff --git a/src/platform/C4SoundSystem.h b/src/platform/C4SoundSystem.h index 2cd9948f7..1460e918c 100644 --- a/src/platform/C4SoundSystem.h +++ b/src/platform/C4SoundSystem.h @@ -31,17 +31,13 @@ const int32_t class C4Object; class C4SoundInstance; -#if defined(HAVE_FMOD) +#if AUDIO_TK == AUDIO_TK_FMOD #include typedef FSOUND_SAMPLE* C4SoundHandle; -#elif defined(HAVE_LIBSDL_MIXER) +#elif AUDIO_TK == AUDIO_TK_SDL_MIXER typedef struct Mix_Chunk* C4SoundHandle; -#elif defined(USE_OPEN_AL) -#ifdef __APPLE__ -#import -#else -#include -#endif +#elif AUDIO_TK == AUDIO_TK_OPENAL +#include typedef ALuint C4SoundHandle; #else typedef void* C4SoundHandle;