Magic_Game/core/transitions/Transition.cpp

165 lines
3.6 KiB
C++
Raw Normal View History

2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2018-04-21 21:24:46 +08:00
#include "..\e2dtransition.h"
2018-09-06 23:26:32 +08:00
#include "..\e2dobject.h"
2018-09-11 17:54:21 +08:00
#include "..\e2dmodule.h"
2018-10-16 14:13:15 +08:00
easy2d::Transition::Transition(float duration)
2018-09-04 22:42:34 +08:00
: done_(false)
, started_()
2018-09-11 15:12:25 +08:00
, process_(0)
2018-09-11 00:37:52 +08:00
, window_size_()
2018-09-04 22:42:34 +08:00
, out_scene_(nullptr)
2018-09-10 20:22:47 +08:00
, in_scene_(nullptr)
2018-09-04 22:42:34 +08:00
, out_layer_(nullptr)
, in_layer_(nullptr)
, out_layer_param_()
, in_layer_param_()
{
2018-09-04 22:42:34 +08:00
duration_ = std::max(duration, 0.f);
}
2018-10-16 14:13:15 +08:00
easy2d::Transition::~Transition()
2018-04-17 01:11:56 +08:00
{
2018-09-04 22:42:34 +08:00
SafeRelease(out_layer_);
SafeRelease(in_layer_);
2018-09-07 00:28:54 +08:00
SafeRelease(out_scene_);
SafeRelease(in_scene_);
2018-04-17 01:11:56 +08:00
}
2018-10-16 14:13:15 +08:00
bool easy2d::Transition::IsDone()
{
2018-09-04 22:42:34 +08:00
return done_;
}
2018-10-16 14:13:15 +08:00
void easy2d::Transition::Init(Scene * prev, Scene * next, Game * game)
{
2018-09-04 22:42:34 +08:00
started_ = Time::Now();
out_scene_ = prev;
2018-09-10 15:28:19 +08:00
in_scene_ = next;
2018-08-14 00:26:20 +08:00
2018-09-04 22:42:34 +08:00
if (out_scene_)
out_scene_->Retain();
2018-09-10 15:28:19 +08:00
if (in_scene_)
in_scene_->Retain();
2018-08-14 00:26:20 +08:00
2018-10-03 18:04:04 +08:00
auto graphics = Device::GetGraphics();
2018-09-04 22:42:34 +08:00
if (in_scene_)
2018-08-14 00:26:20 +08:00
{
2018-09-10 15:28:19 +08:00
ThrowIfFailed(
2018-09-30 14:54:43 +08:00
graphics->GetRenderTarget()->CreateLayer(&in_layer_)
2018-09-10 15:28:19 +08:00
);
2018-08-14 00:26:20 +08:00
}
2018-09-10 15:28:19 +08:00
if (out_scene_)
{
2018-09-10 15:28:19 +08:00
ThrowIfFailed(
2018-09-30 14:54:43 +08:00
graphics->GetRenderTarget()->CreateLayer(&out_layer_)
2018-09-10 15:28:19 +08:00
);
}
2018-10-03 18:04:04 +08:00
window_size_ = game->GetSize();
2018-09-04 22:42:34 +08:00
out_layer_param_ = in_layer_param_ = D2D1::LayerParameters(
2018-09-11 00:37:52 +08:00
D2D1::RectF(
0.f,
0.f,
window_size_.width,
window_size_.height
),
nullptr,
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
D2D1::Matrix3x2F::Identity(),
2018-07-28 20:06:27 +08:00
1.f,
2018-09-30 14:54:43 +08:00
graphics->GetSolidBrush(),
D2D1_LAYER_OPTIONS_NONE
);
}
2018-10-16 14:13:15 +08:00
void easy2d::Transition::Update()
{
2018-09-04 22:42:34 +08:00
if (duration_ == 0)
{
2018-09-11 15:12:25 +08:00
process_ = 1;
}
else
{
2018-09-11 15:12:25 +08:00
process_ = (Time::Now() - started_).Seconds() / duration_;
process_ = std::min(process_, 1.f);
}
if (process_ >= 1)
{
this->Stop();
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Transition::Draw()
{
2018-10-03 18:04:04 +08:00
auto render_target = Device::GetGraphics()->GetRenderTarget();
2018-09-11 00:37:52 +08:00
if (out_scene_)
{
2018-09-11 00:37:52 +08:00
render_target->SetTransform(out_scene_->GetTransform());
render_target->PushAxisAlignedClip(
D2D1::RectF(
0.f,
0.f,
window_size_.width,
window_size_.height
),
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE
);
2018-09-05 00:09:23 +08:00
render_target->PushLayer(out_layer_param_, out_layer_);
2018-09-06 23:26:32 +08:00
out_scene_->Draw();
2018-09-05 00:09:23 +08:00
render_target->PopLayer();
render_target->PopAxisAlignedClip();
}
2018-09-11 00:37:52 +08:00
if (in_scene_)
{
2018-09-11 00:37:52 +08:00
render_target->SetTransform(in_scene_->GetTransform());
render_target->PushAxisAlignedClip(
D2D1::RectF(
0.f,
0.f,
window_size_.width,
window_size_.height
),
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE
);
2018-09-05 00:09:23 +08:00
render_target->PushLayer(in_layer_param_, in_layer_);
2018-09-06 23:26:32 +08:00
in_scene_->Draw();
2018-09-05 00:09:23 +08:00
render_target->PopLayer();
render_target->PopAxisAlignedClip();
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Transition::Stop()
{
2018-09-04 22:42:34 +08:00
done_ = true;
Reset();
}