Skip to content

Commit d3d8065

Browse files
committed
devtools: add a bad/hacky FPS counter
1 parent e65b500 commit d3d8065

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "StdInc.h"
2+
#include <ConsoleHost.h>
3+
4+
#include <imgui.h>
5+
6+
#include <CoreConsole.h>
7+
8+
#include <mmsystem.h>
9+
10+
static InitFunction initFunction([]()
11+
{
12+
static bool drawFpsEnabled;
13+
static bool streamingListEnabled;
14+
static bool streamingMemoryEnabled;
15+
16+
static ConVar<bool> drawFps("cl_drawFPS", ConVar_Archive, false, &drawFpsEnabled);
17+
18+
ConHost::OnShouldDrawGui.Connect([](bool* should)
19+
{
20+
*should = *should || drawFpsEnabled;
21+
});
22+
23+
ConHost::OnDrawGui.Connect([]()
24+
{
25+
if (!drawFpsEnabled)
26+
{
27+
return;
28+
}
29+
30+
auto& io = ImGui::GetIO();
31+
32+
static std::chrono::high_resolution_clock::duration previous;
33+
static uint32_t index;
34+
static std::chrono::microseconds previousTimes[6];
35+
36+
auto t = std::chrono::high_resolution_clock::now().time_since_epoch();
37+
auto frameTime = std::chrono::duration_cast<std::chrono::microseconds>(t - previous);
38+
previous = t;
39+
40+
previousTimes[index % std::size(previousTimes)] = frameTime;
41+
index++;
42+
43+
ImGui::SetNextWindowBgAlpha(0.0f);
44+
ImGui::SetNextWindowPos(ImVec2(10, 10), 0, ImVec2(0.0f, 0.0f));
45+
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
46+
47+
if (ImGui::Begin("DrawFps", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize))
48+
{
49+
if (index > 6)
50+
{
51+
std::chrono::microseconds total{ 0 };
52+
53+
for (int i = 0; i < std::size(previousTimes); i++)
54+
{
55+
total += previousTimes[i];
56+
}
57+
58+
if (total.count() == 0)
59+
{
60+
total = { 1 };
61+
}
62+
63+
uint64_t fps = ((uint64_t)1000000 * 1000) * std::size(previousTimes) / total.count();
64+
fps = (fps + 500) / 1000;
65+
66+
ImGui::Text("%llufps", fps);
67+
}
68+
}
69+
70+
ImGui::End();
71+
});
72+
});

0 commit comments

Comments
 (0)