Debian Codegraph @

src/timer.h

raw
1 /*
2 * Copyright (C) 2005-2021 Team Kodi (https://kodi.tv)
3 * Copyright (C) 2005 Joakim Eriksson <[email protected]>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSE.md for more information.
7 */
8
9 #pragma once
10
11 #include "types.h"
12 #include <chrono>
13
14 /***************************** D E F I N E S *******************************/
15 /****************************** M A C R O S ********************************/
16 /************************** S T R U C T U R E S ****************************/
17
18 ////////////////////////////////////////////////////////////////////////////
19 //
20 class CTimer
21 {
22 public:
23 CTimer();
24 void Init(void);
25 void Update(void);
26 f32 GetDeltaTime(void);
27
28 protected:
29 double m_OldCount;
30 f32 m_DeltaTime;
31
32 static double WallTime ()
33 {
34 auto now = std::chrono::system_clock::now().time_since_epoch();
35 return std::chrono::duration_cast<std::chrono::microseconds>(now).count() / 1.0e6;
36 }
37 };
38
39 /***************************** G L O B A L S *******************************/
40 /***************************** I N L I N E S *******************************/
41
42 ////////////////////////////////////////////////////////////////////////////
43 //
44 inline CTimer::CTimer()
45 {
46 m_DeltaTime = 0.0f;
47 }
48
49 ////////////////////////////////////////////////////////////////////////////
50 //
51 inline void CTimer::Init(void)
52 {
53 m_OldCount = WallTime();
54 }
55
56 ////////////////////////////////////////////////////////////////////////////
57 //
58 inline void CTimer::Update(void)
59 {
60 m_DeltaTime = WallTime()-m_OldCount;
61 m_OldCount = WallTime();
62 }
63
64 ////////////////////////////////////////////////////////////////////////////
65 //
66 inline f32 CTimer::GetDeltaTime(void)
67 {
68 return m_DeltaTime;
69 }