Create DebugInfoModule

Provides F2 debugging info
This commit is contained in:
Drew DeVault 2015-09-24 22:28:44 -04:00
parent 8fe7329135
commit 3722468d01
6 changed files with 121 additions and 45 deletions

View File

@ -13,6 +13,7 @@ namespace TrueCraft.Client.Modules
{
public TrueCraftGame Game { get; set; }
public ChunkRenderer ChunkRenderer { get; set; }
public int ChunksRendered { get; set; }
private List<Mesh> ChunkMeshes { get; set; }
private ConcurrentBag<Mesh> IncomingChunks { get; set; }
@ -79,7 +80,7 @@ namespace TrueCraft.Client.Modules
}
}
private static readonly BlendState ColorWriteDisable = new BlendState()
private static readonly BlendState ColorWriteDisable = new BlendState
{
ColorWriteChannels = ColorWriteChannels.None
};
@ -89,13 +90,12 @@ namespace TrueCraft.Client.Modules
Game.Camera.ApplyTo(OpaqueEffect);
Game.Camera.ApplyTo(TransparentEffect);
int verticies = 0, chunks = 0;
int chunks = 0;
Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
for (int i = 0; i < ChunkMeshes.Count; i++)
{
if (Game.Camera.Frustum.Intersects(ChunkMeshes[i].BoundingBox))
{
verticies += ChunkMeshes[i].GetTotalVertices();
chunks++;
ChunkMeshes[i].Draw(OpaqueEffect, 0);
}
@ -105,23 +105,17 @@ namespace TrueCraft.Client.Modules
for (int i = 0; i < ChunkMeshes.Count; i++)
{
if (Game.Camera.Frustum.Intersects(ChunkMeshes[i].BoundingBox))
{
if (!ChunkMeshes[i].IsDisposed)
verticies += ChunkMeshes[i].GetTotalVertices();
ChunkMeshes[i].Draw(TransparentEffect, 1);
}
}
Game.GraphicsDevice.BlendState = BlendState.NonPremultiplied;
for (int i = 0; i < ChunkMeshes.Count; i++)
{
if (Game.Camera.Frustum.Intersects(ChunkMeshes[i].BoundingBox))
{
if (!ChunkMeshes[i].IsDisposed)
verticies += ChunkMeshes[i].GetTotalVertices();
ChunkMeshes[i].Draw(TransparentEffect, 1);
}
}
ChunksRendered = chunks;
}
}
}

View File

@ -0,0 +1,87 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TrueCraft.Client.Input;
using TrueCraft.Client.Rendering;
namespace TrueCraft.Client.Modules
{
public class DebugInfoModule : IGraphicalModule, IInputModule
{
public bool Chunks { get; set; }
private TrueCraftGame Game { get; set; }
private FontRenderer Font { get; set; }
private SpriteBatch SpriteBatch { get; set; }
private bool Enabled { get; set; }
public DebugInfoModule(TrueCraftGame game, FontRenderer font)
{
Game = game;
Font = font;
SpriteBatch = new SpriteBatch(Game.GraphicsDevice);
}
public bool KeyDown(GameTime gameTime, KeyboardKeyEventArgs e)
{
switch (e.Key)
{
case Keys.F2:
return true;
}
return false;
}
public bool KeyUp(GameTime gameTime, KeyboardKeyEventArgs e)
{
switch (e.Key)
{
case Keys.F2:
Enabled = !Enabled;
return true;
}
return false;
}
public void MouseMove(GameTime gameTime, MouseMoveEventArgs e)
{
}
public void Update(GameTime gameTime)
{
}
public void Draw(GameTime gameTime)
{
if (!Enabled)
return;
var fps = (int)(1 / gameTime.ElapsedGameTime.TotalSeconds) + 1;
const int xOrigin = 10;
const int yOrigin = 5;
const int yOffset = 25;
SpriteBatch.Begin();
Font.DrawText(SpriteBatch, xOrigin, yOrigin, string.Format("§lRunning at {0}{1} FPS",
GetFPSColor(fps), fps), 1);
Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 1), string.Format("§o{0} vertices, {1} indicies",
Mesh.VerticiesRendered, Mesh.IndiciesRendered), 1);
Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 2),
string.Format("§o{0} chunks", Game.ChunkModule.ChunksRendered), 1);
Font.DrawText(SpriteBatch, xOrigin, yOrigin + (yOffset * 3),
string.Format("§o<{0:N2}, {1:N2}, {2:N2}>",
Game.Client.Position.X, Game.Client.Position.Y, Game.Client.Position.Z), 1);
SpriteBatch.End();
}
private string GetFPSColor(int fps)
{
if (fps <= 16)
return "§c";
if (fps <= 32)
return "§e";
return "§a";
}
}
}

View File

@ -192,12 +192,6 @@ namespace TrueCraft.Client.Rendering
new Vector3(-0.5f, -0.5f, 0.5f),
new Vector3(0.5f, -0.5f, 0.5f)
};
// TEMP
return;
for (int i = 0; i < CubeMesh.Length; i++)
for (int j = 0; j < CubeMesh[0].Length; j++)
CubeMesh[i][j] *= new Vector3(0.5f);
}
}
}

View File

@ -10,6 +10,15 @@ namespace TrueCraft.Client.Rendering
/// </summary>
public class Mesh : IDisposable
{
public static int VerticiesRendered { get; set; }
public static int IndiciesRendered { get; set; }
public static void ResetStats()
{
VerticiesRendered = 0;
IndiciesRendered = 0;
}
/// <summary>
/// The maximum number of submeshes stored in a single mesh.
/// </summary>
@ -61,9 +70,6 @@ namespace TrueCraft.Client.Rendering
/// <summary>
/// Creates a new mesh.
/// </summary>
/// <param name="graphicsDevice">The graphics device to store the mesh on.</param>
/// <param name="submeshes">The number of submeshes in the mesh.</param>
/// <param name="recalculateBounds">Whether the mesh should recalculate its bounding box when changed.</param>
public Mesh(TrueCraftGame game, int submeshes = 1, bool recalculateBounds = true)
{
if ((submeshes < 0) || (submeshes >= Mesh.SubmeshLimit))
@ -78,12 +84,8 @@ namespace TrueCraft.Client.Rendering
/// <summary>
/// Creates a new mesh.
/// </summary>
/// <param name="graphicsDevice">The graphics device to store the mesh on.</param>
/// <param name="vertices">The vertices in the mesh.</param>
/// <param name="submeshes">The number of submeshes in the mesh.</param>
/// <param name="recalculateBounds">Whether the mesh should recalculate its bounding box when changed.</param>
public Mesh(TrueCraftGame game, VertexPositionNormalColorTexture[] vertices, int submeshes = 1, bool recalculateBounds = true)
: this(game, submeshes, recalculateBounds)
public Mesh(TrueCraftGame game, VertexPositionNormalColorTexture[] vertices,
int submeshes = 1, bool recalculateBounds = true) : this(game, submeshes, recalculateBounds)
{
Vertices = vertices;
}
@ -91,12 +93,8 @@ namespace TrueCraft.Client.Rendering
/// <summary>
/// Creates a new mesh.
/// </summary>
/// <param name="graphicsDevice">The graphics device to store the mesh on.</param>
/// <param name="vertices">The vertices in the mesh.</param>
/// <param name="indices">The first (and only) submesh in the mesh.</param>
/// <param name="recalculateBounds">Whether the mesh should recalculate its bounding box when changed.</param>
public Mesh(TrueCraftGame game, VertexPositionNormalColorTexture[] vertices, int[] indices, bool recalculateBounds = true)
: this(game, 1, recalculateBounds)
public Mesh(TrueCraftGame game, VertexPositionNormalColorTexture[] vertices,
int[] indices, bool recalculateBounds = true) : this(game, 1, recalculateBounds)
{
Vertices = vertices;
SetSubmesh(0, indices);
@ -105,8 +103,6 @@ namespace TrueCraft.Client.Rendering
/// <summary>
/// Sets a submesh in this mesh.
/// </summary>
/// <param name="index">The submesh index.</param>
/// <param name="data">The indices for the submesh.</param>
public void SetSubmesh(int index, int[] indices)
{
if ((index < 0) || (index > _indices.Length))
@ -163,12 +159,13 @@ namespace TrueCraft.Client.Rendering
effect.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
0, 0, _indices[index].IndexCount, 0, _indices[index].IndexCount / 3);
}
VerticiesRendered += _vertices.VertexCount;
IndiciesRendered += _indices[index].IndexCount;
}
/// <summary>
/// Returns the total vertices in this mesh.
/// </summary>
/// <returns></returns>
public int GetTotalVertices()
{
if (_vertices == null)
@ -181,7 +178,6 @@ namespace TrueCraft.Client.Rendering
/// <summary>
/// Returns the total indices for all the submeshes in this mesh.
/// </summary>
/// <returns></returns>
public int GetTotalIndices()
{
lock (_syncLock)

View File

@ -135,6 +135,7 @@
<Compile Include="Modules\ChunkModule.cs" />
<Compile Include="Modules\HighlightModule.cs" />
<Compile Include="Modules\PlayerControlModule.cs" />
<Compile Include="Modules\DebugInfoModule.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

View File

@ -14,7 +14,6 @@ using TrueCraft.API.World;
using TrueCraft.Core;
using TrueCraft.Core.Networking.Packets;
using TrueCraft.Client.Input;
using TrueCraft.Client.Interface;
using TrueCraft.Client.Modules;
using TrueCraft.Client.Rendering;
@ -28,6 +27,7 @@ namespace TrueCraft.Client
public Camera Camera { get; private set; }
public ConcurrentBag<Action> PendingMainThreadActions { get; set; }
public double Bobbing { get; set; }
public ChunkModule ChunkModule { get; set; }
private List<IGameplayModule> Modules { get; set; }
private SpriteBatch SpriteBatch { get; set; }
@ -41,7 +41,7 @@ namespace TrueCraft.Client
private DateTime NextPhysicsUpdate { get; set; }
private bool MouseCaptured { get; set; }
private GameTime GameTime { get; set; }
private Microsoft.Xna.Framework.Vector3 Delta { get; set; }
private DebugInfoModule DebugInfoModule { get; set; }
public static readonly int Reach = 5;
@ -62,6 +62,7 @@ namespace TrueCraft.Client
Graphics.IsFullScreen = UserSettings.Local.IsFullscreen;
Graphics.PreferredBackBufferWidth = UserSettings.Local.WindowResolution.Width;
Graphics.PreferredBackBufferHeight = UserSettings.Local.WindowResolution.Height;
Graphics.ApplyChanges();
Client = client;
EndPoint = endPoint;
LastPhysicsUpdate = DateTime.MinValue;
@ -85,9 +86,13 @@ namespace TrueCraft.Client
base.Initialize(); // (calls LoadContent)
Modules.Add(new ChunkModule(this));
ChunkModule = new ChunkModule(this);
DebugInfoModule = new DebugInfoModule(this, Pixel);
Modules.Add(ChunkModule);
Modules.Add(new HighlightModule(this));
Modules.Add(new PlayerControlModule(this));
Modules.Add(DebugInfoModule);
Client.PropertyChanged += HandleClientPropertyChanged;
Client.Connect(EndPoint);
@ -136,9 +141,7 @@ namespace TrueCraft.Client
Pixel = new FontRenderer(
new Font(Content, "Fonts/Pixel"),
new Font(Content, "Fonts/Pixel", FontStyle.Bold),
null, // No support for underlined or strikethrough yet. The FontRenderer will revert to using the regular font style.
null, // (I don't think BMFont has those options?)
new Font(Content, "Fonts/Pixel", FontStyle.Bold), null, null,
new Font(Content, "Fonts/Pixel", FontStyle.Italic));
base.LoadContent();
@ -250,6 +253,7 @@ namespace TrueCraft.Client
GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
Mesh.ResetStats();
foreach (var module in Modules)
{
var drawable = module as IGraphicalModule;