aboutsummaryrefslogtreecommitdiff
path: root/engines/director/score.cpp
blob: b95a50e352e2d8999d79a108c0fd5e0d080bf4d8 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/* 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 "director/score.h"
#include "common/stream.h"
#include "common/debug.h"
#include "common/file.h"
#include "common/config-manager.h"

#include "common/system.h"
#include "director/dib.h"
#include "director/resource.h"
#include "lingo/lingo.h"

#include "graphics/palette.h"
#include "common/events.h"
#include "engines/util.h"
#include "graphics/managed_surface.h"

namespace Director {

Score::Score(Archive &movie, Lingo &lingo) {

	_surface = new Graphics::ManagedSurface;
	_movieArchive = &movie;
	_lingo = &lingo;

	_lingo->processEvent(kEventPrepareMovie, 0);

	assert(_movieArchive->hasResource(MKTAG('V','W','S','C'), 1024));
	assert(_movieArchive->hasResource(MKTAG('V','W','C','F'), 1024));
	assert(_movieArchive->hasResource(MKTAG('V','W','C','R'), 1024));

	loadFrames(*_movieArchive->getResource(MKTAG('V','W','S','C'), 1024));
	loadConfig(*_movieArchive->getResource(MKTAG('V','W','C','F'), 1024));
	loadCastData(*_movieArchive->getResource(MKTAG('V','W','C','R'), 1024));

	if (_movieArchive->hasResource(MKTAG('M','C','N','M'), 0)) {
		_macName = _movieArchive->getName(MKTAG('M','C','N','M'), 0).c_str();
	}

	if (_movieArchive->hasResource(MKTAG('V','W','L','B'), 1024)) {
		loadLabels(*_movieArchive->getResource(MKTAG('V','W','L','B'), 1024));
	}

	if (_movieArchive->hasResource(MKTAG('V','W','A','C'), 1024)) {
		loadActions(*_movieArchive->getResource(MKTAG('V','W','A','C'), 1024));
	}

	if (_movieArchive->hasResource(MKTAG('V','W','F','I'), 1024)) {
		loadFileInfo(*_movieArchive->getResource(MKTAG('V','W','F','I'), 1024));
	}

	if (_movieArchive->hasResource(MKTAG('V','W','F','M'), 1024)) {
		loadFontMap(*_movieArchive->getResource(MKTAG('V','W','F','M'), 1024));
	}

	Common::Array<uint16> vwci = _movieArchive->getResourceIDList(MKTAG('V','W','C','I'));
	if (vwci.size() > 0) {
		Common::Array<uint16>::iterator iterator;
		for (iterator = vwci.begin(); iterator != vwci.end(); ++iterator)
			loadCastInfo(*_movieArchive->getResource(MKTAG('V','W','C','I'), *iterator));
	}

	DIBDecoder palette;
	Common::Array<uint16> clutList = _movieArchive->getResourceIDList(MKTAG('C','L','U','T'));

	if (clutList.size() > 1)
		error("More than one palette was found");
	if (clutList.size() == 0)
		error("CLUT not found");

	Common::SeekableReadStream *pal = _movieArchive->getResource(MKTAG('C', 'L', 'U', 'T'), clutList[0]);
	palette.loadPalette(*pal);
	g_system->getPaletteManager()->setPalette(palette.getPalette(), 0, 255);
}

void Score::loadFrames(Common::SeekableReadStream &stream) {
	uint32 size = stream.readUint32BE();
	size -= 4;
	uint16 channelSize;
	uint16 channelOffset;

	Frame *initial = new Frame();
	_frames.push_back(initial);

	while (size != 0) {
		uint16 frameSize = stream.readUint16BE();
		size -= frameSize;
		frameSize -= 2;
		Frame *frame = new Frame(*_frames.back());
		while (frameSize != 0) {
			channelSize = stream.readByte() * 2;
			channelOffset = stream.readByte() * 2;
			frame->readChannel(stream, channelOffset, channelSize);
			frameSize -= channelSize + 2;
		}
		_frames.push_back(frame);
	}
	//remove initial frame
	_frames.remove_at(0);
}

void Score::loadConfig(Common::SeekableReadStream &stream) {
	/*uint16 unk1 = */ stream.readUint16BE();
	/*ver1 = */ stream.readUint16BE();
	_movieRect = Score::readRect(stream);

	_castArrayStart = stream.readUint16BE();
	_castArrayEnd = stream.readUint16BE();
	_currentFrameRate = stream.readByte();
	stream.skip(9);
	_stageColor = stream.readUint16BE();
}

void Score::readVersion(uint32 rid) {
	_versionMinor = rid & 0xffff;
	_versionMajor = rid >> 16;
	debug("%d.%d", _versionMajor, _versionMinor);
}

void Score::loadCastData(Common::SeekableReadStream &stream) {
	for (uint16 id = _castArrayStart; id <= _castArrayEnd; id++) {
		byte size = stream.readByte();
		if (size == 0)
			continue;
		uint8 castType = stream.readByte();
		switch (castType) {
		case kCastBitmap:
			_casts[id] = new BitmapCast(stream);
			_casts[id]->type = kCastBitmap;
			break;
		case kCastText:
			_casts[id] = new TextCast(stream);
			_casts[id]->type = kCastText;
			break;
		case kCastShape:
			_casts[id] = new ShapeCast(stream);
			_casts[id]->type = kCastShape;
			break;
		case kCastButton:
			_casts[id] = new ButtonCast(stream);
			_casts[id]->type = kCastButton;
			break;
		default:
			warning("Unhandled cast type: %d", castType);
			stream.skip(size - 1);
			break;
		}
	}
	//Set cast pointers to sprites
	for (uint16 i = 0; i < _frames.size(); i++) {
		for (uint16 j = 0; j < _frames[i]->_sprites.size(); j++) {
			byte castId = _frames[i]->_sprites[j]->_castId;
			if (_casts.contains(castId))
				_frames[i]->_sprites[j]->_cast = _casts.find(castId)->_value;
		}
	}
}

void Score::loadLabels(Common::SeekableReadStream &stream) {
	uint16 count = stream.readUint16BE() + 1;
	uint16 offset = count * 4 + 2;

	uint16 frame = stream.readUint16BE();
	uint16 stringPos = stream.readUint16BE() + offset;

	for (uint16 i = 0; i < count; i++) {

		uint16 nextFrame = stream.readUint16BE();
		uint16 nextStringPos = stream.readUint16BE() + offset;
		uint16 streamPos = stream.pos();

		stream.seek(stringPos);

		for (uint16 j = stringPos; j < nextStringPos; j++) {
			_labels[frame] += stream.readByte();
		}

		stream.seek(streamPos);

		frame = nextFrame;
		stringPos = nextStringPos;
	}

	Common::HashMap<uint16, Common::String>::iterator j;
	for (j = _labels.begin(); j != _labels.end(); ++j) {
		debug("Frame %d, Label %s", j->_key, j->_value.c_str());
	}
}

void Score::loadActions(Common::SeekableReadStream &stream) {

	uint16 count = stream.readUint16BE() + 1;
	uint16 offset = count * 4 + 2;

	byte id = stream.readByte();
	/*byte subId = */ stream.readByte(); //I couldn't find how it used in continuity (except print). Frame actionId = 1 byte.
	uint16 stringPos = stream.readUint16BE() + offset;

	for (uint16 i = 0; i < count; i++) {

		uint16 nextId = stream.readByte();
		/*byte subId = */ stream.readByte();
		uint16 nextStringPos = stream.readUint16BE() + offset;
		uint16 streamPos = stream.pos();

		stream.seek(stringPos);

		for (uint16 j = stringPos; j < nextStringPos; j++) {
			_actions[id] += stream.readByte();
		}

		stream.seek(streamPos);

		id = nextId;
		stringPos = nextStringPos;
		if (stringPos == stream.size())
			break;
	}

	Common::HashMap<uint16, Common::String>::iterator j;

	if (!ConfMan.getBool("dump_scripts"))
		return;

	for (j = _actions.begin(); j != _actions.end(); ++j) {
		dumpScript(j->_key, kFrameScript, j->_value);
	}
}

void Score::dumpScript(uint16 id, scriptType type, Common::String script) {
	Common::DumpFile out;
	Common::String typeName;
	char buf[256];

	switch (type) {
	case kFrameScript:
		typeName = "frame";
		break;
	case kMovieScript:
		typeName = "movie";
		break;
	case kSpriteScript:
		typeName = "sprite";
		break;
	}

	sprintf(buf, "./dumps/%s-%s-%d.txt", _macName.c_str(), typeName.c_str(), id);

	if (!out.open(buf)) {
		warning("Can not open dump file %s", buf);
		return;
	}
	out.writeString(script);

	out.flush();
	out.close();
}

void Score::loadCastInfo(Common::SeekableReadStream &stream) {
	uint32 entryType = 0;
	Common::Array<Common::String> castStrings = loadStrings(stream, entryType);
	CastInfo ci;
	ci.script = castStrings[0];
	ci.name = castStrings[1];
	ci.directory = castStrings[2];
	ci.fileName = castStrings[3];
	ci.type = castStrings[4];
	//TODO storage in array, and use this info
}

void Score::loadFileInfo(Common::SeekableReadStream &stream) {
	Common::Array<Common::String> fileInfoStrings = loadStrings(stream, _flags);
	_script = fileInfoStrings[0];
	_changedBy = fileInfoStrings[1];
	_createdBy = fileInfoStrings[2];
	_directory = fileInfoStrings[3];
}

Common::Array<Common::String> Score::loadStrings(Common::SeekableReadStream &stream, uint32 &entryType, bool hasHeader) {
	Common::Array<Common::String> strings;
	uint32 offset = 0;
	if (hasHeader) {
		offset = stream.readUint32BE();
		/*uint32 unk1 = */ stream.readUint32BE();
		/*uint32 unk2 = */ stream.readUint32BE();
		entryType = stream.readUint32BE();
		stream.seek(offset);
	}

	uint16 count = stream.readUint16BE();
	offset += (count + 1) * 4 + 2; //positions info + uint16 count
	uint32 startPos = stream.readUint32BE() + offset;
	for (uint16 i = 0; i < count; i++) {
		Common::String entryString;
		uint32 nextPos = stream.readUint32BE() + offset;
		uint32 streamPos = stream.pos();

		stream.seek(startPos);

		while (startPos != nextPos) {
			entryString += stream.readByte();
			++startPos;
		}

		strings.push_back(entryString);

		stream.seek(streamPos);
		startPos = nextPos;
	}
	return strings;
}

void Score::loadFontMap(Common::SeekableReadStream &stream) {
	uint16 count = stream.readUint16BE();
	uint32 offset = (count * 2) + 2;
	uint16 currentRawPosition = offset;

	for (uint16 i = 0; i < count; i++) {
		uint16 id = stream.readUint16BE();
		uint32 positionInfo = stream.pos();

		stream.seek(currentRawPosition);

		uint16 size = stream.readByte();
		Common::String font;

		for (uint16 k = 0; k < size; k++) {
			font += stream.readByte();
		}

		_fontMap[id] = font;
		currentRawPosition = stream.pos();
		stream.seek(positionInfo);
	}
}

BitmapCast::BitmapCast(Common::SeekableReadStream &stream) {
	/*byte flags = */ stream.readByte();
	uint16 someFlaggyThing = stream.readUint16BE();
	initialRect = Score::readRect(stream);
	boundingRect = Score::readRect(stream);
	regY = stream.readUint16BE();
	regX = stream.readUint16BE();
	if (someFlaggyThing & 0x8000) {
		/*uint16 unk1 =*/ stream.readUint16BE();
		/*uint16 unk2 =*/ stream.readUint16BE();
	}
}

TextCast::TextCast(Common::SeekableReadStream &stream) {
	/*byte flags =*/ stream.readByte();
	borderSize = stream.readByte();
	gutterSize = stream.readByte();
	boxShadow = stream.readByte();
	textType = stream.readByte();
	textAlign = stream.readUint16BE();
	stream.skip(6); //palinfo
	/*uint32 unk1 = */ stream.readUint32BE();
	initialRect = Score::readRect(stream);
	textShadow = stream.readByte();
	textFlags = stream.readByte();
	/*uint16 unk2 =*/ stream.readUint16BE();
}

ShapeCast::ShapeCast(Common::SeekableReadStream &stream) {
	/*byte flags = */ stream.readByte();
	/*unk1 = */ stream.readByte();
	shapeType = stream.readByte();
	initialRect = Score::readRect(stream);
	pattern = stream.readUint16BE();
	fgCol = stream.readByte();
	bgCol = stream.readByte();
	fillType = stream.readByte();
	lineThickness = stream.readByte();
	lineDirection = stream.readByte();
}

Common::Rect Score::readRect(Common::SeekableReadStream &stream) {
	Common::Rect *rect = new Common::Rect();
	rect->top = stream.readUint16BE();
	rect->left = stream.readUint16BE();
	rect->bottom = stream.readUint16BE();
	rect->right = stream.readUint16BE();
	return *rect;
}

void Score::startLoop() {
	initGraphics(_movieRect.width(), _movieRect.height(), true);
	_surface->create(_movieRect.width(), _movieRect.height());
	_currentFrame = 0;
	_stopPlay = false;
	_nextFrameTime = 0;

	_lingo->processEvent(kEventStartMovie, 0);

	while (!_stopPlay && _currentFrame < _frames.size() - 2) {
		update();
		processEvents();
		g_system->updateScreen();
		g_system->delayMillis(10);
	}
}

void Score::update() {
	if (g_system->getMillis() < _nextFrameTime)
		return;

	_surface->clear(_stageColor);

	//Exit from previous frame, and step to next
	_lingo->processEvent(kEventExitFrame, _currentFrame - 1);
	_lingo->processEvent(kEventStepFrame, _currentFrame);

	//Director step: send beginSprite event to any sprites whose span begin in the upcoming frame
	for (uint16 i = 0; i < CHANNEL_COUNT; i++) {
		if (_frames[_currentFrame]->_sprites[i]->_enabled)
			_lingo->processEvent(kEventBeginSprite, i);
	}

	//Director step: send prepareFrame event to all sprites and the script channel in upcoming frame
	_lingo->processEvent(kEventPrepareFrame, _currentFrame);
	_frames[_currentFrame]->prepareFrame(*_movieArchive, *_surface, _movieRect);
	//Stage is drawn between the prepareFrame and enterFrame events (Lingo in a Nutshell)
	_lingo->processEvent(kEventEnterFrame, _currentFrame);

	_currentFrame++;
	byte tempo = _frames[_currentFrame]->_tempo;

	if (tempo) {
		if (tempo > 161) {
			//Delay
			_nextFrameTime = g_system->getMillis() + (256 - tempo) * 1000;
		} else if (tempo <= 60) {
			//FPS
			_nextFrameTime = g_system->getMillis() + (float)tempo / 60 * 1000;
			_currentFrameRate = tempo;
		} else if (tempo >= 136) {
			//TODO Wait for channel tempo - 135
		} else if (tempo == 128) {
			//TODO Wait for Click/Key
		} else if (tempo == 135) {
			//TODO Wait for sound channel 1
		} else if (tempo == 134) {
			//TODO Wait for sound channel 2
		}
	}
	_nextFrameTime = g_system->getMillis() + (float)_currentFrameRate / 60 * 1000;
}

void Score::processEvents() {
	Common::Event event;
	while (g_system->getEventManager()->pollEvent(event)) {
		if (event.type == Common::EVENT_QUIT)
			_stopPlay = true;
	}
}

Frame::Frame() {
	_transFlags = 0;
	_transChunkSize = 0;
	_tempo = 0;

	_sound1 = 0;
	_sound2 = 0;
	_soundType1 = 0;
	_soundType2 = 0;

	_actionId = 0;
	_skipFrameFlag = 0;
	_blend = 0;

	_sprites.resize(CHANNEL_COUNT);
	for (uint16 i = 0; i < _sprites.size(); i++) {
		Sprite *sp = new Sprite();
		_sprites[i] = sp;
	}
}

Frame::Frame(const Frame &frame) {
	_actionId = frame._actionId;
	_transFlags = frame._transFlags;
	_transType = frame._transType;
	_transChunkSize = frame._transChunkSize;
	_tempo = frame._tempo;
	_sound1 = frame._sound1;
	_sound2 = frame._sound2;
	_soundType1 = frame._soundType1;
	_soundType2 = frame._soundType2;
	_skipFrameFlag = frame._skipFrameFlag;
	_blend = frame._blend;

	_sprites.resize(CHANNEL_COUNT);
	for (uint16 i = 0; i < CHANNEL_COUNT; i++) {
		_sprites[i] = new Sprite(*frame._sprites[i]);
	}
}

Frame::~Frame() {
	for (uint16 i = 0; i < _sprites.size(); i++) {
		delete _sprites[i];
	}
}

void Frame::readChannel(Common::SeekableReadStream &stream, uint16 offset, uint16 size) {
	if (offset >= 32) {
		if (size <= 16)
			readSprite(stream, offset, size);
		else {
			//read > 1 sprites channel
			while (size > 16) {
				byte spritePosition = (offset - 32) / 16;
				uint16 nextStart = (spritePosition + 1) * 16 + 32;
				uint16 needSize = nextStart - offset;
				readSprite(stream, offset, needSize);
				offset += needSize;
				size -= needSize;
			}
			readSprite(stream, offset, size);
		}
	} else {
		readMainChannels(stream, offset, size);
	}
}

void Frame::readMainChannels(Common::SeekableReadStream &stream, uint16 offset, uint16 size) {
	uint16 finishPosition = offset + size;

	while (offset < finishPosition) {
		switch(offset) {
		case kScriptIdPosition:
			_actionId = stream.readByte();
			offset++;
			break;
		case kSoundType1Position:
			_soundType1 = stream.readByte();
			offset++;
			break;
		case kTransFlagsPosition:
			_transFlags = stream.readByte();
			offset++;
			break;
		case kTransChunkSizePosition:
			_transChunkSize = stream.readByte();
			offset++;
			break;
		case kTempoPosition:
			_tempo = stream.readByte();
			offset++;
			break;
		case kTransTypePosition:
			_transType = static_cast<transitionType>(stream.readByte());
			offset++;
			break;
		case kSound1Position:
			_sound1 = stream.readUint16BE();
			offset+=2;
			break;
		case kSkipFrameFlagsPosition:
			_skipFrameFlag = stream.readByte();
			offset++;
			break;
		case kBlendPosition:
			_blend = stream.readByte();
			offset++;
			break;
		case kSound2Position:
			_sound2 = stream.readUint16BE();
			offset += 2;
			break;
		case kSound2TypePosition:
			_soundType2 = stream.readByte();
			offset += 1;
			break;
		case kPaletePosition:
			//TODO palette channel
			stream.skip(16);
			offset += 16;
		default:
			offset++;
			stream.readByte();
			debug("Field Position %d, Finish Position %d", offset, finishPosition);
			break;
		}
	}
}

void Frame::readSprite(Common::SeekableReadStream &stream, uint16 offset, uint16 size) {
	uint16 spritePosition = (offset - 32) / 16;
	uint16 spriteStart = spritePosition * 16 + 32;

	uint16 fieldPosition = offset - spriteStart;
	uint16 finishPosition = fieldPosition + size;

	Sprite &sprite = *_sprites[spritePosition];

	while (fieldPosition < finishPosition) {
		switch (fieldPosition) {
		case kSpritePositionUnk1:
			/*byte x1 = */ stream.readByte();
			fieldPosition++;
			break;
		case kSpritePositionEnabled:
			sprite._enabled = (stream.readByte() != 0);
			fieldPosition++;
			break;
		case kSpritePositionUnk2:
			/*byte x2 = */ stream.readUint16BE();
			fieldPosition += 2;
			break;
		case kSpritePositionFlags:
			sprite._flags = stream.readUint16BE();
			sprite._ink = static_cast<inkType>(sprite._flags & 0x3f); //TODO more flags?
			sprite._trails = sprite._flags & 0x40;
			debug("%d", sprite._trails);
			fieldPosition += 2;
			break;
		case kSpritePositionCastId:
			sprite._castId = stream.readUint16BE();
			fieldPosition += 2;
			break;
		case kSpritePositionY:
			sprite._startPoint.y = stream.readUint16BE();
			fieldPosition += 2;
			break;
		case kSpritePositionX:
			sprite._startPoint.x = stream.readUint16BE();
			fieldPosition += 2;
			break;
		case kSpritePositionWidth:
			sprite._width = stream.readUint16BE();
			fieldPosition += 2;
			break;
		case kSpritePositionHeight:
			sprite._height = stream.readUint16BE();
			fieldPosition += 2;
			break;
		default:
			//end cycle, go to next sprite channel
			readSprite(stream, spriteStart + 16, finishPosition - fieldPosition);
			fieldPosition = finishPosition;
			break;
		}
	}
}

void Frame::prepareFrame(Archive &_movie, Graphics::ManagedSurface &surface, Common::Rect movieRect) {
	renderSprites(_movie, surface, movieRect);
	renderTrailSprites(_movie, surface, movieRect);
	if (_transType != 0)
		playTranisition();
	if (_sound1 != 0)
		playSoundChannel(_sound1);
	if (_sound2 != 0)
		playSoundChannel(_sound2);
}

void Frame::playSoundChannel(uint16 id) {
	warning("STUB: playSound(%d)", id);
}

void Frame::playTranisition() {
	warning("STUB: playTranisition(%d, %d, %d)", _transType, _transFlags, _transChunkSize);
}

void Frame::renderSprites(Archive &_movie, Graphics::ManagedSurface &surface, Common::Rect movieRect) {
	for (uint16 i = 0; i < CHANNEL_COUNT; i++) {
		//TODO if trails == 0 -> render as trail sprite
		if (_sprites[i]->_enabled) {
			DIBDecoder img;
			uint32 imgId = 1024 + _sprites[i]->_castId;
			if (!_movie.hasResource(MKTAG('D', 'I', 'B', ' '), imgId)) {
				continue;
			}

			img.loadStream(*_movie.getResource(MKTAG('D', 'I', 'B', ' '), imgId));

			uint32 regX = static_cast<BitmapCast *>(_sprites[i]->_cast)->regX;
			uint32 regY = static_cast<BitmapCast *>(_sprites[i]->_cast)->regY;
			uint32 rectLeft = static_cast<BitmapCast *>(_sprites[i]->_cast)->initialRect.left;
			uint32 rectTop = static_cast<BitmapCast *>(_sprites[i]->_cast)->initialRect.top;

			int x = _sprites[i]->_startPoint.x - regX + rectLeft;
			int y = _sprites[i]->_startPoint.y - regY + rectTop;
			int height = _sprites[i]->_height;
			int width = _sprites[i]->_width;

			if (x < 0) {
				width += x;
				x = 0;
			}
			if (y < 0) {
				height += y;
				y = 0;
			}
			Common::Rect drawRect = Common::Rect(x, y, x + width, y + height);

			switch (_sprites[i]->_ink) {
			case kInkTypeCopy:
				surface.blitFrom(*img.getSurface(), Common::Point(x, y));
				break;
			case kInkTypeBackgndTrans:
				drawBackgndTransSprite(surface, *img.getSurface(), drawRect);
				break;
			case kInkTypeMatte:
				drawMatteSprite(surface, *img.getSurface(), drawRect);
				break;
			default:
				warning("Unhandled ink type %d", _sprites[i]->_ink);
				surface.blitFrom(*img.getSurface(), Common::Point(x, y));
				break;
			}
		}
	}
	g_system->copyRectToScreen(surface.getPixels(), surface.pitch, 0, 0, surface.getBounds().width(), surface.getBounds().height());
}

void Frame::renderTrailSprites(Archive &_movie, Graphics::ManagedSurface &surface, Common::Rect movieRect) {
	for (uint16 i = 0; i < CHANNEL_COUNT; i++) {
		if (_sprites[i]->_enabled && _sprites[i]->_trails != 0)
			warning("STUB: renderTrailSprites(%d)", i);
	}
}

void Frame::drawBackgndTransSprite(Graphics::ManagedSurface &target, const Graphics::Surface &sprite, Common::Rect &drawRect) {
	uint8 skipColor = 15; //FIXME is it always white (last entry in pallette) ?
	for (int ii = 0; ii < sprite.h; ii++) {
		const byte *src = (const byte *)sprite.getBasePtr(0, ii);
		byte *dst = (byte *)target.getBasePtr(drawRect.left, drawRect.top + ii);
		for (int j = 0; j < drawRect.width(); j++) {
			if (*src != skipColor)
				*dst = *src;
			src++;
			dst++;
		}
	}
}

void Frame::drawMatteSprite(Graphics::ManagedSurface &target, const Graphics::Surface &sprite, Common::Rect &drawRect) {
	//Like background trans, but all white pixels NOT ENCLOSED by coloured pixels are transparent
	Graphics::Surface tmp;
	tmp.copyFrom(sprite);

	Graphics::FloodFill ff(&tmp, *(byte *)tmp.getBasePtr(0, 0), 0);

	for (int yy = 0; yy < tmp.h; yy++) {
		ff.addSeed(0, yy);
		ff.addSeed(tmp.w - 1, yy);
	}
	for (int xx = 0; xx < tmp.w; xx++) {
		ff.addSeed(xx, 0);
		ff.addSeed(xx, tmp.h - 1);
	}
	ff.fillMask();

	for (int yy = 0; yy < tmp.h; yy++) {
		const byte *src = (const byte *)tmp.getBasePtr(0, yy);
		const byte *mask = (const byte *)ff.getMask()->getBasePtr(0, yy);
		byte *dst = (byte *)target.getBasePtr(drawRect.left, drawRect.top + yy);

		for (int xx = 0; xx < drawRect.width(); xx++, src++, dst++, mask++)
			if (*mask == 0)
				*dst = *src;
	}

	tmp.free();
}

Sprite::Sprite() {
	_enabled = false;
	_width = 0;
	_ink = kInkTypeCopy;
	_flags = 0;
	_height = 0;
	_castId = 0;
	_castId = 0;
}

Sprite::Sprite(const Sprite &sprite) {
	_enabled = sprite._enabled;
	_castId = sprite._castId;
	_flags = sprite._flags;
	_ink = sprite._ink;
	_width = sprite._width;
	_height = sprite._height;
	_startPoint.x = sprite._startPoint.x;
	_startPoint.y = sprite._startPoint.y;
}

} //End of namespace Director