MonoDebugHud.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. var ui = application.UI;
  41. var root = ui.Root;
  42. var cache = application.ResourceCache;
  43. text = new Text();
  44. text.VerticalAlignment = VerticalAlignment.Top;
  45. text.HorizontalAlignment = HorizontalAlignment.Right;
  46. text.TextAlignment = HorizontalAlignment.Right;
  47. text.SetFont(cache.GetFont("Fonts/Anonymous Pro.ttf"), 18);
  48. root.AddChild(text);
  49. subscription = application.Engine.SubscribeToPostUpdate(OnPostUpdate);
  50. }
  51. public void Hide()
  52. {
  53. if (text == null)
  54. return;
  55. subscription.Unsubscribe();
  56. application.UI.Root.RemoveChild(text, 0);
  57. text = null;
  58. subscription = null;
  59. }
  60. }
  61. }