diff --git a/src/openrct2/audio/Audio.cpp b/src/openrct2/audio/Audio.cpp index 7faedd1df8..508a9cd1cb 100644 --- a/src/openrct2/audio/Audio.cpp +++ b/src/openrct2/audio/Audio.cpp @@ -53,8 +53,6 @@ rct_ride_music_params gRideMusicParamsList[AUDIO_MAX_RIDE_MUSIC]; rct_ride_music_params* gRideMusicParamsListEnd; rct_vehicle_sound gVehicleSoundList[AUDIO_MAX_VEHICLE_SOUNDS]; -rct_vehicle_sound_params gVehicleSoundParamsList[AUDIO_MAX_VEHICLE_SOUNDS]; -rct_vehicle_sound_params* gVehicleSoundParamsListEnd; // clang-format off static int32_t SoundVolumeAdjust[RCT2SoundCount] = diff --git a/src/openrct2/audio/audio.h b/src/openrct2/audio/audio.h index 0645292d24..b326b55ed9 100644 --- a/src/openrct2/audio/audio.h +++ b/src/openrct2/audio/audio.h @@ -168,8 +168,6 @@ extern rct_ride_music_params gRideMusicParamsList[AUDIO_MAX_RIDE_MUSIC]; extern rct_ride_music_params* gRideMusicParamsListEnd; extern rct_vehicle_sound gVehicleSoundList[AUDIO_MAX_VEHICLE_SOUNDS]; -extern rct_vehicle_sound_params gVehicleSoundParamsList[AUDIO_MAX_VEHICLE_SOUNDS]; -extern rct_vehicle_sound_params* gVehicleSoundParamsListEnd; /** * Deregisters the audio device. diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index f959fd6183..2aed396a03 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -998,34 +998,36 @@ rct_vehicle_sound_params Vehicle::CreateSoundParam(uint16_t priority) const * * rct2: 0x006BB9FF */ -void Vehicle::UpdateSoundParams() const +void Vehicle::UpdateSoundParams(std::vector& vehicleSoundParamsList) const { if (!SoundCanPlay()) return; uint16_t soundPriority = GetSoundPriority(); - rct_vehicle_sound_params* soundParam; // Find a sound param of lower priority to use - for (soundParam = &gVehicleSoundParamsList[0]; - soundParam < gVehicleSoundParamsListEnd && soundPriority <= soundParam->priority; soundParam++) - ; + auto soundParamIter = std::find_if( + vehicleSoundParamsList.begin(), vehicleSoundParamsList.end(), + [soundPriority](rct_vehicle_sound_params param) { return soundPriority > param.priority; }); - if (soundParam >= &gVehicleSoundParamsList[std::size(gVehicleSoundParamsList)]) - return; - - if (gVehicleSoundParamsListEnd < &gVehicleSoundParamsList[std::size(gVehicleSoundParamsList)]) + if (soundParamIter == std::end(vehicleSoundParamsList)) { - gVehicleSoundParamsListEnd++; + if (vehicleSoundParamsList.size() < AUDIO_MAX_VEHICLE_SOUNDS) + { + vehicleSoundParamsList.push_back(CreateSoundParam(soundPriority)); + } } - - // Shift all sound params down one if using a free space - if (soundParam != gVehicleSoundParamsListEnd) + else { - std::memmove( - soundParam + 1, soundParam, ((gVehicleSoundParamsListEnd - soundParam) - 1) * sizeof(rct_vehicle_sound_params)); + if (vehicleSoundParamsList.size() < AUDIO_MAX_VEHICLE_SOUNDS) + { + // Shift all sound params down one if using a free space + vehicleSoundParamsList.insert(soundParamIter, CreateSoundParam(soundPriority)); + } + else + { + *soundParamIter = CreateSoundParam(soundPriority); + } } - - *soundParam = CreateSoundParam(soundPriority); } static void vehicle_sounds_update_window_setup() @@ -1272,12 +1274,14 @@ void vehicle_sounds_update() if (gAudioCurrentDevice == -1 || gGameSoundsOff || !gConfigSound.sound_enabled || gOpenRCT2Headless) return; + std::vector vehicleSoundParamsList; + vehicleSoundParamsList.reserve(AUDIO_MAX_VEHICLE_SOUNDS); + vehicle_sounds_update_window_setup(); - gVehicleSoundParamsListEnd = &gVehicleSoundParamsList[0]; for (uint16_t i = gSpriteListHead[SPRITE_LIST_VEHICLE_HEAD]; i != SPRITE_INDEX_NULL; i = get_sprite(i)->vehicle.next) { - get_sprite(i)->vehicle.UpdateSoundParams(); + get_sprite(i)->vehicle.UpdateSoundParams(vehicleSoundParamsList); } // Stop all playing sounds that no longer have priority to play after vehicle_update_sound_params @@ -1286,10 +1290,9 @@ void vehicle_sounds_update() if (vehicle_sound.id != SOUND_ID_NULL) { bool keepPlaying = false; - for (rct_vehicle_sound_params* vehicle_sound_params = &gVehicleSoundParamsList[0]; - vehicle_sound_params != gVehicleSoundParamsListEnd; vehicle_sound_params++) + for (auto vehicleSoundParams : vehicleSoundParamsList) { - if (vehicle_sound.id == vehicle_sound_params->id) + if (vehicle_sound.id == vehicleSoundParams.id) { keepPlaying = true; break; @@ -1311,21 +1314,20 @@ void vehicle_sounds_update() } } - for (rct_vehicle_sound_params* vehicleSoundParams = &gVehicleSoundParamsList[0]; - vehicleSoundParams < gVehicleSoundParamsListEnd; vehicleSoundParams++) + for (auto& vehicleSoundParams : vehicleSoundParamsList) { - uint8_t panVol = vehicle_sounds_update_get_pan_volume(vehicleSoundParams); + uint8_t panVol = vehicle_sounds_update_get_pan_volume(&vehicleSoundParams); - rct_vehicle_sound* vehicleSound = vehicle_sounds_update_get_vehicle_sound(vehicleSoundParams); + rct_vehicle_sound* vehicleSound = vehicle_sounds_update_get_vehicle_sound(&vehicleSoundParams); // No free vehicle sound slots (RCT2 corrupts the pointer here) if (vehicleSound == nullptr) continue; // Move the Sound Volume towards the SoundsParam Volume int32_t tempvolume = vehicleSound->volume; - if (tempvolume != vehicleSoundParams->volume) + if (tempvolume != vehicleSoundParams.volume) { - if (tempvolume < vehicleSoundParams->volume) + if (tempvolume < vehicleSoundParams.volume) { tempvolume += 4; } @@ -1337,9 +1339,9 @@ void vehicle_sounds_update() vehicleSound->volume = tempvolume; panVol = std::max(0, panVol - tempvolume); - Vehicle* vehicle = GET_VEHICLE(vehicleSoundParams->id); - vehicle_sounds_update_sound_1(vehicle, vehicleSoundParams, vehicleSound, panVol); - vehicle_sounds_update_sound_2(vehicle, vehicleSoundParams, vehicleSound, panVol); + Vehicle* vehicle = GET_VEHICLE(vehicleSoundParams.id); + vehicle_sounds_update_sound_1(vehicle, &vehicleSoundParams, vehicleSound, panVol); + vehicle_sounds_update_sound_2(vehicle, &vehicleSoundParams, vehicleSound, panVol); } } @@ -7901,7 +7903,8 @@ static void sub_6DBF3E(Vehicle* vehicle) if (vehicleEntry->flags & VEHICLE_ENTRY_FLAG_GO_KART) { // Determine the stop positions for the karts. If in left lane it's further along the track than the right lane. - // Since it's not possible to overtake when the race has ended, this does not check for overtake states (7 and 8). + // Since it's not possible to overtake when the race has ended, this does not check for overtake states (7 and + // 8). cx = vehicle->TrackSubposition == VEHICLE_TRACK_SUBPOSITION_GO_KARTS_RIGHT_LANE ? 18 : 20; } diff --git a/src/openrct2/ride/Vehicle.h b/src/openrct2/ride/Vehicle.h index c6137ad4e1..c76efbd2d0 100644 --- a/src/openrct2/ride/Vehicle.h +++ b/src/openrct2/ride/Vehicle.h @@ -10,6 +10,7 @@ #ifndef _VEHICLE_H_ #define _VEHICLE_H_ +#include "../audio/audio.h" #include "../common.h" #include "../ride/RideTypes.h" #include "../world/Location.hpp" @@ -299,7 +300,7 @@ struct Vehicle : SpriteBase void Invalidate(); void SetState(VEHICLE_STATUS vehicleStatus, uint8_t subState = 0); bool IsGhost() const; - void UpdateSoundParams() const; + void UpdateSoundParams(std::vector& vehicleSoundParamsList) const; private: bool SoundCanPlay() const;