Files
GoakeFlax/Source/Game/Cabrito/FpsScript.cs
GoaLitiuM 649b8a803c WIP
2021-02-19 18:54:36 +02:00

88 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
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;
string currentRenderer = "Unknown";
RenderTask t;
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;
}
}
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 = "FPS: " + ((int)Math.Round(1.0f / updateTimeAvg)).ToString();
label.Text += "\nrFPS: " + ((int)Math.Round(1.0f / drawTimeAvg)).ToString();
label.Text += "\nCon: " + conTime.ToString() + "ms";
label.Text += "\n" + currentRenderer;
label.Text += "\nGC memory: " + (GC.GetTotalMemory(false)/1000000.0f).ToString() + "MB";
}
void OnDraw(RenderTask tt, GPUContext context)
{
drawTimeCount++;
double elapsed = sw2.Elapsed.TotalSeconds;
if (elapsed >= drawInterval)
{
sw2.Restart();
drawTimeAvg = elapsed / drawTimeCount;
drawTimeCount = 0;
}
}
}
}