Magic_Game/core/Base/Renderer.cpp

357 lines
6.7 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dbase.h"
#include "..\e2dmanager.h"
#include "..\e2dnode.h"
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
e2d::Renderer* e2d::Renderer::instance_ = nullptr;
ID2D1Factory* e2d::Renderer::factory_ = nullptr;
IWICImagingFactory* e2d::Renderer::imaging_factory_ = nullptr;
IDWriteFactory* e2d::Renderer::write_factory_ = nullptr;
ID2D1StrokeStyle* e2d::Renderer::miter_stroke_style_ = nullptr;
ID2D1StrokeStyle* e2d::Renderer::bevel_stroke_style_ = nullptr;
ID2D1StrokeStyle* e2d::Renderer::round_stroke_style_ = nullptr;
e2d::Renderer * e2d::Renderer::GetInstance()
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
if (!instance_)
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
instance_ = new (std::nothrow) Renderer;
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
return instance_;
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Renderer::DestroyInstance()
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
if (instance_)
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
delete instance_;
instance_ = nullptr;
SafeRelease(miter_stroke_style_);
SafeRelease(bevel_stroke_style_);
SafeRelease(round_stroke_style_);
SafeRelease(factory_);
SafeRelease(imaging_factory_);
SafeRelease(write_factory_);
2018-08-28 00:06:10 +08:00
}
}
e2d::Renderer::Renderer()
2018-09-04 22:42:34 +08:00
: show_fps_(false)
, last_render_time_(Time::Now())
, render_times_(0)
, fps_text_format_(nullptr)
, fps_text_layout_(nullptr)
, render_target_(nullptr)
, solid_brush_(nullptr)
, text_renderer_(nullptr)
, clear_color_(D2D1::ColorF(D2D1::ColorF::Black))
{
::CoInitialize(nullptr);
2018-01-30 16:45:38 +08:00
}
2018-08-28 00:06:10 +08:00
e2d::Renderer::~Renderer()
{
2018-09-04 22:42:34 +08:00
SafeRelease(fps_text_format_);
SafeRelease(fps_text_layout_);
SafeRelease(text_renderer_);
SafeRelease(solid_brush_);
SafeRelease(render_target_);
2018-08-28 00:06:10 +08:00
::CoUninitialize();
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Renderer::BeginDraw()
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
auto render_target = GetRenderTarget();
render_target->BeginDraw();
2018-01-30 16:45:38 +08:00
// ʹ<>ñ<EFBFBD><C3B1><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ
2018-09-04 22:42:34 +08:00
render_target->Clear(clear_color_);
2018-08-15 23:30:23 +08:00
}
2018-01-30 16:45:38 +08:00
2018-09-04 22:42:34 +08:00
void e2d::Renderer::EndDraw()
2018-08-15 23:30:23 +08:00
{
2018-09-04 22:42:34 +08:00
if (show_fps_)
2018-08-19 15:11:20 +08:00
{
2018-09-04 22:42:34 +08:00
int duration = (Time::Now() - last_render_time_).Milliseconds();
2018-08-19 15:11:20 +08:00
2018-09-04 22:42:34 +08:00
++render_times_;
2018-08-19 15:11:20 +08:00
if (duration >= 100)
{
2018-09-04 22:42:34 +08:00
String fpsText = String::Format(L"FPS: %.1f", (1000.f / duration * render_times_));
last_render_time_ = Time::Now();
render_times_ = 0;
2018-08-19 15:11:20 +08:00
2018-09-04 22:42:34 +08:00
if (!fps_text_format_)
2018-08-19 15:11:20 +08:00
{
ThrowIfFailed(
2018-09-04 22:42:34 +08:00
GetWriteFactory()->CreateTextFormat(
2018-08-19 15:11:20 +08:00
L"",
nullptr,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
20,
L"",
2018-09-04 22:42:34 +08:00
&fps_text_format_
2018-08-19 15:11:20 +08:00
)
);
ThrowIfFailed(
2018-09-04 22:42:34 +08:00
fps_text_format_->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP)
2018-08-19 15:11:20 +08:00
);
}
2018-09-04 22:42:34 +08:00
SafeRelease(fps_text_layout_);
2018-08-19 15:11:20 +08:00
ThrowIfFailed(
2018-09-04 22:42:34 +08:00
GetWriteFactory()->CreateTextLayout(
(const wchar_t *)fpsText,
2018-09-04 22:42:34 +08:00
(UINT32)fpsText.GetLength(),
fps_text_format_,
2018-08-19 15:11:20 +08:00
0,
0,
2018-09-04 22:42:34 +08:00
&fps_text_layout_
2018-08-19 15:11:20 +08:00
)
);
}
2018-09-04 22:42:34 +08:00
if (fps_text_layout_)
2018-08-19 15:11:20 +08:00
{
2018-09-04 23:20:00 +08:00
GetRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
GetSolidBrush()->SetOpacity(1.0f);
GetTextRenderer()->SetTextStyle(
2018-08-19 15:11:20 +08:00
D2D1::ColorF(D2D1::ColorF::White),
TRUE,
D2D1::ColorF(D2D1::ColorF::Black, 0.4f),
1.5f,
D2D1_LINE_JOIN_ROUND
);
ThrowIfFailed(
2018-09-04 22:42:34 +08:00
fps_text_layout_->Draw(nullptr, text_renderer_, 10, 0)
2018-08-19 15:11:20 +08:00
);
}
}
2018-09-04 22:42:34 +08:00
HRESULT hr = render_target_->EndDraw();
2018-01-30 16:45:38 +08:00
if (hr == D2DERR_RECREATE_TARGET)
{
// <20><><EFBFBD><EFBFBD> Direct3D <20><EFBFBD><E8B1B8>ִ<EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD>ʱ<EFBFBD>ؽ<EFBFBD><D8BD><EFBFBD>Դ
hr = S_OK;
2018-08-15 00:06:03 +08:00
2018-09-04 22:42:34 +08:00
SafeRelease(fps_text_format_);
SafeRelease(fps_text_layout_);
SafeRelease(text_renderer_);
SafeRelease(solid_brush_);
SafeRelease(render_target_);
2018-01-30 16:45:38 +08:00
}
2018-09-04 23:20:00 +08:00
ThrowIfFailed(hr);
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
e2d::E2DTextRenderer * e2d::Renderer::GetTextRenderer()
{
if (!text_renderer_)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE>
ThrowIfFailed(
E2DTextRenderer::Create(
&text_renderer_,
GetFactory(),
GetRenderTarget(),
GetSolidBrush()
)
);
}
return text_renderer_;
}
ID2D1HwndRenderTarget * e2d::Renderer::GetRenderTarget()
{
if (!render_target_)
{
HWND hWnd = Window::GetInstance()->GetHWnd();
RECT rc;
GetClientRect(hWnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU(
rc.right - rc.left,
rc.bottom - rc.top
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9>ԴӦ<D4B4><D3A6> Direct2D <20><EFBFBD><E8B1B8>ʧʱ<CAA7>ؽ<EFBFBD>
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Direct2D <20><>ȾĿ<C8BE><C4BF>
ThrowIfFailed(
GetFactory()->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hWnd,
size,
D2D1_PRESENT_OPTIONS_NONE),
&render_target_
)
);
}
return render_target_;
}
ID2D1SolidColorBrush * e2d::Renderer::GetSolidBrush()
{
if (!solid_brush_)
{
ThrowIfFailed(
GetRenderTarget()->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&solid_brush_
)
);
}
return solid_brush_;
}
e2d::Color e2d::Renderer::GetBackgroundColor()
2018-01-30 16:45:38 +08:00
{
2018-09-04 22:42:34 +08:00
return clear_color_;
}
2018-09-04 22:42:34 +08:00
void e2d::Renderer::SetBackgroundColor(const Color& color)
{
2018-09-04 22:42:34 +08:00
clear_color_ = (D2D1_COLOR_F)color;
2018-01-30 16:45:38 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Renderer::ShowFps(bool show)
2018-08-19 15:11:20 +08:00
{
2018-09-04 22:42:34 +08:00
show_fps_ = show;
2018-08-19 15:11:20 +08:00
}
2018-09-04 22:42:34 +08:00
ID2D1Factory * e2d::Renderer::GetFactory()
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
if (!factory_)
2018-08-28 00:06:10 +08:00
{
::CoInitialize(nullptr);
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
2018-09-04 22:42:34 +08:00
&factory_
2018-08-28 00:06:10 +08:00
)
);
::CoUninitialize();
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
return factory_;
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
IWICImagingFactory * e2d::Renderer::GetImagingFactory()
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
if (!imaging_factory_)
2018-08-28 00:06:10 +08:00
{
::CoInitialize(nullptr);
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
2018-09-04 22:42:34 +08:00
reinterpret_cast<void**>(&imaging_factory_)
2018-08-28 00:06:10 +08:00
)
);
::CoUninitialize();
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
return imaging_factory_;
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
IDWriteFactory * e2d::Renderer::GetWriteFactory()
2018-08-28 00:06:10 +08:00
{
2018-09-04 22:42:34 +08:00
if (!write_factory_)
2018-08-28 00:06:10 +08:00
{
::CoInitialize(nullptr);
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
2018-09-04 22:42:34 +08:00
reinterpret_cast<IUnknown**>(&write_factory_)
2018-08-28 00:06:10 +08:00
)
);
::CoUninitialize();
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
return write_factory_;
2018-08-28 00:06:10 +08:00
}
2018-09-04 22:42:34 +08:00
ID2D1StrokeStyle * e2d::Renderer::GetMiterStrokeStyle()
{
2018-09-04 22:42:34 +08:00
if (!miter_stroke_style_)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
2018-09-04 22:42:34 +08:00
GetFactory()->CreateStrokeStyle(
2018-08-15 00:06:03 +08:00
D2D1::StrokeStyleProperties(
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_FLAT,
D2D1_LINE_JOIN_MITER,
2.0f,
D2D1_DASH_STYLE_SOLID,
0.0f),
nullptr,
0,
2018-09-04 22:42:34 +08:00
&miter_stroke_style_
2018-08-15 00:06:03 +08:00
)
);
}
2018-09-04 22:42:34 +08:00
return miter_stroke_style_;
}
2018-09-04 22:42:34 +08:00
ID2D1StrokeStyle * e2d::Renderer::GetBevelStrokeStyle()
{
2018-09-04 22:42:34 +08:00
if (!bevel_stroke_style_)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
2018-09-04 22:42:34 +08:00
GetFactory()->CreateStrokeStyle(
2018-08-15 00:06:03 +08:00
D2D1::StrokeStyleProperties(
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_FLAT,
D2D1_LINE_JOIN_BEVEL,
2.0f,
D2D1_DASH_STYLE_SOLID,
0.0f),
nullptr,
0,
2018-09-04 22:42:34 +08:00
&bevel_stroke_style_
2018-08-15 00:06:03 +08:00
)
);
}
2018-09-04 22:42:34 +08:00
return bevel_stroke_style_;
}
2018-09-04 22:42:34 +08:00
ID2D1StrokeStyle * e2d::Renderer::GetRoundStrokeStyle()
{
2018-09-04 22:42:34 +08:00
if (!round_stroke_style_)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
2018-09-04 22:42:34 +08:00
GetFactory()->CreateStrokeStyle(
2018-08-15 00:06:03 +08:00
D2D1::StrokeStyleProperties(
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_FLAT,
D2D1_CAP_STYLE_FLAT,
D2D1_LINE_JOIN_ROUND,
2.0f,
D2D1_DASH_STYLE_SOLID,
0.0f),
nullptr,
0,
2018-09-04 22:42:34 +08:00
&round_stroke_style_
2018-08-15 00:06:03 +08:00
)
);
}
2018-09-04 22:42:34 +08:00
return round_stroke_style_;
}