diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 14 | ||||
-rw-r--r-- | src/sound/oalsound/alsound.cpp | 47 | ||||
-rw-r--r-- | src/sound/oalsound/alsound.h | 1 | ||||
-rw-r--r-- | src/sound/oalsound/channel.cpp | 7 |
4 files changed, 43 insertions, 26 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 69164dd..238b8ba 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ # Compile flags as defined in global CMakeLists -set(CMAKE_CXX_FLAGS ${COLOBOT_CXX_FLAGS}) +set(CMAKE_CXX_FLAGS "${COLOBOT_CXX_FLAGS} ${MXE_CFLAGS}") set(CMAKE_CXX_FLAGS_RELEASE ${COLOBOT_CXX_FLAGS_RELEASE}) set(CMAKE_CXX_FLAGS_DEBUG ${COLOBOT_CXX_FLAGS_DEBUG}) @@ -27,11 +27,7 @@ endif() set(OPTIONAL_LIBS "") if (${OPENAL_SOUND}) - if (${MXE}) - set(OPTIONAL_LIBS - ${CMAKE_FIND_ROOT_PATH}/lib/libOpenAL32.a - ) - elseif (${PLATFORM_WINDOWS}) + if (${PLATFORM_WINDOWS}) set(OPTIONAL_LIBS OpenAL32 ) @@ -43,8 +39,6 @@ if (${OPENAL_SOUND}) endif() # Additional libraries per platform -set(PLATFORM_LIBS "") - if (${MXE}) # MXE requires special treatment set(PLATFORM_LIBS ${MXE_LIBS}) elseif (${PLATFORM_WINDOWS}) @@ -201,8 +195,8 @@ ${PNG_LIBRARIES} ${GLEW_LIBRARY} ${Boost_LIBRARIES} ${OPTIONAL_LIBS} -${PLATFORM_LIBS} ${LIBSNDFILE_LIBRARY} +${PLATFORM_LIBS} ) # Local @@ -210,7 +204,6 @@ include_directories( . .. ${CMAKE_CURRENT_BINARY_DIR} -${LIBSNDFILE_INCLUDE_DIR} ) # System @@ -223,6 +216,7 @@ ${PNG_INCLUDE_DIRS} ${GLEW_INCLUDE_PATH} ${Boost_INCLUDE_DIRS} ${OPTIONAL_INCLUDE_DIRS} +${LIBSNDFILE_INCLUDE_DIR} ) link_directories(${CMAKE_CURRENT_SOURCE_DIR}/CBot) diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 0228498..2e44eef 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -29,7 +29,7 @@ ALSound::ALSound() mAudioVolume = 1.0f; mMusicVolume = 1.0f; mMute = false; - mCurrentMusic = new Channel(); + mCurrentMusic = nullptr; auto pointer = CInstanceManager::GetInstancePointer(); if (pointer != nullptr) CInstanceManager::GetInstancePointer()->AddInstance(CLASS_SOUND, this); @@ -90,6 +90,7 @@ bool ALSound::Create(bool b3D) } alcMakeContextCurrent(mContext); + mCurrentMusic = new Channel(); GetLogger()->Info("Done.\n"); mEnabled = true; return true; @@ -125,8 +126,8 @@ bool ALSound::GetEnable() void ALSound::SetAudioVolume(int volume) { - alListenerf(AL_GAIN, MIN(static_cast<float>(volume) / MAXVOLUME, 1.0f)); mAudioVolume = MIN(static_cast<float>(volume) / MAXVOLUME, 1.0f); + alListenerf(AL_GAIN, mAudioVolume); } @@ -141,8 +142,10 @@ int ALSound::GetAudioVolume() void ALSound::SetMusicVolume(int volume) { - alListenerf(AL_GAIN, MIN(static_cast<float>(volume) / MAXVOLUME, 1.0f)); mMusicVolume = MIN(static_cast<float>(volume) / MAXVOLUME, 1.0f); + if (mCurrentMusic) { + mCurrentMusic->SetVolume(mMusicVolume); + } } @@ -310,29 +313,28 @@ int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequenc } int channel; - bool bAlreadyLoaded; + bool bAlreadyLoaded = false; if (!SearchFreeBuffer(sound, channel, bAlreadyLoaded)) return -1; - - bAlreadyLoaded = false; + if (!bAlreadyLoaded) { if (!mChannels[channel]->SetBuffer(mSounds[sound])) { mChannels[channel]->SetBuffer(nullptr); return -1; } } - Position(channel, pos); // setting initial values - mChannels[channel]->SetStartAmplitude(amplitude); + mChannels[channel]->SetStartAmplitude(amplitude * mAudioVolume); mChannels[channel]->SetStartFrequency(frequency); mChannels[channel]->SetChangeFrequency(1.0f); mChannels[channel]->ResetOper(); - mChannels[channel]->SetFrequency(frequency * mChannels[channel]->GetFrequency()); - mChannels[channel]->SetVolume(amplitude); + mChannels[channel]->SetFrequency(frequency); + mChannels[channel]->SetVolume(amplitude * mAudioVolume); mChannels[channel]->SetLoop(bLoop); mChannels[channel]->Play(); + return channel; } @@ -464,10 +466,10 @@ void ALSound::FrameMove(float delta) oper.currentTime += delta; progress = oper.currentTime / oper.totalTime; progress = MIN(progress, 1.0f); - + // setting volume volume = progress * (oper.finalAmplitude - it.second->GetStartAmplitude()); - it.second->SetVolume(volume + it.second->GetStartAmplitude()); + it.second->SetVolume((volume + it.second->GetStartAmplitude()) * mAudioVolume); // setting frequency frequency = progress * (oper.finalFrequency - it.second->GetStartFrequency()) * it.second->GetStartFrequency() * it.second->GetChangeFrequency() * it.second->GetInitFrequency(); @@ -506,7 +508,24 @@ bool ALSound::PlayMusic(int rank, bool bRepeat) } if (static_cast<int>(mCurrentMusic->GetSoundType()) != rank) { - mCurrentMusic->FreeBuffer(); + // check if we have music in cache + for (auto music : mMusicCache) { + if (static_cast<int>(music->GetSoundType()) == rank) { + GetLogger()->Debug("Music loaded from cache\n"); + mCurrentMusic->SetBuffer(music); + + mCurrentMusic->SetVolume(mMusicVolume); + mCurrentMusic->SetLoop(bRepeat); + mCurrentMusic->Play(); + return true; + } + } + + // we cache only 3 music files + if (mMusicCache.size() == 3) { + mCurrentMusic->FreeBuffer(); + mMusicCache.pop_back(); + } if (mMusic.find(rank) == mMusic.end()) { GetLogger()->Info("Requested music %d was not found.\n", rank); @@ -514,8 +533,10 @@ bool ALSound::PlayMusic(int rank, bool bRepeat) } Buffer *buffer = new Buffer(); + mMusicCache.push_front(buffer); buffer->LoadFromFile(mMusic.at(rank), static_cast<Sound>(rank)); mCurrentMusic->SetBuffer(buffer); + mMusicCache[rank] = buffer; } mCurrentMusic->SetVolume(mMusicVolume); diff --git a/src/sound/oalsound/alsound.h b/src/sound/oalsound/alsound.h index 4651e04..530aa5e 100644 --- a/src/sound/oalsound/alsound.h +++ b/src/sound/oalsound/alsound.h @@ -92,5 +92,6 @@ class ALSound : public CSoundInterface ALCcontext* mContext; std::map<Sound, Buffer*> mSounds; std::map<int, Channel*> mChannels; + std::deque<Buffer*> mMusicCache; Channel *mCurrentMusic; }; diff --git a/src/sound/oalsound/channel.cpp b/src/sound/oalsound/channel.cpp index 83420ea..2b9af9b 100644 --- a/src/sound/oalsound/channel.cpp +++ b/src/sound/oalsound/channel.cpp @@ -54,7 +54,7 @@ Channel::~Channel() { bool Channel::Play() { if (!mReady || mBuffer == nullptr) return false; - + alSourcei(mSource, AL_LOOPING, static_cast<ALint>(mLoop)); alSourcePlay(mSource); if (alCheck()) @@ -223,7 +223,8 @@ Sound Channel::GetSoundType() { bool Channel::SetBuffer(Buffer *buffer) { if (!mReady) return false; - + + Stop(); mBuffer = buffer; if (buffer == nullptr) { alSourcei(mSource, AL_BUFFER, 0); @@ -276,7 +277,7 @@ bool Channel::IsReady() { } bool Channel::IsLoaded() { - return mBuffer == nullptr; + return mBuffer != nullptr; } |