From 2d35c50a9a1ff749df917d003cb6b0d1cc2fad33 Mon Sep 17 00:00:00 2001 From: UltimaBGD Date: Mon, 7 Oct 2019 17:01:57 -0400 Subject: [PATCH 1/2] Refactor gVehicleSoundParamsList to use std::array --- src/openrct2/audio/Audio.cpp | 2 -- src/openrct2/audio/audio.h | 2 -- src/openrct2/ride/Vehicle.cpp | 34 +++++++++++++++++++--------------- src/openrct2/ride/Vehicle.h | 5 ++++- 4 files changed, 23 insertions(+), 20 deletions(-) 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 613890a82a..4960b6f528 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -998,7 +998,9 @@ rct_vehicle_sound_params Vehicle::CreateSoundParam(uint16_t priority) const * * rct2: 0x006BB9FF */ -void Vehicle::UpdateSoundParams() const +void Vehicle::UpdateSoundParams( + std::array vehicleSoundParamsList, + rct_vehicle_sound_params* vehicleSoundParamsListEnd) const { if (!SoundCanPlay()) return; @@ -1006,25 +1008,24 @@ void Vehicle::UpdateSoundParams() const 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++) + for (soundParam = &vehicleSoundParamsList[0]; + soundParam < vehicleSoundParamsListEnd && soundPriority <= soundParam->priority; soundParam++) ; - if (soundParam >= &gVehicleSoundParamsList[std::size(gVehicleSoundParamsList)]) + if (soundParam >= &vehicleSoundParamsList[std::size(vehicleSoundParamsList)]) return; - if (gVehicleSoundParamsListEnd < &gVehicleSoundParamsList[std::size(gVehicleSoundParamsList)]) + if (vehicleSoundParamsListEnd < &vehicleSoundParamsList[std::size(vehicleSoundParamsList)]) { - gVehicleSoundParamsListEnd++; + vehicleSoundParamsListEnd++; } // Shift all sound params down one if using a free space - if (soundParam != gVehicleSoundParamsListEnd) + if (soundParam != vehicleSoundParamsListEnd) { std::memmove( - soundParam + 1, soundParam, ((gVehicleSoundParamsListEnd - soundParam) - 1) * sizeof(rct_vehicle_sound_params)); + soundParam + 1, soundParam, ((vehicleSoundParamsListEnd - soundParam) - 1) * sizeof(rct_vehicle_sound_params)); } - *soundParam = CreateSoundParam(soundPriority); } @@ -1272,12 +1273,15 @@ void vehicle_sounds_update() if (gAudioCurrentDevice == -1 || gGameSoundsOff || !gConfigSound.sound_enabled || gOpenRCT2Headless) return; + std::array vehicleSoundParamsList; + rct_vehicle_sound_params* vehicleSoundParamsListEnd; + vehicle_sounds_update_window_setup(); - gVehicleSoundParamsListEnd = &gVehicleSoundParamsList[0]; + vehicleSoundParamsListEnd = &vehicleSoundParamsList[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, vehicleSoundParamsListEnd); } // Stop all playing sounds that no longer have priority to play after vehicle_update_sound_params @@ -1286,8 +1290,8 @@ 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 (rct_vehicle_sound_params* vehicle_sound_params = &vehicleSoundParamsList[0]; + vehicle_sound_params != vehicleSoundParamsListEnd; vehicle_sound_params++) { if (vehicle_sound.id == vehicle_sound_params->id) { @@ -1311,8 +1315,8 @@ void vehicle_sounds_update() } } - for (rct_vehicle_sound_params* vehicleSoundParams = &gVehicleSoundParamsList[0]; - vehicleSoundParams < gVehicleSoundParamsListEnd; vehicleSoundParams++) + for (rct_vehicle_sound_params* vehicleSoundParams = &vehicleSoundParamsList[0]; + vehicleSoundParams < vehicleSoundParamsListEnd; vehicleSoundParams++) { uint8_t panVol = vehicle_sounds_update_get_pan_volume(vehicleSoundParams); diff --git a/src/openrct2/ride/Vehicle.h b/src/openrct2/ride/Vehicle.h index 3afeb66878..53369b0beb 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" @@ -301,7 +302,9 @@ struct Vehicle : SpriteBase void Invalidate(); void SetState(VEHICLE_STATUS vehicleStatus, uint8_t subState = 0); bool IsGhost() const; - void UpdateSoundParams() const; + void UpdateSoundParams( + std::array vehicleSoundParamsList, + rct_vehicle_sound_params* vehicleSoundParamsListEnd) const; private: bool SoundCanPlay() const; From 0aab236c011d3e9bed5668ed57fbbd5a1f8d5222 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Sun, 19 Jan 2020 22:06:26 -0300 Subject: [PATCH 2/2] Use vector for soundParamList instead of array --- src/openrct2/ride/Vehicle.cpp | 69 +++++++++++++++++------------------ src/openrct2/ride/Vehicle.h | 4 +- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index 4960b6f528..cb85c37cc5 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -998,35 +998,36 @@ rct_vehicle_sound_params Vehicle::CreateSoundParam(uint16_t priority) const * * rct2: 0x006BB9FF */ -void Vehicle::UpdateSoundParams( - std::array vehicleSoundParamsList, - rct_vehicle_sound_params* vehicleSoundParamsListEnd) 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 = &vehicleSoundParamsList[0]; - soundParam < vehicleSoundParamsListEnd && 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 >= &vehicleSoundParamsList[std::size(vehicleSoundParamsList)]) - return; - - if (vehicleSoundParamsListEnd < &vehicleSoundParamsList[std::size(vehicleSoundParamsList)]) + if (soundParamIter == std::end(vehicleSoundParamsList)) { - vehicleSoundParamsListEnd++; + 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 != vehicleSoundParamsListEnd) + else { - std::memmove( - soundParam + 1, soundParam, ((vehicleSoundParamsListEnd - 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() @@ -1273,15 +1274,14 @@ void vehicle_sounds_update() if (gAudioCurrentDevice == -1 || gGameSoundsOff || !gConfigSound.sound_enabled || gOpenRCT2Headless) return; - std::array vehicleSoundParamsList; - rct_vehicle_sound_params* vehicleSoundParamsListEnd; + std::vector vehicleSoundParamsList; + vehicleSoundParamsList.reserve(AUDIO_MAX_VEHICLE_SOUNDS); vehicle_sounds_update_window_setup(); - vehicleSoundParamsListEnd = &vehicleSoundParamsList[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(vehicleSoundParamsList, vehicleSoundParamsListEnd); + get_sprite(i)->vehicle.UpdateSoundParams(vehicleSoundParamsList); } // Stop all playing sounds that no longer have priority to play after vehicle_update_sound_params @@ -1290,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 = &vehicleSoundParamsList[0]; - vehicle_sound_params != vehicleSoundParamsListEnd; vehicle_sound_params++) + for (auto vehicleSoundParams : vehicleSoundParamsList) { - if (vehicle_sound.id == vehicle_sound_params->id) + if (vehicle_sound.id == vehicleSoundParams.id) { keepPlaying = true; break; @@ -1315,21 +1314,20 @@ void vehicle_sounds_update() } } - for (rct_vehicle_sound_params* vehicleSoundParams = &vehicleSoundParamsList[0]; - vehicleSoundParams < vehicleSoundParamsListEnd; 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; } @@ -1341,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); } } @@ -7921,7 +7919,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 53369b0beb..d9b106259c 100644 --- a/src/openrct2/ride/Vehicle.h +++ b/src/openrct2/ride/Vehicle.h @@ -302,9 +302,7 @@ struct Vehicle : SpriteBase void Invalidate(); void SetState(VEHICLE_STATUS vehicleStatus, uint8_t subState = 0); bool IsGhost() const; - void UpdateSoundParams( - std::array vehicleSoundParamsList, - rct_vehicle_sound_params* vehicleSoundParamsListEnd) const; + void UpdateSoundParams(std::vector& vehicleSoundParamsList) const; private: bool SoundCanPlay() const;