Browse Source

make Toasts a list & implement FPS counter as a toast

GitOrigin-RevId: bae35697a0
master
Colin McMillen 4 years ago
parent
commit
34e7ab3ee1
  1. 36
      Shared/Debug.cs
  2. 8
      Shared/SneakGame.cs

36
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<string> toasts = new LinkedList<string>();
static List<DebugRect> rects = new List<DebugRect>();
static List<DebugLine> lines = new List<DebugLine>();
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) {

8
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();

Loading…
Cancel
Save