ScummVM API documentation
SynchroTimer.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef QDENGINE_QDCORE_UTIL_SYNCRO_TIMER
23 #define QDENGINE_QDCORE_UTIL_SYNCRO_TIMER
24 
25 #include "common/system.h"
26 
27 typedef uint32 time_type;
28 
29 namespace QDEngine {
30 
31 class SyncroTimer {
32 public:
33  // Main property
34  time_type operator()() const {
35  return round(_time);
36  }
37  // Last delta
38  time_type delta() const {
39  return _time - _time_prev;
40  }
41 
42  SyncroTimer() {
43  set(1, 15, 100);
44  _time_prev = _time = 1;
45  _time_offset = 0;
46  _time_speed = 1;
47  }
48 
49  void set(int syncro_by_clock, time_type time_per_frame, time_type max_time_interval) {
50  _syncro_by_clock = syncro_by_clock;
51  _time_per_frame = time_per_frame;
52  _max_time_interval = max_time_interval;
53  }
54 
55  SyncroTimer &adjust() {
56  _time_prev = _time;
57 
58  if (_syncro_by_clock) {
59  float t = float(g_system->getMillis());
60  float dt = (t - _time - _time_offset) * _time_speed;
61  if (dt > _max_time_interval)
62  dt = _max_time_interval;
63  _time += dt;
64  _time_offset = t - _time;
65  }
66  return *this;
67  }
68 
69  void next_frame() {
70  if (_syncro_by_clock)
71  adjust();
72  else {
73  _time_prev = _time;
74  _time += _time_per_frame * _time_speed;
75 // _time += round(_time_per_frame*_time_speed);
76  }
77  }
78 
79  void skip() {
80  if (_syncro_by_clock)
81  _time_offset = g_system->getMillis() - _time;
82  }
83 
84  void setTime(time_type t) {
85  _time_prev = _time = t;
86  _time_offset = _syncro_by_clock ? g_system->getMillis() - _time : 0;
87  }
88 
89  void setSpeed(float speed) {
90  _time_speed = speed;
91  }
92 
93 private:
94  float _time;
95  float _time_prev;
96  float _time_offset;
97  time_type _max_time_interval;
98  time_type _time_per_frame;
99  int _syncro_by_clock;
100  float _time_speed;
101 };
102 
103 } // namespace QDEngine
104 
105 #endif // QDENGINE_QDCORE_UTIL_SYNCRO_TIMER
virtual uint32 getMillis(bool skipRecord=false)=0
OSystem * g_system
Definition: SynchroTimer.h:31
Базовый класс для игровых ресурсов.
Definition: console.h:28