Magic_Game/core/Base/Renderer.cpp

390 lines
7.4 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* e2d::Renderer::_instance = nullptr;
ID2D1Factory* e2d::Renderer::_d2dFactory = 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)
2018-05-24 12:24:39 +08:00
{
_instance = new (std::nothrow) Renderer;
2018-01-30 16:45:38 +08:00
}
return _instance;
}
2018-01-30 16:45:38 +08:00
void e2d::Renderer::destroyInstance()
{
if (_instance)
2018-05-24 12:24:39 +08:00
{
delete _instance;
_instance = nullptr;
SafeRelease(_miterStrokeStyle);
SafeRelease(_bevelStrokeStyle);
SafeRelease(_roundStrokeStyle);
SafeRelease(_d2dFactory);
SafeRelease(_imagingFactory);
SafeRelease(_writeFactory);
2018-05-24 12:24:39 +08:00
}
}
e2d::Renderer::Renderer()
2018-07-24 12:49:32 +08:00
: _lastRenderTime(0)
, _fpsFormat(nullptr)
, _renderTarget(nullptr)
, _solidBrush(nullptr)
, _textRenderer(nullptr)
, _clearColor(D2D1::ColorF(D2D1::ColorF::Black))
{
2018-07-04 15:33:09 +08:00
CoInitialize(nullptr);
this->__createDeviceResources();
}
2018-04-17 11:41:33 +08:00
e2d::Renderer::~Renderer()
{
SafeRelease(_fpsFormat);
SafeRelease(_textRenderer);
SafeRelease(_solidBrush);
SafeRelease(_renderTarget);
2018-07-04 15:33:09 +08:00
CoUninitialize();
2018-01-30 16:45:38 +08:00
}
bool e2d::Renderer::__createDeviceResources()
2018-01-30 16:45:38 +08:00
{
HRESULT hr = S_OK;
if (!_renderTarget)
2018-01-30 16:45:38 +08:00
{
HWND hWnd = Window::getInstance()->getHWnd();
2018-01-30 16:45:38 +08:00
2018-07-07 01:48:39 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9>ԴӦ<D4B4><D3A6> Direct3D <20><EFBFBD><E8B1B8>ʧʱ<CAA7>ؽ<EFBFBD>
2018-01-30 16:45:38 +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><D2BB> Direct2D <20><>ȾĿ<C8BE><C4BF>
hr = Renderer::getFactory()->CreateHwndRenderTarget(
2018-01-30 16:45:38 +08:00
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(hWnd, size, D2D1_PRESENT_OPTIONS_IMMEDIATELY),
&_renderTarget
2018-01-30 16:45:38 +08:00
);
2018-05-24 12:24:39 +08:00
if (FAILED(hr))
{
throw SystemException(L"Create ID2D1HwndRenderTarget failed");
}
else
2018-02-01 09:38:25 +08:00
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˢ
hr = _renderTarget->CreateSolidColorBrush(
2018-02-01 09:38:25 +08:00
D2D1::ColorF(D2D1::ColorF::White),
&_solidBrush
2018-02-01 09:38:25 +08:00
);
}
2018-05-24 12:24:39 +08:00
if (FAILED(hr))
{
throw SystemException(L"Create ID2D1SolidColorBrush failed");
}
2018-01-30 16:45:38 +08:00
}
2018-01-30 16:45:38 +08:00
return SUCCEEDED(hr);
}
void e2d::Renderer::__discardDeviceResources()
2018-01-30 16:45:38 +08:00
{
SafeRelease(_renderTarget);
SafeRelease(_solidBrush);
SafeRelease(_textRenderer);
2018-01-30 16:45:38 +08:00
}
void e2d::Renderer::render()
2018-01-30 16:45:38 +08:00
{
HRESULT hr = S_OK;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
Renderer::__createDeviceResources();
2018-01-30 16:45:38 +08:00
// <20><>ʼ<EFBFBD><CABC>Ⱦ
_renderTarget->BeginDraw();
2018-01-30 16:45:38 +08:00
// ʹ<>ñ<EFBFBD><C3B1><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ
_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>Ⱦ
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;
this->__discardDeviceResources();
2018-01-30 16:45:38 +08:00
}
if (FAILED(hr))
{
2018-05-24 12:24:39 +08:00
throw SystemException(L"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-07-22 00:41:24 +08:00
double duration = Game::getInstance()->getTotalDuration().seconds();
2018-07-24 12:49:32 +08:00
if (duration == _lastRenderTime)
return;
String fpsText = String::format(L"FPS: %.1lf", (1.0 / (duration - _lastRenderTime)));
_lastRenderTime = duration;
auto writeFactory = Renderer::getWriteFactory();
if (!_fpsFormat)
2018-07-17 12:32:20 +08:00
{
2018-07-24 12:49:32 +08:00
HRESULT hr = writeFactory->CreateTextFormat(
L"",
nullptr,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
20,
L"",
&_fpsFormat
);
2018-07-17 12:32:20 +08:00
2018-07-24 12:49:32 +08:00
if (SUCCEEDED(hr))
{
2018-07-24 12:49:32 +08:00
_fpsFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
}
2018-07-24 12:49:32 +08:00
else
{
2018-07-24 12:49:32 +08:00
return;
}
}
2018-07-17 12:32:20 +08:00
2018-07-24 12:49:32 +08:00
IDWriteTextLayout * fpsLayout = nullptr;
HRESULT hr = writeFactory->CreateTextLayout(
(const WCHAR *)fpsText,
(UINT32)fpsText.getLength(),
_fpsFormat,
0,
0,
&fpsLayout
);
if (SUCCEEDED(hr))
2018-07-17 12:32:20 +08:00
{
_renderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
_solidBrush->SetOpacity(1.0f);
auto textRenderer = this->getTextRenderer();
textRenderer->SetTextStyle(
D2D1::ColorF(D2D1::ColorF::White),
TRUE,
D2D1::ColorF(D2D1::ColorF::Black, 0.4f),
1.5f,
D2D1_LINE_JOIN_ROUND
);
2018-07-24 12:49:32 +08:00
fpsLayout->Draw(nullptr, textRenderer, 10, 0);
2018-07-17 12:32:20 +08:00
}
2018-07-24 12:49:32 +08:00
SafeRelease(fpsLayout);
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)
{
_clearColor = color.toD2DColorF();
2018-01-30 16:45:38 +08:00
}
ID2D1HwndRenderTarget * e2d::Renderer::getRenderTarget()
{
return _renderTarget;
}
ID2D1SolidColorBrush * e2d::Renderer::getSolidColorBrush()
{
return _solidBrush;
}
e2d::TextRenderer * e2d::Renderer::getTextRenderer()
2018-01-30 16:45:38 +08:00
{
if (!_textRenderer)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE>
HRESULT hr = TextRenderer::Create(
&_textRenderer,
Renderer::getFactory(),
this->getRenderTarget(),
this->getSolidColorBrush()
);
if (FAILED(hr))
{
throw SystemException(L"Create TextRenderer failed");
}
}
return _textRenderer;
2018-01-30 16:45:38 +08:00
}
ID2D1Factory * e2d::Renderer::getFactory()
2018-01-30 16:45:38 +08:00
{
if (!_d2dFactory)
{
HRESULT hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&_d2dFactory
);
2018-01-30 16:45:38 +08:00
if (FAILED(hr))
{
throw SystemException(L"Create ID2D1Factory failed");
}
}
return _d2dFactory;
2018-01-30 16:45:38 +08:00
}
IWICImagingFactory * e2d::Renderer::getImagingFactory()
2018-01-30 16:45:38 +08:00
{
if (!_imagingFactory)
{
// <20><><EFBFBD><EFBFBD> WIC <20><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳһ<CDB3><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>ʽ<EFBFBD><CABD>ͼƬ
HRESULT hr = CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
reinterpret_cast<void**>(&_imagingFactory)
);
if (FAILED(hr))
{
throw SystemException(L"Create IWICImagingFactory failed");
}
}
return _imagingFactory;
2018-01-30 16:45:38 +08:00
}
IDWriteFactory * e2d::Renderer::getWriteFactory()
2018-01-30 16:45:38 +08:00
{
if (!_writeFactory)
{
// <20><><EFBFBD><EFBFBD> DirectWrite <20><><EFBFBD><EFBFBD>
HRESULT hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&_writeFactory)
);
if (FAILED(hr))
{
throw SystemException(L"Create IDWriteFactory failed");
}
}
return _writeFactory;
2018-01-30 16:45:38 +08:00
}
ID2D1StrokeStyle * e2d::Renderer::getMiterStrokeStyle()
{
if (!_miterStrokeStyle)
{
HRESULT hr = Renderer::getFactory()->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
);
if (FAILED(hr))
{
throw SystemException(L"Create ID2D1StrokeStyle failed");
}
}
return _miterStrokeStyle;
}
ID2D1StrokeStyle * e2d::Renderer::getBevelStrokeStyle()
{
if (!_bevelStrokeStyle)
{
HRESULT hr = Renderer::getFactory()->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
);
if (FAILED(hr))
{
throw SystemException(L"Create ID2D1StrokeStyle failed");
}
}
return _bevelStrokeStyle;
}
ID2D1StrokeStyle * e2d::Renderer::getRoundStrokeStyle()
{
if (!_roundStrokeStyle)
{
HRESULT hr = Renderer::getFactory()->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
);
if (FAILED(hr))
{
throw SystemException(L"Create ID2D1StrokeStyle failed");
}
}
return _roundStrokeStyle;
}