From facc29b6d782c070b506d75a86a9d00f39647bbc Mon Sep 17 00:00:00 2001 From: nordsoft Date: Thu, 1 Sep 2022 15:54:39 +0400 Subject: [PATCH] Show objects layer --- mapeditor/maphandler.cpp | 61 ++++++++++++++++++++++++++------ mapeditor/maphandler.h | 2 ++ mapeditor/mapview.cpp | 76 ++++++++++++++++++++++++++++++++++++++++ mapeditor/mapview.h | 18 ++++++++++ 4 files changed, 147 insertions(+), 10 deletions(-) diff --git a/mapeditor/maphandler.cpp b/mapeditor/maphandler.cpp index 375cc7e09..242f6c7c8 100644 --- a/mapeditor/maphandler.cpp +++ b/mapeditor/maphandler.cpp @@ -331,28 +331,32 @@ MapHandler::AnimBitmapHolder MapHandler::findObjectBitmap(const CGObjectInstance return MapHandler::AnimBitmapHolder(bitmap); } +const std::vector & MapHandler::getObjects(int x, int y, int z) +{ + return ttiles[z * (sizes.x * sizes.y) + y * sizes.x + x].objects; +} + void MapHandler::drawObjects(QPainter & painter, int x, int y, int z) { - auto & objects = ttiles[z * (sizes.x * sizes.y) + y * sizes.x + x].objects; - for(auto & object : objects) + for(auto & object : getObjects(x, y, z)) { - - //if(object.visi) const CGObjectInstance * obj = object.obj; if (!obj) { logGlobal->error("Stray map object that isn't fading"); - continue; + return; } - + uint8_t animationFrame = 0; - + auto objData = findObjectBitmap(obj, animationFrame); if (objData.objBitmap) { - QRect srcRect(object.rect.x() + x * 32, object.rect.y() + y * 32, tileSize, tileSize); - - painter.drawImage(x * 32 - object.rect.x(), y * 32 - object.rect.y(), *objData.objBitmap); + auto pos = obj->getPosition(); + QRect srcRect(object.rect.x() + pos.x * 32, object.rect.y() + pos.y * 32, tileSize, tileSize); + + painter.drawImage(QPoint(x * 32, y * 32), *objData.objBitmap, object.rect); + //painter.drawImage(pos.x * 32 - object.rect.x(), pos.y * 32 - object.rect.y(), *objData.objBitmap); //drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving); if (objData.flagBitmap) { @@ -371,3 +375,40 @@ void MapHandler::drawObjects(QPainter & painter, int x, int y, int z) } } } + +void MapHandler::drawObject(QPainter & painter, const TerrainTileObject & object) +{ + //if(object.visi) + const CGObjectInstance * obj = object.obj; + if (!obj) + { + logGlobal->error("Stray map object that isn't fading"); + return; + } + + uint8_t animationFrame = 0; + + auto objData = findObjectBitmap(obj, animationFrame); + if (objData.objBitmap) + { + auto pos = obj->getPosition(); + QRect srcRect(object.rect.x() + pos.x * 32, object.rect.y() + pos.y * 32, tileSize, tileSize); + + painter.drawImage(pos.x * 32 - object.rect.x(), pos.y * 32 - object.rect.y(), *objData.objBitmap); + //drawObject(targetSurf, objData.objBitmap, &srcRect, objData.isMoving); + if (objData.flagBitmap) + { + /*if (objData.isMoving) + { + srcRect.y += FRAMES_PER_MOVE_ANIM_GROUP * 2 - tileSize; + Rect dstRect(realPos.x, realPos.y - tileSize / 2, tileSize, tileSize); + drawHeroFlag(targetSurf, objData.flagBitmap, &srcRect, &dstRect, true); + } + else if (obj->pos.x == pos.x && obj->pos.y == pos.y) + { + Rect dstRect(realPos.x - 2 * tileSize, realPos.y - tileSize, 3 * tileSize, 2 * tileSize); + drawHeroFlag(targetSurf, objData.flagBitmap, nullptr, &dstRect, false); + }*/ + } + } +} diff --git a/mapeditor/maphandler.h b/mapeditor/maphandler.h index d8431219d..144c3fb3b 100644 --- a/mapeditor/maphandler.h +++ b/mapeditor/maphandler.h @@ -93,6 +93,8 @@ public: //void drawRoad(const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const; /// draws all objects on current tile (higher-level logic, unlike other draw*** methods) void drawObjects(QPainter & painter, int x, int y, int z); + void drawObject(QPainter & painter, const TerrainTileObject & object); + const std::vector & getObjects(int x, int y, int z); //void drawObject(SDL_Surface * targetSurf, std::shared_ptr source, SDL_Rect * sourceRect, bool moving) const; //void drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr source, SDL_Rect * sourceRect, SDL_Rect * destRect, bool moving) const; diff --git a/mapeditor/mapview.cpp b/mapeditor/mapview.cpp index 70bd58791..846c65535 100644 --- a/mapeditor/mapview.cpp +++ b/mapeditor/mapview.cpp @@ -102,6 +102,7 @@ MapScene::MapScene(MainWindow *parent, int l): passabilityView(parent, this), selectionTerrainView(parent, this), terrainView(parent, this), + objectsView(parent, this), main(parent), level(l) { @@ -109,12 +110,15 @@ MapScene::MapScene(MainWindow *parent, int l): void MapScene::updateViews() { + //sequence is important because it defines rendering order terrainView.update(); + objectsView.update(); gridView.update(); passabilityView.update(); selectionTerrainView.update(); terrainView.show(true); + objectsView.show(true); selectionTerrainView.show(true); } @@ -330,6 +334,8 @@ void TerrainView::draw(bool onlyDirty) return; auto map = main->getMap(); + if(!map) + return; QPainter painter(pixmap.get()); painter.setCompositionMode(QPainter::CompositionMode_Source); @@ -366,5 +372,75 @@ void TerrainView::draw(bool onlyDirty) main->getMapHandler()->drawTerrainTile(painter, i, j, scene->level); } + dirty.clear(); redraw(); } + +ObjectsView::ObjectsView(MainWindow * m, MapScene * s): BasicView(m, s) +{ +} + +void ObjectsView::update() +{ + auto map = main->getMap(); + if(!map) + return; + + pixmap.reset(new QPixmap(map->width * 32, map->height * 32)); + pixmap->fill(QColor(0, 0, 0, 0)); + draw(false); +} + +void ObjectsView::draw(bool onlyDirty) +{ + if(!pixmap) + return; + + auto map = main->getMap(); + if(!map) + return; + + pixmap->fill(QColor(0, 0, 0, 0)); + QPainter painter(pixmap.get()); + //painter.setCompositionMode(QPainter::CompositionMode_SourceOver); + std::set drawen; + + + for(int j = 0; j < map->height; ++j) + { + for(int i = 0; i < map->width; ++i) + { + main->getMapHandler()->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); + } + }*/ + } + } + + dirty.clear(); + redraw(); +} + +void ObjectsView::setDirty(int x, int y) +{ + auto & objects = main->getMapHandler()->getObjects(x, y, scene->level); + for(auto & object : objects) + { + if(object.obj) + dirty.insert(object.obj); + } +} + +void ObjectsView::setDirty(const CGObjectInstance * object) +{ + dirty.insert(object); +} diff --git a/mapeditor/mapview.h b/mapeditor/mapview.h index c1ff64118..ade95f457 100644 --- a/mapeditor/mapview.h +++ b/mapeditor/mapview.h @@ -6,6 +6,7 @@ #include "../lib/int3.h" class int3; +class CGObjectInstance; class MainWindow; class MapScene; @@ -108,6 +109,22 @@ private: std::set dirty; }; +class ObjectsView: public BasicView +{ +public: + ObjectsView(MainWindow * m, MapScene * s); + + void update() override; + + void draw(bool onlyDirty = true); + + void setDirty(int x, int y); + void setDirty(const CGObjectInstance * object); + +private: + std::set dirty; +}; + class MapScene : public QGraphicsScene { public: @@ -119,6 +136,7 @@ public: PassabilityView passabilityView; SelectionTerrainView selectionTerrainView; TerrainView terrainView; + ObjectsView objectsView; const int level;