| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using Urho.Gui;
- namespace Urho
- {
- public class MonoDebugHud
- {
- const int FrameSampleCount = 50;
- Application application;
- Text text;
- Subscription subscription;
- int frameCount = 0;
- DateTime dateTime;
- TimeSpan span;
- public MonoDebugHud(Application application)
- {
- this.application = application;
- }
- void OnPostUpdate(PostUpdateEventArgs args)
- {
- Graphics graphics = application.Graphics;
- var now = DateTime.UtcNow;
- span += now - dateTime;
- dateTime = now;
- if (++frameCount >= FrameSampleCount)
- {
- float average = (float)(span.TotalMilliseconds / frameCount);
- float fps = 1000;
- if (average != 0)
- fps /= average;
- frameCount = 0;
- span = TimeSpan.Zero;
- text.Value = $"{(int)fps} FPS\n{graphics.NumBatches} batches\n{Runtime.KnownObjectsCount} MCW\n" + AdditionalText;
- }
- }
- public string AdditionalText { get; set; }
- public void Show()
- {
- if (text != null)
- return;
- text = new Text();
- text.VerticalAlignment = VerticalAlignment.Top;
- text.HorizontalAlignment = HorizontalAlignment.Right;
- text.TextAlignment = HorizontalAlignment.Right;
- text.SetFont(CoreAssets.Fonts.AnonymousPro, 18);
- application.UI.Root.AddChild(text);
- subscription = application.Engine.SubscribeToPostUpdate(OnPostUpdate);
- }
- public void Hide()
- {
- if (text == null)
- return;
- subscription.Unsubscribe();
- application.UI.Root.RemoveChild(text, 0);
- text = null;
- subscription = null;
- }
- }
- }
|