]> git.localhorst.tv Git - blank.git/blob - src/app/FrameCounter.hpp
16b6b4e5bd17152e7b88bc79ef2952bd91dbfc2e
[blank.git] / src / app / FrameCounter.hpp
1 #ifndef BLANK_APP_FRAMECOUNTER_HPP_
2 #define BLANK_APP_FRAMECOUNTER_HPP_
3
4 #include <iosfwd>
5 #include <SDL.h>
6
7
8 namespace blank {
9
10 class FrameCounter {
11
12 public:
13         template<class T>
14         struct Frame {
15                 T handle = T(0);
16                 T update = T(0);
17                 T render = T(0);
18                 T running = T(0);
19                 T waiting = T(0);
20                 T total = T(0);
21         };
22
23
24 public:
25         void EnterFrame() noexcept;
26         void EnterHandle() noexcept;
27         void ExitHandle() noexcept;
28         void EnterUpdate() noexcept;
29         void ExitUpdate() noexcept;
30         void EnterRender() noexcept;
31         void ExitRender() noexcept;
32         void ExitFrame() noexcept;
33
34         const Frame<int> &Peak() const noexcept { return peak; }
35         const Frame<float> &Average() const noexcept { return avg; }
36
37         bool Changed() const noexcept { return changed; }
38
39 private:
40         int Tick() noexcept;
41
42         void Accumulate() noexcept;
43         void Push() noexcept;
44
45 private:
46         static constexpr int NUM_FRAMES = 32;
47         static constexpr float factor = 1.0f / float(NUM_FRAMES);
48
49         Uint32 last_enter = 0;
50         Uint32 last_tick = 0;
51
52         int cur_frame = 0;
53         Frame<int> current = Frame<int>{};
54         Frame<int> sum = Frame<int>{};
55         Frame<int> max = Frame<int>{};
56
57         Frame<int> peak = Frame<int>{};
58         Frame<float> avg = Frame<float>{};
59
60         bool changed = false;
61
62 };
63
64 }
65
66 #endif