Skip to content

Commit

Permalink
Fpsの描画を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Aug 29, 2023
1 parent 1340d3e commit 5b479d2
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/Beutl.Engine/Rendering/FpsText.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
using Beutl.Graphics;
using Beutl.Graphics.Shapes;
using Beutl.Media;
using Beutl.Media.Immutable;

namespace Beutl.Rendering;

internal sealed class FpsText
{
private static readonly IBrush s_background = new ImmutableSolidColorBrush(Colors.Black, 50);
private double _maxFps;
private double _minFps = double.MaxValue;
private double _avgFps;
private double _prevFps;
private readonly TextElement _fpsText = new() { Size = 72 };
private readonly TextElement _minFpsText = new() { Size = 72 };
private readonly TextElement _maxFpsText = new() { Size = 72 };
private readonly TextElement _avgFpsText = new() { Size = 72 };
private readonly TextBlock _fpsFText;
private readonly TextBlock _textBlock;

public FpsText()
{
_fpsFText = new TextBlock
_textBlock = new TextBlock
{
Elements = new TextElements(new TextElement[]
{
_fpsText,
_minFpsText,
_maxFpsText,
_avgFpsText,
})
AlignmentX = AlignmentX.Left,
AlignmentY = AlignmentY.Top,
Size = 72,
Fill = Brushes.White
};
}

Expand Down Expand Up @@ -63,16 +59,21 @@ public void Dispose()
_fpsText._prevFps = fps;
_fpsText._avgFps = (_fpsText._prevFps + fps) / 2;

_fpsText._fpsText.Text = $"{fps:N2} FPS\n";
_fpsText._minFpsText.Text = $"Min: {_fpsText._minFps:N2} FPS\n";
_fpsText._maxFpsText.Text = $"Max: {_fpsText._maxFps:N2} FPS\n";
_fpsText._avgFpsText.Text = $"Avg: {_fpsText._avgFps:N2} FPS";
_fpsText._textBlock.Text = $"""
{fps:N2} FPS
Min: {_fpsText._minFps:N2} FPS
Max: {_fpsText._maxFps:N2} FPS
Avg: {_fpsText._avgFps:N2} FPS
""";

_fpsText._textBlock.Measure(_canvas.Size.ToSize(1));
float width = _fpsText._textBlock.Bounds.Size.Width;
float height = _fpsText._textBlock.Bounds.Size.Height;

_fpsText._fpsFText.Measure(_canvas.Size.ToSize(1));
using (_canvas.PushClip(_fpsText._fpsFText.Bounds))
using (_canvas.PushTransform(Matrix.CreateTranslation(width / 2, height / 2)))
{
_canvas.Clear();
_fpsText._fpsFText.Render(_canvas);
_canvas.DrawRectangle(_fpsText._textBlock.Bounds, s_background, null);
_fpsText._textBlock.Render(_canvas);
}
}
}
Expand Down

0 comments on commit 5b479d2

Please sign in to comment.