Skip to content

Commit

Permalink
Added missing #if defined(WITH_AUDIO)
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Jun 6, 2024
1 parent c4997c1 commit 3036e5b
Show file tree
Hide file tree
Showing 36 changed files with 169 additions and 73 deletions.
4 changes: 4 additions & 0 deletions Sources/Jazz2/Actors/ActorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,17 @@ namespace Jazz2::Actors

std::shared_ptr<AudioBufferPlayer> ActorBase::PlaySfx(const StringView identifier, float gain, float pitch)
{
#if defined(WITH_AUDIO)
auto it = _metadata->Sounds.find(String::nullTerminatedView(identifier));
if (it != _metadata->Sounds.end()) {
int idx = (it->second.Buffers.size() > 1 ? Random().Next(0, (int)it->second.Buffers.size()) : 0);
return _levelHandler->PlaySfx(this, identifier, &it->second.Buffers[idx]->Buffer, Vector3f(_pos.X, _pos.Y, 0.0f), false, gain, pitch);
} else {
return nullptr;
}
#else
return nullptr;
#endif
}

bool ActorBase::SetAnimation(AnimState state, bool skipAnimation)
Expand Down
6 changes: 6 additions & 0 deletions Sources/Jazz2/Actors/Enemies/Bosses/TurtleBoss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ namespace Jazz2::Actors::Bosses

TurtleBoss::Mace::~Mace()
{
#if defined(WITH_AUDIO)
if (_sound != nullptr) {
_sound->stop();
_sound = nullptr;
}
#endif
}

Task<bool> TurtleBoss::Mace::OnActivatedAsync(const ActorActivationDetails& details)
Expand All @@ -225,10 +227,12 @@ namespace Jazz2::Actors::Bosses

FollowNearestPlayer();

#if defined(WITH_AUDIO)
_sound = PlaySfx("Mace"_s, 0.7f);
if (_sound != nullptr) {
_sound->setLooping(true);
}
#endif

async_return true;
}
Expand All @@ -237,9 +241,11 @@ namespace Jazz2::Actors::Bosses
{
MoveInstantly(Vector2f(_speed.X * timeMult, _speed.Y * timeMult), MoveType::Relative | MoveType::Force);

#if defined(WITH_AUDIO)
if (_sound != nullptr) {
_sound->setPosition(Vector3f(_pos.X, _pos.Y, 0.8f));
}
#endif

if (_returning) {
Vector2f diff = (_targetSpeed - _speed);
Expand Down
2 changes: 2 additions & 0 deletions Sources/Jazz2/Actors/Enemies/Bosses/TurtleBoss.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ namespace Jazz2::Actors::Bosses
bool _returning;
float _returnTime;
Vector2f _targetSpeed;
#if defined(WITH_AUDIO)
std::shared_ptr<AudioBufferPlayer> _sound;
#endif

void FollowNearestPlayer();
};
Expand Down
6 changes: 6 additions & 0 deletions Sources/Jazz2/Actors/Environment/Copter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ namespace Jazz2::Actors::Environment

Copter::~Copter()
{
#if defined(WITH_AUDIO)
if (_noise != nullptr) {
_noise->stop();
_noise = nullptr;
}
#endif
}

Task<bool> Copter::OnActivatedAsync(const ActorActivationDetails& details)
Expand Down Expand Up @@ -56,6 +58,7 @@ namespace Jazz2::Actors::Environment
}
}

#if defined(WITH_AUDIO)
if (_noise != nullptr) {
float newGain = _noise->gain() - (_noiseDec * timeMult);
if (newGain <= 0.0f) {
Expand All @@ -66,6 +69,7 @@ namespace Jazz2::Actors::Environment
_noise->setPosition(Vector3f(_pos.X, _pos.Y, 0.8f));
}
}
#endif
}

bool Copter::OnHandleCollision(std::shared_ptr<ActorBase> other)
Expand All @@ -91,11 +95,13 @@ namespace Jazz2::Actors::Environment
_state = State::Unmounted;
_phase = timeLeft;

#if defined(WITH_AUDIO)
_noise = PlaySfx("Copter"_s, 0.8f, 0.8f);
if (_noise != nullptr) {
_noise->setLooping(true);
_noiseDec = _noise->gain() * 0.005f;
}
#endif

SetState(ActorState::ApplyGravitation, true);
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Jazz2/Actors/Environment/Copter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ namespace Jazz2::Actors::Environment
Vector2f _originPos;
float _phase;
State _state;
#if defined(WITH_AUDIO)
std::shared_ptr<AudioBufferPlayer> _noise;
#endif
float _noiseDec;
};
}
37 changes: 31 additions & 6 deletions Sources/Jazz2/Actors/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ namespace Jazz2::Actors
_spawnedBird->FlyAway();
_spawnedBird = nullptr;
}
#if defined(WITH_AUDIO)
if (_copterSound != nullptr) {
_copterSound->stop();
_copterSound = nullptr;
Expand All @@ -115,6 +116,7 @@ namespace Jazz2::Actors
_weaponSound->stop();
_weaponSound = nullptr;
}
#endif
}

Task<bool> Player::OnActivatedAsync(const ActorActivationDetails& details)
Expand Down Expand Up @@ -535,6 +537,7 @@ namespace Jazz2::Actors
}
}

#if defined(WITH_AUDIO)
if (_copterSound != nullptr) {
if ((_currentAnimation->State & AnimState::Copter) == AnimState::Copter) {
_copterSound->setPosition(Vector3f(_pos.X, _pos.Y, 0.8f));
Expand All @@ -543,6 +546,7 @@ namespace Jazz2::Actors
_copterSound = nullptr;
}
}
#endif

// Trail
if (PreferencesCache::ShowPlayerTrails) {
Expand Down Expand Up @@ -601,12 +605,13 @@ namespace Jazz2::Actors
SetState(ActorState::ApplyGravitation | ActorState::CollideWithTileset, true);
} else {
// Skip controls, player is not controllable in tube
#if defined(WITH_AUDIO)
// Weapons are automatically disabled if player is not controllable
if (_weaponSound != nullptr) {
_weaponSound->stop();
_weaponSound = nullptr;
}

#endif
return;
}
}
Expand Down Expand Up @@ -700,14 +705,15 @@ namespace Jazz2::Actors
}

if (!_controllable || !_controllableExternal || _currentSpecialMove == SpecialMoveType::Buttstomp) {
#if defined(WITH_AUDIO)
// Weapons are automatically disabled if player is not controllable
if (_currentWeapon != WeaponType::Thunderbolt || _fireFramesLeft <= 0.0f) {
if (_weaponSound != nullptr) {
_weaponSound->stop();
_weaponSound = nullptr;
}
}

#endif
return;
}

Expand Down Expand Up @@ -834,13 +840,14 @@ namespace Jazz2::Actors
SetAnimation(AnimState::Copter);
}
_copterFramesLeft = 70.0f;

#if defined(WITH_AUDIO)
if (_copterSound == nullptr) {
_copterSound = PlaySfx("Copter"_s, 0.6f, 1.5f);
if (_copterSound != nullptr) {
_copterSound->setLooping(true);
}
}
#endif
}
}
break;
Expand Down Expand Up @@ -891,13 +898,14 @@ namespace Jazz2::Actors
SetAnimation(AnimState::Copter);
}
_copterFramesLeft = 70.0f;

#if defined(WITH_AUDIO)
if (_copterSound == nullptr) {
_copterSound = PlaySfx("Copter"_s, 0.6f, 1.5f);
if (_copterSound != nullptr) {
_copterSound->setLooping(true);
}
}
#endif
}
}
break;
Expand Down Expand Up @@ -1000,6 +1008,7 @@ namespace Jazz2::Actors
_weaponCooldown = 0.0f;
}

#if defined(WITH_AUDIO)
if (_weaponSound != nullptr) {
if (weaponInUse) {
_weaponSound->setPosition(Vector3f(_pos.X, _pos.Y, 0.8f));
Expand All @@ -1008,6 +1017,7 @@ namespace Jazz2::Actors
_weaponSound = nullptr;
}
}
#endif

if (_controllable && _controllableExternal && _playerType != PlayerType::Frog) {
bool isGamepad;
Expand Down Expand Up @@ -2407,13 +2417,17 @@ namespace Jazz2::Actors

std::shared_ptr<AudioBufferPlayer> Player::PlayPlayerSfx(const StringView identifier, float gain, float pitch)
{
#if defined(WITH_AUDIO)
auto it = _metadata->Sounds.find(String::nullTerminatedView(identifier));
if (it != _metadata->Sounds.end()) {
int idx = (it->second.Buffers.size() > 1 ? Random().Next(0, (int)it->second.Buffers.size()) : 0);
return _levelHandler->PlaySfx(this, identifier, &it->second.Buffers[idx]->Buffer, Vector3f(0.0f, 0.0f, 0.0f), true, gain, pitch);
} else {
return nullptr;
}
#else
return nullptr;
#endif
}

bool Player::SetPlayerTransition(AnimState state, bool cancellable, bool removeControl, SpecialMoveType specialMove, const std::function<void()>& callback)
Expand Down Expand Up @@ -2448,6 +2462,8 @@ namespace Jazz2::Actors
void Player::OnPerishInner()
{
_trailLastPos = _pos;

#if defined(WITH_AUDIO)
if (_copterSound != nullptr) {
_copterSound->stop();
_copterSound = nullptr;
Expand All @@ -2456,6 +2472,7 @@ namespace Jazz2::Actors
_weaponSound->stop();
_weaponSound = nullptr;
}
#endif

SetState(ActorState::CanJump, false);
_speed.X = 0.0f;
Expand Down Expand Up @@ -2551,10 +2568,12 @@ namespace Jazz2::Actors

void Player::SwitchToNextWeapon()
{
#if defined(WITH_AUDIO)
if (_weaponSound != nullptr) {
_weaponSound->stop();
_weaponSound = nullptr;
}
#endif

// Find next available weapon
WeaponType weaponType = (WeaponType)(((std::int32_t)_currentWeapon + 1) % (std::int32_t)WeaponType::Count);
Expand All @@ -2572,12 +2591,12 @@ namespace Jazz2::Actors
PlayPlayerSfx("ChangeWeapon"_s);
return;
}

#if defined(WITH_AUDIO)
if (_weaponSound != nullptr) {
_weaponSound->stop();
_weaponSound = nullptr;
}

#endif
SetCurrentWeapon((WeaponType)weaponIndex);
_weaponCooldown = 1.0f;
}
Expand Down Expand Up @@ -2744,6 +2763,7 @@ namespace Jazz2::Actors
AddExternalForce(IsFacingLeft() ? 2.0f : -2.0f, 0.0f);
}

#if defined(WITH_AUDIO)
if (_weaponSound == nullptr) {
PlaySfx("WeaponThunderboltStart"_s, 0.5f);
_weaponSound = PlaySfx("WeaponThunderbolt"_s, 1.0f);
Expand All @@ -2753,6 +2773,7 @@ namespace Jazz2::Actors
_weaponSound->setLowPass(0.9f);
}
}
#endif

return true;
}
Expand Down Expand Up @@ -2805,12 +2826,14 @@ namespace Jazz2::Actors
return false;
}
FireWeapon<Weapons::ToasterShot, WeaponType::Toaster>(6.0f, 0.0f);
#if defined(WITH_AUDIO)
if (_weaponSound == nullptr) {
_weaponSound = PlaySfx("WeaponToaster"_s, 0.6f);
if (_weaponSound != nullptr) {
_weaponSound->setLooping(true);
}
}
#endif
ammoDecrease = 50;
break;
}
Expand Down Expand Up @@ -3402,12 +3425,14 @@ namespace Jazz2::Actors

_copterFramesLeft = 10.0f * FrameTimer::FramesPerSecond;

#if defined(WITH_AUDIO)
if (_copterSound == nullptr) {
_copterSound = PlaySfx("Copter"_s, 0.6f, 1.5f);
if (_copterSound != nullptr) {
_copterSound->setLooping(true);
}
}
#endif
break;
}
case Modifier::LizardCopter: {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Jazz2/Actors/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ namespace Jazz2::Actors
bool _canDoubleJump;
float _externalForceCooldown;
float _springCooldown;
#if defined(WITH_AUDIO)
std::shared_ptr<AudioBufferPlayer> _copterSound;
#endif

std::int32_t _lives, _coins, _coinsCheckpoint, _foodEaten, _foodEatenCheckpoint, _score;
Vector2f _checkpointPos;
Expand Down Expand Up @@ -264,7 +266,9 @@ namespace Jazz2::Actors
std::uint16_t _weaponAmmoCheckpoint[(std::int32_t)WeaponType::Count];
std::uint8_t _weaponUpgrades[(std::int32_t)WeaponType::Count];
std::uint8_t _weaponUpgradesCheckpoint[(std::int32_t)WeaponType::Count];
#if defined(WITH_AUDIO)
std::shared_ptr<AudioBufferPlayer> _weaponSound;
#endif
WeaponWheelState _weaponWheelState;

Task<bool> OnActivatedAsync(const ActorActivationDetails& details) override;
Expand Down
3 changes: 1 addition & 2 deletions Sources/Jazz2/Actors/Weapons/BlasterShot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ using namespace Jazz2::Tiles;
namespace Jazz2::Actors::Weapons
{
BlasterShot::BlasterShot()
:
_fired(0)
: _fired(0)
{
}

Expand Down
5 changes: 1 addition & 4 deletions Sources/Jazz2/Actors/Weapons/BouncerShot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ using namespace Jazz2::Tiles;
namespace Jazz2::Actors::Weapons
{
BouncerShot::BouncerShot()
:
_fired(0),
_hitLimit(0.0f),
_targetSpeedX(0.0f)
: _fired(0), _hitLimit(0.0f), _targetSpeedX(0.0f)
{
}

Expand Down
5 changes: 1 addition & 4 deletions Sources/Jazz2/Actors/Weapons/ElectroShot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ using namespace Jazz2::Tiles;
namespace Jazz2::Actors::Weapons
{
ElectroShot::ElectroShot()
:
_fired(0),
_currentStep(0.0f),
_particleSpawnTime(0.0f)
: _fired(0), _currentStep(0.0f), _particleSpawnTime(0.0f)
{
}

Expand Down
Loading

0 comments on commit 3036e5b

Please sign in to comment.