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  */
22 
23 /*
24  * This file is based on WME Lite.
25  * http://dead-code.org/redir.php?target=wmelite
26  * Copyright (c) 2011 Jan Nedoma
27  */
28 
29 #include "engines/wintermute/base/base_fader.h"
30 #include "engines/wintermute/base/base_engine.h"
31 #include "engines/wintermute/base/timer.h"
32 #include "engines/wintermute/base/gfx/base_renderer.h"
33 #include "common/util.h"
34 
35 namespace Wintermute {
36 
37 //////////////////////////////////////////////////////////////////////
38 // Construction/Destruction
39 //////////////////////////////////////////////////////////////////////
40 
IMPLEMENT_PERSISTENT(BaseFader,false)41 IMPLEMENT_PERSISTENT(BaseFader, false)
42 
43 //////////////////////////////////////////////////////////////////////////
44 BaseFader::BaseFader(BaseGame *inGame) : BaseObject(inGame) {
45 	_active = false;
46 	_red = _green = _blue = 0;
47 	_currentAlpha = 0x00;
48 	_sourceAlpha = 0;
49 	_targetAlpha = 0;
50 	_duration = 1000;
51 	_startTime = 0;
52 	_system = false;
53 }
54 
55 
56 //////////////////////////////////////////////////////////////////////////
~BaseFader()57 BaseFader::~BaseFader() {
58 
59 }
60 
61 
62 //////////////////////////////////////////////////////////////////////////
update()63 bool BaseFader::update() {
64 	if (!_active) {
65 		return STATUS_OK;
66 	}
67 
68 	int alphaDelta = _targetAlpha - _sourceAlpha;
69 
70 	uint32 time;
71 
72 	if (_system) {
73 		time = g_system->getMillis() - _startTime;
74 	} else {
75 		time = BaseEngine::getTimer()->getTime() - _startTime;
76 	}
77 
78 	if (time >= _duration) {
79 		_currentAlpha = _targetAlpha;
80 	} else {
81 		_currentAlpha = (byte)(_sourceAlpha + (float)time / (float)_duration * alphaDelta);
82 	}
83 	_currentAlpha = MIN((unsigned char)255, MAX(_currentAlpha, (byte)0));  // TODO: clean
84 
85 	_ready = time >= _duration;
86 	if (_ready && _currentAlpha == 0x00) {
87 		_active = false;
88 	}
89 
90 	return STATUS_OK;
91 }
92 
93 
94 //////////////////////////////////////////////////////////////////////////
display()95 bool BaseFader::display() {
96 	if (!_active) {
97 		return STATUS_OK;
98 	}
99 
100 	if (_currentAlpha > 0x00) {
101 		BaseEngine::getRenderer()->fadeToColor(_red, _green, _blue, _currentAlpha);
102 	}
103 	return STATUS_OK;
104 }
105 
106 
107 //////////////////////////////////////////////////////////////////////////
deactivate()108 bool BaseFader::deactivate() {
109 	_active = false;
110 	_ready = true;
111 	return STATUS_OK;
112 }
113 
114 
115 //////////////////////////////////////////////////////////////////////////
fadeIn(uint32 sourceColor,uint32 duration,bool system)116 bool BaseFader::fadeIn(uint32 sourceColor, uint32 duration, bool system) {
117 	_ready = false;
118 	_active = true;
119 
120 	_red   = RGBCOLGetR(sourceColor);
121 	_green = RGBCOLGetG(sourceColor);
122 	_blue  = RGBCOLGetB(sourceColor);
123 
124 	_sourceAlpha = RGBCOLGetA(sourceColor);
125 	_targetAlpha = 0;
126 
127 	_duration = duration;
128 	_system = system;
129 
130 	if (_system) {
131 		_startTime = g_system->getMillis();
132 	} else {
133 		_startTime = BaseEngine::getTimer()->getTime();
134 	}
135 
136 	return STATUS_OK;
137 }
138 
139 
140 //////////////////////////////////////////////////////////////////////////
fadeOut(uint32 targetColor,uint32 duration,bool system)141 bool BaseFader::fadeOut(uint32 targetColor, uint32 duration, bool system) {
142 	_ready = false;
143 	_active = true;
144 
145 	_red   = RGBCOLGetR(targetColor);
146 	_green = RGBCOLGetG(targetColor);
147 	_blue  = RGBCOLGetB(targetColor);
148 
149 	//_sourceAlpha = 0;
150 	_sourceAlpha = _currentAlpha;
151 	_targetAlpha = RGBCOLGetA(targetColor);
152 
153 	_duration = duration;
154 	_system = system;
155 
156 	if (_system) {
157 		_startTime = g_system->getMillis();
158 	} else {
159 		_startTime = BaseEngine::getTimer()->getTime();
160 	}
161 
162 
163 	return STATUS_OK;
164 }
165 
166 
167 //////////////////////////////////////////////////////////////////////////
getCurrentColor() const168 uint32 BaseFader::getCurrentColor() const {
169 	return BYTETORGBA(_red, _green, _blue, _currentAlpha);
170 }
171 
172 
173 
174 //////////////////////////////////////////////////////////////////////////
persist(BasePersistenceManager * persistMgr)175 bool BaseFader::persist(BasePersistenceManager *persistMgr) {
176 	BaseObject::persist(persistMgr);
177 
178 	persistMgr->transferBool(TMEMBER(_active));
179 	persistMgr->transferByte(TMEMBER(_blue));
180 	persistMgr->transferByte(TMEMBER(_currentAlpha));
181 	persistMgr->transferUint32(TMEMBER(_duration));
182 	persistMgr->transferByte(TMEMBER(_green));
183 	persistMgr->transferByte(TMEMBER(_red));
184 	persistMgr->transferByte(TMEMBER(_sourceAlpha));
185 	persistMgr->transferUint32(TMEMBER(_startTime));
186 	persistMgr->transferByte(TMEMBER(_targetAlpha));
187 	persistMgr->transferBool(TMEMBER(_system));
188 
189 	if (_system && !persistMgr->getIsSaving()) {
190 		_startTime = 0;
191 	}
192 
193 	return STATUS_OK;
194 }
195 
196 } // End of namespace Wintermute
197