2022-12-06 13:41:29 +02:00
|
|
|
/*
|
2022-12-11 23:16:23 +02:00
|
|
|
* BattleFieldController.cpp, part of VCMI engine
|
2022-12-06 13:41:29 +02:00
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
2022-12-09 13:38:46 +02:00
|
|
|
#include "BattleRenderer.h"
|
2022-12-06 13:41:29 +02:00
|
|
|
|
2022-12-09 13:38:46 +02:00
|
|
|
#include "BattleInterface.h"
|
|
|
|
#include "BattleEffectsController.h"
|
|
|
|
#include "BattleSiegeController.h"
|
|
|
|
#include "BattleStacksController.h"
|
|
|
|
#include "BattleObstacleController.h"
|
2022-12-06 13:41:29 +02:00
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
void BattleRenderer::collectObjects()
|
2022-12-06 13:41:29 +02:00
|
|
|
{
|
|
|
|
owner->collectRenderableObjects(*this);
|
|
|
|
owner->effectsController->collectRenderableObjects(*this);
|
|
|
|
owner->obstacleController->collectRenderableObjects(*this);
|
|
|
|
owner->stacksController->collectRenderableObjects(*this);
|
2022-12-06 14:11:37 +02:00
|
|
|
if (owner->siegeController)
|
|
|
|
owner->siegeController->collectRenderableObjects(*this);
|
2022-12-06 13:41:29 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
void BattleRenderer::sortObjects()
|
2022-12-06 13:41:29 +02:00
|
|
|
{
|
|
|
|
auto getRow = [](const RenderableInstance & object) -> int
|
|
|
|
{
|
|
|
|
if (object.tile.isValid())
|
|
|
|
return object.tile.getY();
|
|
|
|
|
|
|
|
if ( object.tile == BattleHex::HEX_BEFORE_ALL )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ( object.tile == BattleHex::HEX_AFTER_ALL )
|
|
|
|
return GameConstants::BFIELD_HEIGHT;
|
|
|
|
|
|
|
|
if ( object.tile == BattleHex::INVALID )
|
|
|
|
return GameConstants::BFIELD_HEIGHT;
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return GameConstants::BFIELD_HEIGHT;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::stable_sort(objects.begin(), objects.end(), [&](const RenderableInstance & left, const RenderableInstance & right){
|
|
|
|
if ( getRow(left) != getRow(right))
|
|
|
|
return getRow(left) < getRow(right);
|
|
|
|
return left.layer < right.layer;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
void BattleRenderer::renderObjects(BattleRenderer::RendererPtr targetCanvas)
|
2022-12-06 13:41:29 +02:00
|
|
|
{
|
|
|
|
for (auto const & object : objects)
|
|
|
|
object.functor(targetCanvas);
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
BattleRenderer::BattleRenderer(BattleInterface * owner):
|
2022-12-06 13:41:29 +02:00
|
|
|
owner(owner)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
void BattleRenderer::insert(EBattleFieldLayer layer, BattleHex tile, BattleRenderer::RenderFunctor functor)
|
2022-12-06 13:41:29 +02:00
|
|
|
{
|
|
|
|
objects.push_back({ functor, layer, tile });
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:26:17 +02:00
|
|
|
void BattleRenderer::execute(BattleRenderer::RendererPtr targetCanvas)
|
2022-12-06 13:41:29 +02:00
|
|
|
{
|
|
|
|
collectObjects();
|
|
|
|
sortObjects();
|
|
|
|
renderObjects(targetCanvas);
|
|
|
|
}
|