using System; using System.Diagnostics; using System.Text; using FlaxEditor.Content.Settings; using FlaxEngine; using FlaxEngine.GUI; namespace Cabrito { [ExecuteInEditMode] public class FpsScript : Script { private const double updateInterval = 0.25; private const double drawInterval = 0.25; private const double physicsInterval = 0.25; private double conTime; public UIControl control; private string currentRenderer = "Unknown"; private double drawAccumTime; private double drawTimeAvg; private ulong drawTimeCount; private Label label; private double physicsAccumTime; private double physicsTimeAvg; private ulong physicsTimeCount; private Stopwatch sw; private Stopwatch sw0; private Stopwatch sw2; private Stopwatch sw3; private RenderTask t; private TimeSettings timeSettings; private double updateAccumTime; private double updateTimeAvg; private ulong updateTimeCount; public override void OnAwake() { label = (Label)control.Control; sw = Stopwatch.StartNew(); sw2 = Stopwatch.StartNew(); sw3 = Stopwatch.StartNew(); sw0 = Stopwatch.StartNew(); currentRenderer = GPUDevice.Instance.RendererType.ToString(); if (t == null) { //Destroy(t); t = new RenderTask(); t.Render += OnDraw; } GameSettings settings = GameSettings.Load(); timeSettings = settings.Time.CreateInstance(); } public override void OnDestroy() { Destroy(t); t = null; } public override void OnUpdate() { updateAccumTime += Time.DeltaTime; updateTimeCount++; double elapsed = sw.Elapsed.TotalSeconds; if (elapsed >= updateInterval) { sw.Restart(); updateTimeAvg = elapsed / updateTimeCount; updateTimeCount = 0; conTime = ConsoleContentTextBox.accumDrawTime / ConsoleContentTextBox.accumDrawTimes * 1000.0; ConsoleContentTextBox.accumDrawTime = 0.0; ConsoleContentTextBox.accumDrawTimes = 0; } StringBuilder sb = new StringBuilder(); sb.Append("eFPS: " + Engine.FramesPerSecond); sb.Append(" uTime: " + sw0.Elapsed.TotalSeconds); sb.Append("\nuFPS: " + (int)Math.Round(1.0f / updateTimeAvg)); sb.Append(" uTime: " + updateAccumTime); sb.Append("\nrFPS: " + (int)Math.Round(1.0f / drawTimeAvg)); sb.Append(" rTime: " + drawAccumTime); sb.Append("\npFPS: " + (int)Math.Round(1.0f / physicsTimeAvg)); sb.Append(" pTime: " + physicsAccumTime); //sb.Append("\nCon: " + conTime.ToString() + "ms"); //sb.Append("\nGC memory: " + (GC.GetTotalMemory(false) / 1000000.0f).ToString() + "MB"); //sb.Append("\nUpdate profiler: " + updateProfTime.ToString() + "ms"); #if false #if BUILD_DEVELOPMENT var nameOffset = Marshal.OffsetOf(typeof(ProfilerCPU.Event), "Name0"); foreach (var eventsCpu in FlaxEngine.ProfilingTools.EventsCPU) { sb.Append("\nName: " + eventsCpu.Name + " "); foreach (var eventt in eventsCpu.Events) { #if FLAX_EDITOR string eventName = eventt.Name; #else string eventName; unsafe { var ptr1 = &eventt; char* ptr2 = (char*)(new IntPtr((byte*)ptr1 + nameOffset.ToInt64())); //fixed (char* name = &eventt) { eventName = new string(ptr2); } } #endif sb.Append(eventName + " " + (eventt.End-eventt.Start) + "ms, "); } } #endif #endif ((Label)control.Control).Text = sb.ToString(); /*if (!Platform.HasFocus) { Time.UpdateFPS = 15; Time.DrawFPS = 15; Time.PhysicsFPS = 15; } #if FLAX_EDITOR else if (!FlaxEditor.Editor.IsPlayMode) { var editorFPS = FlaxEditor.Editor.Instance.Options.Options.General.EditorFPS; Time.UpdateFPS = editorFPS; Time.DrawFPS = editorFPS; Time.PhysicsFPS = timeSettings.PhysicsFPS; } #endif else { Time.UpdateFPS = timeSettings.UpdateFPS; Time.DrawFPS = timeSettings.DrawFPS; Time.PhysicsFPS = timeSettings.PhysicsFPS; }*/ } public override void OnFixedUpdate() { physicsAccumTime += Time.DeltaTime; physicsTimeCount++; double elapsed = sw3.Elapsed.TotalSeconds; if (elapsed >= physicsInterval) { sw3.Restart(); physicsTimeAvg = elapsed / physicsTimeCount; physicsTimeCount = 0; } } private void OnDraw(RenderTask tt, GPUContext context) { drawAccumTime += Time.DeltaTime; drawTimeCount++; double elapsed = sw2.Elapsed.TotalSeconds; if (elapsed >= drawInterval) { sw2.Restart(); drawTimeAvg = elapsed / drawTimeCount; drawTimeCount = 0; } } } }