aboutsummaryrefslogtreecommitdiff
path: root/engines/m4/animation.cpp
blob: 4b063241fcb9b58f188ef1b3838bab2e917c8211 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "m4/assets.h"
#include "m4/animation.h"
#include "m4/compression.h"

namespace M4 {

// TODO: this code needs cleanup

Animation::Animation(M4Engine *vm) {
	_vm = vm;
	_playing = false;
}

void Animation::loadFullScreen(const char *filename) {
	_vm->_palette->deleteAllRanges();
	load(filename);
}

void Animation::load(const char *filename) {
	MadsPack anim(filename, _vm);
	char buffer[20];

	// Chunk 1: header
	// header
	// TODO: there are some unknown fields here, plus we don't read
	// the entire chunk
	Common::SeekableReadStream *animStream = anim.getItemStream(0);
	Common::SeekableReadStream *spriteSeriesStream;
	//printf("Chunk 0, size %i\n", animStream->size());
	_seriesCount = animStream->readUint16LE();
	_frameCount = animStream->readUint16LE();
	_frameEntryCount = animStream->readUint16LE();

	// Unknown
	for (int i = 0; i < 43; i++)
		animStream->readByte();

	_spriteSeriesNames = new Common::String[_seriesCount];
	printf("%i sprite series\n", _seriesCount);

	// TODO: for now, we only load the first sprite series
	if (_seriesCount > 1)
		printf("TODO: Anim has %i sprite series, for now, we only load the first one\n", _seriesCount);
	_seriesCount = 1;		// TODO

	for (int i = 0; i < _seriesCount; i++) {
		animStream->read(buffer, 13);
		_spriteSeriesNames[i] = Common::String(buffer);
		//printf("%03d: %s\n", i, _spriteSeriesNames[i].c_str());

		spriteSeriesStream = _vm->res()->get(_spriteSeriesNames[i].c_str());
		_spriteSeries = new SpriteAsset(_vm, spriteSeriesStream,
										spriteSeriesStream->size(), _spriteSeriesNames[i].c_str());
		_vm->res()->toss(_spriteSeriesNames[i].c_str());

		// Adjust the palette of the sprites in the sprite series
		// so that they can be displayed on screen correctly
		RGBList *palData = new RGBList(_spriteSeries->getColorCount(), _spriteSeries->getPalette(), true);
		_vm->_palette->addRange(palData);

		for (int k = 0; k < _spriteSeries->getCount(); k++) {
			M4Sprite *spr = _spriteSeries->getFrame(k);
			spr->translate(palData);		// sprite pixel translation
		}
	}

	//printf("End pos: %i\n", animStream->pos());

	delete animStream;

	// ------------------

	// Chunk 2: anim info
	AnimationFrame frame;
	animStream = anim.getItemStream(1);
	//printf("Chunk 1, size %i\n", animStream->size());

	_frameEntries = new AnimationFrame[_frameEntryCount];

	for (int i = 0; i < _frameEntryCount; i++) {

		frame.animFrameIndex = animStream->readUint16LE();
		frame.u = animStream->readByte();
		frame.seriesIndex = animStream->readByte();
		frame.seriesFrameIndex = animStream->readUint16LE();
		frame.x = animStream->readUint16LE();
		frame.y = animStream->readUint16LE();
		frame.v = animStream->readByte();
		frame.w = animStream->readByte();

		_frameEntries[i] = frame;

		/*
		printf(
		"animFrameIndex = %4d, "
		"u = %3d, "
		"seriesIndex = %3d, "
		"seriesFrameIndex = %6d, "
		"x = %3d, "
		"y = %3d, "
		"v = %3d, "
		"w = %3d\n",

		frame.animFrameIndex,
		frame.u,
		frame.seriesIndex,
		frame.seriesFrameIndex,
		frame.x,
		frame.y,
		frame.v,
		frame.w
		);
		*/
	}
	//printf("End pos: %i\n", animStream->pos());

	delete animStream;

	// Chunk 3: unknown (seems to be sound data?)
	// TODO
}

Animation::~Animation() {
	//delete[] _spriteSeriesNames;
	//delete[] _spriteSeries;
	//delete[] _frameEntries;
}

void Animation::start() {
	_curFrame = 0;
	_curFrameEntry = 0;
	//for (int i = 0; i < _seriesCount; i++) {
		//_spriteSeries[i] = new SpriteSeries((char*)_spriteSeriesNames[i].c_str());
	//}
	_playing = true;
	updateAnim();
}

bool Animation::updateAnim() {
	if (!_playing)
		return true;

	// Get the scene background surface
	M4Surface *bg = _vm->_scene->getBackgroundSurface();

	while (_frameEntries[_curFrameEntry].animFrameIndex == _curFrame) {
		AnimationFrame *frame = &_frameEntries[_curFrameEntry];
		int seriesFrameIndex = (frame->seriesFrameIndex & 0x7FFF) - 1;

		// Write the sprite onto the screen
		M4Sprite *spr = _spriteSeries->getFrame(seriesFrameIndex);

		// FIXME: We assume that the transparent color is the color of the top left pixel
		byte *transparentColor = (byte *)spr->pixels;

		// FIXME: correct x, y
		spr->copyTo(bg, frame->x, frame->y, (int)*transparentColor);

		// HACK: wait a bit
		g_system->delayMillis(100);

		//printf("_curFrameEntry = %d\n", _curFrameEntry);
		_curFrameEntry++;
	}

	//printf("_curFrame = %d\n", _curFrame);

	_curFrame++;
	if (_curFrame >= _frameCount)		// anim done
		stop();

	return _curFrame >= _frameCount;
}

void Animation::stop() {
	_playing = false;

	for (int i = 0; i < _seriesCount; i++) {
		// TODO: cleanup
		//delete _spriteSeries[i];
		//_spriteSeries[i] = NULL;
	}
}

} // End of namespace M4