make Toasts a list & implement FPS counter as a toast

GitOrigin-RevId: bae35697a0329f7d5c1c45cdbc13c3be500b8c73
This commit is contained in:
Colin McMillen 2020-01-23 12:19:16 -05:00
parent bee17a99af
commit 34e7ab3ee1
2 changed files with 27 additions and 19 deletions

View File

@ -27,13 +27,16 @@ namespace SemiColinGames {
} }
public static bool Enabled; public static bool Enabled;
// This is a LinkedList instead of a List because SetFpsText() adds to its front.
static LinkedList<string> toasts = new LinkedList<string>();
static List<DebugRect> rects = new List<DebugRect>(); static List<DebugRect> rects = new List<DebugRect>();
static List<DebugLine> lines = new List<DebugLine>(); static List<DebugLine> lines = new List<DebugLine>();
static Texture2D whiteTexture;
static string toast = null;
public static void Toast(string s) { static Texture2D whiteTexture;
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) { public static void WriteLine(string s) {
@ -43,17 +46,22 @@ namespace SemiColinGames {
public static void WriteLine(string s, params object[] args) { public static void WriteLine(string s, params object[] args) {
System.Diagnostics.Debug.WriteLine(s, 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() { public static void Clear() {
toasts.Clear();
rects.Clear(); rects.Clear();
lines.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) { public static void AddRect(Rectangle rect, Color color) {
rects.Add(new DebugRect(rect, color)); rects.Add(new DebugRect(rect, color));
} }
@ -62,12 +70,12 @@ namespace SemiColinGames {
lines.Add(new DebugLine(start, end, color)); lines.Add(new DebugLine(start, end, color));
} }
public static void DrawToast(SpriteBatch spriteBatch, SpriteFont font) { public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) {
if (toast == null) { int y = 10;
return; 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) { public static void Draw(SpriteBatch spriteBatch, Camera camera) {

View File

@ -93,6 +93,9 @@ namespace SemiColinGames {
// We need to update the FPS counter in Draw() since Update() might get called more // We need to update the FPS counter in Draw() since Update() might get called more
// frequently, especially when gameTime.IsRunningSlowly. // frequently, especially when gameTime.IsRunningSlowly.
fpsCounter.Update(); fpsCounter.Update();
string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
$"{fpsCounter.Fps} FPS";
Debug.SetFpsText(fpsText);
// Draw scene to RenderTarget. // Draw scene to RenderTarget.
GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.SetRenderTarget(renderTarget);
@ -131,10 +134,7 @@ namespace SemiColinGames {
spriteBatch.Draw(renderTarget, drawRect, Color.White); spriteBatch.Draw(renderTarget, drawRect, Color.White);
if (Debug.Enabled) { if (Debug.Enabled) {
string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " + Debug.DrawToasts(spriteBatch, font);
$"{fpsCounter.Fps} FPS";
spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.Teal);
Debug.DrawToast(spriteBatch, font);
} }
spriteBatch.End(); spriteBatch.End();