ScummVM API documentation
resourcesystem.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 ILLUSIONS_RESOURCESYSTEM_H
23 #define ILLUSIONS_RESOURCESYSTEM_H
24 
25 #include "common/array.h"
26 #include "common/file.h"
27 #include "common/hashmap.h"
28 #include "common/memstream.h"
29 #include "common/str.h"
30 #include "common/substream.h"
31 #include "common/system.h"
32 
33 namespace Illusions {
34 
35 #define ResourceTypeId(x) ((x) & 0xFFFF0000)
36 
37 class BaseResourceLoader;
38 class BaseResourceReader;
39 class IllusionsEngine;
40 struct Resource;
41 
43 public:
44  virtual void load(Resource *resource);
45  virtual void unload();
46  virtual void pause();
47  virtual void unpause();
48  virtual ~ResourceInstance();
49 };
50 
51 struct Resource {
52  bool _loaded;
53  uint32 _resId;
54  uint32 _sceneId;
55  uint32 _threadId;
56  byte *_data;
57  uint32 _dataSize;
58  int _gameId;
59  Common::String _filename;
60  ResourceInstance *_instance;
61  Resource() : _loaded(false), _resId(0), _sceneId(0), _threadId(0), _data(0), _dataSize(0), _instance(0) {
62  }
63  ~Resource() {
64  if (_instance)
65  _instance->unload();
66  delete _instance;
67  unloadData();
68  }
69  void loadData(BaseResourceReader *resReader);
70  void unloadData();
71 };
72 
73 enum {
74  kRlfLoadFile,
75  kRlfFreeDataAfterLoad
76 };
77 
79 public:
80  virtual ~BaseResourceLoader() {}
81  virtual void load(Resource *resource) = 0;
82  virtual bool isFlag(int flag) = 0;
83 };
84 
86 public:
88  ~ResourceSystem();
89 
90  void addResourceLoader(uint32 resTypeId, BaseResourceLoader *resourceLoader);
91 
92  void loadResource(uint32 resId, uint32 sceneId, uint32 threadId);
93  void unloadResourceById(uint32 resId);
94  void unloadResourcesBySceneId(uint32 sceneId);
95  void unloadSceneResources(uint32 sceneId1, uint32 sceneId2);
96  Resource *getResource(uint32 resId);
97 
98 protected:
100  typedef ResourceLoadersMap::iterator ResourceLoadersMapIterator;
101  IllusionsEngine *_vm;
102  ResourceLoadersMap _resourceLoaders;
103  BaseResourceLoader *getResourceLoader(uint32 resId);
104 
106  typedef ResourcesArray::iterator ResourcesArrayIterator;
107  ResourcesArray _resources;
108 
109  struct ResourceEqualById : public Common::UnaryFunction<const Resource*, bool> {
110  uint32 _resId;
111  ResourceEqualById(uint32 resId) : _resId(resId) {}
112  bool operator()(const Resource *resource) const {
113  return resource->_resId == _resId;
114  }
115  };
116 
117  struct ResourceEqualByValue : public Common::UnaryFunction<const Resource*, bool> {
118  const Resource *_resource;
119  ResourceEqualByValue(const Resource *resource) : _resource(resource) {}
120  bool operator()(const Resource *resource) const {
121  return resource == _resource;
122  }
123  };
124 
125  struct ResourceEqualBySceneId : public Common::UnaryFunction<const Resource*, bool> {
126  uint32 _sceneId;
127  ResourceEqualBySceneId(uint32 sceneId) : _sceneId(sceneId) {}
128  bool operator()(const Resource *resource) const {
129  return resource->_sceneId == _sceneId;
130  }
131  };
132 
133  struct ResourceNotEqualByScenes : public Common::UnaryFunction<const Resource*, bool> {
134  uint32 _sceneId1, _sceneId2;
135  ResourceNotEqualByScenes(uint32 sceneId1, uint32 sceneId2) : _sceneId1(sceneId1), _sceneId2(sceneId2) {}
136  bool operator()(const Resource *resource) const {
137  return resource->_sceneId != _sceneId1 && resource->_sceneId != _sceneId2;
138  }
139  };
140 
141  void unloadResource(Resource *resource);
142 
143 };
144 
145 } // End of namespace Illusions
146 
147 #endif // ILLUSIONS_RESOURCESYSTEM_H
Definition: str.h:59
Definition: resourcesystem.h:85
T * iterator
Definition: array.h:54
Definition: actor.h:34
Definition: resourcesystem.h:51
Definition: resourcesystem.h:109
Definition: resourcereader.h:29
Definition: resourcesystem.h:42
Definition: resourcesystem.h:78
Definition: func.h:43
Definition: illusions.h:80
Definition: resourcesystem.h:117