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
|
/***************************************************************************
* Copyright (C) 2005-2013 by the FIFE team *
* http://www.fifengine.net *
* This file is part of FIFE. *
* *
* FIFE is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
// Standard C++ library includes
#include <cfloat>
// 3rd party library includes
// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "model/metamodel/grids/cellgrid.h"
#include "model/metamodel/action.h"
#include "model/structures/layer.h"
#include "model/structures/instance.h"
#include "model/structures/location.h"
#include "util/base/exception.h"
#include "util/log/logger.h"
#include "util/math/fife_math.h"
#include "util/math/angles.h"
#include "video/renderbackend.h"
#include "video/image.h"
#include "video/animation.h"
#include "video/imagemanager.h"
#include "camera.h"
#include "layercache.h"
#include "visual.h"
namespace FIFE {
/** Logger to use for this source file.
* @relates Logger
*/
static Logger _log(LM_CAMERA);
// Due to image alignment, there is additional additions on image dimensions.
const double OVERDRAW = 1.5;
class CacheLayerChangeListener : public LayerChangeListener {
public:
CacheLayerChangeListener(LayerCache* cache) {
m_cache = cache;
}
virtual ~CacheLayerChangeListener() {};
virtual void onLayerChanged(Layer* layer, std::vector<Instance*>& instances) {
for(std::vector<Instance*>::iterator i = instances.begin();
i != instances.end(); ++i) {
m_cache->updateInstance(*i);
}
}
virtual void onInstanceCreate(Layer* layer, Instance* instance) {
m_cache->addInstance(instance);
}
virtual void onInstanceDelete(Layer* layer, Instance* instance) {
m_cache->removeInstance(instance);
}
private:
LayerCache* m_cache;
};
/** Comparison functions for sorting
*/
// used screenpoint z for sorting, calculated from camera
class InstanceDistanceSortCamera {
public:
inline bool operator()(RenderItem* const & lhs, RenderItem* const & rhs) {
if (Mathd::Equal(lhs->screenpoint.z, rhs->screenpoint.z)) {
InstanceVisual* liv = lhs->instance->getVisual<InstanceVisual>();
InstanceVisual* riv = rhs->instance->getVisual<InstanceVisual>();
return liv->getStackPosition() < riv->getStackPosition();
}
return lhs->screenpoint.z < rhs->screenpoint.z;
}
};
// used instance location and camera rotation for sorting
class InstanceDistanceSortLocation {
public:
InstanceDistanceSortLocation(double rotation) {
if ((rotation >= 0) && (rotation <= 60)) { // 30 deg
xtox = 0;
xtoy = -1;
ytox = 1;
ytoy = 0.5;
} else if ((rotation >= 60) && (rotation <= 120)) { // 90 deg
xtox = -1;
xtoy = -1;
ytox = 0.5;
ytoy = -0.5;
} else if ((rotation >= 120) && (rotation <= 180)) { // 150 deg
xtox = 0;
xtoy = -1;
ytox = -1;
ytoy = -0.5;
} else if ((rotation >= 180) && (rotation <= 240)) { // 210 deg
xtox = 0;
xtoy = 1;
ytox = -1;
ytoy = -0.5;
} else if ((rotation >= 240) && (rotation <= 300)) { // 270 deg
xtox = 1;
xtoy = 1;
ytox = -0.5;
ytoy = 0.5;
} else if ((rotation >= 300) && (rotation <= 360)) { // 330 deg
xtox = 0;
xtoy = 1;
ytox = 1;
ytoy = 0.5;
}
}
inline bool operator()(RenderItem* const & lhs, RenderItem* const & rhs) {
ExactModelCoordinate lpos = lhs->instance->getLocationRef().getExactLayerCoordinates();
ExactModelCoordinate rpos = rhs->instance->getLocationRef().getExactLayerCoordinates();
lpos.x += lpos.y / 2;
rpos.x += rpos.y / 2;
InstanceVisual* liv = lhs->instance->getVisual<InstanceVisual>();
InstanceVisual* riv = rhs->instance->getVisual<InstanceVisual>();
int32_t lvc = ceil(xtox*lpos.x + ytox*lpos.y) + ceil(xtoy*lpos.x + ytoy*lpos.y) + liv->getStackPosition();
int32_t rvc = ceil(xtox*rpos.x + ytox*rpos.y) + ceil(xtoy*rpos.x + ytoy*rpos.y) + riv->getStackPosition();
if (lvc == rvc) {
if (Mathd::Equal(lpos.z, rpos.z)) {
return liv->getStackPosition() < riv->getStackPosition();
}
return lpos.z < rpos.z;
}
return lvc < rvc;
}
private:
double xtox;
double xtoy;
double ytox;
double ytoy;
};
// used screenpoint z for sorting and as fallback first the instance location z and then the stack position
class InstanceDistanceSortCameraAndLocation {
public:
inline bool operator()(RenderItem* const & lhs, RenderItem* const & rhs) {
if (Mathd::Equal(lhs->screenpoint.z, rhs->screenpoint.z)) {
const ExactModelCoordinate& lpos = lhs->instance->getLocationRef().getExactLayerCoordinatesRef();
const ExactModelCoordinate& rpos = rhs->instance->getLocationRef().getExactLayerCoordinatesRef();
if (Mathd::Equal(lpos.z, rpos.z)) {
InstanceVisual* liv = lhs->instance->getVisual<InstanceVisual>();
InstanceVisual* riv = rhs->instance->getVisual<InstanceVisual>();
return liv->getStackPosition() < riv->getStackPosition();
}
return lpos.z < rpos.z;
}
return lhs->screenpoint.z < rhs->screenpoint.z;
}
};
LayerCache::LayerCache(Camera* camera) {
m_camera = camera;
m_layer = 0;
m_tree = 0;
m_zMin = 0.0;
m_zMax = 0.0;
m_zoom = camera->getZoom();
m_zoomed = !Mathd::Equal(m_zoom, 1.0);
m_straightZoom = Mathd::Equal(fmod(m_zoom, 1.0), 0.0);
if(RenderBackend::instance()->getName() == "OpenGLe") {
m_needSorting = false;
} else {
m_needSorting = true;
}
}
LayerCache::~LayerCache() {
// removes all Entries
for (std::vector<Entry*>::iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
delete *it;
}
// removes all RenderItems
for (std::vector<RenderItem*>::iterator it = m_renderItems.begin(); it != m_renderItems.end(); ++it) {
delete *it;
}
m_layer->removeChangeListener(m_layerObserver);
delete m_layerObserver;
delete m_tree;
}
void LayerCache::setLayer(Layer* layer) {
if (m_layer != layer) {
if (m_layer) {
m_layer->removeChangeListener(m_layerObserver);
delete m_layerObserver;
}
m_layer = layer;
m_layerObserver = new CacheLayerChangeListener(this);
layer->addChangeListener(m_layerObserver);
reset();
}
}
void LayerCache::reset() {
// removes all Entries
for (std::vector<Entry*>::iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
delete *it;
}
m_entries.clear();
// removes all RenderItems
for (std::vector<RenderItem*>::iterator it = m_renderItems.begin(); it != m_renderItems.end(); ++it) {
delete *it;
}
m_renderItems.clear();
m_instance_map.clear();
m_entriesToUpdate.clear();
m_freeEntries.clear();
m_cacheImage.reset();
delete m_tree;
m_tree = new CacheTree;
const std::vector<Instance*>& instances = m_layer->getInstances();
for(std::vector<Instance*>::const_iterator i = instances.begin();
i != instances.end(); ++i) {
addInstance(*i);
}
}
void LayerCache::addInstance(Instance* instance) {
assert(m_instance_map.find(instance) == m_instance_map.end());
RenderItem* item;
Entry* entry;
if (m_freeEntries.empty()) {
// creates new RenderItem
item = new RenderItem(instance);
m_renderItems.push_back(item);
m_instance_map[instance] = m_renderItems.size() - 1;
// creates new Entry
entry = new Entry();
m_entries.push_back(entry);
entry->instanceIndex = m_renderItems.size() - 1;
entry->entryIndex = m_entries.size() - 1;
} else {
// uses free/unused RenderItem
int32_t index = m_freeEntries.front();
m_freeEntries.pop_front();
item = m_renderItems[index];
item->instance = instance;
m_instance_map[instance] = index;
// uses free/unused Entry
entry = m_entries[index];
entry->instanceIndex = index;
entry->entryIndex = index;
}
entry->node = 0;
entry->forceUpdate = true;
entry->visible = true;
entry->updateInfo = EntryFullUpdate;
m_entriesToUpdate.insert(entry->entryIndex);
}
void LayerCache::removeInstance(Instance* instance) {
assert(m_instance_map.find(instance) != m_instance_map.end());
Entry* entry = m_entries[m_instance_map[instance]];
assert(entry->instanceIndex == m_instance_map[instance]);
RenderItem* item = m_renderItems[entry->instanceIndex];
// removes entry from updates
std::set<int32_t>::iterator it = m_entriesToUpdate.find(entry->entryIndex);
if (it != m_entriesToUpdate.end()) {
m_entriesToUpdate.erase(it);
}
// removes entry from CacheTree
if (entry->node) {
entry->node->data().erase(entry->entryIndex);
entry->node = 0;
}
entry->instanceIndex = -1;
entry->forceUpdate = false;
m_instance_map.erase(instance);
// removes instance from RenderList
RenderList& renderList = m_camera->getRenderListRef(m_layer);
for (RenderList::iterator it = renderList.begin(); it != renderList.end(); ++it) {
if ((*it)->instance == instance) {
renderList.erase(it);
break;
}
}
// resets RenderItem
item->reset();
// adds free entry
m_freeEntries.push_back(entry->entryIndex);
}
void LayerCache::updateInstance(Instance* instance) {
Entry* entry = m_entries[m_instance_map[instance]];
if (entry->instanceIndex == -1) {
return;
}
// convert necessary instance update flags to entry update flags
const InstanceChangeInfo ici = instance->getChangeInfo();
if ((ici & ICHANGE_LOC) == ICHANGE_LOC) {
entry->updateInfo |= EntryPositionUpdate;
}
if ((ici & ICHANGE_ROTATION) == ICHANGE_ROTATION ||
(ici & ICHANGE_ACTION) == ICHANGE_ACTION ||
(ici & ICHANGE_TRANSPARENCY) == ICHANGE_TRANSPARENCY ||
(ici & ICHANGE_VISIBLE) == ICHANGE_VISIBLE) {
entry->updateInfo |= EntryVisualUpdate;
}
// if entry is not already inserted
if (!entry->forceUpdate && entry->updateInfo != EntryNoneUpdate) {
entry->forceUpdate = true;
m_entriesToUpdate.insert(entry->entryIndex);
}
}
class CacheTreeCollector {
std::vector<int32_t>& m_indices;
Rect m_viewport;
public:
CacheTreeCollector(std::vector<int32_t>& indices, const Rect& viewport)
: m_indices(indices), m_viewport(viewport) {
}
bool visit(LayerCache::CacheTree::Node* node, int32_t d = -1);
};
bool CacheTreeCollector::visit(LayerCache::CacheTree::Node* node, int32_t d) {
if(!m_viewport.intersects(Rect(node->x(), node->y(),node->size(),node->size()))) {
return false;
}
m_indices.insert(m_indices.end(), node->data().begin(), node->data().end());
return true;
}
void LayerCache::collect(const Rect& viewport, std::vector<int32_t>& index_list) {
CacheTree::Node * node = m_tree->find_container(viewport);
CacheTreeCollector collector(index_list, viewport);
node->apply_visitor(collector);
node = node->parent();
while(node) {
collector.visit(node);
node = node->parent();
}
}
void LayerCache::update(Camera::Transform transform, RenderList& renderlist) {
if(!m_layer->areInstancesVisible()) {
FL_DBG(_log, "Layer instances hidden");
renderlist.clear();
return;
}
// if transform is none then we have only to update the instances with an update info.
if (transform == Camera::NoneTransform) {
if (!m_entriesToUpdate.empty()) {
std::set<int32_t> entryToRemove;
updateEntries(entryToRemove, renderlist);
//std::cout << "update entries: " << int32_t(m_entriesToUpdate.size()) << " remove entries: " << int32_t(entryToRemove.size()) <<"\n";
if (!entryToRemove.empty()) {
std::set<int32_t>::iterator entry_it = entryToRemove.begin();
for (; entry_it != entryToRemove.end(); ++entry_it) {
m_entriesToUpdate.erase(*entry_it);
}
}
}
} else {
m_zoom = m_camera->getZoom();
m_zoomed = !Mathd::Equal(m_zoom, 1.0);
m_straightZoom = Mathd::Equal(fmod(m_zoom, 1.0), 0.0);
// clear old renderlist
renderlist.clear();
// update all entries
fullUpdate(transform);
// create viewport coordinates to collect entries
Rect viewport = m_camera->getViewPort();
Rect screenViewport = viewport;
DoublePoint3D viewport_a = m_camera->screenToVirtualScreen(Point3D(viewport.x, viewport.y));
DoublePoint3D viewport_b = m_camera->screenToVirtualScreen(Point3D(viewport.right(), viewport.bottom()));
viewport.x = static_cast<int32_t>(std::min(viewport_a.x, viewport_b.x));
viewport.y = static_cast<int32_t>(std::min(viewport_a.y, viewport_b.y));
viewport.w = static_cast<int32_t>(std::max(viewport_a.x, viewport_b.x) - viewport.x);
viewport.h = static_cast<int32_t>(std::max(viewport_a.y, viewport_b.y) - viewport.y);
m_zMin = 0.0;
m_zMax = 0.0;
// FL_LOG(_log, LMsg("camera-update viewport") << viewport);
std::vector<int32_t> index_list;
collect(viewport, index_list);
// fill renderlist
for (uint32_t i = 0; i != index_list.size(); ++i) {
Entry* entry = m_entries[index_list[i]];
RenderItem* item = m_renderItems[entry->instanceIndex];
if (!item->image || !entry->visible) {
continue;
}
if (item->dimensions.intersects(screenViewport)) {
renderlist.push_back(item);
if (!m_needSorting) {
m_zMin = std::min(m_zMin, item->screenpoint.z);
m_zMax = std::max(m_zMax, item->screenpoint.z);
}
}
}
if (m_needSorting) {
sortRenderList(renderlist);
} else {
m_zMin -= 0.5;
m_zMax += 0.5;
sortRenderList(renderlist);
}
}
}
void LayerCache::fullUpdate(Camera::Transform transform) {
bool rotationChange = (transform & Camera::RotationTransform) == Camera::RotationTransform;
for (uint32_t i = 0; i != m_entries.size(); ++i) {
Entry* entry = m_entries[i];
if (entry->instanceIndex != -1) {
if (rotationChange || entry->forceUpdate) {
updateVisual(entry);
}
updatePosition(entry);
}
}
}
void LayerCache::updateEntries(std::set<int32_t>& removes, RenderList& renderlist) {
RenderList needSorting;
Rect viewport = m_camera->getViewPort();
std::set<int32_t>::const_iterator entry_it = m_entriesToUpdate.begin();
for (; entry_it != m_entriesToUpdate.end(); ++entry_it) {
Entry* entry = m_entries[*entry_it];
entry->forceUpdate = false;
if (entry->instanceIndex == -1) {
entry->updateInfo = EntryNoneUpdate;
removes.insert(*entry_it);
continue;
}
RenderItem* item = m_renderItems[entry->instanceIndex];
bool positionUpdate = (entry->updateInfo & EntryPositionUpdate) == EntryPositionUpdate;
if ((entry->updateInfo & EntryVisualUpdate) == EntryVisualUpdate) {
positionUpdate |= updateVisual(entry);
}
bool onScreenA = entry->visible && item->image && item->dimensions.intersects(viewport);
if (positionUpdate) {
updatePosition(entry);
}
bool onScreenB = entry->visible && item->image && item->dimensions.intersects(viewport);
if (onScreenA != onScreenB) {
if (!onScreenA) {
// add to renderlist and sort
renderlist.push_back(item);
needSorting.push_back(item);
} else {
// remove from renderlist
for (RenderList::iterator it = renderlist.begin(); it != renderlist.end(); ++it) {
if ((*it)->instance == item->instance) {
renderlist.erase(it);
break;
}
}
}
} else if (onScreenA && onScreenB && positionUpdate) {
// sort
needSorting.push_back(item);
}
if (!entry->forceUpdate || !entry->visible) {
entry->forceUpdate = false;
entry->updateInfo = EntryNoneUpdate;
removes.insert(*entry_it);
} else {
entry->updateInfo = EntryVisualUpdate;
}
}
if (!needSorting.empty()) {
if (m_needSorting) {
sortRenderList(renderlist);
} else {
sortRenderList(needSorting);
}
}
}
bool LayerCache::updateVisual(Entry* entry) {
RenderItem* item = m_renderItems[entry->instanceIndex];
Instance* instance = item->instance;
InstanceVisual* visual = instance->getVisual<InstanceVisual>();
item->facingAngle = instance->getRotation();
int32_t angle = static_cast<int32_t>(m_camera->getRotation()) + item->facingAngle;
Action* action = instance->getCurrentAction();
ImagePtr image;
if (visual) {
uint8_t layerTrans = m_layer->getLayerTransparency();
uint8_t instanceTrans = visual->getTransparency();
if (layerTrans != 0) {
if (instanceTrans != 0) {
uint8_t calcTrans = layerTrans - instanceTrans;
if (calcTrans >= 0) {
instanceTrans = calcTrans;
} else {
instanceTrans = 0;
}
} else {
instanceTrans = layerTrans;
}
}
item->transparency = 255 - instanceTrans;
// only visible if visual is visible and item is not totally transparent
entry->visible = (visual->isVisible() && item->transparency != 0);
}
if (!action) {
// Try static images then default action.
int32_t image_id = item->getStaticImageIndexByAngle(angle, instance);
if (image_id == -1) {
if (!instance->getObject()->isStatic()) {
action = instance->getObject()->getDefaultAction();
}
} else {
image = ImageManager::instance()->get(image_id);
}
}
entry->forceUpdate = (action != 0);
if (action) {
AnimationPtr animation = action->getVisual<ActionVisual>()->getAnimationByAngle(angle);
uint32_t animationTime = instance->getActionRuntime() % animation->getDuration();
image = animation->getFrameByTimestamp(animationTime);
int32_t actionFrame = animation->getActionFrame();
if (actionFrame != -1) {
if (item->image != image) {
int32_t new_frame = animation->getFrameIndex(animationTime);
if (actionFrame == new_frame) {
instance->callOnActionFrame(action, actionFrame);
// if action frame was skipped
} else if (new_frame > actionFrame && item->currentFrame < actionFrame) {
instance->callOnActionFrame(action, actionFrame);
}
item->currentFrame = new_frame;
}
}
}
bool newPosition = false;
if (image != item->image) {
if (!item->image || !image) {
newPosition = true;
} else if (image->getWidth() != item->image->getWidth() ||
image->getHeight() != item->image->getHeight() ||
image->getXShift() != item->image->getXShift() ||
image->getYShift() != item->image->getYShift()) {
newPosition = true;
}
item->image = image;
}
return newPosition;
}
void LayerCache::updatePosition(Entry* entry) {
RenderItem* item = m_renderItems[entry->instanceIndex];
Instance* instance = item->instance;
ExactModelCoordinate mapCoords = instance->getLocationRef().getMapCoordinates();
DoublePoint3D screenPosition = m_camera->toVirtualScreenCoordinates(mapCoords);
ImagePtr image = item->image;
if (image) {
int32_t w = image->getWidth();
int32_t h = image->getHeight();
screenPosition.x = (screenPosition.x - w / 2.0) + image->getXShift();
screenPosition.y = (screenPosition.y - h / 2.0) + image->getYShift();
item->bbox.w = w;
item->bbox.h = h;
} else {
item->bbox.w = 0;
item->bbox.h = 0;
}
item->screenpoint = screenPosition;
item->bbox.x = static_cast<int32_t>(screenPosition.x);
item->bbox.y = static_cast<int32_t>(screenPosition.y);
// seems wrong but these rounds fix the "wobbling" and gaps between tiles
// in case the zoom is 1.0
if (m_straightZoom) {
item->screenpoint.x = round(item->screenpoint.x);
item->screenpoint.y = round(item->screenpoint.y);
}
Point3D screenPoint = m_camera->virtualScreenToScreen(item->screenpoint);
// NOTE:
// One would expect this to be necessary here,
// however it works the same without, sofar
// m_camera->calculateZValue(screenPoint);
// item->screenpoint.z = -screenPoint.z;
item->dimensions.x = screenPoint.x;
item->dimensions.y = screenPoint.y;
if (m_zoomed) {
// NOTE: Due to image alignment, there is additional additions on image dimensions
// There's probabaly some better solution for this, but works "good enough" for now.
// In case additions are removed, gaps appear between tiles.
// This is only needed if the zoom is a non-integer value.
if (!m_straightZoom) {
item->dimensions.w = round(static_cast<double>(item->bbox.w) * m_zoom + OVERDRAW);
item->dimensions.h = round(static_cast<double>(item->bbox.h) * m_zoom + OVERDRAW);
} else {
item->dimensions.w = round(static_cast<double>(item->bbox.w) * m_zoom);
item->dimensions.h = round(static_cast<double>(item->bbox.h) * m_zoom);
}
} else {
item->dimensions.w = item->bbox.w;
item->dimensions.h = item->bbox.h;
}
CacheTree::Node* node = m_tree->find_container(item->bbox);
if (node) {
if (node != entry->node) {
if (entry->node) {
entry->node->data().erase(entry->entryIndex);
}
entry->node = node;
node->data().insert(entry->entryIndex);
}
}
}
void LayerCache::sortRenderList(RenderList& renderlist) {
if (renderlist.empty()) {
return;
}
// We want to put every z value in [-10,10] range.
// To do it, we simply solve
// { y1 = a*x1 + b
// { y2 = a*x2 + b
// where [y1,y2]' = [-10,10]' is required z range,
// and [x1,x2]' is expected min,max z coords.
if (!m_needSorting) {
double det = m_zMin - m_zMax;
if (fabs(det) > FLT_EPSILON) {
double det_a = -10.0 - 10.0;
double det_b = 10.0 * m_zMin - (-10.0) * m_zMax;
double a = static_cast<float>(det_a / det);
double b = static_cast<float>(det_b / det);
float estimate = sqrtf(static_cast<float>(renderlist.size()));
float stack_delta = fabs(-10.0f - 10.0f) / estimate * 0.1f;
RenderList::iterator it = renderlist.begin();
for ( ; it != renderlist.end(); ++it) {
double& z = (*it)->screenpoint.z;
z = a * z + b;
InstanceVisual* vis = (*it)->instance->getVisual<InstanceVisual>();
z += vis->getStackPosition() * stack_delta;
}
}
} else {
SortingStrategy strat = m_layer->getSortingStrategy();
switch (strat) {
case SORTING_CAMERA: {
InstanceDistanceSortCamera ids;
std::stable_sort(renderlist.begin(), renderlist.end(), ids);
} break;
case SORTING_LOCATION: {
InstanceDistanceSortLocation ids(m_camera->getRotation());
std::stable_sort(renderlist.begin(), renderlist.end(), ids);
} break;
case SORTING_CAMERA_AND_LOCATION: {
InstanceDistanceSortCameraAndLocation ids;
std::stable_sort(renderlist.begin(), renderlist.end(), ids);
} break;
default: {
InstanceDistanceSortCamera ids;
std::stable_sort(renderlist.begin(), renderlist.end(), ids);
} break;
}
}
}
ImagePtr LayerCache::getCacheImage() {
return m_cacheImage;
}
void LayerCache::setCacheImage(ImagePtr image) {
m_cacheImage = image;
}
}
|