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 MADE_RESOURCE_H
23#define MADE_RESOURCE_H
24
25#include "made/sound.h"
26
27#include "common/endian.h"
28#include "common/array.h"
29#include "common/hashmap.h"
30
31namespace Common {
32class File;
33}
34
35namespace Audio {
36class AudioStream;
37}
38
39namespace Graphics {
40struct Surface;
41}
42
43namespace Made {
44
49#ifndef __DS__
50const int kMaxResourceCacheSize = 1000 * 1024;
51#else
52const int kMaxResourceCacheSize = 400 * 1024;
53#endif
54
55
56enum ResourceType {
57 kResARCH = MKTAG('A','R','C','H'),
58 kResFREE = MKTAG('F','R','E','E'),
59 kResOMNI = MKTAG('O','M','N','I'),
60 kResFLEX = MKTAG('F','L','E','X'),
61 kResSNDS = MKTAG('S','N','D','S'),
62 kResANIM = MKTAG('A','N','I','M'),
63 kResMENU = MKTAG('M','E','N','U'),
64 kResFONT = MKTAG('F','O','N','T'),
65 kResXMID = MKTAG('X','M','I','D'),
66 kResMIDI = MKTAG('M','I','D','I')
67};
68
69struct ResourceSlot;
70
71class Resource {
72public:
73 ResourceSlot *_slot;
74 virtual ~Resource();
75};
76
77class PictureResource : public Resource {
78public:
80 ~PictureResource() override;
81 void load(byte *source, int size);
82 Graphics::Surface *getPicture() const { return _picture; }
83 byte *getPalette() const { return _picturePalette; }
84 bool hasPalette() const { return _hasPalette; }
85 int getPaletteColorCount() const { return _paletteColorCount; }
86protected:
87 Graphics::Surface *_picture;
88 byte *_picturePalette;
89 int _paletteColorCount;
90 bool _hasPalette;
91 void loadRaw(byte *source, int size);
92 void loadChunked(byte *source, int size);
93};
94
96public:
98 ~AnimationResource() override;
99 void load(byte *source, int size);
100 int getCount() const { return _frames.size(); }
101 Graphics::Surface *getFrame(int index) const {
102 if ((uint)index < _frames.size()) {
103 return _frames[index];
104 } else {
105 warning("getFrame: Tried to obtain invalid frame %i, array has %i frames", index, _frames.size());
106 return _frames[_frames.size() - 1];
107 }
108 }
109 uint16 getFlags() const { return _flags; }
110 int16 getWidth() const { return _width; }
111 int16 getHeight() const { return _height; }
112protected:
114 uint16 _flags;
115 int16 _width, _height;
116};
117
118class SoundResource : public Resource {
119public:
121 ~SoundResource() override;
122 virtual void load(byte *source, int size);
123 Audio::AudioStream *getAudioStream(int soundRate, bool loop = false);
124 SoundEnergyArray *getSoundEnergyArray() const { return _soundEnergyArray; }
125 int getSoundSize() const { return _soundSize; }
126protected:
127 byte *_soundData;
128 int _soundSize;
129 SoundEnergyArray *_soundEnergyArray;
130};
131
133public:
134 SoundResourceV1() {}
135 ~SoundResourceV1() override {}
136 void load(byte *source, int size) override;
137};
138
139class MenuResource : public Resource {
140public:
141 MenuResource();
142 ~MenuResource() override;
143 void load(byte *source, int size);
144 int getCount() const { return _strings.size(); }
145 const char *getString(uint index) const;
146protected:
148};
149
150class FontResource : public Resource {
151public:
152 FontResource();
153 ~FontResource() override;
154 void load(byte *source, int size);
155 int getHeight() const;
156 int getCharWidth(uint c) const;
157 int getTextWidth(const char *text);
158 byte *getChar(uint c) const;
159protected:
160 byte *_data;
161 int _size;
162 byte *getCharData(uint c) const;
163};
164
165class GenericResource : public Resource {
166public:
168 ~GenericResource() override;
169 void load(byte *source, int size);
170 byte *getData() const { return _data; }
171 int getSize() const { return _size; }
172protected:
173 byte *_data;
174 int _size;
175};
176
178 uint32 offs;
179 uint32 size;
180 Resource *res;
181 int refCount;
182 ResourceSlot() : offs(0), size(0), res(NULL), refCount(0) {
183 }
184 ResourceSlot(uint32 roffs, uint32 rsize) : offs(roffs), size(rsize), res(NULL), refCount(0) {
185 }
186};
187
189public:
192
193 void open(const char *filename);
194
195 void openPicsBlock(bool altFormat);
196 void openMusicBlock();
197 void openSndsBlock();
198
199 PictureResource *getPicture(int index);
200 AnimationResource *getAnimation(int index);
201 SoundResource *getSound(int index);
202 MenuResource *getMenu(int index);
203 FontResource *getFont(int index);
204 GenericResource *getXmidi(int index);
205 GenericResource *getMidi(int index);
206
207 void freeResource(Resource *resource);
208
209protected:
210
211 Common::File *_fd;
212 Common::File *_fdPics, *_fdSounds, *_fdMusic; // V1
213 bool _isV1;
214
217 void openResourceBlock(const char *filename, Common::File *blockFile, uint32 resType, bool altFormat);
218
219 ResMap _resSlots;
220 int _cacheCount;
221 int _cacheDataSize;
222
223 void loadIndex(ResourceSlots *slots);
224
225 template<class T>
226 T *createResource(uint32 resType, int index) {
227 ResourceSlot *slot = getResourceSlot(resType, index);
228 if (!slot)
229 return NULL;
230 T *res = (T *)getResourceFromCache(slot);
231 if (!res) {
232 byte *buffer;
233 uint32 size;
234
235 // Read from the correct file for V1 games
236 if (_isV1) {
237 switch (resType) {
238 case kResSNDS:
239 _fd = _fdSounds;
240 break;
241 case kResMIDI:
242 _fd = _fdMusic;
243 break;
244 default:
245 _fd = _fdPics;
246 break;
247 }
248 }
249
250 if (loadResource(slot, buffer, size)) {
251 res = new T();
252 res->_slot = slot;
253 res->load(buffer, size);
254 addResourceToCache(slot, res);
255 delete[] buffer;
256 }
257 }
258 return res;
259 }
260
261 bool loadResource(ResourceSlot *slot, byte *&buffer, uint32 &size);
262 ResourceSlot *getResourceSlot(uint32 resType, uint index);
263 Resource *getResourceFromCache(ResourceSlot *slot);
264 void addResourceToCache(ResourceSlot *slot, Resource *res);
265 void tossResourceFromCache(ResourceSlot *slot);
266 void purgeCache();
267
268};
269
270} // End of namespace Made
271
272#endif /* MADE_H */
Definition: audiostream.h:50
size_type size() const
Definition: array.h:316
Definition: file.h:47
Definition: resource.h:95
Definition: resource.h:150
Definition: resource.h:165
Definition: resource.h:139
Definition: resource.h:77
Definition: resource.h:71
Definition: resource.h:188
Definition: resource.h:118
Definition: resource.h:132
#define MKTAG(a0, a1, a2, a3)
Definition: endian.h:188
void warning(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: system.h:39
Definition: algorithm.h:29
Definition: formatinfo.h:28
Definition: console.h:27
const int kMaxResourceCacheSize
Definition: resource.h:50
Definition: surface.h:67
Definition: resource.h:177