ScummVM API documentation
resource.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 GNAP_RESOURCE_H
23#define GNAP_RESOURCE_H
24
25#include "common/array.h"
26#include "common/events.h"
27#include "common/file.h"
28#include "common/memstream.h"
29#include "common/str.h"
30#include "common/stream.h"
31#include "common/substream.h"
32#include "common/system.h"
33
34#include "graphics/surface.h"
35
36#include "gnap/datarchive.h"
37
38namespace Gnap {
39
40enum {
41 kResTypeSprite = 0,
42 kResTypeBitmap = 1,
43 kResTypeSound = 2,
44 kResTypeSequence = 3
45};
46
48 int16 _duration;
49 bool _isScaled;
50 Common::Rect _rect;
51 int32 _spriteId;
52 int32 _soundId;
53 void loadFromStream(Common::MemoryReadStream &stream);
54};
55
57 int32 _additionalDelay;
58 int16 _framesCount;
59 int16 _maxTotalDuration;
60 SequenceFrame *frames;
61
62 SequenceAnimation() : frames(nullptr), _additionalDelay(0), _framesCount(0), _maxTotalDuration(0) {}
63 ~SequenceAnimation() { delete[] frames; }
64 void loadFromStream(Common::MemoryReadStream &stream);
65};
66
68public:
69 SequenceResource(byte *data, uint32 size);
71public:
72 int32 _sequenceId;
73 int32 _defaultId;
74 int32 _sequenceId2;
75 uint32 _defaultId2;
76 uint32 _flags;
77 int32 _totalDuration;
78 int16 _xOffs;
79 int16 _yOffs;
80 int32 _animationsCount;
81 SequenceAnimation *_animations;
82};
83
85public:
86 SpriteResource(byte *data, uint32 size);
88public:
89 byte *_data;
90 byte *_pixels;
91 uint32 *_palette;
92 int16 _width, _height;
93 uint16 _unknownVal1;
94 uint16 _unknownVal2;
95 bool _transparent;
96 uint16 _colorsCount;
97};
98
100public:
101 SoundResource(byte *data, uint32 size);
103public:
104 byte *_data;
105 uint32 _size;
106};
107
108template <class ResourceClass, int ResourceType, bool FreeAfterLoad>
110public:
111
112 ResourceCacheTemplate(DatManager *dat) : _dat(dat) {
113 }
114
116 }
117
118 ResourceClass *get(int resourceId) {
119 Resource *resource = find(resourceId);
120 if (!resource) {
121 debug(9, "Loading resource type %d with ID %08X from disk", ResourceType, resourceId);
122 resource = new Resource(load(resourceId));
123 _cache[resourceId] = resource;
124 } else {
125 debug(9, "Resource type %d with ID %08X was in cache", ResourceType, resourceId);
126 }
127 resource->_isLocked = true;
128 return resource->_obj;
129 }
130
131 void release(int resourceId) {
132 Resource *resource = find(resourceId);
133 if (resource)
134 resource->_isLocked = false;
135 }
136
137 void purge(bool force = false) {
138 for (CacheMapIterator it = _cache.begin(); it != _cache.end(); ++it) {
139 Resource *resource = it->_value;
140 if (force || !resource->_isLocked) {
141 delete resource;
142 _cache.erase(it);
143 }
144 }
145 }
146
147protected:
148
149 struct Resource {
150 ResourceClass *_obj;
151 bool _isLocked;
152 Resource(ResourceClass *obj) : _obj(obj), _isLocked(false) {}
153 ~Resource() { delete _obj; }
154 };
155
157 typedef typename CacheMap::iterator CacheMapIterator;
158
159 DatManager *_dat;
160 CacheMap _cache;
161
162 Resource *find(int resourceId) {
163 CacheMapIterator it = _cache.find(resourceId);
164 if (it != _cache.end())
165 return it->_value;
166 return nullptr;
167 }
168
169 ResourceClass *load(int resourceId) {
170 if (_dat->getResourceType(resourceId) != ResourceType)
171 error("ResourceCache::load() Wrong resource type: Expected %d, got %d", ResourceType, _dat->getResourceType(resourceId));
172
173 byte *resourceData = _dat->loadResource(resourceId);
174 uint32 resourceSize = _dat->getResourceSize(resourceId);
175 ResourceClass *obj = new ResourceClass(resourceData, resourceSize);
176 if (FreeAfterLoad)
177 delete[] resourceData;
178 return obj;
179 }
180
181};
182
183typedef ResourceCacheTemplate<SpriteResource, kResTypeSprite, false> SpriteCache;
184typedef ResourceCacheTemplate<SoundResource, kResTypeSound, false> SoundCache;
185typedef ResourceCacheTemplate<SequenceResource, kResTypeSequence, true> SequenceCache;
186
187} // End of namespace Gnap
188
189#endif // GNAP_RESOURCE_H
Definition: memstream.h:43
Definition: datarchive.h:60
Definition: resource.h:109
Definition: resource.h:67
Definition: resource.h:99
Definition: resource.h:84
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
void erase(iterator entry)
Definition: hashmap.h:714
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: rect.h:536
Definition: resource.h:149
Definition: resource.h:56
Definition: resource.h:47