1 /*
2  *  Copyright (C) 2005-2020 Team Kodi (https://kodi.tv)
3  *  Copyright (C) 2005 Joakim Eriksson <je@plane9.com>
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 
24   CTimer();
25   void Init(void);
26   void Update(void);
27   f32 GetDeltaTime(void);
28   void SetSpeed(f32 speed);
29 
30 protected:
31   double m_OldCount;
32   f32 m_DeltaTime;
33   f32 m_speed;
34 };
35 
36 /***************************** G L O B A L S *******************************/
37 /***************************** I N L I N E S *******************************/
38 
39 ////////////////////////////////////////////////////////////////////////////
40 //
CTimer()41 inline CTimer::CTimer()
42 {
43   m_DeltaTime = 0.0f;
44   m_speed = 1.0f;
45 }
46 
47 ////////////////////////////////////////////////////////////////////////////
48 //
Init(void)49 inline void CTimer::Init(void)
50 {
51   m_OldCount = std::chrono::duration<double>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////
55 //
Update(void)56 inline void CTimer::Update(void)
57 {
58   double time = std::chrono::duration<double>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
59   m_DeltaTime = (time - m_OldCount) * m_speed;
60   m_OldCount = time;
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////
64 //
GetDeltaTime(void)65 inline f32 CTimer::GetDeltaTime(void)
66 {
67   return m_DeltaTime;
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////
71 //
SetSpeed(f32 speed)72 void CTimer::SetSpeed(f32 speed)
73 {
74   m_speed = speed;
75 }
76