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