Magic_Game/core/Base/Renderer.cpp

341 lines
6.2 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-08-28 00:06:10 +08:00
e2d::Renderer* e2d::Renderer::_instance = nullptr;
ID2D1Factory* e2d::Renderer::_factory = nullptr;
IWICImagingFactory* e2d::Renderer::_imagingFactory = nullptr;
IDWriteFactory* e2d::Renderer::_writeFactory = nullptr;
ID2D1StrokeStyle* e2d::Renderer::_miterStrokeStyle = nullptr;
ID2D1StrokeStyle* e2d::Renderer::_bevelStrokeStyle = nullptr;
ID2D1StrokeStyle* e2d::Renderer::_roundStrokeStyle = nullptr;
e2d::Renderer * e2d::Renderer::getInstance()
{
if (!_instance)
{
_instance = new (std::nothrow) Renderer;
}
return _instance;
}
void e2d::Renderer::destroyInstance()
{
if (_instance)
{
delete _instance;
_instance = nullptr;
SafeRelease(_miterStrokeStyle);
SafeRelease(_bevelStrokeStyle);
SafeRelease(_roundStrokeStyle);
SafeRelease(_factory);
SafeRelease(_imagingFactory);
SafeRelease(_writeFactory);
}
}
e2d::Renderer::Renderer()
2018-08-19 15:11:20 +08:00
: _showFps(false)
, _lastRenderTime(Time::now())
2018-07-25 00:07:52 +08:00
, _renderTimes(0)
, _fpsFormat(nullptr)
2018-07-25 00:07:52 +08:00
, _fpsLayout(nullptr)
, _renderTarget(nullptr)
, _solidBrush(nullptr)
, _textRenderer(nullptr)
, _clearColor(D2D1::ColorF(D2D1::ColorF::Black))
{
::CoInitialize(nullptr);
2018-08-15 00:06:03 +08:00
2018-08-28 00:06:10 +08:00
HWND hWnd = Window::getInstance()->getHWnd();
2018-08-15 00:06:03 +08:00
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(
2018-08-28 00:06:10 +08:00
getFactory()->CreateHwndRenderTarget(
2018-08-15 00:06:03 +08:00
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hWnd,
size,
D2D1_PRESENT_OPTIONS_NONE),
&_renderTarget
)
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˢ
ThrowIfFailed(
_renderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&_solidBrush
)
);
// <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE>
ThrowIfFailed(
TextRenderer::Create(
&_textRenderer,
2018-08-28 00:06:10 +08:00
getFactory(),
2018-08-15 00:06:03 +08:00
_renderTarget,
_solidBrush
)
);
2018-01-30 16:45:38 +08:00
}
2018-08-28 00:06:10 +08:00
e2d::Renderer::~Renderer()
{
SafeRelease(_fpsFormat);
SafeRelease(_fpsLayout);
SafeRelease(_textRenderer);
SafeRelease(_solidBrush);
SafeRelease(_renderTarget);
::CoUninitialize();
2018-08-28 00:06:10 +08:00
}
2018-08-15 23:30:23 +08:00
void e2d::Renderer::beginDraw()
2018-01-30 16:45:38 +08:00
{
// <20><>ʼ<EFBFBD><CABC>Ⱦ
2018-08-15 00:06:03 +08:00
_renderTarget->BeginDraw();
2018-01-30 16:45:38 +08:00
// ʹ<>ñ<EFBFBD><C3B1><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ
2018-08-15 00:06:03 +08:00
_renderTarget->Clear(_clearColor);
2018-08-15 23:30:23 +08:00
}
2018-01-30 16:45:38 +08:00
2018-08-15 23:30:23 +08:00
void e2d::Renderer::endDraw()
{
2018-08-19 15:11:20 +08:00
if (_showFps)
{
int duration = (Time::now() - _lastRenderTime).milliseconds();
++_renderTimes;
if (duration >= 100)
{
String fpsText = String::format(L"FPS: %.1f", (1000.f / duration * _renderTimes));
_lastRenderTime = Time::now();
_renderTimes = 0;
if (!_fpsFormat)
{
ThrowIfFailed(
2018-08-28 00:06:10 +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"",
&_fpsFormat
)
);
ThrowIfFailed(
_fpsFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP)
);
}
SafeRelease(_fpsLayout);
ThrowIfFailed(
2018-08-28 00:06:10 +08:00
getWriteFactory()->CreateTextLayout(
2018-08-19 15:11:20 +08:00
(const WCHAR *)fpsText,
(UINT32)fpsText.length(),
_fpsFormat,
0,
0,
&_fpsLayout
)
);
}
if (_fpsLayout)
{
_renderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
_solidBrush->SetOpacity(1.0f);
_textRenderer->SetTextStyle(
D2D1::ColorF(D2D1::ColorF::White),
TRUE,
D2D1::ColorF(D2D1::ColorF::Black, 0.4f),
1.5f,
D2D1_LINE_JOIN_ROUND
);
ThrowIfFailed(
_fpsLayout->Draw(nullptr, _textRenderer, 10, 0)
);
}
}
2018-01-30 16:45:38 +08:00
// <20><>ֹ<EFBFBD><D6B9>Ⱦ
2018-08-15 00:06:03 +08:00
HRESULT hr = _renderTarget->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
SafeRelease(_fpsFormat);
SafeRelease(_fpsLayout);
SafeRelease(_textRenderer);
SafeRelease(_solidBrush);
SafeRelease(_renderTarget);
2018-01-30 16:45:38 +08:00
}
if (FAILED(hr))
{
2018-08-15 00:06:03 +08:00
throw SystemException("Device loss recovery failed");
2018-01-30 16:45:38 +08:00
}
}
e2d::Color e2d::Renderer::getBackgroundColor()
2018-01-30 16:45:38 +08:00
{
2018-08-23 16:58:32 +08:00
return _clearColor;
}
void e2d::Renderer::setBackgroundColor(Color color)
{
2018-07-29 02:24:34 +08:00
_clearColor = (D2D1_COLOR_F)color;
2018-01-30 16:45:38 +08:00
}
2018-08-19 15:11:20 +08:00
void e2d::Renderer::showFps(bool show)
{
_showFps = show;
}
2018-08-28 00:06:10 +08:00
ID2D1Factory * e2d::Renderer::getFactory()
{
if (!_factory)
{
::CoInitialize(nullptr);
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&_factory
)
);
::CoUninitialize();
2018-08-28 00:06:10 +08:00
}
return _factory;
}
IWICImagingFactory * e2d::Renderer::getImagingFactory()
{
if (!_imagingFactory)
{
::CoInitialize(nullptr);
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
reinterpret_cast<void**>(&_imagingFactory)
)
);
::CoUninitialize();
2018-08-28 00:06:10 +08:00
}
return _imagingFactory;
}
IDWriteFactory * e2d::Renderer::getWriteFactory()
{
if (!_writeFactory)
{
::CoInitialize(nullptr);
2018-08-28 00:06:10 +08:00
ThrowIfFailed(
DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&_writeFactory)
)
);
::CoUninitialize();
2018-08-28 00:06:10 +08:00
}
return _writeFactory;
}
ID2D1StrokeStyle * e2d::Renderer::getMiterStrokeStyle()
{
if (!_miterStrokeStyle)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
2018-08-28 00:06:10 +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,
&_miterStrokeStyle
)
);
}
return _miterStrokeStyle;
}
ID2D1StrokeStyle * e2d::Renderer::getBevelStrokeStyle()
{
if (!_bevelStrokeStyle)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
2018-08-28 00:06:10 +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,
&_bevelStrokeStyle
)
);
}
return _bevelStrokeStyle;
}
ID2D1StrokeStyle * e2d::Renderer::getRoundStrokeStyle()
{
if (!_roundStrokeStyle)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
2018-08-28 00:06:10 +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,
&_roundStrokeStyle
)
);
}
return _roundStrokeStyle;
}