1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-04 22:14:25 +02:00

Smart invalidation for move/erase/new object

This commit is contained in:
nordsoft 2022-12-03 23:11:40 +04:00
parent fd21f9ead2
commit bc37ceb7f3
2 changed files with 42 additions and 28 deletions

View File

@ -311,6 +311,7 @@ void MapController::commitObjectErase(int level)
{ {
//invalidate tiles under objects //invalidate tiles under objects
_mapHandler->invalidate(_mapHandler->getTilesUnderObject(obj)); _mapHandler->invalidate(_mapHandler->getTilesUnderObject(obj));
_scenes[level]->objectsView.setDirty(obj);
} }
_scenes[level]->selectionObjectsView.clear(); _scenes[level]->selectionObjectsView.clear();
@ -371,7 +372,7 @@ void MapController::commitObstacleFill(int level)
_scenes[level]->selectionTerrainView.clear(); _scenes[level]->selectionTerrainView.clear();
_scenes[level]->selectionTerrainView.draw(); _scenes[level]->selectionTerrainView.draw();
_scenes[level]->objectsView.draw(); _scenes[level]->objectsView.draw(false); //TODO: enable smart invalidation (setDirty)
_scenes[level]->passabilityView.update(); _scenes[level]->passabilityView.update();
_miniscenes[level]->updateViews(); _miniscenes[level]->updateViews();
@ -380,8 +381,8 @@ void MapController::commitObstacleFill(int level)
void MapController::commitObjectChange(int level) void MapController::commitObjectChange(int level)
{ {
//for( auto * o : _scenes[level]->selectionObjectsView.getSelection()) for( auto * o : _scenes[level]->selectionObjectsView.getSelection())
//_mapHandler->invalidate(o); _scenes[level]->objectsView.setDirty(o);
_scenes[level]->objectsView.draw(); _scenes[level]->objectsView.draw();
_scenes[level]->selectionObjectsView.draw(); _scenes[level]->selectionObjectsView.draw();
@ -411,6 +412,7 @@ void MapController::commitObjectShift(int level)
pos.x += shift.x(); pos.y += shift.y(); pos.x += shift.x(); pos.y += shift.y();
auto prevPositions = _mapHandler->getTilesUnderObject(obj); auto prevPositions = _mapHandler->getTilesUnderObject(obj);
_scenes[level]->objectsView.setDirty(obj); //set dirty before movement
_map->getEditManager()->moveObject(obj, pos); _map->getEditManager()->moveObject(obj, pos);
_mapHandler->invalidate(prevPositions); _mapHandler->invalidate(prevPositions);
_mapHandler->invalidate(obj); _mapHandler->invalidate(obj);
@ -450,6 +452,7 @@ void MapController::commitObjectCreate(int level)
_map->getEditManager()->insertObject(newObj); _map->getEditManager()->insertObject(newObj);
_mapHandler->invalidate(newObj); _mapHandler->invalidate(newObj);
_scenes[level]->objectsView.setDirty(newObj);
_scenes[level]->selectionObjectsView.newObject = nullptr; _scenes[level]->selectionObjectsView.newObject = nullptr;
_scenes[level]->selectionObjectsView.shift = QPoint(0, 0); _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
@ -495,7 +498,7 @@ void MapController::undo()
{ {
_map->getEditManager()->getUndoManager().undo(); _map->getEditManager()->getUndoManager().undo();
resetMapHandler(); resetMapHandler();
sceneForceUpdate(); sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
main->mapChanged(); main->mapChanged();
} }
@ -503,6 +506,6 @@ void MapController::redo()
{ {
_map->getEditManager()->getUndoManager().redo(); _map->getEditManager()->getUndoManager().redo();
resetMapHandler(); resetMapHandler();
sceneForceUpdate(); sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
main->mapChanged(); main->mapChanged();
} }

View File

@ -295,28 +295,33 @@ void ObjectsLayer::draw(bool onlyDirty)
if(!map) if(!map)
return; return;
pixmap->fill(Qt::transparent);
QPainter painter(pixmap.get()); QPainter painter(pixmap.get());
std::set<const CGObjectInstance *> drawen; std::set<const CGObjectInstance *> drawen;
if(onlyDirty)
{
//objects could be modified
for(auto * obj : objDirty)
setDirty(obj);
//clear tiles which will be redrawn. It's needed because some object could be replaced
painter.setCompositionMode(QPainter::CompositionMode_Source);
for(auto & p : dirty)
painter.fillRect(p.x * 32, p.y * 32, 32, 32, Qt::transparent);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
for(auto & p : dirty)
handler->drawObjects(painter, p.x, p.y, p.z);
}
else
{
pixmap->fill(Qt::transparent);
for(int j = 0; j < map->height; ++j) for(int j = 0; j < map->height; ++j)
{ {
for(int i = 0; i < map->width; ++i) for(int i = 0; i < map->width; ++i)
{ {
handler->drawObjects(painter, i, j, scene->level); handler->drawObjects(painter, i, j, scene->level);
/*auto & objects = main->getMapHandler()->getObjects(i, j, scene->level);
for(auto & object : objects)
{
if(!object.obj || drawen.count(object.obj))
continue;
if(!onlyDirty || dirty.count(object.obj))
{
main->getMapHandler()->drawObject(painter, object);
drawen.insert(object.obj);
} }
}*/
} }
} }
@ -326,16 +331,22 @@ void ObjectsLayer::draw(bool onlyDirty)
void ObjectsLayer::setDirty(int x, int y) void ObjectsLayer::setDirty(int x, int y)
{ {
/*auto & objects = main->getMapHandler()->getObjects(x, y, scene->level); int3 pos(x, y, scene->level);
for(auto & object : objects) if(map->isInTheMap(pos))
{ dirty.insert(pos);
if(object.obj)
dirty.insert(object.obj);
}*/
} }
void ObjectsLayer::setDirty(const CGObjectInstance * object) void ObjectsLayer::setDirty(const CGObjectInstance * object)
{ {
objDirty.insert(object);
//mark tiles under object as dirty
for(int j = 0; j < object->getHeight(); ++j)
{
for(int i = 0; i < object->getWidth(); ++i)
{
setDirty(object->getPosition().x - i, object->getPosition().y - j);
}
}
} }
SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractLayer(s), newObject(nullptr) SelectionObjectsLayer::SelectionObjectsLayer(MapSceneBase * s): AbstractLayer(s), newObject(nullptr)