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 
37 namespace QDEngine {
38 
39 template<class T>
41 public:
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 
62  const resource_list_t &resource_list() const {
63  return _resource_list;
64  }
66  void get_owners(Std::list<T *> &owners);
67 
68 private:
69 
72  resource_map_t _resource_map;
73 
75  resource_list_t _resource_list;
76 
78  qdResourceDispatcher<T> _resource_dispatcher;
79 };
80 
81 template<class T>
83 
84 template<class T>
86  for (resource_list_t::iterator it = _resource_list.begin(); it != _resource_list.end(); ++it)
87  delete *it;
88 }
89 
90 template<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;
108  case qdResource::RES_SPRITE: {
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 
131 template<class T>
132 bool 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 
155 template<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 
166 template<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
спрайт - .tga.
Definition: qd_resource.h:41
Кадр анимации.
Definition: qd_animation_frame.h:31
Definition: qd_resource_container.h:40
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Звук.
Definition: qd_sound.h:35
Definition: path.h:52
звук - .wav.
Definition: qd_resource.h:43
Definition: qd_resource.h:34
анимация - .qda.
Definition: qd_resource.h:39
const resource_list_t & resource_list() const
Возвращает список ресурсов.
Definition: qd_resource_container.h:62
bool empty() const
Definition: path.h:353
Диспетчер ресурсов.
Definition: qd_resource_dispatcher.h:32
iterator end()
Definition: list.h:240
Базовый класс для игровых ресурсов.
Definition: console.h:28
iterator begin()
Definition: list.h:227
Анимация.
Definition: qd_animation.h:57
bool remove_resource(const Common::Path file_name, const T *owner)
Удаляет ресурс, если он нужен только для объекта owner.
Definition: qd_resource_container.h:132
String toString(char separator='/') const
void erase(iterator entry)
Definition: hashmap.h:710
void get_owners(Std::list< T *> &owners)
Возвращает всех владельцев ресурсов
Definition: qd_resource_container.h:167
Definition: list_intern.h:51
iterator erase(iterator pos)
Definition: list.h:95
virtual void set_resource_file(const Common::Path file_name)=0
Устанавливает имя файла, в котором хранятся данные ресурса.
qdResource * add_resource(const Common::Path file_name, const T *owner)
Добавляет ресурс из файла file_name и возвращает указатель на него.
Definition: qd_resource_container.h:91
void push_back(const t_T &element)
Definition: list.h:140
qdResource * get_resource(const Common::Path file_name) const
Возвращает указатель на ресурс, соответствующий файлу с именем file_name.
Definition: qd_resource_container.h:156