Magic_Game/core/Base/Renderer.cpp

294 lines
5.3 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
e2d::Renderer::Renderer()
2018-07-25 00:07:52 +08:00
: _lastRenderTime(Time::now())
, _renderTimes(0)
, _fpsFormat(nullptr)
2018-07-25 00:07:52 +08:00
, _fpsLayout(nullptr)
, _renderTarget(nullptr)
, _solidBrush(nullptr)
, _textRenderer(nullptr)
2018-08-15 00:06:03 +08:00
, _factory(nullptr)
, _imagingFactory(nullptr)
, _writeFactory(nullptr)
, _miterStrokeStyle(nullptr)
, _bevelStrokeStyle(nullptr)
, _roundStrokeStyle(nullptr)
, _clearColor(D2D1::ColorF(D2D1::ColorF::Black))
{
2018-07-04 15:33:09 +08:00
CoInitialize(nullptr);
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&_factory
)
);
ThrowIfFailed(
CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
reinterpret_cast<void**>(&_imagingFactory)
)
);
ThrowIfFailed(
DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&_writeFactory)
)
);
}
2018-04-17 11:41:33 +08:00
e2d::Renderer::~Renderer()
{
SafeRelease(_fpsFormat);
2018-07-25 00:07:52 +08:00
SafeRelease(_fpsLayout);
SafeRelease(_textRenderer);
SafeRelease(_solidBrush);
SafeRelease(_renderTarget);
2018-07-04 15:33:09 +08:00
2018-08-15 00:06:03 +08:00
SafeRelease(_miterStrokeStyle);
SafeRelease(_bevelStrokeStyle);
SafeRelease(_roundStrokeStyle);
SafeRelease(_factory);
SafeRelease(_imagingFactory);
SafeRelease(_writeFactory);
2018-07-04 15:33:09 +08:00
CoUninitialize();
2018-01-30 16:45:38 +08:00
}
2018-08-15 00:06:03 +08:00
void e2d::Renderer::init(Window * window)
2018-01-30 16:45:38 +08:00
{
2018-08-15 00:06:03 +08:00
if (!window)
return;
HWND hWnd = window->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(
_factory->CreateHwndRenderTarget(
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,
_factory,
_renderTarget,
_solidBrush
)
);
2018-01-30 16:45:38 +08:00
}
void e2d::Renderer::render()
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-01-30 16:45:38 +08:00
// <20><>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>
SceneManager::getInstance()->render();
2018-01-30 16:45:38 +08:00
2018-04-17 11:41:33 +08:00
// <20><>Ⱦ FPS
2018-07-24 12:49:32 +08:00
if (Game::getInstance()->getConfig().isFpsShow())
2018-04-17 11:41:33 +08:00
{
2018-07-17 12:32:20 +08:00
_renderFps();
2018-04-17 11:41:33 +08:00
}
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
}
}
2018-07-17 12:32:20 +08:00
void e2d::Renderer::_renderFps()
{
2018-08-02 00:27:45 +08:00
int duration = (Time::now() - _lastRenderTime).milliseconds();
2018-07-24 12:49:32 +08:00
2018-08-02 00:27:45 +08:00
++_renderTimes;
2018-07-25 00:07:52 +08:00
if (duration >= 100)
2018-07-17 12:32:20 +08:00
{
2018-08-02 00:27:45 +08:00
String fpsText = String::format(L"FPS: %.1f", (1000.f / duration * _renderTimes));
_lastRenderTime = Time::now();
_renderTimes = 0;
2018-07-17 12:32:20 +08:00
2018-07-25 00:07:52 +08:00
if (!_fpsFormat)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
_writeFactory->CreateTextFormat(
L"",
nullptr,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
20,
L"",
&_fpsFormat
)
2018-07-25 00:07:52 +08:00
);
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
_fpsFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP)
);
}
2018-07-25 00:07:52 +08:00
SafeRelease(_fpsLayout);
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
_writeFactory->CreateTextLayout(
2018-07-25 00:07:52 +08:00
(const WCHAR *)fpsText,
(UINT32)fpsText.getLength(),
_fpsFormat,
0,
0,
&_fpsLayout
2018-08-15 00:06:03 +08:00
)
);
}
2018-07-17 12:32:20 +08:00
2018-07-25 00:07:52 +08:00
if (_fpsLayout)
2018-07-17 12:32:20 +08:00
{
2018-08-15 00:06:03 +08:00
_renderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
_solidBrush->SetOpacity(1.0f);
_textRenderer->SetTextStyle(
2018-07-17 12:32:20 +08:00
D2D1::ColorF(D2D1::ColorF::White),
TRUE,
D2D1::ColorF(D2D1::ColorF::Black, 0.4f),
1.5f,
D2D1_LINE_JOIN_ROUND
);
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
_fpsLayout->Draw(nullptr, _textRenderer, 10, 0)
);
2018-07-17 12:32:20 +08:00
}
}
e2d::Color e2d::Renderer::getBackgroundColor()
2018-01-30 16:45:38 +08:00
{
return Color(_clearColor.r, _clearColor.g, _clearColor.b, _clearColor.a);
}
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
}
ID2D1StrokeStyle * e2d::Renderer::getMiterStrokeStyle()
{
if (!_miterStrokeStyle)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
_factory->CreateStrokeStyle(
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(
_factory->CreateStrokeStyle(
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(
_factory->CreateStrokeStyle(
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;
}