]> git.localhorst.tv Git - blank.git/blob - src/app/FrameCounter.hpp
collect timing information
[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         void EnterFrame() noexcept;
14         void EnterHandle() noexcept;
15         void ExitHandle() noexcept;
16         void EnterUpdate() noexcept;
17         void ExitUpdate() noexcept;
18         void EnterRender() noexcept;
19         void ExitRender() noexcept;
20         void ExitFrame() noexcept;
21
22         float AvgHandle() const noexcept { return avg.handle; }
23         float AvgUpdate() const noexcept { return avg.update; }
24         float AvgRender() const noexcept { return avg.render; }
25         float AvgFrame() const noexcept { return avg.total; }
26         float AvgRunning() const noexcept { return avg.handle + avg.update + avg.render; }
27         float AvgWaiting() const noexcept { return avg.total - AvgRunning(); }
28
29         bool Changed() const noexcept { return changed; }
30
31         void Print(std::ostream &) const;
32
33 private:
34         int Tick() noexcept;
35
36 private:
37         static constexpr int NUM_FRAMES = 32;
38         static constexpr float factor = 1.0f / float(NUM_FRAMES);
39
40         template<class T>
41         struct Frame {
42                 T handle = T(0);
43                 T update = T(0);
44                 T render = T(0);
45                 T total = T(0);
46         };
47
48         Uint32 last_enter = 0;
49         Uint32 last_tick = 0;
50
51         int cur_frame = 0;
52         Frame<int> running = Frame<int>{};
53         Frame<float> avg = Frame<float>{};
54         bool changed = false;
55
56 };
57
58 }
59
60 #endif