ScummVM API documentation
ResourceDispatcher.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_RESOURCE_DISPATCHER_H
23#define QDENGINE_QDCORE_UTIL_RESOURCE_DISPATCHER_H
24
25#include "qdengine/qdcore/util/Handle.h"
26#include "qdengine/qdcore/util/SynchroTimer.h"
27
28
29namespace QDEngine {
30
32// General Time Resourcing
35 int ID;
36 static int IDs;
37public:
38 ResourceUser(time_type period) {
39 dtime = period;
40 time = 0;
41 ID = ++IDs;
42 }
43 virtual ~ResourceUser() {}
44 virtual int quant() {
45 return 1;
46 }
47
48protected:
49 time_type time;
50 time_type dtime;
51
52 virtual void init_time(time_type time_) {
53 time = time_ + time_step();
54 }
55 virtual time_type time_step() {
56 return dtime;
57 }
58
59 friend class ResourceDispatcher;
60};
61
63public:
64 typedef void (*type)();
65
66 VoidFunctionCallResourceUser(type func_, time_type dtime_) :
67 ResourceUser(dtime_), func(func_) {}
69 int quant() {
70 func();
71 return 1;
72 }
73
74protected:
75 type func;
76};
77
78template<class T>
80public:
81 typedef void (T::*type)();
82
83 MemberFunctionCallResourceUser(T &object_, type func_, time_type dtime_) :
84 ResourceUser(dtime_), object(object_), func(func_) {}
86 int quant() {
87 (object.*func)();
88 return 1;
89 }
90
91protected:
92 T &object;
93 type func;
94};
95
96
98// Resource Dispatcher
101public:
102 ResourceDispatcher() : _start_log(false), _max_time_interval(0) { }
103 void setTimer(int syncro_by_clock, time_type time_per_frame, time_type max_time_interval_) {
104 _syncro_timer.set(syncro_by_clock, time_per_frame, _max_time_interval = max_time_interval_);
105 }
106
107 void attach(ResourceUser *user) {
109 users.push_back(p);
110 users.back() = user;
111 user->init_time(_syncro_timer());
112 }
113 void attach(void (*func)(), time_type dtime) {
114 attach(new VoidFunctionCallResourceUser(func, dtime));
115 }
116 template<class T>
117 void attach(T &obj, void (T::*func)(), time_type dtime) {
118 attach(new MemberFunctionCallResourceUser<T>(obj, func, dtime));
119 }
120 void detach(ResourceUser *user) {
122 users.remove(p);
123 p.set(0);
124 }
125
126 void start() {
127 _start_log = true;
128 }
129 void reset();
130 void skip_time() {
131 _syncro_timer.skip();
132 }
133 void quant();
134 void set_speed(float speed) {
135 _syncro_timer.setSpeed(speed);
136 }
137
138private:
139
141 UserList users;
142
143 SyncroTimer _syncro_timer;
144 time_type _max_time_interval;
145 bool _start_log;
146
147 void do_start();
148};
149
150} // namespace QDEngine
151
152#endif // QDENGINE_QDCORE_UTIL_RESOURCE_DISPATCHER_H
t_T & back()
Definition: list.h:206
void push_back(const t_T &element)
Definition: list.h:174
void remove(const t_T &val)
Definition: list.h:142
Definition: ResourceDispatcher.h:79
Definition: Handle.h:33
Definition: ResourceDispatcher.h:100
Definition: ResourceDispatcher.h:34
Definition: SynchroTimer.h:31
Definition: ResourceDispatcher.h:62
Definition: list.h:39
Базовый класс для игровых ресурсов.
Definition: console.h:28