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 TrueCraftGame Game { get; set; }
public ChunkRenderer ChunkRenderer { get; set; } public ChunkRenderer ChunkRenderer { get; set; }
public int ChunksRendered { get; set; }
private List<Mesh> ChunkMeshes { get; set; } private List<Mesh> ChunkMeshes { get; set; }
private ConcurrentBag<Mesh> IncomingChunks { 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 ColorWriteChannels = ColorWriteChannels.None
}; };
@ -89,13 +90,12 @@ namespace TrueCraft.Client.Modules
Game.Camera.ApplyTo(OpaqueEffect); Game.Camera.ApplyTo(OpaqueEffect);
Game.Camera.ApplyTo(TransparentEffect); Game.Camera.ApplyTo(TransparentEffect);
int verticies = 0, chunks = 0; int chunks = 0;
Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default; Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
for (int i = 0; i < ChunkMeshes.Count; i++) for (int i = 0; i < ChunkMeshes.Count; i++)
{ {
if (Game.Camera.Frustum.Intersects(ChunkMeshes[i].BoundingBox)) if (Game.Camera.Frustum.Intersects(ChunkMeshes[i].BoundingBox))
{ {
verticies += ChunkMeshes[i].GetTotalVertices();
chunks++; chunks++;
ChunkMeshes[i].Draw(OpaqueEffect, 0); ChunkMeshes[i].Draw(OpaqueEffect, 0);
} }
@ -105,23 +105,17 @@ namespace TrueCraft.Client.Modules
for (int i = 0; i < ChunkMeshes.Count; i++) for (int i = 0; i < ChunkMeshes.Count; i++)
{ {
if (Game.Camera.Frustum.Intersects(ChunkMeshes[i].BoundingBox)) if (Game.Camera.Frustum.Intersects(ChunkMeshes[i].BoundingBox))
{
if (!ChunkMeshes[i].IsDisposed)
verticies += ChunkMeshes[i].GetTotalVertices();
ChunkMeshes[i].Draw(TransparentEffect, 1); ChunkMeshes[i].Draw(TransparentEffect, 1);
}
} }
Game.GraphicsDevice.BlendState = BlendState.NonPremultiplied; Game.GraphicsDevice.BlendState = BlendState.NonPremultiplied;
for (int i = 0; i < ChunkMeshes.Count; i++) for (int i = 0; i < ChunkMeshes.Count; i++)
{ {
if (Game.Camera.Frustum.Intersects(ChunkMeshes[i].BoundingBox)) if (Game.Camera.Frustum.Intersects(ChunkMeshes[i].BoundingBox))
{
if (!ChunkMeshes[i].IsDisposed)
verticies += ChunkMeshes[i].GetTotalVertices();
ChunkMeshes[i].Draw(TransparentEffect, 1); 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),
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> /// </summary>
public class Mesh : IDisposable 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> /// <summary>
/// The maximum number of submeshes stored in a single mesh. /// The maximum number of submeshes stored in a single mesh.
/// </summary> /// </summary>
@ -61,9 +70,6 @@ namespace TrueCraft.Client.Rendering
/// <summary> /// <summary>
/// Creates a new mesh. /// Creates a new mesh.
/// </summary> /// </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) public Mesh(TrueCraftGame game, int submeshes = 1, bool recalculateBounds = true)
{ {
if ((submeshes < 0) || (submeshes >= Mesh.SubmeshLimit)) if ((submeshes < 0) || (submeshes >= Mesh.SubmeshLimit))
@ -78,12 +84,8 @@ namespace TrueCraft.Client.Rendering
/// <summary> /// <summary>
/// Creates a new mesh. /// Creates a new mesh.
/// </summary> /// </summary>
/// <param name="graphicsDevice">The graphics device to store the mesh on.</param> public Mesh(TrueCraftGame game, VertexPositionNormalColorTexture[] vertices,
/// <param name="vertices">The vertices in the mesh.</param> int submeshes = 1, bool recalculateBounds = true) : this(game, submeshes, recalculateBounds)
/// <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)
{ {
Vertices = vertices; Vertices = vertices;
} }
@ -91,12 +93,8 @@ namespace TrueCraft.Client.Rendering
/// <summary> /// <summary>
/// Creates a new mesh. /// Creates a new mesh.
/// </summary> /// </summary>
/// <param name="graphicsDevice">The graphics device to store the mesh on.</param> public Mesh(TrueCraftGame game, VertexPositionNormalColorTexture[] vertices,
/// <param name="vertices">The vertices in the mesh.</param> int[] indices, bool recalculateBounds = true) : this(game, 1, recalculateBounds)
/// <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)
{ {
Vertices = vertices; Vertices = vertices;
SetSubmesh(0, indices); SetSubmesh(0, indices);
@ -105,8 +103,6 @@ namespace TrueCraft.Client.Rendering
/// <summary> /// <summary>
/// Sets a submesh in this mesh. /// Sets a submesh in this mesh.
/// </summary> /// </summary>
/// <param name="index">The submesh index.</param>
/// <param name="data">The indices for the submesh.</param>
public void SetSubmesh(int index, int[] indices) public void SetSubmesh(int index, int[] indices)
{ {
if ((index < 0) || (index > _indices.Length)) if ((index < 0) || (index > _indices.Length))
@ -163,12 +159,13 @@ namespace TrueCraft.Client.Rendering
effect.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, effect.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
0, 0, _indices[index].IndexCount, 0, _indices[index].IndexCount / 3); 0, 0, _indices[index].IndexCount, 0, _indices[index].IndexCount / 3);
} }
VerticiesRendered += _vertices.VertexCount;
IndiciesRendered += _indices[index].IndexCount;
} }
/// <summary> /// <summary>
/// Returns the total vertices in this mesh. /// Returns the total vertices in this mesh.
/// </summary> /// </summary>
/// <returns></returns>
public int GetTotalVertices() public int GetTotalVertices()
{ {
if (_vertices == null) if (_vertices == null)
@ -181,7 +178,6 @@ namespace TrueCraft.Client.Rendering
/// <summary> /// <summary>
/// Returns the total indices for all the submeshes in this mesh. /// Returns the total indices for all the submeshes in this mesh.
/// </summary> /// </summary>
/// <returns></returns>
public int GetTotalIndices() public int GetTotalIndices()
{ {
lock (_syncLock) lock (_syncLock)
@ -245,4 +241,4 @@ namespace TrueCraft.Client.Rendering
Dispose(false); Dispose(false);
} }
} }
} }

View File

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

View File

@ -14,7 +14,6 @@ using TrueCraft.API.World;
using TrueCraft.Core; using TrueCraft.Core;
using TrueCraft.Core.Networking.Packets; using TrueCraft.Core.Networking.Packets;
using TrueCraft.Client.Input; using TrueCraft.Client.Input;
using TrueCraft.Client.Interface;
using TrueCraft.Client.Modules; using TrueCraft.Client.Modules;
using TrueCraft.Client.Rendering; using TrueCraft.Client.Rendering;
@ -28,6 +27,7 @@ namespace TrueCraft.Client
public Camera Camera { get; private set; } public Camera Camera { get; private set; }
public ConcurrentBag<Action> PendingMainThreadActions { get; set; } public ConcurrentBag<Action> PendingMainThreadActions { get; set; }
public double Bobbing { get; set; } public double Bobbing { get; set; }
public ChunkModule ChunkModule { get; set; }
private List<IGameplayModule> Modules { get; set; } private List<IGameplayModule> Modules { get; set; }
private SpriteBatch SpriteBatch { get; set; } private SpriteBatch SpriteBatch { get; set; }
@ -41,7 +41,7 @@ namespace TrueCraft.Client
private DateTime NextPhysicsUpdate { get; set; } private DateTime NextPhysicsUpdate { get; set; }
private bool MouseCaptured { get; set; } private bool MouseCaptured { get; set; }
private GameTime GameTime { 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; public static readonly int Reach = 5;
@ -62,6 +62,7 @@ namespace TrueCraft.Client
Graphics.IsFullScreen = UserSettings.Local.IsFullscreen; Graphics.IsFullScreen = UserSettings.Local.IsFullscreen;
Graphics.PreferredBackBufferWidth = UserSettings.Local.WindowResolution.Width; Graphics.PreferredBackBufferWidth = UserSettings.Local.WindowResolution.Width;
Graphics.PreferredBackBufferHeight = UserSettings.Local.WindowResolution.Height; Graphics.PreferredBackBufferHeight = UserSettings.Local.WindowResolution.Height;
Graphics.ApplyChanges();
Client = client; Client = client;
EndPoint = endPoint; EndPoint = endPoint;
LastPhysicsUpdate = DateTime.MinValue; LastPhysicsUpdate = DateTime.MinValue;
@ -85,9 +86,13 @@ namespace TrueCraft.Client
base.Initialize(); // (calls LoadContent) 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 HighlightModule(this));
Modules.Add(new PlayerControlModule(this)); Modules.Add(new PlayerControlModule(this));
Modules.Add(DebugInfoModule);
Client.PropertyChanged += HandleClientPropertyChanged; Client.PropertyChanged += HandleClientPropertyChanged;
Client.Connect(EndPoint); Client.Connect(EndPoint);
@ -136,9 +141,7 @@ namespace TrueCraft.Client
Pixel = new FontRenderer( Pixel = new FontRenderer(
new Font(Content, "Fonts/Pixel"), new Font(Content, "Fonts/Pixel"),
new Font(Content, "Fonts/Pixel", FontStyle.Bold), new Font(Content, "Fonts/Pixel", FontStyle.Bold), null, null,
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.Italic)); new Font(Content, "Fonts/Pixel", FontStyle.Italic));
base.LoadContent(); base.LoadContent();
@ -250,6 +253,7 @@ namespace TrueCraft.Client
GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp; GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
Mesh.ResetStats();
foreach (var module in Modules) foreach (var module in Modules)
{ {
var drawable = module as IGraphicalModule; var drawable = module as IGraphicalModule;