MonoDebugHud.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using Urho.Gui;
  3. namespace Urho
  4. {
  5. public class MonoDebugHud
  6. {
  7. const int FrameSampleCount = 50;
  8. Application application;
  9. Text text;
  10. Subscription subscription;
  11. int frameCount = 0;
  12. DateTime dateTime;
  13. TimeSpan span;
  14. public MonoDebugHud(Application application)
  15. {
  16. this.application = application;
  17. }
  18. void OnPostUpdate(PostUpdateEventArgs args)
  19. {
  20. Graphics graphics = application.Graphics;
  21. var now = DateTime.UtcNow;
  22. span += now - dateTime;
  23. dateTime = now;
  24. if (++frameCount >= FrameSampleCount)
  25. {
  26. float average = (float)(span.TotalMilliseconds / frameCount);
  27. float fps = 1000;
  28. if (average != 0)
  29. fps /= average;
  30. frameCount = 0;
  31. span = TimeSpan.Zero;
  32. text.Value = $"{(int)fps} FPS\n{graphics.NumBatches} batches\n{Runtime.KnownObjectsCount} MCW\n" + AdditionalText;
  33. }
  34. }
  35. public string AdditionalText { get; set; }
  36. public void Show()
  37. {
  38. if (text != null)
  39. return;
  40. text = new Text();
  41. text.VerticalAlignment = VerticalAlignment.Top;
  42. text.HorizontalAlignment = HorizontalAlignment.Right;
  43. text.TextAlignment = HorizontalAlignment.Right;
  44. text.SetFont(CoreAssets.Fonts.AnonymousPro, 18);
  45. application.UI.Root.AddChild(text);
  46. subscription = application.Engine.SubscribeToPostUpdate(OnPostUpdate);
  47. }
  48. public void Hide()
  49. {
  50. if (text == null)
  51. return;
  52. subscription.Unsubscribe();
  53. application.UI.Root.RemoveChild(text, 0);
  54. text = null;
  55. subscription = null;
  56. }
  57. }
  58. }