aboutsummaryrefslogtreecommitdiff
path: root/engines/mads/assets.cpp
blob: 0bbf6177eb08bf359b077e832ef68579f6036739 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* 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.
 *
 */

#include "common/scummsys.h"
#include "mads/mads.h"
#include "mads/assets.h"
#include "mads/compression.h"
#include "mads/events.h"
#include "mads/palette.h"

namespace MADS {

SpriteAsset::SpriteAsset(MADSEngine *vm, const Common::String &resourceName, int flags) : _vm(vm) {
	Common::String resName = resourceName;
	if (!resName.hasSuffix(".SS") && !resName.hasSuffix(".ss"))
		resName += ".SS";
	_srcSize = 0;

	File file(resName);
	load(&file, flags);

	file.close();
}

SpriteAsset::SpriteAsset(MADSEngine *vm, Common::SeekableReadStream *stream, int flags) : _vm(vm) {
	_srcSize = 0;

	load(stream, flags);
}

SpriteAsset::~SpriteAsset() {
	if (_usageIndex)
		_vm->_palette->_paletteUsage.resetPalFlags(_usageIndex);

	for (uint i = 0; i < _frames.size(); ++i)
		delete _frames[i]._frame;

	delete _charInfo;
}

void SpriteAsset::load(Common::SeekableReadStream *stream, int flags) {
	int curFrame = 0;
	uint32 frameOffset = 0;
	MadsPack sprite(stream);
	_frameRate = 0;
	_pixelSpeed = 0;
	_maxWidth = 0;
	_maxHeight = 0;
	_usageIndex = -1;

	Common::SeekableReadStream *spriteStream = sprite.getItemStream(0);
	_mode = spriteStream->readByte();
	spriteStream->skip(1);
	int type1 = spriteStream->readUint16LE();
	int type2 = spriteStream->readUint16LE();
	_isBackground = (type1 != 0) && (type2 < 4);
	spriteStream->skip(32);
	_frameCount = spriteStream->readUint16LE();

	if ((flags & SPRITE_SET_CHAR_INFO) == 0)
		_charInfo = nullptr;
	else
		_charInfo = new SpriteSetCharInfo(spriteStream);

	delete spriteStream;

	// Get the palette data
	Common::SeekableReadStream *palStream = sprite.getItemStream(2);
	Common::Array<RGB6> palette;

	int numColors = palStream->readUint16LE();
	assert(numColors <= 252);
	_colorCount = numColors;

	// Load in the palette
	palette.resize(numColors);
	for (int i = 0; i < numColors; ++i)
		palette[i].load(palStream);
	delete palStream;

	// Process the palette data
	if (flags & 9) {
		_usageIndex = 0;

		if (flags & 8) {
			int newPalCtr = 0;

			for (uint i = 0; i < palette.size(); ++i) {
				RGB6 &rgb = palette[i];

				// Scan for existing rgb at beginning of the main palette
				bool found = false;
				for (int pIndex = 0; pIndex < 4 && !found; ++pIndex) {
					byte *palP = &_vm->_palette->_mainPalette[pIndex * 3];
					if (palP[0] == rgb.r && palP[1] == rgb.g && palP[2] == rgb.b) {
						rgb._palIndex = pIndex;
						found = true;
					}
				}

				if (!found) {
					// Existing palette entry not found, so need to add it in
					int palIndex = (0xF7F607 >> (8 * newPalCtr)) & 0xff;
					byte *palP = &_vm->_palette->_mainPalette[palIndex * 3];
					palP[0] = rgb.r;
					palP[1] = rgb.g;
					palP[2] = rgb.b;
					rgb._palIndex = palIndex;

					newPalCtr = MIN(newPalCtr + 1, 2);
				}
			}
		}
	} else {
		_usageIndex = _vm->_palette->_paletteUsage.process(palette, flags);
		assert(_usageIndex >= 0);
	}

	spriteStream = sprite.getItemStream(1);
	Common::SeekableReadStream *spriteDataStream = sprite.getItemStream(3);
	SpriteAssetFrame frame;
	Common::Array<int> frameSizes;
	for (curFrame = 0; curFrame < _frameCount; curFrame++) {
		frame._stream = 0;
		frame._comp = 0;
		frameOffset = spriteStream->readUint32LE();
		_frameOffsets.push_back(frameOffset);
		uint32 frameSize = spriteStream->readUint32LE();
		frameSizes.push_back(frameSize);

		frame._bounds.left = spriteStream->readSint16LE();
		frame._bounds.top  = spriteStream->readSint16LE();
		frame._bounds.setWidth(spriteStream->readUint16LE());
		frame._bounds.setHeight(spriteStream->readUint16LE());

		if (curFrame == 0)
			debugC(1, kDebugGraphics, "%i frames, x = %i, y = %i, w = %i, h = %i\n",
			_frameCount, frame._bounds.left, frame._bounds.top,
			frame._bounds.width(), frame._bounds.height());

		if (_mode == 0) {
			// Create a frame and decompress the raw pixel data
			uint32 currPos = (uint32)spriteDataStream->pos();
			frame._frame = new MSprite(spriteDataStream, palette, frame._bounds);
			assert((uint32)spriteDataStream->pos() == (currPos + frameSize));
		}

		_frames.push_back(frame);
	}

	if (_mode != 0) {
		// Handle decompressing Fab encoded data
		for (curFrame = 0; curFrame < _frameCount; curFrame++) {
			FabDecompressor fab;

			int srcSize = (curFrame == (_frameCount - 1)) ? spriteDataStream->size() - _frameOffsets[curFrame] :
				_frameOffsets[curFrame + 1] - _frameOffsets[curFrame];
			byte *srcData = new byte[srcSize];
			assert(srcData);
			spriteDataStream->read(srcData, srcSize);

			byte *destData = new byte[frameSizes[curFrame]];
			assert(destData);

			fab.decompress(srcData, srcSize, destData, frameSizes[curFrame]);

			// Load the frames
			Common::MemoryReadStream *rs = new Common::MemoryReadStream(destData, frameSizes[curFrame]);
			_frames[curFrame]._frame = new MSprite(rs, palette, _frames[curFrame]._bounds);
			delete rs;

			delete[] srcData;
			delete[] destData;
		}
	}

	delete spriteStream;
	delete spriteDataStream;
}

MSprite *SpriteAsset::getFrame(int frameIndex) {
	if ((uint)frameIndex < _frames.size()) {
		return _frames[frameIndex]._frame;
	} else {
		debugC(kDebugGraphics, "SpriteAsset::getFrame: Invalid frame %d, out of %d", frameIndex, _frames.size());
		return _frames[_frames.size() - 1]._frame;
	}
}

/*------------------------------------------------------------------------*/

SpriteSetCharInfo::SpriteSetCharInfo(Common::SeekableReadStream *s) {
	_totalFrames = s->readByte();
	s->skip(1);
	_numEntries = s->readUint16LE();

	for (int i = 0; i < 16; ++i)
		_startFrames[i] = s->readUint16LE();
	for (int i = 0; i < 16; ++i)
		_stopFrames[i] = s->readUint16LE();
	for (int i = 0; i < 16; ++i)
		_ticksList[i] = s->readUint16LE();

	_velocity = s->readUint16LE();
	_ticksAmount = s->readByte();
	_centerOfGravity = s->readByte();
}

} // End of namespace MADS