1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 21:43:04 +08:00

Remove remaining usage of GameplayClock

This commit is contained in:
Dean Herbert 2022-08-15 19:46:29 +09:00
parent 43442dbf65
commit 704568ae3b
7 changed files with 42 additions and 134 deletions

View File

@ -1,12 +1,8 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Timing; using osu.Framework.Timing;
using osu.Game.Screens.Play; using osu.Game.Screens.Play;
@ -20,20 +16,16 @@ namespace osu.Game.Tests.NonVisual
public void TestTrueGameplayRateWithZeroAdjustment(double underlyingClockRate) public void TestTrueGameplayRateWithZeroAdjustment(double underlyingClockRate)
{ {
var framedClock = new FramedClock(new ManualClock { Rate = underlyingClockRate }); var framedClock = new FramedClock(new ManualClock { Rate = underlyingClockRate });
var gameplayClock = new TestGameplayClock(framedClock); var gameplayClock = new TestGameplayClockContainer(framedClock);
gameplayClock.MutableNonGameplayAdjustments.Add(new BindableDouble());
Assert.That(gameplayClock.TrueGameplayRate, Is.EqualTo(0)); Assert.That(gameplayClock.TrueGameplayRate, Is.EqualTo(0));
} }
private class TestGameplayClock : GameplayClock private class TestGameplayClockContainer : GameplayClockContainer
{ {
public List<Bindable<double>> MutableNonGameplayAdjustments { get; } = new List<Bindable<double>>(); public override IEnumerable<double> NonGameplayAdjustments => new[] { 0.0 };
public override IEnumerable<double> NonGameplayAdjustments => MutableNonGameplayAdjustments.Select(b => b.Value); public TestGameplayClockContainer(IFrameBasedClock underlyingClock)
public TestGameplayClock(IFrameBasedClock underlyingClock)
: base(underlyingClock) : base(underlyingClock)
{ {
} }

View File

@ -39,7 +39,7 @@ namespace osu.Game.Tests.Visual.Gameplay
private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset()); private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset());
[Cached(typeof(IGameplayClock))] [Cached(typeof(IGameplayClock))]
private readonly IGameplayClock gameplayClock = new GameplayClock(new FramedClock()); private readonly IGameplayClock gameplayClock = new GameplayClockContainer(new FramedClock());
// best way to check without exposing. // best way to check without exposing.
private Drawable hideTarget => hudOverlay.KeyCounter; private Drawable hideTarget => hudOverlay.KeyCounter;

View File

@ -30,7 +30,7 @@ namespace osu.Game.Tests.Visual.Gameplay
private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset()); private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset());
[Cached(typeof(IGameplayClock))] [Cached(typeof(IGameplayClock))]
private readonly IGameplayClock gameplayClock = new GameplayClock(new FramedClock()); private readonly IGameplayClock gameplayClock = new GameplayClockContainer(new FramedClock());
[SetUpSteps] [SetUpSteps]
public void SetUpSteps() public void SetUpSteps()

View File

@ -37,7 +37,7 @@ namespace osu.Game.Tests.Visual.Gameplay
private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset()); private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset());
[Cached(typeof(IGameplayClock))] [Cached(typeof(IGameplayClock))]
private readonly IGameplayClock gameplayClock = new GameplayClock(new FramedClock()); private readonly IGameplayClock gameplayClock = new GameplayClockContainer(new FramedClock());
private IEnumerable<HUDOverlay> hudOverlays => CreatedDrawables.OfType<HUDOverlay>(); private IEnumerable<HUDOverlay> hudOverlays => CreatedDrawables.OfType<HUDOverlay>();

View File

@ -1,76 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Timing;
using osu.Framework.Utils;
namespace osu.Game.Screens.Play
{
/// <summary>
/// A clock which is used for gameplay elements that need to follow audio time 1:1.
/// Exposed via DI by <see cref="GameplayClockContainer"/>.
/// <remarks>
/// The main purpose of this clock is to stop components using it from accidentally processing the main
/// <see cref="IFrameBasedClock"/>, as this should only be done once to ensure accuracy.
/// </remarks>
/// </summary>
public class GameplayClock : IGameplayClock
{
internal readonly IFrameBasedClock UnderlyingClock;
public readonly BindableBool IsPaused = new BindableBool();
IBindable<bool> IGameplayClock.IsPaused => IsPaused;
public virtual IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
public GameplayClock(IFrameBasedClock underlyingClock)
{
UnderlyingClock = underlyingClock;
}
public double? StartTime { get; internal set; }
public double CurrentTime => UnderlyingClock.CurrentTime;
public double Rate => UnderlyingClock.Rate;
public double TrueGameplayRate
{
get
{
double baseRate = Rate;
foreach (double adjustment in NonGameplayAdjustments)
{
if (Precision.AlmostEquals(adjustment, 0))
return 0;
baseRate /= adjustment;
}
return baseRate;
}
}
public bool IsRunning => UnderlyingClock.IsRunning;
public void ProcessFrame()
{
// intentionally not updating the underlying clock (handled externally).
}
public double ElapsedFrameTime => UnderlyingClock.ElapsedFrameTime;
public double FramesPerSecond => UnderlyingClock.FramesPerSecond;
public FrameTimeInfo TimeInfo => UnderlyingClock.TimeInfo;
public IClock Source => UnderlyingClock;
}
}

View File

@ -3,13 +3,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Framework.Timing; using osu.Framework.Timing;
using osu.Framework.Utils;
namespace osu.Game.Screens.Play namespace osu.Game.Screens.Play
{ {
@ -40,8 +41,6 @@ namespace osu.Game.Screens.Play
/// </summary> /// </summary>
public event Action? OnSeek; public event Action? OnSeek;
private double? startTime;
/// <summary> /// <summary>
/// The time from which the clock should start. Will be seeked to on calling <see cref="Reset"/>. /// The time from which the clock should start. Will be seeked to on calling <see cref="Reset"/>.
/// </summary> /// </summary>
@ -49,24 +48,14 @@ namespace osu.Game.Screens.Play
/// If not set, a value of zero will be used. /// If not set, a value of zero will be used.
/// Importantly, the value will be inferred from the current ruleset in <see cref="MasterGameplayClockContainer"/> unless specified. /// Importantly, the value will be inferred from the current ruleset in <see cref="MasterGameplayClockContainer"/> unless specified.
/// </remarks> /// </remarks>
public double? StartTime public double? StartTime { get; set; }
{
get => startTime;
set
{
startTime = value;
if (GameplayClock.IsNotNull()) public virtual IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
GameplayClock.StartTime = value;
}
}
public IEnumerable<double> NonGameplayAdjustments => GameplayClock.NonGameplayAdjustments;
/// <summary> /// <summary>
/// The final clock which is exposed to gameplay components. /// The final clock which is exposed to gameplay components.
/// </summary> /// </summary>
protected GameplayClock GameplayClock { get; private set; } = null!; protected IFrameBasedClock GameplayClock { get; private set; } = null!;
/// <summary> /// <summary>
/// Creates a new <see cref="GameplayClockContainer"/>. /// Creates a new <see cref="GameplayClockContainer"/>.
@ -90,9 +79,6 @@ namespace osu.Game.Screens.Play
dependencies.CacheAs<IGameplayClock>(this); dependencies.CacheAs<IGameplayClock>(this);
GameplayClock.StartTime = StartTime;
GameplayClock.IsPaused.BindTo(isPaused);
return dependencies; return dependencies;
} }
@ -126,7 +112,7 @@ namespace osu.Game.Screens.Play
AdjustableSource.Seek(time); AdjustableSource.Seek(time);
// Manually process to make sure the gameplay clock is correctly updated after a seek. // Manually process to make sure the gameplay clock is correctly updated after a seek.
GameplayClock.UnderlyingClock.ProcessFrame(); GameplayClock.ProcessFrame();
OnSeek?.Invoke(); OnSeek?.Invoke();
} }
@ -174,7 +160,7 @@ namespace osu.Game.Screens.Play
protected override void Update() protected override void Update()
{ {
if (!IsPaused.Value) if (!IsPaused.Value)
GameplayClock.UnderlyingClock.ProcessFrame(); GameplayClock.ProcessFrame();
base.Update(); base.Update();
} }
@ -199,7 +185,7 @@ namespace osu.Game.Screens.Play
/// </remarks> /// </remarks>
/// <param name="source">The <see cref="IFrameBasedClock"/> providing the source time.</param> /// <param name="source">The <see cref="IFrameBasedClock"/> providing the source time.</param>
/// <returns>The final <see cref="GameplayClock"/>.</returns> /// <returns>The final <see cref="GameplayClock"/>.</returns>
protected virtual GameplayClock CreateGameplayClock(IFrameBasedClock source) => new GameplayClock(source); protected virtual IFrameBasedClock CreateGameplayClock(IFrameBasedClock source) => source;
#region IAdjustableClock #region IAdjustableClock
@ -238,6 +224,22 @@ namespace osu.Game.Screens.Play
public FrameTimeInfo TimeInfo => GameplayClock.TimeInfo; public FrameTimeInfo TimeInfo => GameplayClock.TimeInfo;
public double TrueGameplayRate => GameplayClock.TrueGameplayRate; public double TrueGameplayRate
{
get
{
double baseRate = Rate;
foreach (double adjustment in NonGameplayAdjustments)
{
if (Precision.AlmostEquals(adjustment, 0))
return 0;
baseRate /= adjustment;
}
return baseRate;
}
}
} }
} }

View File

@ -54,7 +54,6 @@ namespace osu.Game.Screens.Play
private HardwareCorrectionOffsetClock userGlobalOffsetClock = null!; private HardwareCorrectionOffsetClock userGlobalOffsetClock = null!;
private HardwareCorrectionOffsetClock userBeatmapOffsetClock = null!; private HardwareCorrectionOffsetClock userBeatmapOffsetClock = null!;
private HardwareCorrectionOffsetClock platformOffsetClock = null!; private HardwareCorrectionOffsetClock platformOffsetClock = null!;
private MasterGameplayClock masterGameplayClock = null!;
private Bindable<double> userAudioOffset = null!; private Bindable<double> userAudioOffset = null!;
private IDisposable? beatmapOffsetSubscription; private IDisposable? beatmapOffsetSubscription;
@ -150,7 +149,7 @@ namespace osu.Game.Screens.Play
// We must also process underlying gameplay clocks to update rate-adjusted offsets with the new frequency adjustment. // We must also process underlying gameplay clocks to update rate-adjusted offsets with the new frequency adjustment.
// Without doing this, an initial seek may be performed with the wrong offset. // Without doing this, an initial seek may be performed with the wrong offset.
GameplayClock.UnderlyingClock.ProcessFrame(); GameplayClock.ProcessFrame();
} }
} }
@ -191,7 +190,7 @@ namespace osu.Game.Screens.Play
Seek(skipTarget); Seek(skipTarget);
} }
protected override GameplayClock CreateGameplayClock(IFrameBasedClock source) protected override IFrameBasedClock CreateGameplayClock(IFrameBasedClock source)
{ {
// Lazer's audio timings in general doesn't match stable. This is the result of user testing, albeit limited. // Lazer's audio timings in general doesn't match stable. This is the result of user testing, albeit limited.
// This only seems to be required on windows. We need to eventually figure out why, with a bit of luck. // This only seems to be required on windows. We need to eventually figure out why, with a bit of luck.
@ -199,9 +198,7 @@ namespace osu.Game.Screens.Play
// the final usable gameplay clock with user-set offsets applied. // the final usable gameplay clock with user-set offsets applied.
userGlobalOffsetClock = new HardwareCorrectionOffsetClock(platformOffsetClock, pauseFreqAdjust); userGlobalOffsetClock = new HardwareCorrectionOffsetClock(platformOffsetClock, pauseFreqAdjust);
userBeatmapOffsetClock = new HardwareCorrectionOffsetClock(userGlobalOffsetClock, pauseFreqAdjust); return userBeatmapOffsetClock = new HardwareCorrectionOffsetClock(userGlobalOffsetClock, pauseFreqAdjust);
return masterGameplayClock = new MasterGameplayClock(userBeatmapOffsetClock);
} }
/// <summary> /// <summary>
@ -224,8 +221,8 @@ namespace osu.Game.Screens.Play
Track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust); Track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
Track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate); Track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
masterGameplayClock.MutableNonGameplayAdjustments.Add(pauseFreqAdjust); nonGameplayAdjustments.Add(pauseFreqAdjust);
masterGameplayClock.MutableNonGameplayAdjustments.Add(UserPlaybackRate); nonGameplayAdjustments.Add(UserPlaybackRate);
speedAdjustmentsApplied = true; speedAdjustmentsApplied = true;
} }
@ -238,8 +235,8 @@ namespace osu.Game.Screens.Play
Track.RemoveAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust); Track.RemoveAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
Track.RemoveAdjustment(AdjustableProperty.Tempo, UserPlaybackRate); Track.RemoveAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
masterGameplayClock.MutableNonGameplayAdjustments.Remove(pauseFreqAdjust); nonGameplayAdjustments.Remove(pauseFreqAdjust);
masterGameplayClock.MutableNonGameplayAdjustments.Remove(UserPlaybackRate); nonGameplayAdjustments.Remove(UserPlaybackRate);
speedAdjustmentsApplied = false; speedAdjustmentsApplied = false;
} }
@ -252,7 +249,7 @@ namespace osu.Game.Screens.Play
} }
ControlPointInfo IBeatSyncProvider.ControlPoints => beatmap.Beatmap.ControlPointInfo; ControlPointInfo IBeatSyncProvider.ControlPoints => beatmap.Beatmap.ControlPointInfo;
IClock IBeatSyncProvider.Clock => GameplayClock; IClock IBeatSyncProvider.Clock => this;
ChannelAmplitudes IHasAmplitudes.CurrentAmplitudes => beatmap.TrackLoaded ? beatmap.Track.CurrentAmplitudes : ChannelAmplitudes.Empty; ChannelAmplitudes IHasAmplitudes.CurrentAmplitudes => beatmap.TrackLoaded ? beatmap.Track.CurrentAmplitudes : ChannelAmplitudes.Empty;
private class HardwareCorrectionOffsetClock : FramedOffsetClock private class HardwareCorrectionOffsetClock : FramedOffsetClock
@ -300,15 +297,8 @@ namespace osu.Game.Screens.Play
} }
} }
private class MasterGameplayClock : GameplayClock private readonly List<Bindable<double>> nonGameplayAdjustments = new List<Bindable<double>>();
{
public readonly List<Bindable<double>> MutableNonGameplayAdjustments = new List<Bindable<double>>();
public override IEnumerable<double> NonGameplayAdjustments => MutableNonGameplayAdjustments.Select(b => b.Value);
public MasterGameplayClock(FramedOffsetClock underlyingClock) public override IEnumerable<double> NonGameplayAdjustments => nonGameplayAdjustments.Select(b => b.Value);
: base(underlyingClock)
{
}
}
} }
} }