136 lines
4.0 KiB
C#
136 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
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<FlaxEditor.Content.Settings.TimeSettings>();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
((Label)control.Control).Text = "uFPS: " + ((int)Math.Round(1.0f / updateTimeAvg)).ToString();
|
|
label.Text += "\nrFPS: " + ((int)Math.Round(1.0f / drawTimeAvg)).ToString();
|
|
label.Text += "\npFPS: " + ((int)Math.Round(1.0f / physicsTimeAvg)).ToString();
|
|
label.Text += "\nCon: " + conTime.ToString() + "ms";
|
|
label.Text += "\n" + currentRenderer;
|
|
label.Text += "\nGC memory: " + (GC.GetTotalMemory(false)/1000000.0f).ToString() + "MB";
|
|
|
|
/*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;
|
|
}
|
|
}
|
|
}
|
|
}
|