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 #include "graphics/palette.h"
24 #include "video/smk_decoder.h"
25 #include "neverhood/screen.h"
26 
27 namespace Neverhood {
28 
Screen(NeverhoodEngine * vm)29 Screen::Screen(NeverhoodEngine *vm)
30 	: _vm(vm), _paletteData(NULL), _paletteChanged(false), _smackerDecoder(NULL),
31 	_yOffset(0), _fullRefresh(false), _frameDelay(0), _savedSmackerDecoder(NULL),
32 	_savedFrameDelay(0), _savedYOffset(0) {
33 
34 	_ticks = _vm->_system->getMillis();
35 
36 	_backScreen = new Graphics::Surface();
37 	_backScreen->create(640, 480, Graphics::PixelFormat::createFormatCLUT8());
38 
39 	_renderQueue = new RenderQueue();
40 	_prevRenderQueue = new RenderQueue();
41 	_microTiles = new MicroTileArray(640, 480);
42 
43 }
44 
~Screen()45 Screen::~Screen() {
46 	delete _microTiles;
47 	delete _renderQueue;
48 	delete _prevRenderQueue;
49 	_backScreen->free();
50 	delete _backScreen;
51 }
52 
update()53 void Screen::update() {
54 	_ticks = _vm->_system->getMillis();
55 	updatePalette();
56 
57 	if (_fullRefresh) {
58 		// NOTE When playing a fullscreen/doubled Smacker video usually a full screen refresh is needed
59 		_vm->_system->copyRectToScreen((const byte*)_backScreen->getPixels(), _backScreen->pitch, 0, 0, 640, 480);
60 		_fullRefresh = false;
61 		return;
62 	}
63 
64 	_microTiles->clear();
65 
66 	for (RenderQueue::iterator it = _renderQueue->begin(); it != _renderQueue->end(); ++it) {
67 		RenderItem &renderItem = (*it);
68 		renderItem._refresh = true;
69 		for (RenderQueue::iterator jt = _prevRenderQueue->begin(); jt != _prevRenderQueue->end(); ++jt) {
70 			RenderItem &prevRenderItem = (*jt);
71 			if (prevRenderItem == renderItem) {
72 				prevRenderItem._refresh = false;
73 				renderItem._refresh = false;
74 			}
75 		}
76 	}
77 
78 	for (RenderQueue::iterator jt = _prevRenderQueue->begin(); jt != _prevRenderQueue->end(); ++jt) {
79 		RenderItem &prevRenderItem = (*jt);
80 		if (prevRenderItem._refresh)
81 			_microTiles->addRect(Common::Rect(prevRenderItem._destX, prevRenderItem._destY, prevRenderItem._destX + prevRenderItem._width, prevRenderItem._destY + prevRenderItem._height));
82 	}
83 
84 	for (RenderQueue::iterator it = _renderQueue->begin(); it != _renderQueue->end(); ++it) {
85 		RenderItem &renderItem = (*it);
86 		if (renderItem._refresh)
87 			_microTiles->addRect(Common::Rect(renderItem._destX, renderItem._destY, renderItem._destX + renderItem._width, renderItem._destY + renderItem._height));
88 		renderItem._refresh = true;
89 	}
90 
91 	RectangleList *updateRects = _microTiles->getRectangles();
92 
93 	for (RenderQueue::iterator it = _renderQueue->begin(); it != _renderQueue->end(); ++it) {
94 		RenderItem &renderItem = (*it);
95 		for (RectangleList::iterator ri = updateRects->begin(); ri != updateRects->end(); ++ri)
96 			blitRenderItem(renderItem, *ri);
97 	}
98 
99 	SWAP(_renderQueue, _prevRenderQueue);
100 	_renderQueue->clear();
101 
102 	for (RectangleList::iterator ri = updateRects->begin(); ri != updateRects->end(); ++ri) {
103 		Common::Rect &r = *ri;
104 		_vm->_system->copyRectToScreen((const byte*)_backScreen->getBasePtr(r.left, r.top), _backScreen->pitch, r.left, r.top, r.width(), r.height());
105 	}
106 
107 	delete updateRects;
108 
109 }
110 
getNextFrameTime()111 uint32 Screen::getNextFrameTime() {
112 	int32 frameDelay = _frameDelay;
113 	if (_smackerDecoder && _smackerDecoder->isVideoLoaded() && !_smackerDecoder->endOfVideo())
114 		frameDelay = _smackerDecoder->getTimeToNextFrame();
115 	int32 waitTicks = frameDelay - (_vm->_system->getMillis() - _ticks);
116 	return _vm->_system->getMillis() + waitTicks;
117 }
118 
saveParams()119 void Screen::saveParams() {
120 	_savedSmackerDecoder = _smackerDecoder;
121 	_savedFrameDelay = _frameDelay;
122 	_savedYOffset = _yOffset;
123 }
124 
restoreParams()125 void Screen::restoreParams() {
126 	_smackerDecoder = _savedSmackerDecoder;
127 	_frameDelay = _savedFrameDelay;
128 	_yOffset = _savedYOffset;
129 }
130 
setFps(int fps)131 void Screen::setFps(int fps) {
132 	_frameDelay = 1000 / fps;
133 }
134 
getFps()135 int Screen::getFps() {
136 	return 1000 / _frameDelay;
137 }
138 
setYOffset(int16 yOffset)139 void Screen::setYOffset(int16 yOffset) {
140 	_yOffset = yOffset;
141 }
142 
getYOffset()143 int16 Screen::getYOffset() {
144 	return _yOffset;
145 }
146 
setPaletteData(byte * paletteData)147 void Screen::setPaletteData(byte *paletteData) {
148 	_paletteChanged = true;
149 	_paletteData = paletteData;
150 }
151 
unsetPaletteData(byte * paletteData)152 void Screen::unsetPaletteData(byte *paletteData) {
153 	if (_paletteData == paletteData) {
154 		_paletteChanged = false;
155 		_paletteData = NULL;
156 	}
157 }
158 
testPalette(byte * paletteData)159 void Screen::testPalette(byte *paletteData) {
160 	if (_paletteData == paletteData)
161 		_paletteChanged = true;
162 }
163 
updatePalette()164 void Screen::updatePalette() {
165 	if (_paletteChanged && _paletteData) {
166 		byte *tempPalette = new byte[768];
167 		for (int i = 0; i < 256; i++) {
168 			tempPalette[i * 3 + 0] = _paletteData[i * 4 + 0];
169 			tempPalette[i * 3 + 1] = _paletteData[i * 4 + 1];
170 			tempPalette[i * 3 + 2] = _paletteData[i * 4 + 2];
171 		}
172 		_vm->_system->getPaletteManager()->setPalette(tempPalette, 0, 256);
173 		delete[] tempPalette;
174 		_paletteChanged = false;
175 	}
176 }
177 
clear()178 void Screen::clear() {
179 	memset(_backScreen->getPixels(), 0, _backScreen->pitch * _backScreen->h);
180 	_fullRefresh = true;
181 	clearRenderQueue();
182 }
183 
clearRenderQueue()184 void Screen::clearRenderQueue() {
185 	_renderQueue->clear();
186 	_prevRenderQueue->clear();
187 }
188 
drawSurface2(const Graphics::Surface * surface,NDrawRect & drawRect,NRect & clipRect,bool transparent,byte version,const Graphics::Surface * shadowSurface)189 void Screen::drawSurface2(const Graphics::Surface *surface, NDrawRect &drawRect, NRect &clipRect, bool transparent, byte version,
190 	const Graphics::Surface *shadowSurface) {
191 
192 	int16 destX, destY;
193 	NRect ddRect;
194 
195 	if (drawRect.x + drawRect.width >= clipRect.x2)
196 		ddRect.x2 = clipRect.x2 - drawRect.x;
197 	else
198 		ddRect.x2 = drawRect.width;
199 
200 	if (drawRect.x < clipRect.x1) {
201 		destX = clipRect.x1;
202 		ddRect.x1 = clipRect.x1 - drawRect.x;
203 	} else {
204 		destX = drawRect.x;
205 		ddRect.x1 = 0;
206 	}
207 
208 	if (drawRect.y + drawRect.height >= clipRect.y2)
209 		ddRect.y2 = clipRect.y2 - drawRect.y;
210 	else
211 		ddRect.y2 = drawRect.height;
212 
213 	if (drawRect.y < clipRect.y1) {
214 		destY = clipRect.y1;
215 		ddRect.y1 = clipRect.y1 - drawRect.y;
216 	} else {
217 		destY = drawRect.y;
218 		ddRect.y1 = 0;
219 	}
220 
221 	queueBlit(surface, destX, destY, ddRect, transparent, version, shadowSurface);
222 
223 }
224 
drawSurface3(const Graphics::Surface * surface,int16 x,int16 y,NDrawRect & drawRect,NRect & clipRect,bool transparent,byte version)225 void Screen::drawSurface3(const Graphics::Surface *surface, int16 x, int16 y, NDrawRect &drawRect, NRect &clipRect, bool transparent, byte version) {
226 
227 	int16 destX, destY;
228 	NRect ddRect;
229 
230 	if (x + drawRect.width >= clipRect.x2)
231 		ddRect.x2 = clipRect.x2 - drawRect.x - x;
232 	else
233 		ddRect.x2 = drawRect.x + drawRect.width;
234 
235 	if (x < clipRect.x1) {
236 		destX = clipRect.x1;
237 		ddRect.x1 = clipRect.x1 + drawRect.x - x;
238 	} else {
239 		destX = x;
240 		ddRect.x1 = drawRect.x;
241 	}
242 
243 	if (y + drawRect.height >= clipRect.y2)
244 		ddRect.y2 = clipRect.y2 + drawRect.y - y;
245 	else
246 		ddRect.y2 = drawRect.y + drawRect.height;
247 
248 	if (y < clipRect.y1) {
249 		destY = clipRect.y1;
250 		ddRect.y1 = clipRect.y1 + drawRect.y - y;
251 	} else {
252 		destY = y;
253 		ddRect.y1 = drawRect.y;
254 	}
255 
256 	queueBlit(surface, destX, destY, ddRect, transparent, version);
257 
258 }
259 
drawDoubleSurface2(const Graphics::Surface * surface,NDrawRect & drawRect)260 void Screen::drawDoubleSurface2(const Graphics::Surface *surface, NDrawRect &drawRect) {
261 
262 	const byte *source = (const byte*)surface->getPixels();
263 	byte *dest = (byte*)_backScreen->getBasePtr(drawRect.x, drawRect.y);
264 
265 	for (int16 yc = 0; yc < surface->h; yc++) {
266 		byte *row = dest;
267 		for (int16 xc = 0; xc < surface->w; xc++) {
268 			*row++ = *source;
269 			*row++ = *source++;
270 		}
271 		memcpy(dest + _backScreen->pitch, dest, surface->w * 2);
272 		dest += _backScreen->pitch;
273 		dest += _backScreen->pitch;
274 	}
275 
276 	_fullRefresh = true; // See Screen::update
277 
278 }
279 
drawUnk(const Graphics::Surface * surface,NDrawRect & drawRect,NDrawRect & sysRect,NRect & clipRect,bool transparent,byte version)280 void Screen::drawUnk(const Graphics::Surface *surface, NDrawRect &drawRect, NDrawRect &sysRect, NRect &clipRect, bool transparent, byte version) {
281 
282 	int16 x, y;
283 	bool xflag, yflag;
284 	NDrawRect newDrawRect;
285 
286 	x = sysRect.x;
287 	if (sysRect.width <= x || -sysRect.width >= x)
288 		x = x % sysRect.width;
289 	if (x < 0)
290 		x += sysRect.width;
291 
292 	y = sysRect.y;
293 	if (y >= sysRect.height || -sysRect.height >= y)
294 		y = y % sysRect.height;
295 	if (y < 0)
296 		y += sysRect.height;
297 
298 	xflag = x <= 0;
299 	yflag = y <= 0;
300 
301 	newDrawRect.x = x;
302 	newDrawRect.width = sysRect.width - x;
303 	if (drawRect.width < newDrawRect.width) {
304 		xflag = true;
305 		newDrawRect.width = drawRect.width;
306 	}
307 
308 	newDrawRect.y = y;
309 	newDrawRect.height = sysRect.height - y;
310 	if (drawRect.height < newDrawRect.height) {
311 		yflag = true;
312 		newDrawRect.height = drawRect.height;
313 	}
314 
315 	drawSurface3(surface, drawRect.x, drawRect.y, newDrawRect, clipRect, transparent, version);
316 
317 	if (!xflag) {
318 		newDrawRect.x = 0;
319 		newDrawRect.y = y;
320 		newDrawRect.width = x + drawRect.width - sysRect.width;
321 		newDrawRect.height = sysRect.height - y;
322 		if (drawRect.height < newDrawRect.height)
323 			newDrawRect.height = drawRect.height;
324 		drawSurface3(surface, sysRect.width + drawRect.x - x, drawRect.y, newDrawRect, clipRect, transparent, version);
325 	}
326 
327 	if (!yflag) {
328 		newDrawRect.x = x;
329 		newDrawRect.y = 0;
330 		newDrawRect.width = sysRect.width - x;
331 		newDrawRect.height = y + drawRect.height - sysRect.height;
332 		if (drawRect.width < newDrawRect.width)
333 			newDrawRect.width = drawRect.width;
334 		drawSurface3(surface, drawRect.x, sysRect.height + drawRect.y - y, newDrawRect, clipRect, transparent, version);
335 	}
336 
337 	if (!xflag && !yflag) {
338 		newDrawRect.x = 0;
339 		newDrawRect.y = 0;
340 		newDrawRect.width = x + drawRect.width - sysRect.width;
341 		newDrawRect.height = y + drawRect.height - sysRect.height;
342 		drawSurface3(surface, sysRect.width + drawRect.x - x, sysRect.height + drawRect.y - y, newDrawRect, clipRect, transparent, version);
343 	}
344 
345 }
346 
drawSurfaceClipRects(const Graphics::Surface * surface,NDrawRect & drawRect,NRect * clipRects,uint clipRectsCount,bool transparent,byte version)347 void Screen::drawSurfaceClipRects(const Graphics::Surface *surface, NDrawRect &drawRect, NRect *clipRects, uint clipRectsCount, bool transparent, byte version) {
348 	NDrawRect clipDrawRect(0, 0, drawRect.width, drawRect.height);
349 	for (uint i = 0; i < clipRectsCount; i++)
350 		drawSurface3(surface, drawRect.x, drawRect.y, clipDrawRect, clipRects[i], transparent, version);
351 }
352 
queueBlit(const Graphics::Surface * surface,int16 destX,int16 destY,NRect & ddRect,bool transparent,byte version,const Graphics::Surface * shadowSurface)353 void Screen::queueBlit(const Graphics::Surface *surface, int16 destX, int16 destY, NRect &ddRect, bool transparent, byte version,
354 	const Graphics::Surface *shadowSurface) {
355 
356 	const int width = ddRect.x2 - ddRect.x1;
357 	const int height = ddRect.y2 - ddRect.y1;
358 
359 	if (width <= 0 || height <= 0)
360 		return;
361 
362 	RenderItem renderItem;
363 	renderItem._surface = surface;
364 	renderItem._shadowSurface = shadowSurface;
365 	renderItem._destX = destX;
366 	renderItem._destY = destY;
367 	renderItem._srcX = ddRect.x1;
368 	renderItem._srcY = ddRect.y1;
369 	renderItem._width = width;
370 	renderItem._height = height;
371 	renderItem._transparent = transparent;
372 	renderItem._version = version;
373 	_renderQueue->push_back(renderItem);
374 
375 }
376 
blitRenderItem(const RenderItem & renderItem,const Common::Rect & clipRect)377 void Screen::blitRenderItem(const RenderItem &renderItem, const Common::Rect &clipRect) {
378 
379 	const Graphics::Surface *surface = renderItem._surface;
380 	const Graphics::Surface *shadowSurface = renderItem._shadowSurface;
381 	const int16 x0 = MAX<int16>(clipRect.left, renderItem._destX);
382 	const int16 y0 = MAX<int16>(clipRect.top, renderItem._destY);
383 	const int16 x1 = MIN<int16>(clipRect.right, renderItem._destX + renderItem._width);
384 	const int16 y1 = MIN<int16>(clipRect.bottom, renderItem._destY + renderItem._height);
385 	const int16 width = x1 - x0;
386 	int16 height = y1 - y0;
387 
388 	if (width < 0 || height < 0)
389 		return;
390 
391 	const byte *source = (const byte*)surface->getBasePtr(renderItem._srcX + x0 - renderItem._destX, renderItem._srcY + y0 - renderItem._destY);
392 	byte *dest = (byte*)_backScreen->getBasePtr(x0, y0);
393 
394 	if (shadowSurface) {
395 		const byte *shadowSource = (const byte*)shadowSurface->getBasePtr(x0, y0);
396 		while (height--) {
397 			for (int xc = 0; xc < width; xc++)
398 				if (source[xc] != 0)
399 					dest[xc] = shadowSource[xc];
400 			source += surface->pitch;
401 			shadowSource += shadowSurface->pitch;
402 			dest += _backScreen->pitch;
403 		}
404 	} else if (!renderItem._transparent) {
405 		while (height--) {
406 			memcpy(dest, source, width);
407 			source += surface->pitch;
408 			dest += _backScreen->pitch;
409 		}
410 	} else {
411 		while (height--) {
412 			for (int xc = 0; xc < width; xc++)
413 				if (source[xc] != 0)
414 					dest[xc] = source[xc];
415 			source += surface->pitch;
416 			dest += _backScreen->pitch;
417 		}
418 	}
419 
420 }
421 
422 } // End of namespace Neverhood
423