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
|
/* 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.
*
* $URL$
* $Id$
*
*/
#include "common/util.h"
#include "common/stack.h"
#include "graphics/primitives.h"
#include "sci/sci.h"
#include "sci/engine/state.h"
#include "sci/gui/gui_gfx.h"
#include "sci/gui/gui_animate.h"
#include "sci/gui/gui_font.h"
#include "sci/gui/gui_picture.h"
#include "sci/gui/gui_view.h"
#include "sci/gui/gui_screen.h"
#include "sci/gui/gui_palette.h"
#include "sci/gui/gui_text.h"
namespace Sci {
SciGuiGfx::SciGuiGfx(ResourceManager *resMan, SegManager *segMan, Kernel *kernel, SciGuiScreen *screen, SciGuiPalette *palette)
: _resMan(resMan), _segMan(segMan), _kernel(kernel), _screen(screen), _palette(palette) {
}
SciGuiGfx::~SciGuiGfx() {
purgeCache();
delete _mainPort;
delete _menuPort;
}
void SciGuiGfx::init(SciGuiText *text) {
_text = text;
// _mainPort is not known to windowmanager, that's okay according to sierra sci
// its not even used currently in our engine
_mainPort = new GuiPort(0);
SetPort(_mainPort);
OpenPort(_mainPort);
// _menuPort has actually hardcoded id 0xFFFF. Its not meant to be known to windowmanager according to sierra sci
_menuPort = new GuiPort(0xFFFF);
OpenPort(_menuPort);
_text->SetFont(0);
_menuPort->rect = Common::Rect(0, 0, _screen->_width, _screen->_height);
_menuBarRect = Common::Rect(0, 0, _screen->_width, 9);
}
void SciGuiGfx::purgeCache() {
for (ViewCache::iterator iter = _cachedViews.begin(); iter != _cachedViews.end(); ++iter) {
delete iter->_value;
iter->_value = 0;
}
_cachedViews.clear();
}
SciGuiView *SciGuiGfx::getView(GuiResourceId viewNum) {
if (_cachedViews.size() >= MAX_CACHED_VIEWS)
purgeCache();
if (!_cachedViews.contains(viewNum))
_cachedViews[viewNum] = new SciGuiView(_resMan, _screen, _palette, viewNum);
return _cachedViews[viewNum];
}
GuiPort *SciGuiGfx::SetPort(GuiPort *newPort) {
GuiPort *oldPort = _curPort;
_curPort = newPort;
return oldPort;
}
GuiPort *SciGuiGfx::GetPort() {
return _curPort;
}
void SciGuiGfx::SetOrigin(int16 left, int16 top) {
_curPort->left = left;
_curPort->top = top;
}
void SciGuiGfx::MoveTo(int16 left, int16 top) {
_curPort->curTop = top;
_curPort->curLeft = left;
}
void SciGuiGfx::Move(int16 left, int16 top) {
_curPort->curTop += top;
_curPort->curLeft += left;
}
void SciGuiGfx::OpenPort(GuiPort *port) {
port->fontId = 0;
port->fontHeight = 8;
GuiPort *tmp = _curPort;
_curPort = port;
_text->SetFont(port->fontId);
_curPort = tmp;
port->top = 0;
port->left = 0;
port->textFace = 0;
port->penClr = 0;
port->backClr = 255;
port->penMode = 0;
port->rect = _bounds;
}
void SciGuiGfx::PenColor(int16 color) {
_curPort->penClr = color;
}
void SciGuiGfx::BackColor(int16 color) {
_curPort->backClr = color;
}
void SciGuiGfx::PenMode(int16 mode) {
_curPort->penMode = mode;
}
void SciGuiGfx::TextFace(int16 textFace) {
_curPort->textFace = textFace;
}
int16 SciGuiGfx::GetPointSize() {
return _curPort->fontHeight;
}
void SciGuiGfx::ClearScreen(byte color) {
FillRect(_curPort->rect, SCI_SCREEN_MASK_ALL, color, 0, 0);
}
void SciGuiGfx::InvertRect(const Common::Rect &rect) {
int16 oldpenmode = _curPort->penMode;
_curPort->penMode = 2;
FillRect(rect, 1, _curPort->penClr, _curPort->backClr);
_curPort->penMode = oldpenmode;
}
void SciGuiGfx::EraseRect(const Common::Rect &rect) {
FillRect(rect, 1, _curPort->backClr);
}
void SciGuiGfx::PaintRect(const Common::Rect &rect) {
FillRect(rect, 1, _curPort->penClr);
}
void SciGuiGfx::FillRect(const Common::Rect &rect, int16 drawFlags, byte clrPen, byte clrBack, byte bControl) {
Common::Rect r = rect;
r.clip(_curPort->rect);
if (r.isEmpty()) // nothing to fill
return;
int16 oldPenMode = _curPort->penMode;
OffsetRect(r);
int16 x, y;
byte curVisual;
// Doing visual first
if (drawFlags & SCI_SCREEN_MASK_VISUAL) {
if (oldPenMode == 2) { // invert mode
for (y = r.top; y < r.bottom; y++) {
for (x = r.left; x < r.right; x++) {
curVisual = _screen->getVisual(x, y);
if (curVisual == clrPen) {
_screen->putPixel(x, y, 1, clrBack, 0, 0);
} else if (curVisual == clrBack) {
_screen->putPixel(x, y, 1, clrPen, 0, 0);
}
}
}
} else { // just fill rect with ClrPen
for (y = r.top; y < r.bottom; y++) {
for (x = r.left; x < r.right; x++) {
_screen->putPixel(x, y, 1, clrPen, 0, 0);
}
}
}
}
if (drawFlags < 2)
return;
drawFlags &= SCI_SCREEN_MASK_PRIORITY|SCI_SCREEN_MASK_CONTROL;
if (oldPenMode != 2) {
for (y = r.top; y < r.bottom; y++) {
for (x = r.left; x < r.right; x++) {
_screen->putPixel(x, y, drawFlags, 0, clrBack, bControl);
}
}
} else {
for (y = r.top; y < r.bottom; y++) {
for (x = r.left; x < r.right; x++) {
_screen->putPixel(x, y, drawFlags, 0, !_screen->getPriority(x, y), !_screen->getControl(x, y));
}
}
}
}
void SciGuiGfx::FrameRect(const Common::Rect &rect) {
Common::Rect r;
// left
r = rect;
r.right = rect.left + 1;
PaintRect(r);
// right
r.right = rect.right;
r.left = rect.right - 1;
PaintRect(r);
//top
r.left = rect.left;
r.bottom = rect.top + 1;
PaintRect(r);
//bottom
r.bottom = rect.bottom;
r.top = rect.bottom - 1;
PaintRect(r);
}
void SciGuiGfx::OffsetRect(Common::Rect &r) {
r.top += _curPort->top;
r.bottom += _curPort->top;
r.left += _curPort->left;
r.right += _curPort->left;
}
void SciGuiGfx::OffsetLine(Common::Point &start, Common::Point &end) {
start.x += _curPort->left;
start.y += _curPort->top;
end.x += _curPort->left;
end.y += _curPort->top;
}
void SciGuiGfx::BitsShow(const Common::Rect &rect) {
Common::Rect workerRect(rect.left, rect.top, rect.right, rect.bottom);
workerRect.clip(_curPort->rect);
if (workerRect.isEmpty()) // nothing to show
return;
OffsetRect(workerRect);
_screen->copyRectToScreen(workerRect);
}
GuiMemoryHandle SciGuiGfx::BitsSave(const Common::Rect &rect, byte screenMask) {
GuiMemoryHandle memoryId;
byte *memoryPtr;
int size;
Common::Rect workerRect(rect.left, rect.top, rect.right, rect.bottom);
workerRect.clip(_curPort->rect);
if (workerRect.isEmpty()) // nothing to save
return NULL_REG;
OffsetRect(workerRect);
// now actually ask _screen how much space it will need for saving
size = _screen->bitsGetDataSize(workerRect, screenMask);
memoryId = kalloc(_segMan, "SaveBits()", size);
memoryPtr = kmem(_segMan, memoryId);
_screen->bitsSave(workerRect, screenMask, memoryPtr);
return memoryId;
}
void SciGuiGfx::BitsGetRect(GuiMemoryHandle memoryHandle, Common::Rect *destRect) {
byte *memoryPtr = NULL;
if (!memoryHandle.isNull()) {
memoryPtr = kmem(_segMan, memoryHandle);;
if (memoryPtr) {
_screen->bitsGetRect(memoryPtr, destRect);
}
}
}
void SciGuiGfx::BitsRestore(GuiMemoryHandle memoryHandle) {
byte *memoryPtr = NULL;
if (!memoryHandle.isNull()) {
memoryPtr = kmem(_segMan, memoryHandle);;
if (memoryPtr) {
_screen->bitsRestore(memoryPtr);
kfree(_segMan, memoryHandle);
}
}
}
void SciGuiGfx::BitsFree(GuiMemoryHandle memoryHandle) {
if (!memoryHandle.isNull()) {
kfree(_segMan, memoryHandle);
}
}
void SciGuiGfx::drawPicture(GuiResourceId pictureId, int16 animationNr, bool mirroredFlag, bool addToFlag, GuiResourceId paletteId) {
SciGuiPicture *picture;
picture = new SciGuiPicture(_resMan, this, _screen, _palette, pictureId);
// do we add to a picture? if not -> clear screen with white
if (!addToFlag) {
ClearScreen(_screen->_colorWhite);
}
picture->draw(animationNr, mirroredFlag, addToFlag, paletteId);
}
// This one is the only one that updates screen!
void SciGuiGfx::drawCel(GuiResourceId viewId, GuiViewLoopNo loopNo, GuiViewCelNo celNo, uint16 leftPos, uint16 topPos, byte priority, uint16 paletteNo, int16 origHeight) {
SciGuiView *view = getView(viewId);
Common::Rect rect;
Common::Rect clipRect;
if (view) {
rect.left = leftPos;
rect.top = topPos;
rect.right = rect.left + view->getWidth(loopNo, celNo);
rect.bottom = rect.top + view->getHeight(loopNo, celNo);
clipRect = rect;
clipRect.clip(_curPort->rect);
if (clipRect.isEmpty()) { // nothing to draw
return;
}
Common::Rect clipRectTranslated = clipRect;
OffsetRect(clipRectTranslated);
view->draw(rect, clipRect, clipRectTranslated, loopNo, celNo, priority, paletteNo, origHeight);
if (!_screen->_picNotValid)
BitsShow(rect);
}
}
// This version of drawCel is not supposed to call BitsShow()!
void SciGuiGfx::drawCel(GuiResourceId viewId, GuiViewLoopNo loopNo, GuiViewCelNo celNo, Common::Rect celRect, byte priority, uint16 paletteNo) {
SciGuiView *view = getView(viewId);
Common::Rect clipRect;
if (view) {
clipRect = celRect;
clipRect.clip(_curPort->rect);
if (clipRect.isEmpty()) { // nothing to draw
return;
}
Common::Rect clipRectTranslated = clipRect;
OffsetRect(clipRectTranslated);
view->draw(celRect, clipRect, clipRectTranslated, loopNo, celNo, priority, paletteNo);
}
}
// This version of drawCel is not supposed to call BitsShow()!
void SciGuiGfx::drawCel(SciGuiView *view, GuiViewLoopNo loopNo, GuiViewCelNo celNo, Common::Rect celRect, byte priority, uint16 paletteNo) {
Common::Rect clipRect;
clipRect = celRect;
clipRect.clip(_curPort->rect);
if (clipRect.isEmpty()) // nothing to draw
return;
Common::Rect clipRectTranslated = clipRect;
OffsetRect(clipRectTranslated);
view->draw(celRect, clipRect, clipRectTranslated, loopNo, celNo, priority, paletteNo);
}
uint16 SciGuiGfx::onControl(uint16 screenMask, Common::Rect rect) {
Common::Rect outRect(rect.left, rect.top, rect.right, rect.bottom);
int16 x, y;
uint16 result = 0;
outRect.clip(_curPort->rect);
if (outRect.isEmpty()) // nothing to control
return 0;
OffsetRect(outRect);
if (screenMask & SCI_SCREEN_MASK_PRIORITY) {
for (y = outRect.top; y < outRect.bottom; y++) {
for (x = outRect.left; x < outRect.right; x++) {
result |= 1 << _screen->getPriority(x, y);
}
}
} else {
for (y = outRect.top; y < outRect.bottom; y++) {
for (x = outRect.left; x < outRect.right; x++) {
result |= 1 << _screen->getControl(x, y);
}
}
}
return result;
}
static inline int sign_extend_byte(int value) {
if (value & 0x80)
return value - 256;
else
return value;
}
void SciGuiGfx::PriorityBandsInit(int16 bandCount, int16 top, int16 bottom) {
int16 y;
int32 bandSize;
if (bandCount != -1)
_priorityBandCount = bandCount;
_priorityTop = top;
_priorityBottom = bottom;
// Do NOT modify this algo or optimize it anyhow, sierra sci used int32 for calculating the
// priority bands and by using double or anything rounding WILL destroy the result
bandSize = ((_priorityBottom - _priorityTop) * 2000) / _priorityBandCount;
memset(_priorityBands, 0, sizeof(byte) * _priorityTop);
for (y = _priorityTop; y < _priorityBottom; y++)
_priorityBands[y] = 1 + (((y - _priorityTop) * 2000) / bandSize);
if (_priorityBandCount == 15) {
// When having 15 priority bands, we actually replace band 15 with band 14, cause the original sci interpreter also
// does it that way as well
y = _priorityBottom;
while (_priorityBands[--y] == _priorityBandCount)
_priorityBands[y]--;
}
// We fill space that is left over with the highest band
for (y = _priorityBottom; y < _screen->_height; y++)
_priorityBands[y] = _priorityBandCount;
}
void SciGuiGfx::PriorityBandsInit(byte *data) {
int i = 0, inx;
byte priority = 0;
for (inx = 0; inx < 14; inx++) {
priority = *data++;
while (i < priority)
_priorityBands[i++] = inx;
}
while (i < 200)
_priorityBands[i++] = inx;
}
byte SciGuiGfx::CoordinateToPriority(int16 y) {
if (y < _priorityTop)
return _priorityBands[_priorityTop];
if (y > _priorityBottom)
return _priorityBands[_priorityBottom];
return _priorityBands[y];
}
int16 SciGuiGfx::PriorityToCoordinate(byte priority) {
int16 y;
if (priority <= _priorityBandCount) {
for (y = 0; y <= _priorityBottom; y++)
if (_priorityBands[y] == priority)
return y;
}
return _priorityBottom;
}
bool SciGuiGfx::CanBeHereCheckRectList(reg_t checkObject, Common::Rect checkRect, List *list) {
reg_t curAddress = list->first;
Node *curNode = _segMan->lookupNode(curAddress);
reg_t curObject;
uint16 signal;
Common::Rect curRect;
while (curNode) {
curObject = curNode->value;
if (curObject != checkObject) {
signal = GET_SEL32V(_segMan, curObject, signal);
if ((signal & (kSignalIgnoreActor | kSignalRemoveView | kSignalNoUpdate)) == 0) {
curRect.left = GET_SEL32V(_segMan, curObject, brLeft);
curRect.top = GET_SEL32V(_segMan, curObject, brTop);
curRect.right = GET_SEL32V(_segMan, curObject, brRight);
curRect.bottom = GET_SEL32V(_segMan, curObject, brBottom);
// Check if curRect is within checkRect
if (curRect.right > checkRect.left && curRect.left < checkRect.right && curRect.bottom > checkRect.top && curRect.top < checkRect.bottom) {
return false;
}
}
}
curAddress = curNode->succ;
curNode = _segMan->lookupNode(curAddress);
}
return true;
}
void SciGuiGfx::SetNowSeen(reg_t objectReference) {
SciGuiView *view = NULL;
Common::Rect celRect(0, 0);
GuiResourceId viewId = (GuiResourceId)GET_SEL32V(_segMan, objectReference, view);
GuiViewLoopNo loopNo = sign_extend_byte((GuiViewLoopNo)GET_SEL32V(_segMan, objectReference, loop));
GuiViewCelNo celNo = sign_extend_byte((GuiViewCelNo)GET_SEL32V(_segMan, objectReference, cel));
int16 x = (int16)GET_SEL32V(_segMan, objectReference, x);
int16 y = (int16)GET_SEL32V(_segMan, objectReference, y);
int16 z = 0;
if (_kernel->_selectorCache.z > -1)
z = (int16)GET_SEL32V(_segMan, objectReference, z);
// now get cel rectangle
view = getView(viewId);
view->getCelRect(loopNo, celNo, x, y, z, &celRect);
// TODO: sometimes loop is negative. Check what it means
if (lookup_selector(_segMan, objectReference, _kernel->_selectorCache.nsTop, NULL, NULL) == kSelectorVariable) {
PUT_SEL32V(_segMan, objectReference, nsLeft, celRect.left);
PUT_SEL32V(_segMan, objectReference, nsRight, celRect.right);
PUT_SEL32V(_segMan, objectReference, nsTop, celRect.top);
PUT_SEL32V(_segMan, objectReference, nsBottom, celRect.bottom);
}
}
} // End of namespace Sci
|