aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/graphics/transitions32.cpp
blob: bceb0fa84d0a9c6ba32250152d9741aaf6f83289 (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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
/* 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 "sci/engine/segment.h"
#include "sci/engine/seg_manager.h"
#include "sci/engine/state.h"
#include "sci/graphics/frameout.h"
#include "sci/graphics/palette32.h"
#include "sci/graphics/text32.h"
#include "sci/graphics/transitions32.h"
#include "sci/sci.h"

namespace Sci {
static int dissolveSequences[2][20] = {
	/* SCI2.1early- */ { 3, 6, 12, 20, 48, 96, 184, 272, 576, 1280, 3232, 6912, 13568, 24576, 46080 },
	/* SCI2.1mid+ */ { 0, 0, 3, 6, 12, 20, 48, 96, 184, 272, 576, 1280, 3232, 6912, 13568, 24576, 46080, 73728, 132096, 466944 }
};
static int16 divisionsDefaults[2][16] = {
	/* SCI2.1early- */ { 1, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 40, 40, 101, 101 },
	/* SCI2.1mid+ */   { 1, 20, 20, 20, 20, 10, 10, 10, 10, 20, 20,  6, 10, 101, 101, 2 }
};

GfxTransitions32::GfxTransitions32(SegManager *segMan) :
	_segMan(segMan),
	_throttleState(0) {
	for (int i = 0; i < 236; i += 2) {
		_styleRanges[i] = 0;
		_styleRanges[i + 1] = -1;
	}
	for (int i = 236; i < ARRAYSIZE(_styleRanges); ++i) {
		_styleRanges[i] = 0;
	}

	if (getSciVersion() < SCI_VERSION_2_1_MIDDLE) {
		_dissolveSequenceSeeds = dissolveSequences[0];
		_defaultDivisions = divisionsDefaults[0];
	} else {
		_dissolveSequenceSeeds = dissolveSequences[1];
		_defaultDivisions = divisionsDefaults[1];
	}
}

GfxTransitions32::~GfxTransitions32() {
	for (ShowStyleList::iterator it = _showStyles.begin();
		 it != _showStyles.end();
		 it = deleteShowStyle(it));
	_scrolls.clear();
}

void GfxTransitions32::throttle() {
	uint8 throttleTime;
	if (_throttleState == 2) {
		throttleTime = 34;
		_throttleState = 0;
	} else {
		throttleTime = 33;
		++_throttleState;
	}

	g_sci->getEngineState()->speedThrottler(throttleTime);
	g_sci->getEngineState()->_throttleTrigger = true;
}

#pragma mark -
#pragma mark Show styles

void GfxTransitions32::processShowStyles() {
	uint32 now = g_sci->getTickCount();

	bool continueProcessing;
	bool doFrameOut;
	do {
		continueProcessing = false;
		doFrameOut = false;
		ShowStyleList::iterator showStyle = _showStyles.begin();
		while (showStyle != _showStyles.end()) {
			bool finished = false;

			if (!showStyle->animate) {
				doFrameOut = true;
			}

			finished = processShowStyle(*showStyle, now);

			if (!finished) {
				continueProcessing = true;
			}

			if (finished && showStyle->processed) {
				showStyle = deleteShowStyle(showStyle);
			} else {
				showStyle = ++showStyle;
			}
		}

		if (g_engine->shouldQuit()) {
			return;
		}

		if (doFrameOut) {
			g_sci->_gfxFrameout->frameOut(true);
			throttle();
		}
	} while(continueProcessing && doFrameOut);
}

void GfxTransitions32::processEffects(PlaneShowStyle &showStyle) {
	switch(showStyle.type) {
	case kShowStyleHShutterOut:
		processHShutterOut(showStyle);
	break;
	case kShowStyleHShutterIn:
		processHShutterIn(showStyle);
	break;
	case kShowStyleVShutterOut:
		processVShutterOut(showStyle);
	break;
	case kShowStyleVShutterIn:
		processVShutterIn(showStyle);
	break;
	case kShowStyleWipeLeft:
		processWipeLeft(showStyle);
	break;
	case kShowStyleWipeRight:
		processWipeRight(showStyle);
	break;
	case kShowStyleWipeUp:
		processWipeUp(showStyle);
	break;
	case kShowStyleWipeDown:
		processWipeDown(showStyle);
	break;
	case kShowStyleIrisOut:
		processIrisOut(showStyle);
	break;
	case kShowStyleIrisIn:
		processIrisIn(showStyle);
	break;
	case kShowStyleDissolveNoMorph:
	case kShowStyleDissolve:
		processPixelDissolve(showStyle);
	break;
	case kShowStyleNone:
	case kShowStyleFadeOut:
	case kShowStyleFadeIn:
	case kShowStyleMorph:
	break;
	}
}

// TODO: 10-argument version is only in SCI3; argc checks are currently wrong for this version
// and need to be fixed in future
void GfxTransitions32::kernelSetShowStyle(const uint16 argc, const reg_t planeObj, const ShowStyleType type, const int16 seconds, const int16 back, const int16 priority, const int16 animate, const int16 frameOutNow, reg_t pFadeArray, int16 divisions, const int16 blackScreen) {

	bool hasDivisions = false;
	bool hasFadeArray = false;

	// KQ7 2.0b uses a mismatched version of the Styler script (SCI2.1early script
	// for SCI2.1mid engine), so the calls it makes to kSetShowStyle are wrong and
	// put `divisions` where `pFadeArray` is supposed to be
	if (getSciVersion() == SCI_VERSION_2_1_MIDDLE && g_sci->getGameId() == GID_KQ7) {
		hasDivisions = argc > 7;
		hasFadeArray = false;
		divisions = argc > 7 ? pFadeArray.toSint16() : -1;
		pFadeArray = NULL_REG;
	} else if (getSciVersion() < SCI_VERSION_2_1_MIDDLE) {
		hasDivisions = argc > 7;
		hasFadeArray = false;
	} else if (getSciVersion() < SCI_VERSION_3) {
		hasDivisions = argc > 8;
		hasFadeArray = argc > 7;
	} else {
		hasDivisions = argc > 9;
		hasFadeArray = argc > 8;
	}

	bool isFadeUp;
	int16 color;
	if (back != -1) {
		isFadeUp = false;
		color = back;
	} else {
		isFadeUp = true;
		color = 0;
	}

	if ((getSciVersion() < SCI_VERSION_2_1_MIDDLE && g_sci->getGameId() != GID_KQ7 && type == 15) || type > 15) {
		error("Illegal show style %d for plane %04x:%04x", type, PRINT_REG(planeObj));
	}

	Plane *plane = g_sci->_gfxFrameout->getPlanes().findByObject(planeObj);
	if (plane == nullptr) {
		error("Plane %04x:%04x is not present in active planes list", PRINT_REG(planeObj));
	}

	bool createNewEntry = true;
	PlaneShowStyle *entry = findShowStyleForPlane(planeObj);
	if (entry != nullptr) {
		bool useExisting = true;

		if (getSciVersion() < SCI_VERSION_2_1_MIDDLE) {
			useExisting = plane->_gameRect.width() == entry->width && plane->_gameRect.height() == entry->height;
		}

		if (useExisting) {
			useExisting = entry->divisions == (hasDivisions ? divisions : _defaultDivisions[type]);
		}

		if (useExisting) {
			createNewEntry = false;
			isFadeUp = true;
			entry->currentStep = 0;
		} else {
			isFadeUp = true;
			color = entry->color;
			deleteShowStyle(findIteratorForPlane(planeObj));
			entry = nullptr;
		}
	}

	if (type > 0) {
		if (createNewEntry) {
			entry = new PlaneShowStyle;
			// NOTE: SCI2.1 engine tests if allocation returned a null pointer
			// but then only avoids setting currentStep if this is so. Since
			// this is a nonsensical approach, we do not do that here
			entry->currentStep = 0;
			entry->processed = false;
			entry->divisions = hasDivisions ? divisions : _defaultDivisions[type];
			entry->plane = planeObj;
			entry->fadeColorRangesCount = 0;

			if (getSciVersion() < SCI_VERSION_2_1_MIDDLE) {
				// for pixel dissolve
				entry->bitmap = NULL_REG;
				entry->bitmapScreenItem = nullptr;

				// for wipe
				entry->screenItems.clear();
				entry->width = plane->_gameRect.width();
				entry->height = plane->_gameRect.height();
			} else {
				entry->fadeColorRanges = nullptr;
				if (hasFadeArray) {
					// NOTE: SCI2.1mid engine does no check to verify that an array is
					// successfully retrieved, and SegMan will cause a fatal error
					// if we try to use a memory segment that is not an array
					SciArray<reg_t> *table = _segMan->lookupArray(pFadeArray);

					uint32 rangeCount = table->getSize();
					entry->fadeColorRangesCount = rangeCount;

					// NOTE: SCI engine code always allocates memory even if the range
					// table has no entries, but this does not really make sense, so
					// we avoid the allocation call in this case
					if (rangeCount > 0) {
						entry->fadeColorRanges = new uint16[rangeCount];
						for (size_t i = 0; i < rangeCount; ++i) {
							entry->fadeColorRanges[i] = table->getValue(i).toUint16();
						}
					}
				}
			}
		}

		// NOTE: The original engine had no nullptr check and would just crash
		// if it got to here
		if (entry == nullptr) {
			error("Cannot edit non-existing ShowStyle entry");
		}

		entry->fadeUp = isFadeUp;
		entry->color = color;
		entry->nextTick = g_sci->getTickCount();
		entry->type = type;
		entry->animate = animate;
		entry->delay = (seconds * 60 + entry->divisions - 1) / entry->divisions;

		if (entry->delay == 0) {
			error("ShowStyle has no duration");
		}

		if (frameOutNow) {
			// Creates a reference frame for the pixel dissolves to use
			g_sci->_gfxFrameout->frameOut(false);
		}

		if (createNewEntry) {
			if (getSciVersion() <= SCI_VERSION_2_1_EARLY) {
				switch (entry->type) {
				case kShowStyleIrisOut:
				case kShowStyleIrisIn:
					configure21EarlyIris(*entry, priority);
				break;
				case kShowStyleDissolve:
					configure21EarlyDissolve(*entry, priority, plane->_gameRect);
				break;
				default:
					// do nothing
				break;
				}
			}

			_showStyles.push_back(*entry);
			delete entry;
		}
	}
}

void GfxTransitions32::kernelSetPalStyleRange(const uint8 fromColor, const uint8 toColor) {
	if (toColor > fromColor) {
		return;
	}

	for (int i = fromColor; i <= toColor; ++i) {
		_styleRanges[i] = 0;
	}
}

PlaneShowStyle *GfxTransitions32::findShowStyleForPlane(const reg_t planeObj) {
	for (ShowStyleList::iterator it = _showStyles.begin(); it != _showStyles.end(); ++it) {
		if (it->plane == planeObj) {
			return &*it;
		}
	}

	return nullptr;
}

ShowStyleList::iterator GfxTransitions32::findIteratorForPlane(const reg_t planeObj) {
	ShowStyleList::iterator it;
	for (it = _showStyles.begin(); it != _showStyles.end(); ++it) {
		if (it->plane == planeObj) {
			break;
		}
	}

	return it;
}

ShowStyleList::iterator GfxTransitions32::deleteShowStyle(const ShowStyleList::iterator &showStyle) {
	switch (showStyle->type) {
	case kShowStyleDissolveNoMorph:
	case kShowStyleDissolve:
		if (getSciVersion() <= SCI_VERSION_2_1_EARLY) {
			_segMan->freeBitmap(showStyle->bitmap);
			g_sci->_gfxFrameout->deleteScreenItem(*showStyle->bitmapScreenItem);
		}
		break;
	case kShowStyleIrisOut:
	case kShowStyleIrisIn:
		if (getSciVersion() <= SCI_VERSION_2_1_EARLY) {
			for (uint i = 0; i < showStyle->screenItems.size(); ++i) {
				ScreenItem *screenItem = showStyle->screenItems[i];
				if (screenItem != nullptr) {
					g_sci->_gfxFrameout->deleteScreenItem(*screenItem);
				}
			}
		}
		break;
	case kShowStyleFadeIn:
	case kShowStyleFadeOut:
		if (getSciVersion() > SCI_VERSION_2_1_EARLY && showStyle->fadeColorRangesCount > 0) {
			delete[] showStyle->fadeColorRanges;
		}
		break;
	case kShowStyleNone:
	case kShowStyleMorph:
		// do nothing
		break;
	default:
		error("Unknown delete transition type %d", showStyle->type);
	}

	return _showStyles.erase(showStyle);
}

void GfxTransitions32::configure21EarlyIris(PlaneShowStyle &showStyle, const int16 priority) {
	showStyle.numEdges = 4;
	const int numScreenItems = showStyle.numEdges * showStyle.divisions;
	showStyle.screenItems.reserve(numScreenItems);

	CelInfo32 celInfo;
	celInfo.type = kCelTypeColor;
	celInfo.color = showStyle.color;

	const int width = showStyle.width;
	const int height = showStyle.height;
	const int divisions = showStyle.divisions;

	for (int i = 0; i < divisions; ++i) {
		Common::Rect rect;

		// Top
		rect.left = (width * i) / (2 * divisions);
		rect.top = (height * i) / (2 * divisions);
		rect.right = width - rect.left;
		rect.bottom = (height + 1) * (i + 1) / (2 * divisions);
		const int16 topTop = rect.top;
		const int16 topBottom = rect.bottom;

		showStyle.screenItems.push_back(new ScreenItem(showStyle.plane, celInfo, rect));
		showStyle.screenItems.back()->_priority = priority;
		showStyle.screenItems.back()->_fixedPriority = true;

		// Bottom
		rect.top = height - rect.bottom;
		rect.bottom = height - topTop;
		const int16 bottomTop = rect.top;

		showStyle.screenItems.push_back(new ScreenItem(showStyle.plane, celInfo, rect));
		showStyle.screenItems.back()->_priority = priority;
		showStyle.screenItems.back()->_fixedPriority = true;

		// Left
		rect.top = topBottom;
		rect.right = (width + 1) * (i + 1) / (2 * divisions);
		rect.bottom = bottomTop;
		const int16 leftLeft = rect.left;

		showStyle.screenItems.push_back(new ScreenItem(showStyle.plane, celInfo, rect));
		showStyle.screenItems.back()->_priority = priority;
		showStyle.screenItems.back()->_fixedPriority = true;

		// Right
		rect.left = width - rect.right;
		rect.right = width - leftLeft;

		showStyle.screenItems.push_back(new ScreenItem(showStyle.plane, celInfo, rect));
		showStyle.screenItems.back()->_priority = priority;
		showStyle.screenItems.back()->_fixedPriority = true;
	}

	if (showStyle.fadeUp) {
		for (int i = 0; i < numScreenItems; ++i) {
			g_sci->_gfxFrameout->addScreenItem(*showStyle.screenItems[i]);
		}
	}
}

void GfxTransitions32::configure21EarlyDissolve(PlaneShowStyle &showStyle, const int16 priority, const Common::Rect &gameRect) {

	reg_t bitmapId;
	SciBitmap &bitmap = *_segMan->allocateBitmap(&bitmapId, showStyle.width, showStyle.height, kDefaultSkipColor, 0, 0, kLowResX, kLowResY, 0, false, false);

	showStyle.bitmap = bitmapId;

	const Buffer &source = g_sci->_gfxFrameout->getCurrentBuffer();
	Buffer target(showStyle.width, showStyle.height, bitmap.getPixels());

	target.fillRect(Common::Rect(bitmap.getWidth(), bitmap.getHeight()), kDefaultSkipColor);
	target.copyRectToSurface(source, 0, 0, gameRect);

	CelInfo32 celInfo;
	celInfo.type = kCelTypeMem;
	celInfo.bitmap = bitmapId;

	showStyle.bitmapScreenItem = new ScreenItem(showStyle.plane, celInfo, Common::Point(0, 0), ScaleInfo());
	showStyle.bitmapScreenItem->_priority = priority;
	showStyle.bitmapScreenItem->_fixedPriority = true;

	g_sci->_gfxFrameout->addScreenItem(*showStyle.bitmapScreenItem);
}

bool GfxTransitions32::processShowStyle(PlaneShowStyle &showStyle, uint32 now) {
	if (showStyle.nextTick >= now && showStyle.animate) {
		return false;
	}

	switch (showStyle.type) {
	default:
	case kShowStyleNone:
		return processNone(showStyle);
	case kShowStyleHShutterOut:
	case kShowStyleHShutterIn:
	case kShowStyleVShutterOut:
	case kShowStyleVShutterIn:
	case kShowStyleWipeLeft:
	case kShowStyleWipeRight:
	case kShowStyleWipeUp:
	case kShowStyleWipeDown:
	case kShowStyleDissolveNoMorph:
	case kShowStyleMorph:
		return processMorph(showStyle);
	case kShowStyleDissolve:
		if (getSciVersion() > SCI_VERSION_2_1_EARLY) {
			return processMorph(showStyle);
		} else {
			return processPixelDissolve(showStyle);
		}
	case kShowStyleIrisOut:
		if (getSciVersion() > SCI_VERSION_2_1_EARLY) {
			return processMorph(showStyle);
		} else {
			return processIrisOut(showStyle);
		}
	case kShowStyleIrisIn:
		if (getSciVersion() > SCI_VERSION_2_1_EARLY) {
			return processMorph(showStyle);
		} else {
			return processIrisIn(showStyle);
		}
	case kShowStyleFadeOut:
		return processFade(-1, showStyle);
	case kShowStyleFadeIn:
		return processFade(1, showStyle);
	}
}

bool GfxTransitions32::processNone(PlaneShowStyle &showStyle) {
	if (showStyle.fadeUp) {
		g_sci->_gfxPalette32->setFade(100, 0, 255);
	} else {
		g_sci->_gfxPalette32->setFade(0, 0, 255);
	}

	showStyle.processed = true;
	return true;
}

void GfxTransitions32::processHShutterOut(PlaneShowStyle &showStyle) {
	error("HShutterOut is not known to be used by any game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
}

void GfxTransitions32::processHShutterIn(PlaneShowStyle &showStyle) {
	error("HShutterIn is not known to be used by any game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
}

void GfxTransitions32::processVShutterOut(PlaneShowStyle &showStyle) {
	error("VShutterOut is not known to be used by any game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
}

void GfxTransitions32::processVShutterIn(PlaneShowStyle &showStyle) {
	error("VShutterIn is not known to be used by any game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
}

void GfxTransitions32::processWipeLeft(PlaneShowStyle &showStyle) {
	error("WipeLeft is not known to be used by any game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
}

void GfxTransitions32::processWipeRight(PlaneShowStyle &showStyle) {
	error("WipeRight is not known to be used by any game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
}

void GfxTransitions32::processWipeUp(PlaneShowStyle &showStyle) {
	error("WipeUp is not known to be used by any game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
}

void GfxTransitions32::processWipeDown(PlaneShowStyle &showStyle) {
	error("WipeDown is not known to be used by any game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
}

bool GfxTransitions32::processIrisOut(PlaneShowStyle &showStyle) {
	if (getSciVersion() > SCI_VERSION_2_1_EARLY) {
		error("IrisOut is not known to be used by any SCI2.1mid+ game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
	}

	return processWipe(-1, showStyle);
}

bool GfxTransitions32::processIrisIn(PlaneShowStyle &showStyle) {
	if (getSciVersion() > SCI_VERSION_2_1_EARLY) {
		error("IrisIn is not known to be used by any SCI2.1mid+ game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
	}

	return processWipe(1, showStyle);
}

void GfxTransitions32::processDissolveNoMorph(PlaneShowStyle &showStyle) {
	error("DissolveNoMorph is not known to be used by any game. Please submit a bug report with details about the game you were playing and what you were doing that triggered this error. Thanks!");
}

inline int bitWidth(int number) {
	int width = 0;
	while (number != 0) {
		number >>= 1;
		width += 1;
	}
	return width;
}

bool GfxTransitions32::processPixelDissolve(PlaneShowStyle &showStyle) {
	if (getSciVersion() > SCI_VERSION_2_1_EARLY) {
		return processPixelDissolve21Mid(showStyle);
	} else {
		return processPixelDissolve21Early(showStyle);
	}
}

bool GfxTransitions32::processPixelDissolve21Early(PlaneShowStyle &showStyle) {
	bool unchanged = true;

	SciBitmap &bitmap = *_segMan->lookupBitmap(showStyle.bitmap);
	Buffer buffer(showStyle.width, showStyle.height, bitmap.getPixels());

	uint32 numPixels = showStyle.width * showStyle.height;
	uint32 numPixelsPerDivision = (numPixels + showStyle.divisions) / showStyle.divisions;

	uint32 index;
	if (showStyle.currentStep == 0) {
		int i = 0;
		index = numPixels;
		if (index != 1) {
			for (;;) {
				index >>= 1;
				if (index == 1) {
					break;
				}
				i++;
			}
		}

		showStyle.dissolveMask = _dissolveSequenceSeeds[i];
		index = 53427;

		showStyle.firstPixel = index;
		showStyle.pixel = index;
	} else {
		index = showStyle.pixel;
		for (;;) {
			if (index & 1) {
				index >>= 1;
				index ^= showStyle.dissolveMask;
			} else {
				index >>= 1;
			}

			if (index < numPixels) {
				break;
			}
		}

		if (index == showStyle.firstPixel) {
			index = 0;
		}
	}

	if (showStyle.currentStep < showStyle.divisions) {
		for (uint32 i = 0; i < numPixelsPerDivision; ++i) {
			*(byte *)buffer.getBasePtr(index % showStyle.width, index / showStyle.width) = showStyle.color;

			for (;;) {
				if (index & 1) {
					index >>= 1;
					index ^= showStyle.dissolveMask;
				} else {
					index >>= 1;
				}

				if (index < numPixels) {
					break;
				}
			}

			if (index == showStyle.firstPixel) {
				buffer.fillRect(Common::Rect(0, 0, showStyle.width, showStyle.height), showStyle.color);
				break;
			}
		}

		showStyle.pixel = index;
		showStyle.nextTick += showStyle.delay;
		++showStyle.currentStep;
		unchanged = false;
		if (showStyle.bitmapScreenItem->_created == 0) {
			showStyle.bitmapScreenItem->_updated = g_sci->_gfxFrameout->getScreenCount();
		}
	}

	if ((showStyle.currentStep >= showStyle.divisions) && unchanged) {
		if (showStyle.fadeUp) {
			showStyle.processed = true;
		}

		return true;
	}

	return false;
}

bool GfxTransitions32::processPixelDissolve21Mid(PlaneShowStyle &showStyle) {
	// SQ6 room 530

	Plane* plane = g_sci->_gfxFrameout->getVisiblePlanes().findByObject(showStyle.plane);
	const Common::Rect &screenRect = plane->_screenRect;
	Common::Rect rect;

	const int planeWidth = screenRect.width();
	const int planeHeight = screenRect.height();
	const int divisions = showStyle.divisions;
	const int width = planeWidth / divisions + ((planeWidth % divisions) ? 1 : 0);
	const int height = planeHeight / divisions + ((planeHeight % divisions) ? 1 : 0);

	const uint32 mask = _dissolveSequenceSeeds[bitWidth(width * height - 1)];
	int seq = 1;

	uint iteration = 0;
	const uint numIterationsPerTick = (width * height + divisions) / divisions;

	do {
		int row = seq / width;
		int col = seq % width;

		if (row < height) {
			if (row == height && (planeHeight % divisions)) {
				if (col == width && (planeWidth % divisions)) {
					rect.left = col * divisions;
					rect.top = row * divisions;
					rect.right = col * divisions + (planeWidth % divisions);
					rect.bottom = row * divisions + (planeHeight % divisions);
					rect.clip(screenRect);
					g_sci->_gfxFrameout->showRect(rect);
				} else {
					rect.left = col * divisions;
					rect.top = row * divisions;
					rect.right = col * divisions * 2;
					rect.bottom = row * divisions + (planeHeight % divisions);
					rect.clip(screenRect);
					g_sci->_gfxFrameout->showRect(rect);
				}
			} else {
				if (col == width && (planeWidth % divisions)) {
					rect.left = col * divisions;
					rect.top = row * divisions;
					rect.right = col * divisions + (planeWidth % divisions) + 1;
					rect.bottom = row * divisions * 2 + 1;
					rect.clip(screenRect);
					g_sci->_gfxFrameout->showRect(rect);
				} else {
					rect.left = col * divisions;
					rect.top = row * divisions;
					rect.right = col * divisions * 2 + 1;
					rect.bottom = row * divisions * 2 + 1;
					rect.clip(screenRect);
					g_sci->_gfxFrameout->showRect(rect);
				}
			}
		}

		if (seq & 1) {
			seq = (seq >> 1) ^ mask;
		} else {
			seq >>= 1;
		}

		if (++iteration == numIterationsPerTick) {
			throttle();
			iteration = 0;
		}
	} while(seq != 1 && !g_engine->shouldQuit());

	rect.left = screenRect.left;
	rect.top = screenRect.top;
	rect.right = divisions + screenRect.left;
	rect.bottom = divisions + screenRect.bottom;
	rect.clip(screenRect);
	g_sci->_gfxFrameout->showRect(rect);
	throttle();

	g_sci->_gfxFrameout->showRect(screenRect);
	return true;
}

bool GfxTransitions32::processFade(const int8 direction, PlaneShowStyle &showStyle) {
	bool unchanged = true;
	if (showStyle.currentStep < showStyle.divisions) {
		int percent;
		if (direction <= 0) {
			percent = showStyle.divisions - showStyle.currentStep - 1;
		} else {
			percent = showStyle.currentStep;
		}

		percent *= 100;
		percent /= showStyle.divisions - 1;

		if (showStyle.fadeColorRangesCount > 0) {
			for (int i = 0, len = showStyle.fadeColorRangesCount; i < len; i += 2) {
				g_sci->_gfxPalette32->setFade(percent, showStyle.fadeColorRanges[i], showStyle.fadeColorRanges[i + 1]);
			}
		} else {
			g_sci->_gfxPalette32->setFade(percent, 0, 255);
		}

		++showStyle.currentStep;
		showStyle.nextTick += showStyle.delay;
		unchanged = false;
	}

	if (showStyle.currentStep >= showStyle.divisions && unchanged) {
		if (direction > 0) {
			showStyle.processed = true;
		}

		return true;
	}

	return false;
}

bool GfxTransitions32::processMorph(PlaneShowStyle &showStyle) {
	g_sci->_gfxFrameout->palMorphFrameOut(_styleRanges, &showStyle);
	showStyle.processed = true;
	return true;
}

bool GfxTransitions32::processWipe(const int8 direction, PlaneShowStyle &showStyle) {
	bool unchanged = true;
	if (showStyle.currentStep < showStyle.divisions) {
		int index;
		if (direction > 0) {
			index = showStyle.currentStep;
		} else {
			index = showStyle.divisions - showStyle.currentStep - 1;
		}

		index *= showStyle.numEdges;
		for (int i = 0; i < showStyle.numEdges; ++i) {
			ScreenItem *screenItem = showStyle.screenItems[index + i];
			if (showStyle.fadeUp) {
				g_sci->_gfxFrameout->deleteScreenItem(*screenItem);
				showStyle.screenItems[index + i] = nullptr;
			} else {
				g_sci->_gfxFrameout->addScreenItem(*screenItem);
			}
		}

		++showStyle.currentStep;
		showStyle.nextTick += showStyle.delay;
		unchanged = false;
	}

	if (showStyle.currentStep >= showStyle.divisions && unchanged) {
		if (showStyle.fadeUp) {
			showStyle.processed = true;
		}

		return true;
	}

	return false;
}

#pragma mark -
#pragma mark Scrolls

void GfxTransitions32::processScrolls() {
	for (ScrollList::iterator it = _scrolls.begin(); it != _scrolls.end(); ) {
		bool finished = processScroll(*it);
		if (finished) {
			it = _scrolls.erase(it);
		} else {
			++it;
		}
	}

	throttle();
}

void GfxTransitions32::kernelSetScroll(const reg_t planeId, const int16 deltaX, const int16 deltaY, const GuiResourceId pictureId, const bool animate, const bool mirrorX) {

	for (ScrollList::const_iterator it = _scrolls.begin(); it != _scrolls.end(); ++it) {
		if (it->plane == planeId) {
			error("Scroll already exists on plane %04x:%04x", PRINT_REG(planeId));
		}
	}

	if (!deltaX && !deltaY) {
		error("kSetScroll: Scroll has no movement");
	}

	if (deltaX && deltaY) {
		error("kSetScroll: Cannot scroll in two dimensions");
	}

	PlaneScroll *scroll = new PlaneScroll;
	scroll->plane = planeId;
	scroll->x = 0;
	scroll->y = 0;
	scroll->deltaX = deltaX;
	scroll->deltaY = deltaY;
	scroll->newPictureId = pictureId;
	scroll->animate = animate;
	scroll->startTick = g_sci->getTickCount();

	Plane *plane = g_sci->_gfxFrameout->getPlanes().findByObject(planeId);
	if (plane == nullptr) {
		error("kSetScroll: Plane %04x:%04x not found", PRINT_REG(planeId));
	}

	Plane *visiblePlane = g_sci->_gfxFrameout->getPlanes().findByObject(planeId);
	if (visiblePlane == nullptr) {
		error("kSetScroll: Visible plane %04x:%04x not found", PRINT_REG(planeId));
	}

	const Common::Rect &gameRect = visiblePlane->_gameRect;
	Common::Point picOrigin;

	if (deltaX) {
		picOrigin.y = 0;

		if (deltaX > 0) {
			scroll->x = picOrigin.x = -gameRect.width();
		} else {
			scroll->x = picOrigin.x = gameRect.width();
		}
	} else {
		picOrigin.x = 0;

		if (deltaY > 0) {
			scroll->y = picOrigin.y = -gameRect.height();
		} else {
			scroll->y = picOrigin.y = gameRect.height();
		}
	}

	scroll->oldPictureId = plane->addPic(pictureId, picOrigin, mirrorX);

	if (animate) {
		_scrolls.push_front(*scroll);
	} else {
		bool finished = false;
		while (!finished && !g_engine->shouldQuit()) {
			finished = processScroll(*scroll);
			g_sci->_gfxFrameout->frameOut(true);
			throttle();
		}
		delete scroll;
	}
}

bool GfxTransitions32::processScroll(PlaneScroll &scroll) {
	bool finished = false;
	uint32 now = g_sci->getTickCount();
	if (scroll.startTick >= now) {
		return false;
	}

	int deltaX = scroll.deltaX;
	int deltaY = scroll.deltaY;
	if (((scroll.x + deltaX) * scroll.y) <= 0) {
		deltaX = -scroll.x;
	}
	if (((scroll.y + deltaY) * scroll.y) <= 0) {
		deltaY = -scroll.y;
	}

	scroll.x += deltaX;
	scroll.y += deltaY;

	Plane *plane = g_sci->_gfxFrameout->getPlanes().findByObject(scroll.plane);

	if ((scroll.x == 0) && (scroll.y == 0)) {
		plane->deletePic(scroll.oldPictureId, scroll.newPictureId);
		finished = true;
	}

	plane->scrollScreenItems(deltaX, deltaY, true);

	return finished;
}

} // End of namespace Sci