Renderer增加获取三种类型ID2D1StrokeStyle的接口

This commit is contained in:
Nomango 2018-05-24 17:19:54 +08:00
parent 93cb13fe72
commit 6ea9d6ef85
5 changed files with 175 additions and 104 deletions

View File

@ -12,6 +12,9 @@ static ID2D1SolidColorBrush * s_pSolidBrush = nullptr;
static IWICImagingFactory * s_pIWICFactory = nullptr; static IWICImagingFactory * s_pIWICFactory = nullptr;
static IDWriteFactory * s_pDWriteFactory = nullptr; static IDWriteFactory * s_pDWriteFactory = nullptr;
static e2d::TextRenderer * s_pTextRenderer = nullptr; static e2d::TextRenderer * s_pTextRenderer = nullptr;
static ID2D1StrokeStyle * s_pMiterStrokeStyle = nullptr;
static ID2D1StrokeStyle * s_pBevelStrokeStyle = nullptr;
static ID2D1StrokeStyle * s_pRoundStrokeStyle = nullptr;
static D2D1_COLOR_F s_nClearColor = D2D1::ColorF(D2D1::ColorF::Black); static D2D1_COLOR_F s_nClearColor = D2D1::ColorF(D2D1::ColorF::Black);
@ -23,11 +26,80 @@ bool e2d::Renderer::__createDeviceIndependentResources()
&s_pDirect2dFactory &s_pDirect2dFactory
); );
// 工厂将返回当前的系统 DPI这个值也将用来创建窗口
if (SUCCEEDED(hr))
{
s_pDirect2dFactory->GetDesktopDpi(&s_fDpiScaleX, &s_fDpiScaleY);
}
if (FAILED(hr)) if (FAILED(hr))
{ {
throw SystemException(L"Create ID2D1Factory failed"); throw SystemException(L"Create ID2D1Factory failed");
} }
else else
{
hr = s_pDirect2dFactory->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,
&s_pMiterStrokeStyle
);
}
if (FAILED(hr))
{
throw SystemException(L"Create ID2D1StrokeStyle failed");
}
else
{
hr = s_pDirect2dFactory->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,
&s_pBevelStrokeStyle
);
}
if (FAILED(hr))
{
throw SystemException(L"Create ID2D1StrokeStyle failed");
}
else
{
hr = s_pDirect2dFactory->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,
&s_pRoundStrokeStyle
);
}
if (FAILED(hr))
{
throw SystemException(L"Create ID2D1StrokeStyle failed");
}
else
{ {
// 创建 WIC 绘图工厂,用于统一处理各种格式的图片 // 创建 WIC 绘图工厂,用于统一处理各种格式的图片
hr = CoCreateInstance( hr = CoCreateInstance(
@ -59,9 +131,6 @@ bool e2d::Renderer::__createDeviceIndependentResources()
} }
else else
{ {
// 工厂将返回当前的系统 DPI这个值也将用来创建窗口
Renderer::getID2D1Factory()->GetDesktopDpi(&s_fDpiScaleX, &s_fDpiScaleY);
// 创建文本格式化对象 // 创建文本格式化对象
hr = s_pDWriteFactory->CreateTextFormat( hr = s_pDWriteFactory->CreateTextFormat(
L"", L"",
@ -130,7 +199,7 @@ bool e2d::Renderer::__createDeviceResources()
else else
{ {
// 创建自定义的文字渲染器 // 创建自定义的文字渲染器
s_pTextRenderer = new (std::nothrow) TextRenderer( s_pTextRenderer = TextRenderer::Create(
s_pDirect2dFactory, s_pDirect2dFactory,
s_pRenderTarget, s_pRenderTarget,
s_pSolidBrush s_pSolidBrush
@ -146,6 +215,9 @@ void e2d::Renderer::__discardDeviceResources()
SafeRelease(s_pRenderTarget); SafeRelease(s_pRenderTarget);
SafeRelease(s_pSolidBrush); SafeRelease(s_pSolidBrush);
SafeRelease(s_pTextRenderer); SafeRelease(s_pTextRenderer);
SafeRelease(s_pMiterStrokeStyle);
SafeRelease(s_pBevelStrokeStyle);
SafeRelease(s_pRoundStrokeStyle);
} }
void e2d::Renderer::__discardResources() void e2d::Renderer::__discardResources()
@ -155,6 +227,9 @@ void e2d::Renderer::__discardResources()
SafeRelease(s_pRenderTarget); SafeRelease(s_pRenderTarget);
SafeRelease(s_pSolidBrush); SafeRelease(s_pSolidBrush);
SafeRelease(s_pTextRenderer); SafeRelease(s_pTextRenderer);
SafeRelease(s_pMiterStrokeStyle);
SafeRelease(s_pBevelStrokeStyle);
SafeRelease(s_pRoundStrokeStyle);
SafeRelease(s_pIWICFactory); SafeRelease(s_pIWICFactory);
SafeRelease(s_pDWriteFactory); SafeRelease(s_pDWriteFactory);
} }
@ -292,3 +367,18 @@ e2d::TextRenderer * e2d::Renderer::getTextRenderer()
{ {
return s_pTextRenderer; return s_pTextRenderer;
} }
ID2D1StrokeStyle * e2d::Renderer::getMiterID2D1StrokeStyle()
{
return s_pMiterStrokeStyle;
}
ID2D1StrokeStyle * e2d::Renderer::getBevelID2D1StrokeStyle()
{
return s_pBevelStrokeStyle;
}
ID2D1StrokeStyle * e2d::Renderer::getRoundID2D1StrokeStyle()
{
return s_pRoundStrokeStyle;
}

View File

@ -1,5 +1,6 @@
#include "..\e2dcommon.h" #include "..\e2dcommon.h"
#include <iomanip> #include <iomanip>
#include <cwctype>
#include <comutil.h> #include <comutil.h>
#pragma comment(lib, "comsuppw.lib") #pragma comment(lib, "comsuppw.lib")
@ -377,20 +378,14 @@ int e2d::String::compare(const String & str) const
e2d::String e2d::String::toUpper() const e2d::String e2d::String::toUpper() const
{ {
String str(*this); String str(*this);
std::transform(str._str.begin(), str._str.end(), str._str.begin(), std::towupper);
for (size_t i = 0, length = _str.size(); i < length; ++i)
str[i] = towupper(str[i]);
return std::move(str); return std::move(str);
} }
e2d::String e2d::String::toLower() const e2d::String e2d::String::toLower() const
{ {
e2d::String str(*this); e2d::String str(*this);
std::transform(str._str.begin(), str._str.end(), str._str.begin(), std::towlower);
for (size_t i = 0, length = _str.size(); i < length; ++i)
str[i] = towlower(str[i]);
return std::move(str); return std::move(str);
} }

View File

@ -3,24 +3,17 @@
using namespace e2d; using namespace e2d;
TextRenderer::TextRenderer( TextRenderer::TextRenderer()
ID2D1Factory* pD2DFactory,
ID2D1HwndRenderTarget* pRT,
ID2D1SolidColorBrush* pBrush
)
: cRefCount_(0) : cRefCount_(0)
, pD2DFactory_(pD2DFactory) , pD2DFactory_(nullptr)
, pRT_(pRT) , pRT_(nullptr)
, pBrush_(pBrush) , pBrush_(nullptr)
, sFillColor_() , sFillColor_()
, sOutlineColor_() , sOutlineColor_()
, fOutlineWidth(1) , fOutlineWidth(1)
, bShowOutline_(TRUE) , bShowOutline_(TRUE)
, nOutlineJoin_(D2D1_LINE_JOIN_MITER) , pCurrStrokeStyle_(nullptr)
{ {
pD2DFactory_->AddRef();
pRT_->AddRef();
pBrush_->AddRef();
} }
TextRenderer::~TextRenderer() TextRenderer::~TextRenderer()
@ -30,6 +23,26 @@ TextRenderer::~TextRenderer()
SafeRelease(pBrush_); SafeRelease(pBrush_);
} }
TextRenderer * TextRenderer::Create(
ID2D1Factory* pD2DFactory,
ID2D1HwndRenderTarget* pRT,
ID2D1SolidColorBrush* pBrush
)
{
TextRenderer * pTextRenderer = new (std::nothrow) TextRenderer();
if (pTextRenderer)
{
pD2DFactory->AddRef();
pRT->AddRef();
pBrush->AddRef();
pTextRenderer->pD2DFactory_ = pD2DFactory;
pTextRenderer->pRT_ = pRT;
pTextRenderer->pBrush_ = pBrush;
}
return pTextRenderer;
}
STDMETHODIMP_(void) TextRenderer::SetTextStyle( STDMETHODIMP_(void) TextRenderer::SetTextStyle(
CONST D2D1_COLOR_F &fillColor, CONST D2D1_COLOR_F &fillColor,
BOOL hasOutline, BOOL hasOutline,
@ -42,7 +55,22 @@ STDMETHODIMP_(void) TextRenderer::SetTextStyle(
bShowOutline_ = hasOutline; bShowOutline_ = hasOutline;
sOutlineColor_ = outlineColor; sOutlineColor_ = outlineColor;
fOutlineWidth = 2 * outlineWidth; fOutlineWidth = 2 * outlineWidth;
nOutlineJoin_ = outlineJoin;
switch (outlineJoin)
{
case D2D1_LINE_JOIN_MITER:
pCurrStrokeStyle_ = Renderer::getMiterID2D1StrokeStyle();
break;
case D2D1_LINE_JOIN_BEVEL:
pCurrStrokeStyle_ = Renderer::getBevelID2D1StrokeStyle();
break;
case D2D1_LINE_JOIN_ROUND:
pCurrStrokeStyle_ = Renderer::getRoundID2D1StrokeStyle();
break;
default:
pCurrStrokeStyle_ = nullptr;
break;
}
} }
STDMETHODIMP TextRenderer::DrawGlyphRun( STDMETHODIMP TextRenderer::DrawGlyphRun(
@ -107,32 +135,14 @@ STDMETHODIMP TextRenderer::DrawGlyphRun(
if (SUCCEEDED(hr) && bShowOutline_) if (SUCCEEDED(hr) && bShowOutline_)
{ {
ID2D1StrokeStyle * pStrokeStyle = nullptr; pBrush_->SetColor(sOutlineColor_);
hr = Renderer::getID2D1Factory()->CreateStrokeStyle(
D2D1::StrokeStyleProperties( pRT_->DrawGeometry(
D2D1_CAP_STYLE_FLAT, pTransformedGeometry,
D2D1_CAP_STYLE_FLAT, pBrush_,
D2D1_CAP_STYLE_FLAT, fOutlineWidth,
nOutlineJoin_, pCurrStrokeStyle_
2.0f,
D2D1_DASH_STYLE_SOLID,
0.0f),
nullptr,
0,
&pStrokeStyle
); );
if (SUCCEEDED(hr))
{
pBrush_->SetColor(sOutlineColor_);
pRT_->DrawGeometry(
pTransformedGeometry,
pBrush_,
fOutlineWidth,
pStrokeStyle
);
}
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
@ -193,32 +203,14 @@ STDMETHODIMP TextRenderer::DrawUnderline(
if (SUCCEEDED(hr) && bShowOutline_) if (SUCCEEDED(hr) && bShowOutline_)
{ {
ID2D1StrokeStyle * pStrokeStyle = nullptr; pBrush_->SetColor(sOutlineColor_);
hr = Renderer::getID2D1Factory()->CreateStrokeStyle(
D2D1::StrokeStyleProperties( pRT_->DrawGeometry(
D2D1_CAP_STYLE_FLAT, pTransformedGeometry,
D2D1_CAP_STYLE_FLAT, pBrush_,
D2D1_CAP_STYLE_FLAT, fOutlineWidth,
nOutlineJoin_, pCurrStrokeStyle_
2.0f,
D2D1_DASH_STYLE_SOLID,
0.0f),
nullptr,
0,
&pStrokeStyle
); );
if (SUCCEEDED(hr))
{
pBrush_->SetColor(sOutlineColor_);
pRT_->DrawGeometry(
pTransformedGeometry,
pBrush_,
fOutlineWidth,
pStrokeStyle
);
}
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
@ -278,32 +270,14 @@ STDMETHODIMP TextRenderer::DrawStrikethrough(
if (SUCCEEDED(hr) && bShowOutline_) if (SUCCEEDED(hr) && bShowOutline_)
{ {
ID2D1StrokeStyle * pStrokeStyle = nullptr; pBrush_->SetColor(sOutlineColor_);
hr = Renderer::getID2D1Factory()->CreateStrokeStyle(
D2D1::StrokeStyleProperties( pRT_->DrawGeometry(
D2D1_CAP_STYLE_FLAT, pTransformedGeometry,
D2D1_CAP_STYLE_FLAT, pBrush_,
D2D1_CAP_STYLE_FLAT, fOutlineWidth,
nOutlineJoin_, pCurrStrokeStyle_
2.0f,
D2D1_DASH_STYLE_SOLID,
0.0f),
nullptr,
0,
&pStrokeStyle
); );
if (SUCCEEDED(hr))
{
pBrush_->SetColor(sOutlineColor_);
pRT_->DrawGeometry(
pTransformedGeometry,
pBrush_,
fOutlineWidth,
pStrokeStyle
);
}
} }
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))

View File

@ -416,6 +416,15 @@ public:
// 获取文字渲染器 // 获取文字渲染器
static TextRenderer * getTextRenderer(); static TextRenderer * getTextRenderer();
// 获取 Miter 样式的 ID2D1StrokeStyle
static ID2D1StrokeStyle * getMiterID2D1StrokeStyle();
// 获取 Bevel 样式的 ID2D1StrokeStyle
static ID2D1StrokeStyle * getBevelID2D1StrokeStyle();
// 获取 Round 样式的 ID2D1StrokeStyle
static ID2D1StrokeStyle * getRoundID2D1StrokeStyle();
private: private:
// 渲染游戏画面 // 渲染游戏画面
static void __render(); static void __render();

View File

@ -71,15 +71,18 @@ protected:
class TextRenderer class TextRenderer
: public IDWriteTextRenderer : public IDWriteTextRenderer
{ {
private:
TextRenderer();
~TextRenderer();
public: public:
TextRenderer( static TextRenderer * Create(
ID2D1Factory* pD2DFactory, ID2D1Factory* pD2DFactory,
ID2D1HwndRenderTarget* pRT, ID2D1HwndRenderTarget* pRT,
ID2D1SolidColorBrush* pBrush ID2D1SolidColorBrush* pBrush
); );
~TextRenderer();
STDMETHOD_(void, SetTextStyle)( STDMETHOD_(void, SetTextStyle)(
CONST D2D1_COLOR_F &fillColor, CONST D2D1_COLOR_F &fillColor,
BOOL hasOutline, BOOL hasOutline,
@ -153,10 +156,10 @@ private:
D2D1_COLOR_F sOutlineColor_; D2D1_COLOR_F sOutlineColor_;
FLOAT fOutlineWidth; FLOAT fOutlineWidth;
BOOL bShowOutline_; BOOL bShowOutline_;
D2D1_LINE_JOIN nOutlineJoin_;
ID2D1Factory* pD2DFactory_; ID2D1Factory* pD2DFactory_;
ID2D1HwndRenderTarget* pRT_; ID2D1HwndRenderTarget* pRT_;
ID2D1SolidColorBrush* pBrush_; ID2D1SolidColorBrush* pBrush_;
ID2D1StrokeStyle * pCurrStrokeStyle_;
}; };