1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef GNAP_RESOURCE_H
#define GNAP_RESOURCE_H
#include "common/array.h"
#include "common/events.h"
#include "common/file.h"
#include "common/memstream.h"
#include "common/str.h"
#include "common/stream.h"
#include "common/substream.h"
#include "common/system.h"
#include "graphics/surface.h"
#include "gnap/datarchive.h"
namespace Gnap {
enum {
kResTypeSprite = 0,
kResTypeBitmap = 1,
kResTypeSound = 2,
kResTypeSequence = 3
};
struct SequenceFrame {
int16 _duration;
bool _isScaled;
Common::Rect _rect;
int32 _spriteId;
int32 _soundId;
int32 _unusedVal;
void loadFromStream(Common::MemoryReadStream &stream);
};
struct SequenceAnimation {
int16 _unusedVal1;
int16 _unusedVal2;
int32 _additionalDelay;
int16 _framesCount;
int16 _maxTotalDuration;
SequenceFrame *frames;
SequenceAnimation() : frames(nullptr) {}
~SequenceAnimation() { delete[] frames; }
void loadFromStream(Common::MemoryReadStream &stream);
};
class SequenceResource {
public:
SequenceResource(int resourceId, byte *data, uint32 size);
~SequenceResource();
public:
uint32 _unusedVal1;
int32 _sequenceId;
int32 _defaultId;
int32 _sequenceId2;
uint32 _defaultId2;
uint32 _flags;
int32 _totalDuration;
int16 _xOffs;
int16 _yOffs;
int32 _animationsCount;
SequenceAnimation *_animations;
};
class SpriteResource {
public:
SpriteResource(int resourceId, byte *data, uint32 size);
~SpriteResource();
public:
byte *_data;
byte *_pixels;
uint32 *_palette;
int16 _width, _height;
uint16 _unknownVal1;
uint16 _unknownVal2;
bool _transparent;
uint16 _colorsCount;
};
class SoundResource {
public:
SoundResource(int resourceId, byte *data, uint32 size);
~SoundResource();
public:
byte *_data;
uint32 _size;
};
template <class ResourceClass, int ResourceType, bool FreeAfterLoad>
class ResourceCacheTemplate {
public:
ResourceCacheTemplate(DatManager *dat) : _dat(dat) {
}
~ResourceCacheTemplate() {
}
ResourceClass *get(int resourceId) {
Resource *resource = find(resourceId);
if (!resource) {
debug(1, "Loading resource type %d with ID %08X from disk", ResourceType, resourceId);
resource = new Resource(load(resourceId));
_cache[resourceId] = resource;
} else {
debug(1, "Resource type %d with ID %08X was in cache", ResourceType, resourceId);
}
resource->_isLocked = true;
return resource->_obj;
}
void release(int resourceId) {
Resource *resource = find(resourceId);
if (resource)
resource->_isLocked = false;
}
void purge(bool force = false) {
debug("Count before purge: %d", _cache.size());
for (CacheMapIterator it = _cache.begin(); it != _cache.end(); ++it) {
Resource *resource = it->_value;
if (force || !resource->_isLocked) {
delete resource;
_cache.erase(it);
}
}
debug("Count after purge: %d", _cache.size());
}
protected:
struct Resource {
ResourceClass *_obj;
bool _isLocked;
Resource(ResourceClass *obj) : _obj(obj), _isLocked(false) {}
~Resource() { delete _obj; }
};
typedef Common::HashMap<int, Resource*> CacheMap;
typedef typename CacheMap::iterator CacheMapIterator;
DatManager *_dat;
CacheMap _cache;
Resource *find(int resourceId) {
CacheMapIterator it = _cache.find(resourceId);
if (it != _cache.end())
return it->_value;
return nullptr;
}
ResourceClass *load(int resourceId) {
if (_dat->getResourceType(resourceId) != ResourceType) {
error("ResourceCache::load() Wrong resource type: Expected %d, got %d", ResourceType, _dat->getResourceType(resourceId));
return nullptr;
}
byte *resourceData = _dat->loadResource(resourceId);
uint32 resourceSize = _dat->getResourceSize(resourceId);
ResourceClass *obj = new ResourceClass(resourceId, resourceData, resourceSize);
if (FreeAfterLoad)
delete[] resourceData;
return obj;
}
};
typedef ResourceCacheTemplate<SpriteResource, kResTypeSprite, false> SpriteCache;
typedef ResourceCacheTemplate<SoundResource, kResTypeSound, false> SoundCache;
typedef ResourceCacheTemplate<SequenceResource, kResTypeSequence, true> SequenceCache;
} // End of namespace Gnap
#endif // GNAP_RESOURCE_H
|