From 34e7ab3ee146955e7e1a46d7fae60eae26c39723 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Thu, 23 Jan 2020 12:19:16 -0500 Subject: [PATCH] make Toasts a list & implement FPS counter as a toast GitOrigin-RevId: bae35697a0329f7d5c1c45cdbc13c3be500b8c73 --- Shared/Debug.cs | 36 ++++++++++++++++++++++-------------- Shared/SneakGame.cs | 8 ++++---- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Shared/Debug.cs b/Shared/Debug.cs index 7fce50b..e33b371 100644 --- a/Shared/Debug.cs +++ b/Shared/Debug.cs @@ -27,13 +27,16 @@ namespace SemiColinGames { } public static bool Enabled; + // This is a LinkedList instead of a List because SetFpsText() adds to its front. + static LinkedList toasts = new LinkedList(); static List rects = new List(); static List lines = new List(); + static Texture2D whiteTexture; - static string toast = null; - public static void Toast(string s) { - toast = s; + public static void Initialize(GraphicsDevice graphics) { + whiteTexture = new Texture2D(graphics, 1, 1); + whiteTexture.SetData(new Color[] { Color.White }); } public static void WriteLine(string s) { @@ -43,17 +46,22 @@ namespace SemiColinGames { public static void WriteLine(string s, params object[] args) { System.Diagnostics.Debug.WriteLine(s, args); } - - public static void Initialize(GraphicsDevice graphics) { - whiteTexture = new Texture2D(graphics, 1, 1); - whiteTexture.SetData(new Color[] { Color.White }); - } - + public static void Clear() { + toasts.Clear(); rects.Clear(); lines.Clear(); } + public static void AddToast(string s) { + toasts.AddLast(s); + } + + // FPS text is always displayed as the first toast (if set). + public static void SetFpsText(string s) { + toasts.AddFirst(s); + } + public static void AddRect(Rectangle rect, Color color) { rects.Add(new DebugRect(rect, color)); } @@ -62,12 +70,12 @@ namespace SemiColinGames { lines.Add(new DebugLine(start, end, color)); } - public static void DrawToast(SpriteBatch spriteBatch, SpriteFont font) { - if (toast == null) { - return; + public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) { + int y = 10; + foreach (var toast in toasts) { + spriteBatch.DrawString(font, toast, new Vector2(10, y), Color.Teal); + y += 30; } - spriteBatch.DrawString(font, toast, new Vector2(10, 40), Color.Teal); - toast = null; } public static void Draw(SpriteBatch spriteBatch, Camera camera) { diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index 325432d..05a85c2 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -93,6 +93,9 @@ namespace SemiColinGames { // We need to update the FPS counter in Draw() since Update() might get called more // frequently, especially when gameTime.IsRunningSlowly. fpsCounter.Update(); + string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " + + $"{fpsCounter.Fps} FPS"; + Debug.SetFpsText(fpsText); // Draw scene to RenderTarget. GraphicsDevice.SetRenderTarget(renderTarget); @@ -131,10 +134,7 @@ namespace SemiColinGames { spriteBatch.Draw(renderTarget, drawRect, Color.White); if (Debug.Enabled) { - string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " + - $"{fpsCounter.Fps} FPS"; - spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.Teal); - Debug.DrawToast(spriteBatch, font); + Debug.DrawToasts(spriteBatch, font); } spriteBatch.End();