Skip to content

Commit

Permalink
Add FPS counter to Visual Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
thekovic committed Feb 11, 2023
1 parent 38064d7 commit 5b5821e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Source/Core/Rendering/Renderer3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ internal sealed class Renderer3D : Renderer, IRenderer3D
// Things to be rendered, sorted by distance from camera
private BinaryHeap<VisualThing> thingsbydistance;

// FPS-related
private int fps = 0;
private System.Diagnostics.Stopwatch fpsWatch;
private TextLabel fpsLabel;

#endregion

#region ================== Properties
Expand Down Expand Up @@ -135,6 +140,12 @@ internal Renderer3D(D3DDevice graphics) : base(graphics)
frustum = new ProjectedFrustum2D(new Vector2D(), 0.0f, 0.0f, PROJ_NEAR_PLANE,
General.Settings.ViewDistance, Angle2D.DegToRad((float)General.Settings.VisualFOV));

fpsLabel = new TextLabel(7);
fpsLabel.AlignX = TextAlignmentX.Left;
fpsLabel.AlignY = TextAlignmentY.Top;
fpsLabel.Text = "(FPS unavailable)";
fpsWatch = new System.Diagnostics.Stopwatch();

// We have no destructor
GC.SuppressFinalize(this);
}
Expand Down Expand Up @@ -422,6 +433,11 @@ public void ApplyMatrices2D()
// This starts rendering
public bool Start()
{
if (!fpsWatch.IsRunning)
{
fpsWatch.Start();
}

// Create texture
if (General.Map.Data.ThingBox.Texture == null)
General.Map.Data.ThingBox.CreateTexture();
Expand Down Expand Up @@ -549,6 +565,15 @@ public void FinishGeometry()
// Done
graphics.Shaders.World3D.End();
geometry = null;

fps++;
if (fpsWatch.ElapsedMilliseconds > 1000)
{
fpsLabel.Text = string.Format("{0} FPS", fps);
fps = 0;
fpsWatch.Reset();
fpsWatch.Start();
}
}

// villsa 9/15/11
Expand Down Expand Up @@ -989,6 +1014,8 @@ public void RenderCrosshair()
graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, crosshairverts);
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();

General.Map.Renderer2D.RenderText(fpsLabel);
}

// This switches fog on and off
Expand Down

0 comments on commit 5b5821e

Please sign in to comment.