ScummVM API documentation
timers.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  * Additional copyright for this file:
8  * Copyright (C) 1995-1997 Presto Studios, Inc.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24 
25 #ifndef PEGASUS_TIMERS_H
26 #define PEGASUS_TIMERS_H
27 
28 #include "common/rational.h"
29 #include "common/func.h"
30 
31 #include "pegasus/constants.h"
32 #include "pegasus/notification.h"
33 #include "pegasus/util.h"
34 
35 namespace Pegasus {
36 
37 class Idler {
38 friend class PegasusEngine;
39 
40 public:
41  Idler();
42  virtual ~Idler();
43 
44  virtual void startIdling();
45  virtual void stopIdling();
46  bool isIdling() const { return _isIdling; }
47 
48 protected:
49  virtual void useIdleTime() {}
50 
51  bool _isIdling;
52  Idler *_nextIdler, *_prevIdler;
53 };
54 
55 enum {
56  kLoopTimeBase = 1,
57  kPalindromeLoopTimeBase = 2,
58  kMaintainTimeBaseZero = 4
59 };
60 
61 class TimeBaseCallBack;
62 
63 class TimeBase {
64 friend class TimeBaseCallBack;
65 public:
66  TimeBase(const TimeScale = kDefaultTimeScale);
67  virtual ~TimeBase();
68 
69  virtual void setTime(const TimeValue, const TimeScale = 0);
70  virtual TimeValue getTime(const TimeScale = 0);
71 
72  virtual void setScale(const TimeScale scale) { _preferredScale = scale; }
73  virtual TimeScale getScale() const { return _preferredScale; }
74 
75  virtual void setRate(const Common::Rational);
76  virtual Common::Rational getRate() const { return _rate; }
77 
78  virtual void start();
79  virtual void stop();
80  virtual bool isRunning();
81 
82  virtual void pause();
83  virtual void resume();
84  bool isPaused() const { return _paused; }
85 
86  virtual void setFlags(const uint32 flags) { _flags = flags; }
87  virtual uint32 getFlags() const { return _flags; }
88 
89  virtual void setStart(const TimeValue, const TimeScale = 0);
90  virtual TimeValue getStart(const TimeScale = 0) const;
91 
92  virtual void setStop(const TimeValue, const TimeScale = 0);
93  virtual TimeValue getStop(const TimeScale = 0) const;
94 
95  virtual void setSegment(const TimeValue, const TimeValue, const TimeScale = 0);
96  virtual void getSegment(TimeValue&, TimeValue&, const TimeScale = 0) const;
97 
98  virtual TimeValue getDuration(const TimeScale = 0) const;
99 
100  virtual void setMasterTimeBase(TimeBase *timeBase);
101 
102  void disposeAllCallBacks();
103 
104  // ScummVM's API additions (to replace the need for actual timers)
105  virtual void checkCallBacks();
106 
107 protected:
108  void addCallBack(TimeBaseCallBack *);
109  void removeCallBack(TimeBaseCallBack *);
110  virtual void updateTime();
111 
112  TimeBase *_master;
113  TimeScale _preferredScale;
114  TimeBaseCallBack *_callBackList;
115  Common::Rational _rate, _pausedRate;
116  bool _paused;
117  uint32 _startTime, _startScale;
118  uint32 _stopTime, _stopScale;
119  uint32 _flags;
120 
121  Common::Rational _time;
122  uint32 _lastMillis, _pauseStart;
123 };
124 
125 // Type passed to initCallBack()
126 enum CallBackType {
127  kCallBackNone = 0,
128  kCallBackAtTime = 1,
129  kCallBackAtExtremes = 4
130 };
131 
132 // Trigger passed to scheduleCallBack()
133 enum CallBackTrigger {
134  kTriggerNone = 0,
135 
136  // AtTime flags
137  kTriggerTimeFwd = 1,
138 
139  // AtExtremes flags
140  kTriggerAtStart = 1,
141  kTriggerAtStop = 2
142 };
143 
145 friend class TimeBase;
146 
147 public:
149  virtual ~TimeBaseCallBack();
150 
151  void initCallBack(TimeBase *, CallBackType type);
152 
153  void releaseCallBack();
154 
155  void scheduleCallBack(CallBackTrigger trigger, uint32 param2, uint32 param3);
156  void cancelCallBack();
157 
158 protected:
159  virtual void callBack() = 0;
160 
161  TimeBase *_timeBase;
162 
163  // Owned and operated by TimeBase;
164  TimeBaseCallBack *_nextCallBack;
165 
166  // Our storage of the QuickTime timer crap
167  CallBackType _type;
168  CallBackTrigger _trigger;
169  uint32 _param2, _param3;
170  bool _hasBeenTriggered;
171 
172 private:
173  void disposeCallBack();
174 };
175 
176 class IdlerTimeBase : public Idler, public TimeBase {
177 public:
178  IdlerTimeBase();
179  ~IdlerTimeBase() override { stopIdling(); }
180 
181  TimeValue getLastTime() const { return _lastTime; }
182 
183 protected:
184  void useIdleTime() override;
185  virtual void timeChanged(const TimeValue) {}
186 
187  TimeValue _lastTime;
188 
189 };
190 
192 public:
194  ~NotificationCallBack() override {}
195 
196  void setNotification(Notification *notifier) { _notifier = notifier; }
197 
198  void setCallBackFlag(const NotificationFlags flag) { _callBackFlag = flag; }
199  NotificationFlags getCallBackFlag() const { return _callBackFlag; }
200 
201 protected:
202  void callBack() override;
203 
204  Notification *_notifier;
205  NotificationFlags _callBackFlag;
206 };
207 
208 class DynamicElement : public TimeBase {
209 public:
210  TimeValue percentSeconds(const uint32 percent) { return getScale() * percent / 100; }
211 };
212 
213 class Fuse : private NotificationReceiver {
214 public:
215  Fuse();
216  ~Fuse() override {}
217 
218  void primeFuse(const TimeValue, const TimeScale = 1); // An appropriately named function :P
219  void lightFuse();
220  void stopFuse();
221  bool isFuseLit() { return _fuseTimer.isRunning() || _fuseTimer.isPaused(); }
222  void advanceFuse(const TimeValue);
223  TimeValue getTimeRemaining();
224  TimeScale getFuseScale() { return _fuseTimer.getScale(); }
225 
226  void pauseFuse() { _fuseTimer.pause(); }
227  void resumeFuse() { _fuseTimer.resume(); }
228  bool isFusePaused() { return _fuseTimer.isPaused(); }
229 
230 protected:
231  void receiveNotification(Notification *, const NotificationFlags) override;
232  virtual void invokeAction() {}
233 
234  TimeBase _fuseTimer;
235  NotificationCallBack _fuseCallBack;
236  Notification _fuseNotification;
237 };
238 
239 class FuseFunction : public Fuse {
240 public:
241  FuseFunction() : _functor(0) {}
242  ~FuseFunction() override { delete _functor; }
243 
244  void setFunctor(Common::Functor0<void> *functor) { delete _functor; _functor = functor; }
245 protected:
246  void invokeAction() override { if (_functor && _functor->isValid()) (*_functor)(); }
247 
248  Common::Functor0<void> *_functor;
249 };
250 
251 } // End of namespace Pegasus
252 
253 #endif
Definition: timers.h:37
Definition: timers.h:208
Definition: timers.h:176
Definition: timers.h:239
Definition: rational.h:40
Definition: timers.h:63
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: timers.h:144
Definition: notification.h:53
Definition: timers.h:191
Definition: pegasus.h:70
Definition: notification.h:83
Definition: timers.h:213
Definition: ai_action.h:33