22#ifndef GNAP_RESOURCE_H
23#define GNAP_RESOURCE_H
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"
34#include "graphics/surface.h"
36#include "gnap/datarchive.h"
57 int32 _additionalDelay;
59 int16 _maxTotalDuration;
62 SequenceAnimation() : frames(
nullptr), _additionalDelay(0), _framesCount(0), _maxTotalDuration(0) {}
80 int32 _animationsCount;
92 int16 _width, _height;
108template <
class ResourceClass,
int ResourceType,
bool FreeAfterLoad>
118 ResourceClass *get(
int resourceId) {
119 Resource *resource = find(resourceId);
121 debug(9,
"Loading resource type %d with ID %08X from disk", ResourceType, resourceId);
122 resource =
new Resource(load(resourceId));
123 _cache[resourceId] = resource;
125 debug(9,
"Resource type %d with ID %08X was in cache", ResourceType, resourceId);
127 resource->_isLocked =
true;
128 return resource->_obj;
131 void release(
int resourceId) {
132 Resource *resource = find(resourceId);
134 resource->_isLocked =
false;
137 void purge(
bool force =
false) {
138 for (CacheMapIterator it = _cache.begin(); it != _cache.end(); ++it) {
140 if (force || !resource->_isLocked) {
152 Resource(ResourceClass *obj) : _obj(obj), _isLocked(
false) {}
157 typedef typename CacheMap::iterator CacheMapIterator;
163 CacheMapIterator it = _cache.find(resourceId);
164 if (it != _cache.end())
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));
173 byte *resourceData = _dat->loadResource(resourceId);
174 uint32 resourceSize = _dat->getResourceSize(resourceId);
175 ResourceClass *obj =
new ResourceClass(resourceData, resourceSize);
177 delete[] resourceData;
183typedef ResourceCacheTemplate<SpriteResource, kResTypeSprite, false> SpriteCache;
184typedef ResourceCacheTemplate<SoundResource, kResTypeSound, false> SoundCache;
185typedef ResourceCacheTemplate<SequenceResource, kResTypeSequence, true> SequenceCache;
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: resource.h:149
Definition: resource.h:56
Definition: resource.h:47