public class FPSBase
{
public static void Run()
{
CompositionTarget.Rendering += CompositionTarget_Rendering;
}
public static TimeSpan RunTime { get; private set; }
public static int FPS { get; private set; }
static int fps = 0;
public static TimeSpan PreSTime { get; private set; }
public static TimeSpan PreLogicTime { get; private set; }
public static double Logicalnterval { get; set; } = 16d;
private static void CompositionTarget_Rendering(object sender, EventArgs e)
{
RunTime = (e as RenderingEventArgs).RenderingTime;
var span = (RunTime - PreSTime).TotalSeconds;
if (span >= 1d)
{
PreSTime = RunTime;
FPS = fps;
fps = 0;
}
else
fps++;
if ((RunTime - PreLogicTime).TotalMilliseconds > Logicalnterval)
{
PreLogicTime = RunTime;
OnLogicFrame();
}
OnRenderFrame();
}
static EventHandlerList Events = new EventHandlerList();
static readonly object LogicFrameEventObj = new object();
static readonly object RenderFrameEventObj = new object();
public static event EventHandler LogicFrameEvent { add => Events.AddHandler(LogicFrameEventObj, value); remove => Events.RemoveHandler(LogicFrameEventObj, value); }
public static event EventHandler RenderFrameEvent { add => Events.AddHandler(RenderFrameEventObj, value); remove => Events.RemoveHandler(RenderFrameEventObj, value); }
private static void OnLogicFrame()
{
(Events[LogicFrameEventObj] as EventHandler)?.Invoke(null, EventArgs.Empty);
}
static void OnRenderFrame()
{
(Events[RenderFrameEventObj] as EventHandler)?.Invoke(null, EventArgs.Empty);
}
}
标签:set,private,Events,static,FPS,WPF,RunTime,public From: https://www.cnblogs.com/RedSky/p/18543369