1.10
tools.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_TOOLS_H
7#define EGT_TOOLS_H
8
9#include <chrono>
10#include <cstdint>
11#include <egt/detail/meta.h>
12
18namespace egt
19{
20inline namespace v1
21{
22namespace experimental
23{
27class EGT_API CPUMonitorUsage
28{
29public:
30
34 EGT_NODISCARD inline double usage() const { return m_cpu_usage; }
35
41 void update();
42
43private:
44
45 size_t m_last_total_time{0};
46 size_t m_last_idle_time{0};
47 double m_cpu_usage{0};
48};
49
63class EGT_API FramesPerSecond
64{
65public:
66
67 FramesPerSecond() noexcept
68 {
69 start();
70 }
71
75 void start()
76 {
77 m_start = std::chrono::steady_clock::now();
78 m_frames = 0;
79 }
80
84 void end_frame()
85 {
86 m_frames++;
87
88 const auto now = std::chrono::steady_clock::now();
89 const auto diff = std::chrono::duration<double>(now - m_start).count();
90 if (diff > 1.0)
91 {
92 m_fps = m_frames / diff;
93 m_ready = true;
94 start();
95 }
96 }
97
101 EGT_NODISCARD bool ready() const { return m_ready && m_frames > 0; }
102
106 float fps()
107 {
108 m_ready = false;
109 return m_fps;
110 }
111
112protected:
113
115 std::chrono::time_point<std::chrono::steady_clock> m_start{};
116
118 uint64_t m_frames{0};
119
121 float m_fps{0.};
122
124 bool m_ready{false};
125};
126
127}
128}
129}
130
131#endif
Monitor CPU usage of the system.
Definition tools.h:28
EGT_NODISCARD double usage() const
Get the total CPU usage as a percentage.
Definition tools.h:34
void update()
Update the CPU usage.
Calculate "frame-per-second" of something.
Definition tools.h:64
void end_frame()
Call at the end of every frame.
Definition tools.h:84
void start()
Start/reset the counter.
Definition tools.h:75
float fps()
Retrieve the current FPS value.
Definition tools.h:106
EGT_NODISCARD bool ready() const
Is any calculation ready?
Definition tools.h:101
FramesPerSecond() noexcept
Definition tools.h:67
EGT framework namespace.
Definition animation.h:24