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 "sci/sci.h"
24 #include "sci/engine/features.h"
25 #include "sci/engine/state.h"
26 #include "sci/engine/selector.h"
27 #include "sci/engine/workarounds.h"
28 #include "sci/graphics/cache.h"
29 #include "sci/graphics/coordadjuster.h"
30 #include "sci/graphics/ports.h"
31 #include "sci/graphics/paint16.h"
32 #include "sci/graphics/animate.h"
33 #include "sci/graphics/font.h"
34 #include "sci/graphics/picture.h"
35 #include "sci/graphics/view.h"
36 #include "sci/graphics/screen.h"
37 #include "sci/graphics/palette.h"
38 #include "sci/graphics/portrait.h"
39 #include "sci/graphics/text16.h"
40 #include "sci/graphics/transitions.h"
41 
42 namespace Sci {
43 
GfxPaint16(ResourceManager * resMan,SegManager * segMan,GfxCache * cache,GfxPorts * ports,GfxCoordAdjuster16 * coordAdjuster,GfxScreen * screen,GfxPalette * palette,GfxTransitions * transitions,AudioPlayer * audio)44 GfxPaint16::GfxPaint16(ResourceManager *resMan, SegManager *segMan, GfxCache *cache, GfxPorts *ports, GfxCoordAdjuster16 *coordAdjuster, GfxScreen *screen, GfxPalette *palette, GfxTransitions *transitions, AudioPlayer *audio)
45 	: _resMan(resMan), _segMan(segMan), _cache(cache), _ports(ports),
46 	  _coordAdjuster(coordAdjuster), _screen(screen), _palette(palette),
47 	  _transitions(transitions), _audio(audio), _EGAdrawingVisualize(false) {
48 
49 	// _animate and _text16 will be initialized later on
50 	_animate = NULL;
51 	_text16 = NULL;
52 }
53 
~GfxPaint16()54 GfxPaint16::~GfxPaint16() {
55 }
56 
init(GfxAnimate * animate,GfxText16 * text16)57 void GfxPaint16::init(GfxAnimate *animate, GfxText16 *text16) {
58 	_animate = animate;
59 	_text16 = text16;
60 }
61 
debugSetEGAdrawingVisualize(bool state)62 void GfxPaint16::debugSetEGAdrawingVisualize(bool state) {
63 	_EGAdrawingVisualize = state;
64 }
65 
drawPicture(GuiResourceId pictureId,int16 animationNr,bool mirroredFlag,bool addToFlag,GuiResourceId paletteId)66 void GfxPaint16::drawPicture(GuiResourceId pictureId, int16 animationNr, bool mirroredFlag, bool addToFlag, GuiResourceId paletteId) {
67 	GfxPicture *picture = new GfxPicture(_resMan, _coordAdjuster, _ports, _screen, _palette, pictureId, _EGAdrawingVisualize);
68 
69 	// do we add to a picture? if not -> clear screen with white
70 	if (!addToFlag)
71 		clearScreen(_screen->getColorWhite());
72 
73 	picture->draw(animationNr, mirroredFlag, addToFlag, paletteId);
74 	delete picture;
75 
76 	// We make a call to SciPalette here, for increasing sys timestamp and also loading targetpalette, if palvary active
77 	//  (SCI1.1 only)
78 	if (getSciVersion() == SCI_VERSION_1_1)
79 		_palette->drewPicture(pictureId);
80 }
81 
82 // This one is the only one that updates screen!
drawCelAndShow(GuiResourceId viewId,int16 loopNo,int16 celNo,uint16 leftPos,uint16 topPos,byte priority,uint16 paletteNo,uint16 scaleX,uint16 scaleY,uint16 scaleSignal)83 void GfxPaint16::drawCelAndShow(GuiResourceId viewId, int16 loopNo, int16 celNo, uint16 leftPos, uint16 topPos, byte priority, uint16 paletteNo, uint16 scaleX, uint16 scaleY, uint16 scaleSignal) {
84 	GfxView *view = _cache->getView(viewId);
85 	Common::Rect celRect;
86 
87 	if (view) {
88 		celRect.left = leftPos;
89 		celRect.top = topPos;
90 		celRect.right = celRect.left + view->getWidth(loopNo, celNo);
91 		celRect.bottom = celRect.top + view->getHeight(loopNo, celNo);
92 
93 		drawCel(view, loopNo, celNo, celRect, priority, paletteNo, scaleX, scaleY, scaleSignal);
94 
95 		if (getSciVersion() >= SCI_VERSION_1_1) {
96 			if (!_screen->_picNotValidSci11) {
97 				bitsShow(celRect);
98 			}
99 		} else {
100 			if (!_screen->_picNotValid)
101 				bitsShow(celRect);
102 		}
103 	}
104 }
105 
106 // This version of drawCel is not supposed to call BitsShow()!
drawCel(GuiResourceId viewId,int16 loopNo,int16 celNo,const Common::Rect & celRect,byte priority,uint16 paletteNo,uint16 scaleX,uint16 scaleY,uint16 scaleSignal)107 void GfxPaint16::drawCel(GuiResourceId viewId, int16 loopNo, int16 celNo, const Common::Rect &celRect, byte priority, uint16 paletteNo, uint16 scaleX, uint16 scaleY, uint16 scaleSignal) {
108 	drawCel(_cache->getView(viewId), loopNo, celNo, celRect, priority, paletteNo, scaleX, scaleY, scaleSignal);
109 }
110 
111 // This version of drawCel is not supposed to call BitsShow()!
drawCel(GfxView * view,int16 loopNo,int16 celNo,const Common::Rect & celRect,byte priority,uint16 paletteNo,uint16 scaleX,uint16 scaleY,uint16 scaleSignal)112 void GfxPaint16::drawCel(GfxView *view, int16 loopNo, int16 celNo, const Common::Rect &celRect, byte priority, uint16 paletteNo, uint16 scaleX, uint16 scaleY, uint16 scaleSignal) {
113 	Common::Rect clipRect = celRect;
114 	clipRect.clip(_ports->_curPort->rect);
115 	if (clipRect.isEmpty()) // nothing to draw
116 		return;
117 
118 	Common::Rect clipRectTranslated = clipRect;
119 	_ports->offsetRect(clipRectTranslated);
120 	if (scaleX == 128 && scaleY == 128)
121 		view->draw(celRect, clipRect, clipRectTranslated, loopNo, celNo, priority, paletteNo, false, scaleSignal);
122 	else
123 		view->drawScaled(celRect, clipRect, clipRectTranslated, loopNo, celNo, priority, scaleX, scaleY, scaleSignal);
124 }
125 
126 // This is used as replacement for drawCelAndShow() when hires-cels are drawn to
127 // screen. Hires-cels are available only SCI 1.1+.
drawHiresCelAndShow(GuiResourceId viewId,int16 loopNo,int16 celNo,uint16 leftPos,uint16 topPos,byte priority,uint16 paletteNo,reg_t upscaledHiresHandle,uint16 scaleX,uint16 scaleY)128 void GfxPaint16::drawHiresCelAndShow(GuiResourceId viewId, int16 loopNo, int16 celNo, uint16 leftPos, uint16 topPos, byte priority, uint16 paletteNo, reg_t upscaledHiresHandle, uint16 scaleX, uint16 scaleY) {
129 	GfxView *view = _cache->getView(viewId);
130 	Common::Rect celRect, curPortRect, clipRect, clipRectTranslated;
131 	Common::Point curPortPos;
132 	bool upscaledHiresHack = false;
133 
134 	if (view) {
135 		if ((leftPos == 0) && (topPos == 0)) {
136 			// HACK: in kq6, we get leftPos&topPos == 0 SOMETIMES, that's why we
137 			// need to get coordinates from upscaledHiresHandle. I'm not sure if
138 			// this is what we are supposed to do or if there is some other bug
139 			// that actually makes coordinates to be 0 in the first place.
140 			byte *memoryPtr = NULL;
141 			memoryPtr = _segMan->getHunkPointer(upscaledHiresHandle);
142 			if (memoryPtr) {
143 				Common::Rect upscaledHiresRect;
144 				_screen->bitsGetRect(memoryPtr, &upscaledHiresRect);
145 				leftPos = upscaledHiresRect.left;
146 				topPos = upscaledHiresRect.top;
147 				upscaledHiresHack = true;
148 			}
149 		}
150 
151 		celRect.left = leftPos;
152 		celRect.top = topPos;
153 		celRect.right = celRect.left + view->getWidth(loopNo, celNo);
154 		celRect.bottom = celRect.top + view->getHeight(loopNo, celNo);
155 		// adjust curPort to upscaled hires
156 		clipRect = celRect;
157 		curPortRect = _ports->_curPort->rect;
158 		view->adjustToUpscaledCoordinates(curPortRect.top, curPortRect.left);
159 		view->adjustToUpscaledCoordinates(curPortRect.bottom, curPortRect.right);
160 		curPortRect.bottom++;
161 		curPortRect.right++;
162 		clipRect.clip(curPortRect);
163 		if (clipRect.isEmpty()) // nothing to draw
164 			return;
165 
166 		clipRectTranslated = clipRect;
167 		if (!upscaledHiresHack) {
168 			curPortPos.x = _ports->_curPort->left; curPortPos.y = _ports->_curPort->top;
169 			view->adjustToUpscaledCoordinates(curPortPos.y, curPortPos.x);
170 			clipRectTranslated.top += curPortPos.y; clipRectTranslated.bottom += curPortPos.y;
171 			clipRectTranslated.left += curPortPos.x; clipRectTranslated.right += curPortPos.x;
172 		}
173 
174 		view->draw(celRect, clipRect, clipRectTranslated, loopNo, celNo, priority, paletteNo, true);
175 		if (!_screen->_picNotValidSci11) {
176 			_screen->copyDisplayRectToScreen(clipRectTranslated);
177 		}
178 	}
179 }
180 
clearScreen(byte color)181 void GfxPaint16::clearScreen(byte color) {
182 	fillRect(_ports->_curPort->rect, GFX_SCREEN_MASK_ALL, color, 0, 0);
183 }
184 
invertRect(const Common::Rect & rect)185 void GfxPaint16::invertRect(const Common::Rect &rect) {
186 	int16 oldpenmode = _ports->_curPort->penMode;
187 	_ports->_curPort->penMode = 2;
188 	fillRect(rect, GFX_SCREEN_MASK_VISUAL, _ports->_curPort->penClr, _ports->_curPort->backClr);
189 	_ports->_curPort->penMode = oldpenmode;
190 }
191 
192 // used in SCI0early exclusively
invertRectViaXOR(const Common::Rect & rect)193 void GfxPaint16::invertRectViaXOR(const Common::Rect &rect) {
194 	Common::Rect r = rect;
195 	int16 x, y;
196 	byte curVisual;
197 
198 	r.clip(_ports->_curPort->rect);
199 	if (r.isEmpty()) // nothing to invert
200 		return;
201 
202 	_ports->offsetRect(r);
203 	for (y = r.top; y < r.bottom; y++) {
204 		for (x = r.left; x < r.right; x++) {
205 			curVisual = _screen->getVisual(x, y);
206 			_screen->putPixel(x, y, GFX_SCREEN_MASK_VISUAL, curVisual ^ 0x0f, 0, 0);
207 		}
208 	}
209 }
210 
eraseRect(const Common::Rect & rect)211 void GfxPaint16::eraseRect(const Common::Rect &rect) {
212 	fillRect(rect, GFX_SCREEN_MASK_VISUAL, _ports->_curPort->backClr);
213 }
214 
paintRect(const Common::Rect & rect)215 void GfxPaint16::paintRect(const Common::Rect &rect) {
216 	fillRect(rect, GFX_SCREEN_MASK_VISUAL, _ports->_curPort->penClr);
217 }
218 
fillRect(const Common::Rect & rect,int16 drawFlags,byte color,byte priority,byte control)219 void GfxPaint16::fillRect(const Common::Rect &rect, int16 drawFlags, byte color, byte priority, byte control) {
220 	Common::Rect r = rect;
221 	r.clip(_ports->_curPort->rect);
222 	if (r.isEmpty()) // nothing to fill
223 		return;
224 
225 	int16 oldPenMode = _ports->_curPort->penMode;
226 	_ports->offsetRect(r);
227 	int16 x, y;
228 	byte curVisual;
229 
230 	// Doing visual first
231 	if (drawFlags & GFX_SCREEN_MASK_VISUAL) {
232 		if (oldPenMode == 2) { // invert mode
233 			for (y = r.top; y < r.bottom; y++) {
234 				for (x = r.left; x < r.right; x++) {
235 					curVisual = _screen->getVisual(x, y);
236 					if (curVisual == color) {
237 						_screen->putPixel(x, y, GFX_SCREEN_MASK_VISUAL, priority, 0, 0);
238 					} else if (curVisual == priority) {
239 						_screen->putPixel(x, y, GFX_SCREEN_MASK_VISUAL, color, 0, 0);
240 					}
241 				}
242 			}
243 		} else { // just fill rect with color
244 			for (y = r.top; y < r.bottom; y++) {
245 				for (x = r.left; x < r.right; x++) {
246 					_screen->putPixel(x, y, GFX_SCREEN_MASK_VISUAL, color, 0, 0);
247 				}
248 			}
249 		}
250 	}
251 
252 	if (drawFlags < 2)
253 		return;
254 	drawFlags &= GFX_SCREEN_MASK_PRIORITY|GFX_SCREEN_MASK_CONTROL;
255 
256 	// we need to isolate the bits, sierra sci saved priority and control inside one byte, we don't
257 	priority &= 0x0f;
258 	control &= 0x0f;
259 
260 	if (oldPenMode != 2) {
261 		for (y = r.top; y < r.bottom; y++) {
262 			for (x = r.left; x < r.right; x++) {
263 				_screen->putPixel(x, y, drawFlags, 0, priority, control);
264 			}
265 		}
266 	} else {
267 		for (y = r.top; y < r.bottom; y++) {
268 			for (x = r.left; x < r.right; x++) {
269 				_screen->putPixel(x, y, drawFlags, 0, !_screen->getPriority(x, y), !_screen->getControl(x, y));
270 			}
271 		}
272 	}
273 }
274 
frameRect(const Common::Rect & rect)275 void GfxPaint16::frameRect(const Common::Rect &rect) {
276 	Common::Rect r = rect;
277 	// left
278 	r.right = rect.left + 1;
279 	paintRect(r);
280 	// right
281 	r.right = rect.right;
282 	r.left = rect.right - 1;
283 	paintRect(r);
284 	//top
285 	r.left = rect.left;
286 	r.bottom = rect.top + 1;
287 	paintRect(r);
288 	//bottom
289 	r.bottom = rect.bottom;
290 	r.top = rect.bottom - 1;
291 	paintRect(r);
292 }
293 
bitsShow(const Common::Rect & rect)294 void GfxPaint16::bitsShow(const Common::Rect &rect) {
295 	Common::Rect workerRect(rect.left, rect.top, rect.right, rect.bottom);
296 	workerRect.clip(_ports->_curPort->rect);
297 	if (workerRect.isEmpty()) // nothing to show
298 		return;
299 
300 	_ports->offsetRect(workerRect);
301 
302 	// We adjust the left/right coordinates to even coordinates
303 	workerRect.left &= 0xFFFE; // round down
304 	workerRect.right = (workerRect.right + 1) & 0xFFFE; // round up
305 
306 	_screen->copyRectToScreen(workerRect);
307 }
308 
bitsShowHires(const Common::Rect & rect)309 void GfxPaint16::bitsShowHires(const Common::Rect &rect) {
310 	_screen->copyDisplayRectToScreen(rect);
311 }
312 
bitsSave(const Common::Rect & rect,byte screenMask)313 reg_t GfxPaint16::bitsSave(const Common::Rect &rect, byte screenMask) {
314 	reg_t memoryId;
315 	byte *memoryPtr;
316 	int size;
317 
318 	Common::Rect workerRect(rect.left, rect.top, rect.right, rect.bottom);
319 	workerRect.clip(_ports->_curPort->rect);
320 	if (workerRect.isEmpty()) // nothing to save
321 		return NULL_REG;
322 
323 	if (screenMask == GFX_SCREEN_MASK_DISPLAY) {
324 		// The coordinates we are given are actually up-to-including right/bottom - we extend accordingly
325 		workerRect.bottom++;
326 		workerRect.right++;
327 		// Adjust rect to upscaled hires, but dont adjust according to port
328 		_screen->adjustToUpscaledCoordinates(workerRect.top, workerRect.left);
329 		_screen->adjustToUpscaledCoordinates(workerRect.bottom, workerRect.right);
330 	} else {
331 		_ports->offsetRect(workerRect);
332 	}
333 
334 	// now actually ask _screen how much space it will need for saving
335 	size = _screen->bitsGetDataSize(workerRect, screenMask);
336 
337 	memoryId = _segMan->allocateHunkEntry("SaveBits()", size);
338 	memoryPtr = _segMan->getHunkPointer(memoryId);
339 	if (memoryPtr)
340 		_screen->bitsSave(workerRect, screenMask, memoryPtr);
341 	return memoryId;
342 }
343 
bitsGetRect(reg_t memoryHandle,Common::Rect * destRect)344 void GfxPaint16::bitsGetRect(reg_t memoryHandle, Common::Rect *destRect) {
345 	byte *memoryPtr = NULL;
346 
347 	if (!memoryHandle.isNull()) {
348 		memoryPtr = _segMan->getHunkPointer(memoryHandle);
349 
350 		if (memoryPtr) {
351 			_screen->bitsGetRect(memoryPtr, destRect);
352 		}
353 	}
354 }
355 
bitsRestore(reg_t memoryHandle)356 void GfxPaint16::bitsRestore(reg_t memoryHandle) {
357 	byte *memoryPtr = NULL;
358 
359 	if (!memoryHandle.isNull()) {
360 		memoryPtr = _segMan->getHunkPointer(memoryHandle);
361 
362 		if (memoryPtr) {
363 			_screen->bitsRestore(memoryPtr);
364 			bitsFree(memoryHandle);
365 		}
366 	}
367 }
368 
bitsFree(reg_t memoryHandle)369 void GfxPaint16::bitsFree(reg_t memoryHandle) {
370 	if (!memoryHandle.isNull())	// happens in KQ5CD
371 		_segMan->freeHunkEntry(memoryHandle);
372 }
373 
kernelDrawPicture(GuiResourceId pictureId,int16 animationNr,bool animationBlackoutFlag,bool mirroredFlag,bool addToFlag,int16 EGApaletteNo)374 void GfxPaint16::kernelDrawPicture(GuiResourceId pictureId, int16 animationNr, bool animationBlackoutFlag, bool mirroredFlag, bool addToFlag, int16 EGApaletteNo) {
375 	Port *oldPort = _ports->setPort((Port *)_ports->_picWind);
376 
377 	if (_ports->isFrontWindow(_ports->_picWind)) {
378 		_screen->_picNotValid = 1;
379 		drawPicture(pictureId, animationNr, mirroredFlag, addToFlag, EGApaletteNo);
380 		_transitions->setup(animationNr, animationBlackoutFlag);
381 	} else {
382 		// We need to set it for SCI1EARLY+ (sierra sci also did so), otherwise we get at least the following issues:
383 		//  LSL5 (english) - last wakeup (taj mahal flute dream)
384 		//  SQ5 (english v1.03) - during the scene following the scrubbing
385 		//   in both situations a window is shown when kDrawPic is called, which would result otherwise in
386 		//   no showpic getting called from kAnimate and we would get graphic corruption
387 		// XMAS1990 EGA did not set it in this case, VGA did
388 		if (getSciVersion() >= SCI_VERSION_1_EARLY)
389 			_screen->_picNotValid = 1;
390 		_ports->beginUpdate(_ports->_picWind);
391 		drawPicture(pictureId, animationNr, mirroredFlag, addToFlag, EGApaletteNo);
392 		_ports->endUpdate(_ports->_picWind);
393 	}
394 	_ports->setPort(oldPort);
395 }
396 
kernelDrawCel(GuiResourceId viewId,int16 loopNo,int16 celNo,uint16 leftPos,uint16 topPos,int16 priority,uint16 paletteNo,uint16 scaleX,uint16 scaleY,bool hiresMode,reg_t upscaledHiresHandle)397 void GfxPaint16::kernelDrawCel(GuiResourceId viewId, int16 loopNo, int16 celNo, uint16 leftPos, uint16 topPos, int16 priority, uint16 paletteNo, uint16 scaleX, uint16 scaleY, bool hiresMode, reg_t upscaledHiresHandle) {
398 	// some calls are hiresMode even under kq6 DOS, that's why we check for
399 	// upscaled hires here
400 	if ((!hiresMode) || (!_screen->getUpscaledHires())) {
401 		drawCelAndShow(viewId, loopNo, celNo, leftPos, topPos, priority, paletteNo, scaleX, scaleY);
402 	} else {
403 		drawHiresCelAndShow(viewId, loopNo, celNo, leftPos, topPos, priority, paletteNo, upscaledHiresHandle);
404 	}
405 }
406 
kernelGraphFillBoxForeground(const Common::Rect & rect)407 void GfxPaint16::kernelGraphFillBoxForeground(const Common::Rect &rect) {
408 	paintRect(rect);
409 }
410 
kernelGraphFillBoxBackground(const Common::Rect & rect)411 void GfxPaint16::kernelGraphFillBoxBackground(const Common::Rect &rect) {
412 	eraseRect(rect);
413 }
414 
kernelGraphFillBox(const Common::Rect & rect,uint16 colorMask,int16 color,int16 priority,int16 control)415 void GfxPaint16::kernelGraphFillBox(const Common::Rect &rect, uint16 colorMask, int16 color, int16 priority, int16 control) {
416 	fillRect(rect, colorMask, color, priority, control);
417 }
418 
kernelGraphFrameBox(const Common::Rect & rect,int16 color)419 void GfxPaint16::kernelGraphFrameBox(const Common::Rect &rect, int16 color) {
420 	int16 oldColor = _ports->getPort()->penClr;
421 	_ports->penColor(color);
422 	frameRect(rect);
423 	_ports->penColor(oldColor);
424 }
425 
kernelGraphDrawLine(Common::Point startPoint,Common::Point endPoint,int16 color,int16 priority,int16 control)426 void GfxPaint16::kernelGraphDrawLine(Common::Point startPoint, Common::Point endPoint, int16 color, int16 priority, int16 control) {
427 	_ports->clipLine(startPoint, endPoint);
428 	_ports->offsetLine(startPoint, endPoint);
429 	_screen->drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, color, priority, control);
430 }
431 
kernelGraphSaveBox(const Common::Rect & rect,uint16 screenMask)432 reg_t GfxPaint16::kernelGraphSaveBox(const Common::Rect &rect, uint16 screenMask) {
433 	return bitsSave(rect, screenMask);
434 }
435 
kernelGraphSaveUpscaledHiresBox(const Common::Rect & rect)436 reg_t GfxPaint16::kernelGraphSaveUpscaledHiresBox(const Common::Rect &rect) {
437 	return bitsSave(rect, GFX_SCREEN_MASK_DISPLAY);
438 }
439 
kernelGraphRestoreBox(reg_t handle)440 void GfxPaint16::kernelGraphRestoreBox(reg_t handle) {
441 	bitsRestore(handle);
442 }
443 
kernelGraphUpdateBox(const Common::Rect & rect,bool hiresMode)444 void GfxPaint16::kernelGraphUpdateBox(const Common::Rect &rect, bool hiresMode) {
445 	// some calls are hiresMode even under kq6 DOS, that's why we check for
446 	// upscaled hires here
447 	if ((!hiresMode) || (!_screen->getUpscaledHires()))
448 		bitsShow(rect);
449 	else
450 		bitsShowHires(rect);
451 }
452 
kernelGraphRedrawBox(Common::Rect rect)453 void GfxPaint16::kernelGraphRedrawBox(Common::Rect rect) {
454 	_coordAdjuster->kernelLocalToGlobal(rect.left, rect.top);
455 	_coordAdjuster->kernelLocalToGlobal(rect.right, rect.bottom);
456 	Port *oldPort = _ports->setPort((Port *)_ports->_picWind);
457 	_coordAdjuster->kernelGlobalToLocal(rect.left, rect.top);
458 	_coordAdjuster->kernelGlobalToLocal(rect.right, rect.bottom);
459 
460 	_animate->reAnimate(rect);
461 
462 	_ports->setPort(oldPort);
463 }
464 
465 #define SCI_DISPLAY_MOVEPEN				100
466 #define SCI_DISPLAY_SETALIGNMENT		101
467 #define SCI_DISPLAY_SETPENCOLOR			102
468 #define SCI_DISPLAY_SETBACKGROUNDCOLOR	103
469 #define SCI_DISPLAY_SETGREYEDOUTPUT		104
470 #define SCI_DISPLAY_SETFONT				105
471 #define SCI_DISPLAY_WIDTH				106
472 #define SCI_DISPLAY_SAVEUNDER			107
473 #define SCI_DISPLAY_RESTOREUNDER		108
474 #define SCI_DISPLAY_DONTSHOWBITS		121
475 #define SCI_DISPLAY_SETSTROKE			122
476 
kernelDisplay(const char * text,uint16 languageSplitter,int argc,reg_t * argv)477 reg_t GfxPaint16::kernelDisplay(const char *text, uint16 languageSplitter, int argc, reg_t *argv) {
478 	reg_t displayArg;
479 	TextAlignment alignment = SCI_TEXT16_ALIGNMENT_LEFT;
480 	int16 colorPen = -1, colorBack = -1, width = -1, bRedraw = 1;
481 	bool doSaveUnder = false;
482 	Common::Rect rect;
483 	reg_t result = NULL_REG;
484 	int16 stroke = 0; // Kawa's SCI11+
485 
486 	// Make a "backup" of the port settings (required for some SCI0LATE and
487 	// SCI01+ only)
488 	Port oldPort = *_ports->getPort();
489 
490 	// setting defaults
491 	_ports->penMode(0);
492 	_ports->penColor(0);
493 	_ports->textGreyedOutput(false);
494 	// processing codes in argv
495 	while (argc > 0) {
496 		displayArg = argv[0];
497 		if (displayArg.getSegment())
498 			displayArg.setOffset(0xFFFF);
499 		argc--; argv++;
500 		switch (displayArg.getOffset()) {
501 		case SCI_DISPLAY_MOVEPEN:
502 			_ports->moveTo(argv[0].toUint16(), argv[1].toUint16());
503 			argc -= 2; argv += 2;
504 			break;
505 		case SCI_DISPLAY_SETALIGNMENT:
506 			alignment = argv[0].toSint16();
507 			argc--; argv++;
508 			break;
509 		case SCI_DISPLAY_SETPENCOLOR:
510 			colorPen = argv[0].toUint16();
511 			_ports->penColor(colorPen);
512 			argc--; argv++;
513 			break;
514 		case SCI_DISPLAY_SETBACKGROUNDCOLOR:
515 			colorBack = argv[0].toUint16();
516 			argc--; argv++;
517 			break;
518 		case SCI_DISPLAY_SETGREYEDOUTPUT:
519 			_ports->textGreyedOutput(!argv[0].isNull());
520 			argc--; argv++;
521 			break;
522 		case SCI_DISPLAY_SETFONT:
523 			_text16->SetFont(argv[0].toUint16());
524 			argc--; argv++;
525 			break;
526 		case SCI_DISPLAY_WIDTH:
527 			width = argv[0].toUint16();
528 			argc--; argv++;
529 			break;
530 		case SCI_DISPLAY_SAVEUNDER:
531 			doSaveUnder = true;
532 			break;
533 		case SCI_DISPLAY_RESTOREUNDER:
534 			bitsGetRect(argv[0], &rect);
535 			rect.translate(-_ports->getPort()->left, -_ports->getPort()->top);
536 			bitsRestore(argv[0]);
537 			kernelGraphRedrawBox(rect);
538 			// finishing loop
539 			argc = 0;
540 			break;
541 		case SCI_DISPLAY_DONTSHOWBITS:
542 			bRedraw = 0;
543 			break;
544 		case SCI_DISPLAY_SETSTROKE: // From Kawa's SCI11+
545 			stroke = argv[0].toUint16();
546 			argc--; argv++;
547 			break;
548 
549 		default:
550 			SciCallOrigin originReply;
551 			SciWorkaroundSolution solution = trackOriginAndFindWorkaround(0, kDisplay_workarounds, &originReply);
552 			if (solution.type == WORKAROUND_NONE)
553 				error("Unknown kDisplay argument (%04x:%04x) from %s", PRINT_REG(displayArg), originReply.toString().c_str());
554 			assert(solution.type == WORKAROUND_IGNORE);
555 			break;
556 		}
557 	}
558 
559 	// now drawing the text
560 	_text16->Size(rect, text, languageSplitter, -1, width);
561 	rect.moveTo(_ports->getPort()->curLeft, _ports->getPort()->curTop);
562 	// Note: This code has been found in SCI1 middle and newer games. It was
563 	// previously only for SCI1 late and newer, but the LSL1 interpreter contains
564 	// this code.
565 	if (getSciVersion() >= SCI_VERSION_1_MIDDLE) {
566 		int16 leftPos = rect.right <= _screen->getWidth() ? 0 : _screen->getWidth() - rect.right;
567 		int16 topPos = rect.bottom <= _screen->getHeight() ? 0 : _screen->getHeight() - rect.bottom;
568 		_ports->move(leftPos, topPos);
569 		rect.moveTo(_ports->getPort()->curLeft, _ports->getPort()->curTop);
570 	}
571 
572 	// Kawa's SCI11+
573 	if (stroke)
574 		rect.grow(1);
575 
576 	if (doSaveUnder)
577 		result = bitsSave(rect, GFX_SCREEN_MASK_VISUAL);
578 	if (colorBack != -1)
579 		fillRect(rect, GFX_SCREEN_MASK_VISUAL, colorBack, 0, 0);
580 
581 	// Kawa's SCI11+
582 	if (stroke)	{
583 		_ports->penColor(0);
584 		rect.translate(1, 0); if (stroke & 1) _text16->Box(text, languageSplitter, false, rect, alignment, -1); // right
585 		rect.translate(0, 1); if (stroke & 2) _text16->Box(text, languageSplitter, false, rect, alignment, -1); // bottom right
586 		rect.translate(-1, 0); if (stroke & 4) _text16->Box(text, languageSplitter, false, rect, alignment, -1); // bottom
587 		rect.translate(-1, 0); if (stroke & 8) _text16->Box(text, languageSplitter, false, rect, alignment, -1); // bottom left
588 		rect.translate(0, -1); if (stroke & 16) _text16->Box(text, languageSplitter, false, rect, alignment, -1); // left
589 		rect.translate(0, -1); if (stroke & 32) _text16->Box(text, languageSplitter, false, rect, alignment, -1); // top left
590 		rect.translate(1, 0); if (stroke & 64) _text16->Box(text, languageSplitter, false, rect, alignment, -1); // top
591 		rect.translate(1, 0); if (stroke & 128) _text16->Box(text, languageSplitter, false, rect, alignment, -1); // top right
592 		rect.translate(-1, 1); // and back to center
593 		_ports->penColor(colorPen);
594 	}
595 
596 	_text16->Box(text, languageSplitter, false, rect, alignment, -1);
597 	if (_screen->_picNotValid == 0 && bRedraw)
598 		bitsShow(rect);
599 	// restoring port and cursor pos
600 	Port *currport = _ports->getPort();
601 	uint16 tTop = currport->curTop;
602 	uint16 tLeft = currport->curLeft;
603 	if (!g_sci->_features->usesOldGfxFunctions()) {
604 		// Restore port settings for some SCI0LATE and SCI01+ only.
605 		//
606 		// The change actually happened inbetween .530 (hoyle1) and .566 (heros
607 		// quest). We don't have any detection for that currently, so we are
608 		// using oldGfxFunctions (.502). The only games that could get
609 		// regressions because of this are hoyle1, kq4 and funseeker. If there
610 		// are regressions, we should use interpreter version (which would
611 		// require exe version detection).
612 		//
613 		// If we restore the port for whole SCI0LATE, at least sq3old will get
614 		// an issue - font 0 will get used when scanning for planets instead of
615 		// font 600 - a setfont parameter is missing in one of the kDisplay
616 		// calls in script 19. I assume this is a script bug, because it was
617 		// added in sq3new.
618 		*currport = oldPort;
619 	}
620 	currport->curTop = tTop;
621 	currport->curLeft = tLeft;
622 	return result;
623 }
624 
kernelPortraitLoad(const Common::String & resourceName)625 reg_t GfxPaint16::kernelPortraitLoad(const Common::String &resourceName) {
626 	//Portrait *myPortrait = new Portrait(g_sci->getResMan(), _screen, _palette, resourceName);
627 	return NULL_REG;
628 }
629 
kernelPortraitShow(const Common::String & resourceName,Common::Point position,uint16 resourceId,uint16 noun,uint16 verb,uint16 cond,uint16 seq)630 void GfxPaint16::kernelPortraitShow(const Common::String &resourceName, Common::Point position, uint16 resourceId, uint16 noun, uint16 verb, uint16 cond, uint16 seq) {
631 	Portrait *myPortrait = new Portrait(g_sci->getResMan(), g_sci->getEventManager(), _screen, _palette, _audio, resourceName);
632 	// TODO: cache portraits
633 	// adjust given coordinates to curPort (but dont adjust coordinates on upscaledHires_Save_Box and give us hires coordinates
634 	//  on kDrawCel, yeah this whole stuff makes sense)
635 	position.x += _ports->getPort()->left; position.y += _ports->getPort()->top;
636 	_screen->adjustToUpscaledCoordinates(position.y, position.x);
637 	myPortrait->doit(position, resourceId, noun, verb, cond, seq);
638 	delete myPortrait;
639 }
640 
kernelPortraitUnload(uint16 portraitId)641 void GfxPaint16::kernelPortraitUnload(uint16 portraitId) {
642 }
643 
644 } // End of namespace Sci
645