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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
|
/* 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.
*
*/
#include "sci/console.h"
#include "sci/engine/features.h"
#include "sci/engine/kernel.h"
#include "sci/engine/selector.h"
#include "sci/engine/state.h"
#include "sci/graphics/frameout.h"
#include "sci/graphics/helpers.h"
#include "sci/graphics/lists32.h"
#include "sci/graphics/plane32.h"
#include "sci/graphics/remap32.h"
#include "sci/graphics/screen.h"
#include "sci/graphics/screen_item32.h"
namespace Sci {
#pragma mark DrawList
void DrawList::add(ScreenItem *screenItem, const Common::Rect &rect) {
DrawItem *drawItem = new DrawItem;
drawItem->screenItem = screenItem;
drawItem->rect = rect;
DrawListBase::add(drawItem);
}
#pragma mark -
#pragma mark Plane
uint16 Plane::_nextObjectId; // Will be initialized in Plane::init()
uint32 Plane::_nextCreationId; // ditto
Plane::Plane(const Common::Rect &gameRect, PlanePictureCodes pictureId) :
_creationId(_nextCreationId++),
_pictureId(pictureId),
_mirrored(false),
_type(kPlaneTypeColored),
_back(0),
_priorityChanged(false),
_object(make_reg(0, _nextObjectId++)),
_redrawAllCount(g_sci->_gfxFrameout->getScreenCount()),
_created(g_sci->_gfxFrameout->getScreenCount()),
_updated(0),
_deleted(0),
_moved(0),
_gameRect(gameRect) {
convertGameRectToPlaneRect();
_priority = MAX(10000, g_sci->_gfxFrameout->getPlanes().getTopPlanePriority() + 1);
setType();
_screenRect = _planeRect;
}
Plane::Plane(reg_t object) :
_creationId(_nextCreationId++),
_type(kPlaneTypeColored),
_priorityChanged(false),
_object(object),
_redrawAllCount(g_sci->_gfxFrameout->getScreenCount()),
_created(g_sci->_gfxFrameout->getScreenCount()),
_updated(0),
_deleted(0),
_moved(0) {
SegManager *segMan = g_sci->getEngineState()->_segMan;
_vanishingPoint.x = readSelectorValue(segMan, object, SELECTOR(vanishingX));
_vanishingPoint.y = readSelectorValue(segMan, object, SELECTOR(vanishingY));
if (g_sci->_features->usesAlternateSelectors()) {
_gameRect.left = readSelectorValue(segMan, object, SELECTOR(left));
_gameRect.top = readSelectorValue(segMan, object, SELECTOR(top));
_gameRect.right = readSelectorValue(segMan, object, SELECTOR(right)) + 1;
_gameRect.bottom = readSelectorValue(segMan, object, SELECTOR(bottom)) + 1;
} else {
_gameRect.left = readSelectorValue(segMan, object, SELECTOR(inLeft));
_gameRect.top = readSelectorValue(segMan, object, SELECTOR(inTop));
_gameRect.right = readSelectorValue(segMan, object, SELECTOR(inRight)) + 1;
_gameRect.bottom = readSelectorValue(segMan, object, SELECTOR(inBottom)) + 1;
}
convertGameRectToPlaneRect();
_back = readSelectorValue(segMan, object, SELECTOR(back));
_priority = readSelectorValue(segMan, object, SELECTOR(priority));
_pictureId = readSelectorValue(segMan, object, SELECTOR(picture));
setType();
_mirrored = readSelectorValue(segMan, object, SELECTOR(mirrored));
_screenRect = _planeRect;
changePic();
}
Plane::Plane(const Plane &other) :
_creationId(other._creationId),
_pictureId(other._pictureId),
_mirrored(other._mirrored),
_type(other._type),
_back(other._back),
_object(other._object),
_priority(other._priority),
_planeRect(other._planeRect),
_gameRect(other._gameRect),
_screenRect(other._screenRect),
_screenItemList(other._screenItemList) {}
void Plane::operator=(const Plane &other) {
_creationId = other._creationId;
_gameRect = other._gameRect;
_planeRect = other._planeRect;
_vanishingPoint = other._vanishingPoint;
_pictureId = other._pictureId;
_type = other._type;
_mirrored = other._mirrored;
_priority = other._priority;
_back = other._back;
_screenRect = other._screenRect;
_priorityChanged = other._priorityChanged;
}
void Plane::init() {
_nextObjectId = g_sci->_features->detectPlaneIdBase();
_nextCreationId = 0;
}
void Plane::convertGameRectToPlaneRect() {
const int16 screenWidth = g_sci->_gfxFrameout->getScreenWidth();
const int16 screenHeight = g_sci->_gfxFrameout->getScreenHeight();
const int16 scriptWidth = g_sci->_gfxFrameout->getScriptWidth();
const int16 scriptHeight = g_sci->_gfxFrameout->getScriptHeight();
const Ratio ratioX = Ratio(screenWidth, scriptWidth);
const Ratio ratioY = Ratio(screenHeight, scriptHeight);
_planeRect = _gameRect;
mulru(_planeRect, ratioX, ratioY, 1);
}
void Plane::printDebugInfo(Console *con) const {
const char *name;
if (_object.isNumber()) {
name = "-scummvm-";
} else {
name = g_sci->getEngineState()->_segMan->getObjectName(_object);
}
con->debugPrintf("%04x:%04x (%s): type %d, prio %d, ins %u, pic %d, mirror %d, back %d\n",
PRINT_REG(_object),
name,
_type,
_priority,
_creationId,
_pictureId,
_mirrored,
_back
);
con->debugPrintf(" game rect: (%d, %d, %d, %d), plane rect: (%d, %d, %d, %d)\n screen rect: (%d, %d, %d, %d)\n",
PRINT_RECT(_gameRect),
PRINT_RECT(_planeRect),
PRINT_RECT(_screenRect)
);
con->debugPrintf(" # screen items: %d\n", _screenItemList.size());
}
#pragma mark -
#pragma mark Plane - Pic
void Plane::addPicInternal(const GuiResourceId pictureId, const Common::Point *position, const bool mirrorX) {
uint16 celCount = 1000;
bool transparent = true;
for (uint16 celNo = 0; celNo < celCount; ++celNo) {
CelObjPic *celObj = new CelObjPic(pictureId, celNo);
if (celCount == 1000) {
celCount = celObj->_celCount;
}
if (!celObj->_transparent) {
transparent = false;
}
ScreenItem *screenItem = new ScreenItem(_object, celObj->_info);
screenItem->_pictureId = pictureId;
screenItem->_mirrorX = mirrorX;
screenItem->_priority = celObj->_priority;
screenItem->_fixedPriority = true;
if (position != nullptr) {
screenItem->_position = *position + celObj->_relativePosition;
} else {
screenItem->_position = celObj->_relativePosition;
}
screenItem->_celObj.reset(celObj);
_screenItemList.add(screenItem);
}
_type = (g_sci->_features->hasTransparentPicturePlanes() && transparent) ? kPlaneTypeTransparentPicture : kPlaneTypePicture;
}
GuiResourceId Plane::addPic(const GuiResourceId pictureId, const Common::Point &position, const bool mirrorX, const bool deleteDuplicate) {
if (deleteDuplicate) {
deletePic(pictureId);
}
addPicInternal(pictureId, &position, mirrorX);
return _pictureId;
}
void Plane::changePic() {
_pictureChanged = false;
if (_type != kPlaneTypePicture && _type != kPlaneTypeTransparentPicture) {
return;
}
addPicInternal(_pictureId, nullptr, _mirrored);
}
void Plane::deletePic(const GuiResourceId pictureId) {
for (ScreenItemList::iterator it = _screenItemList.begin(); it != _screenItemList.end(); ++it) {
ScreenItem *screenItem = *it;
if (screenItem->_pictureId == pictureId) {
screenItem->_created = 0;
screenItem->_updated = 0;
screenItem->_deleted = g_sci->_gfxFrameout->getScreenCount();
}
}
}
void Plane::deletePic(const GuiResourceId oldPictureId, const GuiResourceId newPictureId) {
deletePic(oldPictureId);
_pictureId = newPictureId;
}
void Plane::deleteAllPics() {
for (ScreenItemList::iterator it = _screenItemList.begin(); it != _screenItemList.end(); ++it) {
ScreenItem *screenItem = *it;
if (screenItem != nullptr && screenItem->_celInfo.type == kCelTypePic) {
if (screenItem->_created == 0) {
screenItem->_created = 0;
screenItem->_updated = 0;
screenItem->_deleted = g_sci->_gfxFrameout->getScreenCount();
} else {
_screenItemList.erase(it);
}
}
}
_screenItemList.pack();
}
#pragma mark -
#pragma mark Plane - Rendering
void Plane::breakDrawListByPlanes(DrawList &drawList, const PlaneList &planeList) const {
const int nextPlaneIndex = planeList.findIndexByObject(_object) + 1;
const PlaneList::size_type planeCount = planeList.size();
for (DrawList::size_type i = 0; i < drawList.size(); ++i) {
for (PlaneList::size_type j = nextPlaneIndex; j < planeCount; ++j) {
if (
planeList[j]->_type != kPlaneTypeTransparent &&
planeList[j]->_type != kPlaneTypeTransparentPicture
) {
Common::Rect outRects[4];
int splitCount = splitRects(drawList[i]->rect, planeList[j]->_screenRect, outRects);
if (splitCount != -1) {
while (splitCount--) {
drawList.add(drawList[i]->screenItem, outRects[splitCount]);
}
drawList.erase_at(i);
break;
}
}
}
}
drawList.pack();
}
void Plane::breakEraseListByPlanes(RectList &eraseList, const PlaneList &planeList) const {
const int nextPlaneIndex = planeList.findIndexByObject(_object) + 1;
const PlaneList::size_type planeCount = planeList.size();
for (RectList::size_type i = 0; i < eraseList.size(); ++i) {
for (PlaneList::size_type j = nextPlaneIndex; j < planeCount; ++j) {
if (
planeList[j]->_type != kPlaneTypeTransparent &&
planeList[j]->_type != kPlaneTypeTransparentPicture
) {
Common::Rect outRects[4];
int splitCount = splitRects(*eraseList[i], planeList[j]->_screenRect, outRects);
if (splitCount != -1) {
while (splitCount--) {
eraseList.add(outRects[splitCount]);
}
eraseList.erase_at(i);
break;
}
}
}
}
eraseList.pack();
}
void Plane::calcLists(Plane &visiblePlane, const PlaneList &planeList, DrawList &drawList, RectList &eraseList) {
const ScreenItemList::size_type screenItemCount = _screenItemList.size();
const ScreenItemList::size_type visiblePlaneItemCount = visiblePlane._screenItemList.size();
for (ScreenItemList::size_type i = 0; i < screenItemCount; ++i) {
// Items can be added to ScreenItemList and we don't want to process
// those new items, but the list also can grow smaller, so we need to
// check that we are still within the upper bound of the list and quit
// if we aren't any more
if (i >= _screenItemList.size()) {
break;
}
ScreenItem *item = _screenItemList[i];
if (item == nullptr) {
continue;
}
// SSCI used an array without bounds checking so could just get the
// visible screen item directly; we need to verify that the index is
// actually within the valid range for the visible plane before
// accessing the item to avoid a range error.
const ScreenItem *visibleItem = nullptr;
if (i < visiblePlaneItemCount) {
visibleItem = visiblePlane._screenItemList[i];
}
// Keep erase rects for this screen item from drawing outside of its
// owner plane
Common::Rect visibleItemScreenRect;
if (visibleItem != nullptr) {
visibleItemScreenRect = visibleItem->_screenRect;
visibleItemScreenRect.clip(_screenRect);
}
if (item->_deleted) {
// Add item's rect to erase list
if (
visibleItem != nullptr &&
!visibleItemScreenRect.isEmpty()
) {
if (g_sci->_gfxRemap32->getRemapCount()) {
mergeToRectList(visibleItemScreenRect, eraseList);
} else {
eraseList.add(visibleItemScreenRect);
}
}
}
if (!item->_created && !item->_updated) {
continue;
}
item->calcRects(*this);
const Common::Rect itemScreenRect(item->_screenRect);
if (item->_created) {
// Add item to draw list
if(!itemScreenRect.isEmpty()) {
if (g_sci->_gfxRemap32->getRemapCount()) {
drawList.add(item, itemScreenRect);
mergeToRectList(itemScreenRect, eraseList);
} else {
drawList.add(item, itemScreenRect);
}
}
} else {
// Add old rect to erase list, new item to draw list
if (g_sci->_gfxRemap32->getRemapCount()) {
// If item and visibleItem don't overlap...
if (itemScreenRect.isEmpty() ||
visibleItem == nullptr ||
visibleItemScreenRect.isEmpty() ||
!visibleItemScreenRect.intersects(itemScreenRect)
) {
// ...add item to draw list, and old rect to erase list...
if (!itemScreenRect.isEmpty()) {
drawList.add(item, itemScreenRect);
mergeToRectList(itemScreenRect, eraseList);
}
if (visibleItem != nullptr && !visibleItemScreenRect.isEmpty()) {
mergeToRectList(visibleItemScreenRect, eraseList);
}
} else {
// ...otherwise, add bounding box of old+new to erase list,
// and item to draw list
Common::Rect extendedScreenRect = visibleItemScreenRect;
extendedScreenRect.extend(itemScreenRect);
drawList.add(item, itemScreenRect);
mergeToRectList(extendedScreenRect, eraseList);
}
} else {
// If no active remaps, just add item to draw list and old rect
// to erase list
// TODO: SCI3 update rects for VMD?
if (!itemScreenRect.isEmpty()) {
drawList.add(item, itemScreenRect);
}
if (visibleItem != nullptr && !visibleItemScreenRect.isEmpty()) {
eraseList.add(visibleItemScreenRect);
}
}
}
}
// Remove parts of eraselist/drawlist that are covered by other planes
breakEraseListByPlanes(eraseList, planeList);
breakDrawListByPlanes(drawList, planeList);
// The current size of the draw list is stored here, as we need to loop over
// only the already-inserted entries later.
DrawList::size_type drawListSizePrimary = drawList.size();
const RectList::size_type eraseListCount = eraseList.size();
if (getSciVersion() == SCI_VERSION_3) {
_screenItemList.sort();
bool pictureDrawn = false;
bool screenItemDrawn = false;
for (RectList::size_type i = 0; i < eraseListCount; ++i) {
const Common::Rect &rect = *eraseList[i];
for (ScreenItemList::size_type j = 0; j < screenItemCount; ++j) {
ScreenItem *item = _screenItemList[j];
if (item == nullptr) {
continue;
}
if (rect.intersects(item->_screenRect)) {
const Common::Rect intersection = rect.findIntersectingRect(item->_screenRect);
if (!item->_deleted) {
if (pictureDrawn) {
if (item->_celInfo.type == kCelTypePic) {
if (screenItemDrawn || item->_celInfo.celNo == 0) {
mergeToDrawList(j, intersection, drawList);
}
} else {
if (!item->_updated && !item->_created) {
mergeToDrawList(j, intersection, drawList);
}
screenItemDrawn = true;
}
} else {
if (!item->_updated && !item->_created) {
mergeToDrawList(j, intersection, drawList);
}
if (item->_celInfo.type == kCelTypePic) {
pictureDrawn = true;
}
}
}
}
}
}
_screenItemList.unsort();
} else {
// Add all items overlapping the erase list to the draw list
for (RectList::size_type i = 0; i < eraseListCount; ++i) {
const Common::Rect &rect = *eraseList[i];
for (ScreenItemList::size_type j = 0; j < screenItemCount; ++j) {
ScreenItem *item = _screenItemList[j];
if (
item != nullptr &&
!item->_created && !item->_updated && !item->_deleted &&
rect.intersects(item->_screenRect)
) {
drawList.add(item, rect.findIntersectingRect(item->_screenRect));
}
}
}
}
if (g_sci->_gfxRemap32->getRemapCount() == 0) {
// Add all items that overlap with items in the drawlist and have higher
// priority.
// We only loop over "primary" items in the draw list, skipping
// those that were added because of the erase list in the previous loop,
// or those to be added in this loop.
for (DrawList::size_type i = 0; i < drawListSizePrimary; ++i) {
const DrawItem *drawListEntry = nullptr;
if (i < drawList.size()) {
drawListEntry = drawList[i];
}
for (ScreenItemList::size_type j = 0; j < screenItemCount; ++j) {
ScreenItem *newItem = nullptr;
if (j < _screenItemList.size()) {
newItem = _screenItemList[j];
}
if (
drawListEntry != nullptr && newItem != nullptr &&
!newItem->_created && !newItem->_updated && !newItem->_deleted
) {
const ScreenItem *drawnItem = drawListEntry->screenItem;
if (newItem->hasPriorityAbove(*drawnItem) &&
drawListEntry->rect.intersects(newItem->_screenRect)
) {
mergeToDrawList(j, drawListEntry->rect.findIntersectingRect(newItem->_screenRect), drawList);
}
}
}
}
}
decrementScreenItemArrayCounts(&visiblePlane, false);
}
void Plane::decrementScreenItemArrayCounts(Plane *visiblePlane, const bool forceUpdate) {
const ScreenItemList::size_type screenItemCount = _screenItemList.size();
for (ScreenItemList::size_type i = 0; i < screenItemCount; ++i) {
ScreenItem *item = _screenItemList[i];
if (item != nullptr) {
// update item in visiblePlane if item is updated
if (visiblePlane != nullptr && (
item->_updated || (forceUpdate && visiblePlane->_screenItemList.findByObject(item->_object) != nullptr))) {
*visiblePlane->_screenItemList[i] = *item;
}
if (item->_updated) {
item->_updated--;
}
// create new item in visiblePlane if item was added
if (item->_created) {
item->_created--;
if (visiblePlane != nullptr) {
visiblePlane->_screenItemList.add(new ScreenItem(*item));
}
}
// delete item from both planes if it was deleted
if (item->_deleted) {
item->_deleted--;
if (!item->_deleted) {
if (visiblePlane != nullptr && visiblePlane->_screenItemList.findByObject(item->_object) != nullptr) {
visiblePlane->_screenItemList.erase_at(i);
}
_screenItemList.erase_at(i);
}
}
}
}
_screenItemList.pack();
if (visiblePlane != nullptr) {
visiblePlane->_screenItemList.pack();
}
}
void Plane::filterDownEraseRects(DrawList &drawList, RectList &eraseList, RectList &higherEraseList) const {
const RectList::size_type higherEraseCount = higherEraseList.size();
if (_type == kPlaneTypeTransparent || _type == kPlaneTypeTransparentPicture) {
for (RectList::size_type i = 0; i < higherEraseCount; ++i) {
const Common::Rect &r = *higherEraseList[i];
const ScreenItemList::size_type screenItemCount = _screenItemList.size();
for (ScreenItemList::size_type j = 0; j < screenItemCount; ++j) {
const ScreenItem *item = _screenItemList[j];
if (item != nullptr && r.intersects(item->_screenRect)) {
mergeToDrawList(j, r, drawList);
}
}
}
} else {
for (RectList::size_type i = 0; i < higherEraseCount; ++i) {
Common::Rect r = *higherEraseList[i];
if (r.intersects(_screenRect)) {
r.clip(_screenRect);
mergeToRectList(r, eraseList);
const ScreenItemList::size_type screenItemCount = _screenItemList.size();
for (ScreenItemList::size_type j = 0; j < screenItemCount; ++j) {
const ScreenItem *item = _screenItemList[j];
if (item != nullptr && r.intersects(item->_screenRect)) {
mergeToDrawList(j, r, drawList);
}
}
Common::Rect outRects[4];
const Common::Rect &r2 = *higherEraseList[i];
int splitCount = splitRects(r2, r, outRects);
if (splitCount > 0) {
while (splitCount--) {
higherEraseList.add(outRects[splitCount]);
}
}
higherEraseList.erase_at(i);
}
}
higherEraseList.pack();
}
}
void Plane::filterUpDrawRects(DrawList &drawList, const DrawList &lowerDrawList) const {
const DrawList::size_type lowerDrawCount = lowerDrawList.size();
for (DrawList::size_type i = 0; i < lowerDrawCount; ++i) {
const Common::Rect &r = lowerDrawList[i]->rect;
const ScreenItemList::size_type screenItemCount = _screenItemList.size();
for (ScreenItemList::size_type j = 0; j < screenItemCount; ++j) {
const ScreenItem *item = _screenItemList[j];
if (item != nullptr && r.intersects(item->_screenRect)) {
mergeToDrawList(j, r, drawList);
}
}
}
}
void Plane::filterUpEraseRects(DrawList &drawList, const RectList &lowerEraseList) const {
const RectList::size_type lowerEraseCount = lowerEraseList.size();
for (RectList::size_type i = 0; i < lowerEraseCount; ++i) {
const Common::Rect &r = *lowerEraseList[i];
const ScreenItemList::size_type screenItemCount = _screenItemList.size();
for (ScreenItemList::size_type j = 0; j < screenItemCount; ++j) {
const ScreenItem *item = _screenItemList[j];
if (item != nullptr && r.intersects(item->_screenRect)) {
mergeToDrawList(j, r, drawList);
}
}
}
}
void Plane::mergeToDrawList(const ScreenItemList::size_type index, const Common::Rect &rect, DrawList &drawList) const {
RectList mergeList;
ScreenItem &item = *_screenItemList[index];
Common::Rect r = item._screenRect;
r.clip(rect);
mergeList.add(r);
for (RectList::size_type i = 0; i < mergeList.size(); ++i) {
r = *mergeList[i];
const DrawList::size_type drawCount = drawList.size();
for (DrawList::size_type j = 0; j < drawCount; ++j) {
const DrawItem &drawItem = *drawList[j];
if (item._object == drawItem.screenItem->_object) {
if (drawItem.rect.contains(r)) {
mergeList.erase_at(i);
break;
}
Common::Rect outRects[4];
int splitCount = splitRects(r, drawItem.rect, outRects);
if (splitCount != -1) {
while (splitCount--) {
mergeList.add(outRects[splitCount]);
}
mergeList.erase_at(i);
// proceed to the next rect
r = *mergeList[++i];
}
}
}
}
mergeList.pack();
for (RectList::size_type i = 0; i < mergeList.size(); ++i) {
drawList.add(&item, *mergeList[i]);
}
}
void Plane::mergeToRectList(const Common::Rect &rect, RectList &eraseList) const {
RectList mergeList;
Common::Rect r;
mergeList.add(rect);
for (RectList::size_type i = 0; i < mergeList.size(); ++i) {
r = *mergeList[i];
const RectList::size_type eraseCount = eraseList.size();
for (RectList::size_type j = 0; j < eraseCount; ++j) {
const Common::Rect &eraseRect = *eraseList[j];
if (eraseRect.contains(r)) {
mergeList.erase_at(i);
break;
}
Common::Rect outRects[4];
int splitCount = splitRects(r, eraseRect, outRects);
if (splitCount != -1) {
while (splitCount--) {
mergeList.add(outRects[splitCount]);
}
mergeList.erase_at(i);
// proceed to the next rect
r = *mergeList[++i];
}
}
}
mergeList.pack();
for (RectList::size_type i = 0; i < mergeList.size(); ++i) {
eraseList.add(*mergeList[i]);
}
}
void Plane::redrawAll(Plane *visiblePlane, const PlaneList &planeList, DrawList &drawList, RectList &eraseList) {
const ScreenItemList::size_type screenItemCount = _screenItemList.size();
for (ScreenItemList::size_type i = 0; i < screenItemCount; ++i) {
ScreenItem *screenItem = _screenItemList[i];
if (screenItem != nullptr && !screenItem->_deleted) {
screenItem->calcRects(*this);
if (!screenItem->_screenRect.isEmpty()) {
mergeToDrawList(i, screenItem->_screenRect, drawList);
}
}
}
eraseList.clear();
if (!_screenRect.isEmpty() && _type != kPlaneTypePicture && _type != kPlaneTypeOpaque) {
eraseList.add(_screenRect);
}
breakEraseListByPlanes(eraseList, planeList);
breakDrawListByPlanes(drawList, planeList);
--_redrawAllCount;
decrementScreenItemArrayCounts(visiblePlane, true);
}
void Plane::setType() {
switch (_pictureId) {
case kPlanePicColored:
_type = kPlaneTypeColored;
break;
case kPlanePicTransparent:
_type = kPlaneTypeTransparent;
break;
case kPlanePicOpaque:
_type = kPlaneTypeOpaque;
break;
case kPlanePicTransparentPicture:
if (g_sci->_features->hasTransparentPicturePlanes()) {
_type = kPlaneTypeTransparentPicture;
break;
}
// The game doesn't have transparent picture planes
// fall through
default:
if (!g_sci->_features->hasTransparentPicturePlanes() || _type != kPlaneTypeTransparentPicture) {
_type = kPlaneTypePicture;
}
break;
}
}
void Plane::sync(const Plane *other, const Common::Rect &screenRect) {
if (other == nullptr) {
if (_pictureChanged) {
deleteAllPics();
setType();
changePic();
_redrawAllCount = g_sci->_gfxFrameout->getScreenCount();
} else {
setType();
}
} else {
if (
_planeRect.top != other->_planeRect.top ||
_planeRect.left != other->_planeRect.left ||
_planeRect.right > other->_planeRect.right ||
_planeRect.bottom > other->_planeRect.bottom
) {
// the plane moved or got larger
_redrawAllCount = g_sci->_gfxFrameout->getScreenCount();
_moved = g_sci->_gfxFrameout->getScreenCount();
} else if (_planeRect != other->_planeRect) {
// the plane got smaller
_moved = g_sci->_gfxFrameout->getScreenCount();
}
if (_priority != other->_priority) {
_priorityChanged = g_sci->_gfxFrameout->getScreenCount();
}
if (_pictureId != other->_pictureId || _mirrored != other->_mirrored || _pictureChanged) {
deleteAllPics();
setType();
changePic();
_redrawAllCount = g_sci->_gfxFrameout->getScreenCount();
}
if (_back != other->_back) {
_redrawAllCount = g_sci->_gfxFrameout->getScreenCount();
}
}
_deleted = 0;
if (_created == 0) {
_updated = g_sci->_gfxFrameout->getScreenCount();
}
convertGameRectToPlaneRect();
_screenRect = _planeRect;
// screenRect was retrieved through globals in SSCI instead of being passed
// into the function. We don't do that just to avoid the extra indirection
clipScreenRect(screenRect);
}
void Plane::update(const reg_t object) {
SegManager *segMan = g_sci->getEngineState()->_segMan;
_vanishingPoint.x = readSelectorValue(segMan, object, SELECTOR(vanishingX));
_vanishingPoint.y = readSelectorValue(segMan, object, SELECTOR(vanishingY));
if (g_sci->_features->usesAlternateSelectors()) {
_gameRect.left = readSelectorValue(segMan, object, SELECTOR(left));
_gameRect.top = readSelectorValue(segMan, object, SELECTOR(top));
_gameRect.right = readSelectorValue(segMan, object, SELECTOR(right)) + 1;
_gameRect.bottom = readSelectorValue(segMan, object, SELECTOR(bottom)) + 1;
} else {
_gameRect.left = readSelectorValue(segMan, object, SELECTOR(inLeft));
_gameRect.top = readSelectorValue(segMan, object, SELECTOR(inTop));
_gameRect.right = readSelectorValue(segMan, object, SELECTOR(inRight)) + 1;
_gameRect.bottom = readSelectorValue(segMan, object, SELECTOR(inBottom)) + 1;
}
convertGameRectToPlaneRect();
_priority = readSelectorValue(segMan, object, SELECTOR(priority));
GuiResourceId pictureId = readSelectorValue(segMan, object, SELECTOR(picture));
if (_pictureId != pictureId) {
_pictureId = pictureId;
_pictureChanged = true;
}
_mirrored = readSelectorValue(segMan, object, SELECTOR(mirrored));
_back = readSelectorValue(segMan, object, SELECTOR(back));
}
void Plane::scrollScreenItems(const int16 deltaX, const int16 deltaY, const bool scrollPics) {
_redrawAllCount = g_sci->_gfxFrameout->getScreenCount();
for (ScreenItemList::iterator it = _screenItemList.begin(); it != _screenItemList.end(); ++it) {
if (*it != nullptr) {
ScreenItem &screenItem = **it;
if (!screenItem._deleted && (screenItem._celInfo.type != kCelTypePic || scrollPics)) {
screenItem._position.x += deltaX;
screenItem._position.y += deltaY;
}
}
}
}
void Plane::remapMarkRedraw() {
ScreenItemList::size_type screenItemCount = _screenItemList.size();
for (ScreenItemList::size_type i = 0; i < screenItemCount; ++i) {
ScreenItem *screenItem = _screenItemList[i];
if (
screenItem != nullptr &&
!screenItem->_deleted && !screenItem->_created &&
screenItem->getCelObj()._remap
) {
screenItem->_updated = g_sci->_gfxFrameout->getScreenCount();
}
}
}
#pragma mark -
#pragma mark PlaneList
void PlaneList::add(Plane *plane) {
for (iterator it = begin(); it != end(); ++it) {
if ((*it)->_priority > plane->_priority) {
insert(it, plane);
return;
}
}
push_back(plane);
}
void PlaneList::clear() {
for (iterator it = begin(); it != end(); ++it) {
delete *it;
}
PlaneListBase::clear();
}
void PlaneList::erase(Plane *plane) {
for (iterator it = begin(); it != end(); ++it) {
if (*it == plane) {
erase(it);
break;
}
}
}
PlaneList::iterator PlaneList::erase(iterator it) {
delete *it;
return PlaneListBase::erase(it);
}
int PlaneList::findIndexByObject(const reg_t object) const {
for (size_type i = 0; i < size(); ++i) {
if ((*this)[i] != nullptr && (*this)[i]->_object == object) {
return i;
}
}
return -1;
}
Plane *PlaneList::findByObject(const reg_t object) const {
const_iterator planeIt = Common::find_if(begin(), end(), FindByObject<Plane *>(object));
if (planeIt == end()) {
return nullptr;
}
return *planeIt;
}
int16 PlaneList::getTopPlanePriority() const {
if (size() > 0) {
return (*this)[size() - 1]->_priority;
}
return 0;
}
int16 PlaneList::getTopSciPlanePriority() const {
int16 priority = 0;
for (const_iterator it = begin(); it != end(); ++it) {
if ((*it)->_priority >= 10000) {
break;
}
priority = (*it)->_priority;
}
return priority;
}
void PlaneList::remove_at(size_type index) {
delete PlaneListBase::remove_at(index);
}
} // End of namespace Sci
|