using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using FlaxEditor.Content.Settings; using FlaxEngine; using FlaxEngine.GUI; namespace Cabrito { [ExecuteInEditMode] public class FpsScript : Script { public UIControl control; Label label; Stopwatch sw; double updateTimeAvg = 0.0; ulong updateTimeCount; const double updateInterval = 0.25; Stopwatch sw2; double drawTimeAvg = 0.0; ulong drawTimeCount; const double drawInterval = 0.25; Stopwatch sw3; double physicsTimeAvg = 0.0; ulong physicsTimeCount; const double physicsInterval = 0.25; string currentRenderer = "Unknown"; RenderTask t; TimeSettings timeSettings; public override void OnAwake() { label = (Label) control.Control; sw = Stopwatch.StartNew(); currentRenderer = GPUDevice.Instance.RendererType.ToString(); sw2 = Stopwatch.StartNew(); if (t == null) { //Destroy(t); t = new RenderTask(); t.Render += OnDraw; } sw3 = Stopwatch.StartNew(); var settings = FlaxEditor.Content.Settings.GameSettings.Load(); timeSettings = settings.Time.CreateInstance(); } public override void OnDestroy() { Destroy(t); t = null; } double conTime = 0.0; public override void OnUpdate() { 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("\nuFPS: " + ((int) Math.Round(1.0f / updateTimeAvg)).ToString()); sb.Append("\nrFPS: " + ((int) Math.Round(1.0f / drawTimeAvg)).ToString()); sb.Append("\npFPS: " + ((int) Math.Round(1.0f / physicsTimeAvg)).ToString()); //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() { physicsTimeCount++; double elapsed = sw3.Elapsed.TotalSeconds; if (elapsed >= physicsInterval) { sw3.Restart(); physicsTimeAvg = elapsed / physicsTimeCount; physicsTimeCount = 0; } } void OnDraw(RenderTask tt, GPUContext context) { drawTimeCount++; double elapsed = sw2.Elapsed.TotalSeconds; if (elapsed >= drawInterval) { sw2.Restart(); drawTimeAvg = elapsed / drawTimeCount; drawTimeCount = 0; } } } }