-
Notifications
You must be signed in to change notification settings - Fork 148
/
FPSCounter.cpp
64 lines (50 loc) · 1.79 KB
/
FPSCounter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "FPSCounter.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Graphics/Colour.h"
#include "Graphics/Gfx.h"
#include "Localisation/StringManager.h"
#include "Ui.h"
#include <chrono>
#include <stdio.h>
namespace OpenLoco::Drawing
{
using Clock_t = std::chrono::high_resolution_clock;
using TimePoint_t = Clock_t::time_point;
static TimePoint_t _referenceTime;
static uint32_t _currentFrameCount;
static float _currentFPS;
static float measureFPS()
{
_currentFrameCount++;
auto currentTime = Clock_t::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - _referenceTime).count() / 1000.0;
if (elapsed > 1.0)
{
_currentFPS = _currentFrameCount / elapsed;
_currentFrameCount = 0;
_referenceTime = currentTime;
}
return _currentFPS;
}
void drawFPS()
{
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
// Measure FPS
const float fps = measureFPS();
// Format string
char buffer[64];
buffer[0] = ControlCodes::Font::bold;
buffer[1] = ControlCodes::Font::outline;
buffer[2] = ControlCodes::Colour::white;
const char* formatString = (_currentFPS >= 10.0f ? "%.0f" : "%.1f");
snprintf(&buffer[3], std::size(buffer) - 3, formatString, fps);
auto& rt = Gfx::getScreenRT();
// Draw text
const int stringWidth = drawingCtx.getStringWidth(buffer);
const auto x = Ui::width() / 2 - (stringWidth / 2);
const auto y = 2;
drawingCtx.drawString(rt, x, y, Colour::black, buffer);
// Make area dirty so the text doesn't get drawn over the last
Gfx::invalidateRegion(x - 16, y - 4, x + 16, 16);
}
}