Magic_Game/core/Transition/Transition.cpp

136 lines
2.8 KiB
C++
Raw Normal View History

2018-01-30 16:45:38 +08:00
#include "..\ebase.h"
#include "..\etransition.h"
2018-04-17 11:41:33 +08:00
#include "..\enode.h"
2018-02-27 21:07:43 +08:00
e2d::Transition::Transition(double duration)
: m_bEnd(false)
2018-01-30 16:45:38 +08:00
, m_fLast(0)
, m_fRateOfProgress(0)
, m_pPrevScene(nullptr)
, m_pNextScene(nullptr)
2018-04-17 01:11:56 +08:00
, m_pPrevLayer(nullptr)
, m_pNextLayer(nullptr)
, m_sPrevLayerParam()
, m_sNextLayerParam()
{
2018-01-30 16:45:38 +08:00
m_fDuration = max(duration, 0);
}
2018-04-17 01:11:56 +08:00
e2d::Transition::~Transition()
{
SafeReleaseInterface(&m_pPrevLayer);
SafeReleaseInterface(&m_pNextLayer);
}
bool e2d::Transition::isEnding()
{
return m_bEnd;
}
void e2d::Transition::destroy()
{
SafeRelease(&m_pPrevScene);
SafeRelease(&m_pNextScene);
}
2018-04-17 01:11:56 +08:00
void e2d::Transition::_init(Scene * prev, Scene * next)
{
2018-04-17 01:11:56 +08:00
// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
HRESULT hr = Renderer::getRenderTarget()->CreateLayer(&m_pNextLayer);
if (SUCCEEDED(hr))
{
hr = Renderer::getRenderTarget()->CreateLayer(&m_pPrevLayer);
}
if (FAILED(hr))
{
ASSERT(false, L"Create layer failed!");
}
m_fLast = Time::getTotalTime();
m_pPrevScene = prev;
m_pNextScene = next;
if (m_pPrevScene) m_pPrevScene->retain();
if (m_pNextScene) m_pNextScene->retain();
2018-04-17 11:41:33 +08:00
m_WindowSize = Window::getSize();
m_sPrevLayerParam = m_sNextLayerParam = D2D1::LayerParameters();
2018-04-17 01:11:56 +08:00
}
void e2d::Transition::_update()
{
// <20><><EFBFBD><EFBFBD><E3B6AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-01-30 16:45:38 +08:00
if (m_fDuration == 0)
{
m_fRateOfProgress = 1;
2018-04-17 01:11:56 +08:00
}
else
{
m_fRateOfProgress = min((Time::getTotalTime() - m_fLast) / m_fDuration, 1);
}
2018-04-17 01:11:56 +08:00
this->_updateCustom();
// <20><><EFBFBD>³<EFBFBD><C2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (m_pPrevScene)
{
m_pPrevScene->_update();
}
if (m_pNextScene)
{
m_pNextScene->_update();
}
}
2018-04-17 01:11:56 +08:00
void e2d::Transition::_render()
{
2018-04-17 01:11:56 +08:00
auto pRT = Renderer::getRenderTarget();
2018-04-19 17:18:44 +08:00
2018-04-17 01:11:56 +08:00
if (m_pPrevScene)
{
2018-04-17 11:41:33 +08:00
Point rootPos = m_pPrevScene->getRoot()->getPos();
auto clipRect = D2D1::RectF(
float(max(rootPos.x, 0)),
float(max(rootPos.y, 0)),
2018-04-19 17:18:44 +08:00
float(min(rootPos.x + m_WindowSize.width, m_WindowSize.width)),
float(min(rootPos.y + m_WindowSize.height, m_WindowSize.height))
2018-04-17 11:41:33 +08:00
);
2018-04-17 01:11:56 +08:00
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
2018-04-17 11:41:33 +08:00
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
2018-04-17 01:11:56 +08:00
pRT->PushLayer(m_sPrevLayerParam, m_pPrevLayer);
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
m_pPrevScene->_render();
pRT->PopLayer();
2018-04-17 11:41:33 +08:00
pRT->PopAxisAlignedClip();
2018-04-17 01:11:56 +08:00
}
if (m_pNextScene)
{
2018-04-17 11:41:33 +08:00
Point rootPos = m_pNextScene->getRoot()->getPos();
auto clipRect = D2D1::RectF(
float(max(rootPos.x, 0)),
float(max(rootPos.y, 0)),
2018-04-19 17:18:44 +08:00
float(min(rootPos.x + m_WindowSize.width, m_WindowSize.width)),
float(min(rootPos.y + m_WindowSize.height, m_WindowSize.height))
2018-04-17 11:41:33 +08:00
);
2018-04-17 01:11:56 +08:00
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
2018-04-17 11:41:33 +08:00
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
2018-04-17 01:11:56 +08:00
pRT->PushLayer(m_sNextLayerParam, m_pNextLayer);
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
m_pNextScene->_render();
pRT->PopLayer();
2018-04-17 11:41:33 +08:00
pRT->PopAxisAlignedClip();
2018-04-17 01:11:56 +08:00
}
}
2018-04-17 01:11:56 +08:00
void e2d::Transition::_stop()
{
2018-04-17 01:11:56 +08:00
m_bEnd = true;
_reset();
}