]> git.localhorst.tv Git - blank.git/blob - src/app/FrameCounter.hpp
gcc 4.8.5/centos compatibility
[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;
16                 T update;
17                 T render;
18                 T running;
19                 T waiting;
20                 T total;
21                 Frame();
22         };
23
24
25 public:
26         void EnterFrame() noexcept;
27         void EnterHandle() noexcept;
28         void ExitHandle() noexcept;
29         void EnterUpdate() noexcept;
30         void ExitUpdate() noexcept;
31         void EnterRender() noexcept;
32         void ExitRender() noexcept;
33         void ExitFrame() noexcept;
34
35         const Frame<int> &Peak() const noexcept { return peak; }
36         const Frame<float> &Average() const noexcept { return avg; }
37
38         bool Changed() const noexcept { return changed; }
39
40         void Print(std::ostream &) const;
41
42 private:
43         int Tick() noexcept;
44
45         void Accumulate() noexcept;
46         void Push() noexcept;
47
48 private:
49         static constexpr int NUM_FRAMES = 32;
50         static constexpr float factor = 1.0f / float(NUM_FRAMES);
51
52         Uint32 last_enter = 0;
53         Uint32 last_tick = 0;
54
55         int cur_frame = 0;
56         Frame<int> current = Frame<int>{};
57         Frame<int> sum = Frame<int>{};
58         Frame<int> max = Frame<int>{};
59
60         Frame<int> peak = Frame<int>{};
61         Frame<float> avg = Frame<float>{};
62
63         bool changed = false;
64
65 };
66
67
68 template<class T>
69 FrameCounter::Frame<T>::Frame()
70 : handle(0)
71 , update(0)
72 , render(0)
73 , running(0)
74 , waiting(0)
75 , total(0) {
76
77 }
78
79 }
80
81 #endif