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 
38 namespace Gnap {
39 
40 enum {
41  kResTypeSprite = 0,
42  kResTypeBitmap = 1,
43  kResTypeSound = 2,
44  kResTypeSequence = 3
45 };
46 
47 struct SequenceFrame {
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 
68 public:
69  SequenceResource(byte *data, uint32 size);
71 public:
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 
85 public:
86  SpriteResource(byte *data, uint32 size);
87  ~SpriteResource();
88 public:
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 
100 public:
101  SoundResource(byte *data, uint32 size);
102  ~SoundResource();
103 public:
104  byte *_data;
105  uint32 _size;
106 };
107 
108 template <class ResourceClass, int ResourceType, bool FreeAfterLoad>
110 public:
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 
147 protected:
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 
186 
187 } // End of namespace Gnap
188 
189 #endif // GNAP_RESOURCE_H
Definition: resource.h:149
In find(In first, In last, const T &v)
Definition: algorithm.h:168
Definition: rect.h:144
Definition: resource.h:56
Definition: resource.h:47
void debug(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: datarchive.h:60
Definition: resource.h:84
Definition: resource.h:67
Definition: memstream.h:43
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: character.h:25
Definition: resource.h:109
Definition: resource.h:99