Use vector for soundParamList instead of array

This commit is contained in:
Tulio Leao 2020-01-19 22:06:26 -03:00
parent 2d35c50a9a
commit 0aab236c01
2 changed files with 35 additions and 38 deletions

View File

@ -998,35 +998,36 @@ rct_vehicle_sound_params Vehicle::CreateSoundParam(uint16_t priority) const
*
* rct2: 0x006BB9FF
*/
void Vehicle::UpdateSoundParams(
std::array<rct_vehicle_sound_params, AUDIO_MAX_VEHICLE_SOUNDS> vehicleSoundParamsList,
rct_vehicle_sound_params* vehicleSoundParamsListEnd) const
void Vehicle::UpdateSoundParams(std::vector<rct_vehicle_sound_params>& 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<rct_vehicle_sound_params, AUDIO_MAX_VEHICLE_SOUNDS> vehicleSoundParamsList;
rct_vehicle_sound_params* vehicleSoundParamsListEnd;
std::vector<rct_vehicle_sound_params> 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;
}

View File

@ -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<rct_vehicle_sound_params, AUDIO_MAX_VEHICLE_SOUNDS> vehicleSoundParamsList,
rct_vehicle_sound_params* vehicleSoundParamsListEnd) const;
void UpdateSoundParams(std::vector<rct_vehicle_sound_params>& vehicleSoundParamsList) const;
private:
bool SoundCanPlay() const;