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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * Palette Fader and Flasher processes.
22  */
23 
24 #include "tinsel/actors.h"
25 #include "tinsel/faders.h"	// fader defs
26 #include "tinsel/handle.h"
27 #include "tinsel/palette.h"	// Palette Manager defs
28 #include "tinsel/pid.h"	// list of all process IDs
29 #include "tinsel/sched.h"	// scheduler defs
30 #include "tinsel/sysvar.h"
31 #include "tinsel/tinsel.h"
32 
33 namespace Tinsel {
34 
35 /** structure used by the "FadeProcess" process */
36 struct FADE {
37 	const long *pColorMultTable;	// list of fixed point color multipliers - terminated with negative entry
38 	PALQ *pPalQ;		// palette queue entry to fade
39 };
40 
41 // fixed point fade multiplier tables
42 //const long fadeout[] = {0xf000, 0xd000, 0xb000, 0x9000, 0x7000, 0x5000, 0x3000, 0x1000, 0, -1};
43 //const long fadein[] = {0, 0x1000, 0x3000, 0x5000, 0x7000, 0x9000, 0xb000, 0xd000, 0x10000L, -1};
44 
45 /**
46  * Scale 'color' by the fixed point color multiplier 'colorMult'
47  * @param color			Color to scale
48  * @param colorMult		Fixed point multiplier
49  */
ScaleColor(COLORREF color,uint32 colorMult)50 static COLORREF ScaleColor(COLORREF color, uint32 colorMult)	{
51 	// apply multiplier to RGB components
52 	uint32 red   = ((TINSEL_GetRValue(color) * colorMult) << 8) >> 24;
53 	uint32 green = ((TINSEL_GetGValue(color) * colorMult) << 8) >> 24;
54 	uint32 blue  = ((TINSEL_GetBValue(color) * colorMult) << 8) >> 24;
55 
56 	// return new color
57 	return TINSEL_RGB(red, green, blue);
58 }
59 
60 /**
61  * Applies the fixed point multiplier 'mult' to all colors in
62  * 'pOrig' to produce 'pNew'. Each color in the palette will be
63  * multiplied by 'mult'.
64  * @param pNew				Pointer to new palette
65  * @param pOrig				Pointer to original palette
66  * @param numColors		Number of colors in the above palettes
67  * @param mult				Fixed point multiplier
68  */
FadePalette(COLORREF * pNew,COLORREF * pOrig,int numColors,uint32 mult)69 static void FadePalette(COLORREF *pNew, COLORREF *pOrig, int numColors, uint32 mult) {
70 	for (int i = 0; i < numColors; i++, pNew++, pOrig++) {
71 		if (!TinselV2)
72 			// apply multiplier to RGB components
73 			*pNew = ScaleColor(*pOrig, mult);
74 		else if (i == (TalkColor() - 1)) {
75 			*pNew = GetTalkColorRef();
76 			*pNew = ScaleColor(*pNew, mult);
77 		} else if (SysVar(SV_TAGCOLOR) && i == (SysVar(SV_TAGCOLOR) - 1)) {
78 			*pNew = GetTagColorRef();
79 			*pNew = ScaleColor(*pNew, mult);
80 		} else {
81 			*pNew = ScaleColor(*pOrig, mult);
82 		}
83 	}
84 }
85 
86 /**
87  * Process to fade one palette.
88  * A pointer to a 'FADE' structure must be passed to this process when
89  * it is created.
90  */
FadeProcess(CORO_PARAM,const void * param)91 static void FadeProcess(CORO_PARAM, const void *param) {
92 	// COROUTINE
93 	CORO_BEGIN_CONTEXT;
94 		COLORREF fadeRGB[MAX_COLORS];	// local copy of palette
95 		const long *pColMult;			// pointer to color multiplier table
96 		PALETTE *pPalette;		// pointer to palette
97 	CORO_END_CONTEXT(_ctx);
98 
99 	// get the fade data structure - copied to process when it was created
100 	const FADE *pFade = (const FADE *)param;
101 
102 	CORO_BEGIN_CODE(_ctx);
103 
104 	if (TinselV2)
105 		// Note that this palette is being faded
106 		FadingPalette(pFade->pPalQ, true);
107 
108 	// get pointer to palette - reduce pointer indirection a bit
109 	_ctx->pPalette = (PALETTE *)LockMem(pFade->pPalQ->hPal);
110 
111 	for (_ctx->pColMult = pFade->pColorMultTable; *_ctx->pColMult >= 0; _ctx->pColMult++) {
112 		// go through all multipliers in table - until a negative entry
113 
114 		// fade palette using next multiplier
115 		if (TinselV2)
116 			FadePalette(_ctx->fadeRGB, pFade->pPalQ->palRGB,
117 				pFade->pPalQ->numColors, (uint32) *_ctx->pColMult);
118 		else
119 			FadePalette(_ctx->fadeRGB, _ctx->pPalette->palRGB,
120 				FROM_32(_ctx->pPalette->numColors), (uint32) *_ctx->pColMult);
121 
122 		// send new palette to video DAC
123 		UpdateDACqueue(pFade->pPalQ->posInDAC, FROM_32(_ctx->pPalette->numColors), _ctx->fadeRGB);
124 
125 		// allow time for video DAC to be updated
126 		CORO_SLEEP(1);
127 	}
128 
129 	if (TinselV2)
130 		// Note that this palette is being faded
131 		FadingPalette(pFade->pPalQ, false);
132 
133 	CORO_END_CODE;
134 }
135 
136 /**
137  * Generic palette fader/unfader. Creates a 'FadeProcess' process
138  * for each palette that is to fade.
139  * @param multTable			Fixed point color multiplier table
140  */
Fader(const long multTable[])141 static void Fader(const long multTable[]) {
142 	PALQ *pPal;	// palette manager iterator
143 
144 	if (TinselV2) {
145 		// The is only ever one cuncurrent fade
146 		// But this could be a fade out and the fade in is still going!
147 		CoroScheduler.killMatchingProcess(PID_FADER);
148 		NoFadingPalettes();
149 	}
150 
151 	// create a process for each palette in the palette queue
152 	for (pPal = GetNextPalette(NULL); pPal != NULL; pPal = GetNextPalette(pPal)) {
153 		FADE fade;
154 
155 		// fill in FADE struct
156 		fade.pColorMultTable	= multTable;
157 		fade.pPalQ		= pPal;
158 
159 		// create a fader process for this palette
160 		CoroScheduler.createProcess(PID_FADER, FadeProcess, (void *)&fade, sizeof(FADE));
161 	}
162 }
163 
164 /**
165  * Fades a list of palettes down to black.
166  */
FadeOutMedium()167 void FadeOutMedium() {
168 	// Fixed point fade multiplier table
169 	static const long fadeout[] = {0xea00, 0xd000, 0xb600, 0x9c00,
170 		0x8200, 0x6800, 0x4e00, 0x3400, 0x1a00, 0, -1};
171 
172 	// call generic fader
173 	Fader(fadeout);
174 }
175 
176 /**
177  * Fades a list of palettes down to black.
178  */
FadeOutFast()179 void FadeOutFast() {
180 	// Fixed point fade multiplier table
181 	static const long fadeout[] = {0xd000, 0xa000, 0x7000, 0x4000, 0x1000, 0, -1};
182 
183 	// call generic fader
184 	Fader(fadeout);
185 }
186 
187 /**
188  * Fades a list of palettes from black to their current colors.
189  */
FadeInMedium()190 void FadeInMedium() {
191 	// Fade multiplier table
192 	static const long fadein[] = {0, 0x1a00, 0x3400, 0x4e00, 0x6800,
193 		0x8200, 0x9c00, 0xb600, 0xd000, 0xea00, 0x10000L, -1};
194 
195 	// call generic fader
196 	Fader(fadein);
197 }
198 
199 /**
200  * Fades a list of palettes from black to their current colors.
201  */
FadeInFast()202 void FadeInFast() {
203 	// Fade multiplier table
204 	static const long fadein[] = {0, 0x1000, 0x4000, 0x7000, 0xa000, 0xd000, 0x10000L, -1};
205 
206 	// call generic fader
207 	Fader(fadein);
208 }
209 
PokeInTagColor()210 void PokeInTagColor() {
211 	if (SysVar(SV_TAGCOLOR)) {
212 		const COLORREF c = GetActorRGB(-1);
213 		UpdateDACqueue(SysVar(SV_TAGCOLOR), c);
214 	}
215 }
216 
217 } // End of namespace Tinsel
218