Files
stgsharp/源代码/StgSharp/Graphics/FrameTime.cs
T

132 lines
3.8 KiB
C#

using StgSharp;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Graphics
{
/// <summary>
/// A timer counting frames after its construction.
/// </summary>
public class FrameTimer
{
private Form timeProvider;
internal readonly uint bornTick;
/// <summary>
/// Frames after this timer created.
/// </summary>
public uint Time
{
get { return timeProvider.timer.totalFrame - bornTick; }
}
public FrameTimer(Form provaider)
{
timeProvider = provaider;
bornTick = timeProvider.timer.totalFrame;
}
/// <summary>
/// Convert a <see cref="FrameTimer"/> into uint value.
/// this will get total frames since the timer was created.
/// </summary>
/// <param name="timer"><see cref="FrameTimer"/> to be converted</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator uint(FrameTimer timer)
{
return timer.Time;
}
}
public partial class Form
{
internal class FrameUpdateTimer
{
private Counter<uint> tickCounter;
private float frameLength;
private double frameTime;
private bool refreshed;
private SemaphoreSlim pong;
private bool isOnceFulFrame = false;
public uint totalFrame
{
get { return tickCounter; }
}
public double timeAfterFrame
{
get { return InternalIO.glfw.GetTime() - frameTime; }
}
internal bool IsOnceFullFrame
{
get { return isOnceFulFrame; }
set
{
if (!isOnceFulFrame)
{
isOnceFulFrame = value;
}
}
}
internal void Init(SemaphoreSlim semaphore)
{
pong = semaphore;
}
internal void Refresh()
{
refreshed = false;
}
internal bool GoNextFrame()
{
try
{
while (refreshed) { }
refreshed = true;
return refreshed;
}
catch (Exception ex)
{
InternalIO.InternalWriteLog(ex.Message, LogType.Error);
return false;
}
}
}
private bool isFullyRendered = false;
/// <summary>
/// Wait until the next frame of a certain <see cref="Form"/> started,
/// all GL function after this method will be blocked until the next frame started.
/// When a frame refreshed before
/// It should be noticed that do not use this method too often to avoid unexpected performance problem
/// </summary>
/// <param name="form">The <see cref="Form"/> that can provide a frame refresh benchmark</param>
/// <param name="action"><see cref="Action"/> to be done and aligned.</param>
/// <returns>
/// A bool value indicating if the <see cref="Form"/> successfully refresh the frame.
/// If the retur value is false, please set a new <see cref="Form"/> as benchmark.
/// </returns>
protected bool AlignFrame(Form form, Action action)
{
try
{
bool isAligned = form.timer.GoNextFrame();
action();
return isAligned;
}
catch (Exception)
{
return false;
}
}
}
}