Magic_Game/core/Transition/Transition.cpp

129 lines
2.6 KiB
C++
Raw Normal View History

#include "..\e2dbase.h"
2018-04-21 21:24:46 +08:00
#include "..\e2dtransition.h"
#include "..\e2dnode.h"
2018-07-28 20:06:27 +08:00
e2d::Transition::Transition(float duration)
: _end(false)
2018-08-02 00:27:45 +08:00
, _started()
, _delta(0)
, _outScene(nullptr)
, _inScene(nullptr)
, _outLayer(nullptr)
, _inLayer(nullptr)
, _outLayerParam()
, _inLayerParam()
{
2018-07-28 20:06:27 +08:00
_duration = std::max(duration, 0.f);
}
e2d::Transition::~Transition()
2018-04-17 01:11:56 +08:00
{
2018-05-22 12:24:43 +08:00
SafeRelease(_outLayer);
SafeRelease(_inLayer);
2018-07-22 21:22:27 +08:00
GC::getInstance()->safeRelease(_outScene);
GC::getInstance()->safeRelease(_inScene);
2018-04-17 01:11:56 +08:00
}
bool e2d::Transition::isDone()
{
return _end;
}
2018-08-02 00:27:45 +08:00
bool e2d::Transition::init(Scene * prev, Scene * next)
{
auto renderer = Renderer::getInstance();
// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
HRESULT hr = renderer->getRenderTarget()->CreateLayer(&_inLayer);
if (SUCCEEDED(hr))
{
hr = renderer->getRenderTarget()->CreateLayer(&_outLayer);
}
if (FAILED(hr))
{
2018-08-02 00:27:45 +08:00
return false;
}
2018-08-02 00:27:45 +08:00
_started = Time::now();
_outScene = prev;
_inScene = next;
2018-07-22 21:22:27 +08:00
if (_outScene) _outScene->retain();
if (_inScene) _inScene->retain();
_windowSize = Window::getInstance()->getSize();
_outLayerParam = _inLayerParam = D2D1::LayerParameters(
D2D1::InfiniteRect(),
nullptr,
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
D2D1::Matrix3x2F::Identity(),
2018-07-28 20:06:27 +08:00
1.f,
renderer->getSolidColorBrush(),
D2D1_LAYER_OPTIONS_NONE
);
2018-08-02 00:27:45 +08:00
return true;
}
2018-08-02 00:27:45 +08:00
void e2d::Transition::update()
{
if (_duration == 0)
{
_delta = 1;
}
else
{
2018-08-02 00:27:45 +08:00
_delta = (Time::now() - _started).seconds() / _duration;
2018-07-28 20:06:27 +08:00
_delta = std::min(_delta, 1.f);
}
}
2018-08-02 00:27:45 +08:00
void e2d::Transition::render()
{
auto pRT = Renderer::getInstance()->getRenderTarget();
if (_outScene)
{
Point rootPos = _outScene->getRoot()->getPos();
auto clipRect = D2D1::RectF(
2018-07-28 20:06:27 +08:00
std::max(rootPos.x, 0.f),
std::max(rootPos.y, 0.f),
std::min(rootPos.x + _windowSize.width, _windowSize.width),
std::min(rootPos.y + _windowSize.height, _windowSize.height)
);
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
pRT->PushLayer(_outLayerParam, _outLayer);
2018-07-07 19:05:39 +08:00
_outScene->render();
pRT->PopLayer();
pRT->PopAxisAlignedClip();
}
if (_inScene)
{
Point rootPos = _inScene->getRoot()->getPos();
auto clipRect = D2D1::RectF(
2018-07-28 20:06:27 +08:00
std::max(rootPos.x, 0.f),
std::max(rootPos.y, 0.f),
std::min(rootPos.x + _windowSize.width, _windowSize.width),
std::min(rootPos.y + _windowSize.height, _windowSize.height)
);
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
pRT->PushLayer(_inLayerParam, _inLayer);
2018-07-07 19:05:39 +08:00
_inScene->render();
pRT->PopLayer();
pRT->PopAxisAlignedClip();
}
}
2018-08-02 00:27:45 +08:00
void e2d::Transition::stop()
{
_end = true;
2018-08-02 00:27:45 +08:00
reset();
}