File: FrameTimer.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (85 lines) | stat: -rw-r--r-- 2,621 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once

#include "TimeStamp.h"

namespace nCine
{
	/// Frame interval and average FPS calculator
	class FrameTimer
	{
	public:
		/** @brief Frames per second */
		static constexpr float FramesPerSecond = 60.0f;
		/** @brief Seconds per frame */
		static constexpr float SecondsPerFrame = 1.0f / FramesPerSecond;

		/// Constructor
		FrameTimer(float logInterval, float avgInterval);

		/// Adds a frame to the counter and calculates the interval since the previous one
		void AddFrame();

		/// Starts counting the suspension time
		void Suspend();
		/// Drifts timers by the duration of last suspension
		/*! \return A timestamp with last suspension duration */
		TimeStamp Resume();

		/// Returns the total number of frames counted
		inline std::uint32_t GetTotalNumberFrames() const {
			return _totNumFrames;
		}
		/// Returns the last frame duration in seconds between the last two subsequent calls to `addFrame()`
		inline float GetLastFrameDuration() const {
			return _frameDuration;
		}
		/// Returns current frame duration in seconds since the last call to `addFrame()`
		inline float GetFrameDuration() const {
			return _frameStart.secondsSince();
		}
		/// Returns current frame duration in ticks since the last call to `addFrame()`
		inline std::uint64_t GetFrameDurationAsTicks() const {
			return _frameStart.timeSince().ticks();
		}
		/// Returns the average FPS during the update interval
		inline float GetAverageFps() const {
			return _avgFps;
		}

		/// Returns a factor that represents how long the last frame took relative to the desired frame time
		inline float GetTimeMult() const {
			return _timeMults[0];
		}

	private:
		/// Number of seconds between two average FPS calculations (user defined)
		float _averageInterval;
		/// Number of seconds between two logging events (user defined)
		float _loggingInterval;

		/// Timestamp at the beginning of a frame
		TimeStamp _frameStart;
		/// Last frame duration in seconds
		float _frameDuration;
		/// Timestamp at the begininng of application suspension
		TimeStamp _suspensionStart;

		/// Total number of frames counted
		std::uint32_t _totNumFrames;
		/// Frame counter for average FPS calculation
		std::uint32_t _avgNumFrames;
		/// Frame counter for logging
		std::uint32_t _logNumFrames;

		/// Time stamp at last average FPS calculation
		TimeStamp _lastAvgUpdate;
		/// Time stamp at last log event
		TimeStamp _lastLogUpdate;

		/// Average FPS calulated during the specified interval
		float _avgFps;

		/// Factor that represents how long the last frame took relative to the desired frame time
		float _timeMults[3];
	};
}