ScummVM API documentation
qd_resource_container.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
23#ifndef QDENGINE_QDCORE_QD_RESOURCE_CONTAINER_H
24#define QDENGINE_QDCORE_QD_RESOURCE_CONTAINER_H
25
26#include "qdengine/qdcore/qd_animation.h"
27#include "qdengine/qdcore/qd_resource_dispatcher.h"
28#include "qdengine/qdcore/qd_sound.h"
29
30
32
37namespace QDEngine {
38
39template<class T>
41public:
43 virtual ~qdResourceContainer();
44
46 qdResource *add_resource(const Common::Path &file_name, const T *owner);
47
49
52 bool remove_resource(const Common::Path &file_name, const T *owner);
53
55
58 qdResource *get_resource(const Common::Path &file_name) const;
59
63 return _resource_list;
64 }
67
68private:
69
72 resource_map_t _resource_map;
73
75 resource_list_t _resource_list;
76
78 qdResourceDispatcher<T> _resource_dispatcher;
79};
80
81template<class T>
83
84template<class T>
85qdResourceContainer<T>::~qdResourceContainer() {
86 for (resource_list_t::iterator it = _resource_list.begin(); it != _resource_list.end(); ++it)
87 delete *it;
88}
89
90template<class T>
92 typename resource_map_t::iterator it = _resource_map.find(file_name.toString());
93 if (it != _resource_map.end()) {
94 _resource_dispatcher.register_resource(it->_value, owner);
95 return it->_value;
96 }
97
98 qdResource *p = NULL;
99 switch (qdResource::file_format(file_name)) {
101 p = new qdAnimation;
102 p->set_resource_file(file_name);
103 break;
105 p = new qdSound;
106 p->set_resource_file(file_name);
107 break;
109 qdAnimation *anm = new qdAnimation;
111 fr->set_file(file_name);
112 anm->add_frame(fr);
113 p = anm;
114 }
115 break;
116 default:
117 break;
118 }
119
120 if (!p)
121 return NULL;
122
123 _resource_map[file_name.toString()] = p;
124 _resource_list.push_back(p);
125
126 _resource_dispatcher.register_resource(p, owner);
127
128 return p;
129}
130
131template<class T>
132bool qdResourceContainer<T>::remove_resource(const Common::Path &file_name, const T *owner) {
133 typename resource_map_t::iterator it = _resource_map.find(file_name.toString());
134
135 if (it == _resource_map.end()) return false;
136
137 qdResource *p = it->_value;
138 _resource_dispatcher.unregister_resource(p, owner);
139
140 if (!_resource_dispatcher.is_registered(p)) {
141 _resource_map.erase(it);
142 resource_list_t::iterator it2 = Common::find(_resource_list.begin(), _resource_list.end(), p);
143 if (it2 != _resource_list.end()) {
144 delete p;
145 _resource_list.erase(it2);
146 return true;
147 }
148
149 return false;
150 }
151
152 return true;
153}
154
155template<class T>
157 if (file_name.empty()) return NULL;
158
159 typename resource_map_t::const_iterator it = _resource_map.find(file_name.toString());
160 if (it != _resource_map.end())
161 return it->_value;
162
163 return NULL;
164}
165
166template<class T>
168 for (resource_list_t::iterator it = _resource_list.begin(); it != _resource_list.end(); ++it) {
169 if (!(*it)->resource_file().empty()) {
170 T *ptr = const_cast<T *>(_resource_dispatcher.find_owner(*it));
171 owners.push_back(ptr);
172 }
173 }
174}
175
176} // namespace QDEngine
177
178#endif // QDENGINE_QDCORE_QD_RESOURCE_CONTAINER_H
void push_back(const t_T &element)
Definition: list.h:174
Definition: path.h:52
String toString(char separator='/') const
bool empty() const
Definition: path.h:353
Кадр анимации.
Definition: qd_animation_frame.h:31
Анимация.
Definition: qd_animation.h:57
Definition: qd_resource_container.h:40
bool remove_resource(const Common::Path &file_name, const T *owner)
Удаляет ресурс, если он нужен только для объекта owner.
Definition: qd_resource_container.h:132
qdResource * add_resource(const Common::Path &file_name, const T *owner)
Добавляет ресурс из файла file_name и возвращает указатель на него.
Definition: qd_resource_container.h:91
void get_owners(Std::list< T * > &owners)
Возвращает всех владельцев ресурсов
Definition: qd_resource_container.h:167
qdResource * get_resource(const Common::Path &file_name) const
Возвращает указатель на ресурс, соответствующий файлу с именем file_name.
Definition: qd_resource_container.h:156
const resource_list_t & resource_list() const
Возвращает список ресурсов.
Definition: qd_resource_container.h:62
Диспетчер ресурсов.
Definition: qd_resource_dispatcher.h:32
Definition: qd_resource.h:34
virtual void set_resource_file(const Common::Path &file_name)=0
Устанавливает имя файла, в котором хранятся данные ресурса.
@ RES_SPRITE
спрайт - .tga
Definition: qd_resource.h:41
@ RES_SOUND
звук - .wav
Definition: qd_resource.h:43
@ RES_ANIMATION
анимация - .qda
Definition: qd_resource.h:39
Звук.
Definition: qd_sound.h:35
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Базовый класс для игровых ресурсов.
Definition: console.h:28
Definition: list_intern.h:54