Magic_Game/core/Transition/TransitionBase.cpp

136 lines
2.7 KiB
C++
Raw Normal View History

#include "..\e2dbase.h"
#include "..\e2dtransition.h"
#include "..\e2dnode.h"
e2d::TransitionBase::TransitionBase(double duration)
2018-05-08 17:40:36 +08:00
: _bEnd(false)
, _fLast(0)
, _delta(0)
2018-05-08 17:40:36 +08:00
, _pPrevScene(nullptr)
, _pNextScene(nullptr)
, _pPrevLayer(nullptr)
, _pNextLayer(nullptr)
, _sPrevLayerParam()
, _sNextLayerParam()
{
_duration = max(duration, 0);
}
e2d::TransitionBase::~TransitionBase()
{
2018-05-08 17:40:36 +08:00
SafeReleaseInterface(&_pPrevLayer);
SafeReleaseInterface(&_pNextLayer);
}
2018-05-08 11:37:11 +08:00
bool e2d::TransitionBase::isDone()
{
2018-05-08 17:40:36 +08:00
return _bEnd;
}
void e2d::TransitionBase::onDestroy()
{
2018-05-08 17:40:36 +08:00
SafeRelease(&_pPrevScene);
SafeRelease(&_pNextScene);
}
void e2d::TransitionBase::_init(Scene * prev, Scene * next)
{
// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
2018-05-08 17:40:36 +08:00
HRESULT hr = Renderer::getRenderTarget()->CreateLayer(&_pNextLayer);
if (SUCCEEDED(hr))
{
2018-05-08 17:40:36 +08:00
hr = Renderer::getRenderTarget()->CreateLayer(&_pPrevLayer);
}
if (FAILED(hr))
{
ASSERT(false, "Create layer failed!");
}
2018-05-08 17:40:36 +08:00
_fLast = Time::getTotalTime();
_pPrevScene = prev;
_pNextScene = next;
if (_pPrevScene) _pPrevScene->retain();
if (_pNextScene) _pNextScene->retain();
2018-05-08 17:40:36 +08:00
_WindowSize = Window::getSize();
_sPrevLayerParam = _sNextLayerParam = D2D1::LayerParameters();
}
void e2d::TransitionBase::_update()
{
// <20><><EFBFBD><EFBFBD><E3B6AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (_duration == 0)
{
_delta = 1;
}
else
{
_delta = min((Time::getTotalTime() - _fLast) / _duration, 1);
}
this->_updateCustom();
// <20><><EFBFBD>³<EFBFBD><C2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
if (_pPrevScene)
{
2018-05-08 17:40:36 +08:00
_pPrevScene->_update();
}
2018-05-08 17:40:36 +08:00
if (_pNextScene)
{
2018-05-08 17:40:36 +08:00
_pNextScene->_update();
}
}
void e2d::TransitionBase::_render()
{
auto pRT = Renderer::getRenderTarget();
2018-05-08 17:40:36 +08:00
if (_pPrevScene)
{
2018-05-08 17:40:36 +08:00
Point rootPos = _pPrevScene->getRoot()->getPos();
auto clipRect = D2D1::RectF(
float(max(rootPos.x, 0)),
float(max(rootPos.y, 0)),
2018-05-08 17:40:36 +08:00
float(min(rootPos.x + _WindowSize.width, _WindowSize.width)),
float(min(rootPos.y + _WindowSize.height, _WindowSize.height))
);
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
2018-05-08 17:40:36 +08:00
pRT->PushLayer(_sPrevLayerParam, _pPrevLayer);
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
_pPrevScene->_render();
pRT->PopLayer();
pRT->PopAxisAlignedClip();
}
2018-05-08 17:40:36 +08:00
if (_pNextScene)
{
2018-05-08 17:40:36 +08:00
Point rootPos = _pNextScene->getRoot()->getPos();
auto clipRect = D2D1::RectF(
float(max(rootPos.x, 0)),
float(max(rootPos.y, 0)),
2018-05-08 17:40:36 +08:00
float(min(rootPos.x + _WindowSize.width, _WindowSize.width)),
float(min(rootPos.y + _WindowSize.height, _WindowSize.height))
);
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
2018-05-08 17:40:36 +08:00
pRT->PushLayer(_sNextLayerParam, _pNextLayer);
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
2018-05-08 17:40:36 +08:00
_pNextScene->_render();
pRT->PopLayer();
pRT->PopAxisAlignedClip();
}
}
void e2d::TransitionBase::_stop()
{
2018-05-08 17:40:36 +08:00
_bEnd = true;
_reset();
}