From b67fab8fdb0f31d37f4c8f41a290cd782d011de4 Mon Sep 17 00:00:00 2001 From: BeaR Date: Mon, 13 Oct 2014 12:10:56 +0200 Subject: [PATCH] Fix parallax scrolling for audio sources and remove global property --- src/game/client/components/mapsounds.cpp | 81 ++++++++++++++++++------ src/game/editor/layer_sounds.cpp | 1 - src/game/editor/popups.cpp | 3 - src/game/mapitems.h | 1 - 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/src/game/client/components/mapsounds.cpp b/src/game/client/components/mapsounds.cpp index 505bec77b..31c11c89a 100644 --- a/src/game/client/components/mapsounds.cpp +++ b/src/game/client/components/mapsounds.cpp @@ -2,6 +2,7 @@ #include #include +#include #include // envelope #include @@ -120,10 +121,7 @@ void CMapSounds::OnRender() CSourceVoice Voice; Voice.m_pSource = pSource->m_pSource; - if(pSource->m_pSource->m_Global) - Voice.m_Voice = m_pClient->m_pSounds->PlaySample(CSounds::CHN_AMBIENT, m_aSounds[pSource->m_Sound], 1.0f, Flags); - else - Voice.m_Voice = m_pClient->m_pSounds->PlaySampleAt(CSounds::CHN_AMBIENT, m_aSounds[pSource->m_Sound], 1.0f, vec2(fx2f(pSource->m_pSource->m_Position.x), fx2f(pSource->m_pSource->m_Position.y)), Flags); + Voice.m_Voice = m_pClient->m_pSounds->PlaySampleAt(CSounds::CHN_AMBIENT, m_aSounds[pSource->m_Sound], 1.0f, vec2(fx2f(pSource->m_pSource->m_Position.x), fx2f(pSource->m_pSource->m_Position.y)), Flags); Sound()->SetVoiceMaxDistance(Voice.m_Voice, pSource->m_pSource->m_FalloffDistance); @@ -132,31 +130,72 @@ void CMapSounds::OnRender() } } - // update voices - for(int i = 0; i < m_lVoices.size(); i++) + vec2 Center = m_pClient->m_pCamera->m_Center; + for(int g = 0; g < Layers()->NumGroups(); g++) { - CSourceVoice *pVoice = &m_lVoices[i];; + CMapItemGroup *pGroup = Layers()->GetGroup(g); - if(!pVoice->m_Voice.IsValid()) + if(!pGroup) continue; - if(pVoice->m_pSource->m_PosEnv >= 0) + for(int l = 0; l < pGroup->m_NumLayers; l++) { - float aChannels[4]; - CMapLayers::EnvelopeEval(pVoice->m_pSource->m_PosEnvOffset/1000.0f, pVoice->m_pSource->m_PosEnv, aChannels, m_pClient->m_pMapLayersBackGround); - float OffsetX = aChannels[0]; - float OffsetY = aChannels[1]; + CMapItemLayer *pLayer = Layers()->GetLayer(pGroup->m_StartLayer+l); - Sound()->SetVoiceLocation(pVoice->m_Voice, fx2f(pVoice->m_pSource->m_Position.x)+OffsetX, fx2f(pVoice->m_pSource->m_Position.y)+OffsetY); - } + if(!pLayer) + continue; - if(pVoice->m_pSource->m_SoundEnv >= 0) - { - float aChannels[4]; - CMapLayers::EnvelopeEval(pVoice->m_pSource->m_SoundEnvOffset/1000.0f, pVoice->m_pSource->m_SoundEnv, aChannels, m_pClient->m_pMapLayersBackGround); - float Volume = clamp(aChannels[0], 0.0f, 1.0f); + if(pLayer->m_Type == LAYERTYPE_SOUNDS) + { + CMapItemLayerSounds *pSoundLayer = (CMapItemLayerSounds *)pLayer; - Sound()->SetVoiceVolume(pVoice->m_Voice, Volume); + CSoundSource *pSources = (CSoundSource *)Layers()->Map()->GetDataSwapped(pSoundLayer->m_Data); + + if(!pSources) + continue; + + for(int s = 0; s < pSoundLayer->m_NumSources; s++) { + for(int i = 0; i < m_lVoices.size(); i++) + { + CSourceVoice *pVoice = &m_lVoices[i]; + + if(pVoice->m_pSource != &pSources[s]) + continue; + + if(!pVoice->m_Voice.IsValid()) + continue; + + float OffsetX = 0, OffsetY = 0; + + if(pVoice->m_pSource->m_PosEnv >= 0) + { + float aChannels[4]; + CMapLayers::EnvelopeEval(pVoice->m_pSource->m_PosEnvOffset/1000.0f, pVoice->m_pSource->m_PosEnv, aChannels, m_pClient->m_pMapLayersBackGround); + OffsetX = aChannels[0]; + OffsetY = aChannels[1]; + } + + float x = fx2f(pVoice->m_pSource->m_Position.x)+OffsetX; + float y = fx2f(pVoice->m_pSource->m_Position.y)+OffsetY; + + x += Center.x*(1.0f-pGroup->m_ParallaxX/100.0f); + y += Center.y*(1.0f-pGroup->m_ParallaxY/100.0f); + + x -= pGroup->m_OffsetX; y -= pGroup->m_OffsetY; + + Sound()->SetVoiceLocation(pVoice->m_Voice, x, y); + + if(pVoice->m_pSource->m_SoundEnv >= 0) + { + float aChannels[4]; + CMapLayers::EnvelopeEval(pVoice->m_pSource->m_SoundEnvOffset/1000.0f, pVoice->m_pSource->m_SoundEnv, aChannels, m_pClient->m_pMapLayersBackGround); + float Volume = clamp(aChannels[0], 0.0f, 1.0f); + + Sound()->SetVoiceVolume(pVoice->m_Voice, Volume); + } + } + } + } } } } \ No newline at end of file diff --git a/src/game/editor/layer_sounds.cpp b/src/game/editor/layer_sounds.cpp index 7f8afb5a7..b8ff4f56d 100644 --- a/src/game/editor/layer_sounds.cpp +++ b/src/game/editor/layer_sounds.cpp @@ -75,7 +75,6 @@ CSoundSource *CLayerSounds::NewSource() pSource->m_Position.x = 0; pSource->m_Position.y = 0; - pSource->m_Global = 0; pSource->m_Loop = 1; pSource->m_TimeDelay = 0; pSource->m_FalloffDistance = 1500; diff --git a/src/game/editor/popups.cpp b/src/game/editor/popups.cpp index 107b10e93..c92e76798 100644 --- a/src/game/editor/popups.cpp +++ b/src/game/editor/popups.cpp @@ -628,7 +628,6 @@ int CEditor::PopupSource(CEditor *pEditor, CUIRect View) { PROP_POS_X=0, PROP_POS_Y, - PROP_GLOBAL, PROP_LOOP, PROP_TIME_DELAY, PROP_DISTANCE, @@ -642,7 +641,6 @@ int CEditor::PopupSource(CEditor *pEditor, CUIRect View) CProperty aProps[] = { {"Pos X", pSource->m_Position.x/1000, PROPTYPE_INT_SCROLL, -1000000, 1000000}, {"Pos Y", pSource->m_Position.y/1000, PROPTYPE_INT_SCROLL, -1000000, 1000000}, - {"Global", pSource->m_Global, PROPTYPE_BOOL, 0, 1}, {"Loop", pSource->m_Loop, PROPTYPE_BOOL, 0, 1}, {"Delay", pSource->m_TimeDelay, PROPTYPE_INT_SCROLL, 0, 1000000}, {"Distance", pSource->m_FalloffDistance, PROPTYPE_INT_SCROLL, 0, 1000000}, @@ -662,7 +660,6 @@ int CEditor::PopupSource(CEditor *pEditor, CUIRect View) if(Prop == PROP_POS_X) pSource->m_Position.x = NewVal*1000; if(Prop == PROP_POS_Y) pSource->m_Position.y = NewVal*1000; - if(Prop == PROP_GLOBAL) pSource->m_Global = NewVal; if(Prop == PROP_LOOP) pSource->m_Loop = NewVal; if(Prop == PROP_TIME_DELAY) pSource->m_TimeDelay = NewVal; if(Prop == PROP_DISTANCE) pSource->m_FalloffDistance = NewVal; diff --git a/src/game/mapitems.h b/src/game/mapitems.h index a512193e8..b0aa01a95 100644 --- a/src/game/mapitems.h +++ b/src/game/mapitems.h @@ -332,7 +332,6 @@ struct CMapItemEnvelope : public CMapItemEnvelope_v1 struct CSoundSource { CPoint m_Position; - int m_Global; int m_Loop; int m_TimeDelay; // in s int m_FalloffDistance;