Magic_Game/core/modules/Graphics.cpp

322 lines
6.7 KiB
C++
Raw Normal View History

2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2018-09-05 13:17:07 +08:00
#include "..\e2dmodule.h"
2018-09-06 23:26:32 +08:00
#include "..\e2dobject.h"
2018-01-30 16:45:38 +08:00
2018-10-16 14:13:15 +08:00
easy2d::Graphics::Graphics(HWND hwnd)
2018-10-03 18:04:04 +08:00
: factory_(nullptr)
, imaging_factory_(nullptr)
, write_factory_(nullptr)
, miter_stroke_style_(nullptr)
, bevel_stroke_style_(nullptr)
, round_stroke_style_(nullptr)
2018-09-04 22:42:34 +08:00
, fps_text_format_(nullptr)
, fps_text_layout_(nullptr)
, render_target_(nullptr)
, solid_brush_(nullptr)
, text_renderer_(nullptr)
, clear_color_(D2D1::ColorF(D2D1::ColorF::Black))
{
2018-10-03 18:04:04 +08:00
ThrowIfFailed(
D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&factory_
)
);
ThrowIfFailed(
CoCreateInstance(
CLSID_WICImagingFactory,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
reinterpret_cast<void**>(&imaging_factory_)
)
);
ThrowIfFailed(
DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&write_factory_)
)
);
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),
&render_target_
)
);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˢ
ThrowIfFailed(
render_target_->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::White),
&solid_brush_
)
);
// <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE>
ThrowIfFailed(
2018-10-03 22:02:46 +08:00
E2DTextRenderer::Create(
2018-10-03 18:04:04 +08:00
&text_renderer_,
factory_,
render_target_,
solid_brush_
)
);
2018-01-30 16:45:38 +08:00
}
2018-10-16 14:13:15 +08:00
easy2d::Graphics::~Graphics()
2018-08-28 00:06:10 +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-08-28 00:06:10 +08:00
2018-10-03 18:04:04 +08:00
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
}
2018-10-16 14:13:15 +08:00
void easy2d::Graphics::BeginDraw()
2018-01-30 16:45:38 +08:00
{
2018-10-03 18:04:04 +08:00
render_target_->BeginDraw();
render_target_->Clear(clear_color_);
2018-08-15 23:30:23 +08:00
}
2018-01-30 16:45:38 +08:00
2018-10-16 14:13:15 +08:00
void easy2d::Graphics::EndDraw()
2018-08-15 23:30:23 +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-10-16 14:13:15 +08:00
void easy2d::Graphics::DrawDebugInfo()
2018-09-30 14:54:43 +08:00
{
2018-10-03 18:04:04 +08:00
static int render_times_ = 0;
static Time last_render_time_ = Time::Now();
2018-09-30 14:54:43 +08:00
int duration = (Time::Now() - last_render_time_).Milliseconds();
2018-10-03 18:04:04 +08:00
if (!fps_text_format_)
{
ThrowIfFailed(
write_factory_->CreateTextFormat(
L"",
nullptr,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
20,
L"",
&fps_text_format_
)
);
ThrowIfFailed(
fps_text_format_->SetWordWrapping(
DWRITE_WORD_WRAPPING_NO_WRAP
)
);
}
2018-09-30 14:54:43 +08:00
++render_times_;
if (duration >= 100)
{
2018-10-03 18:04:04 +08:00
String fps_text = String::Format(L"FPS: %.1f", (1000.f / duration * render_times_));
2018-09-30 14:54:43 +08:00
last_render_time_ = Time::Now();
render_times_ = 0;
SafeRelease(fps_text_layout_);
ThrowIfFailed(
2018-10-03 18:04:04 +08:00
write_factory_->CreateTextLayout(
(const wchar_t *)fps_text,
2018-10-16 14:57:28 +08:00
(UINT32)fps_text.Length(),
2018-09-30 14:54:43 +08:00
fps_text_format_,
0,
0,
&fps_text_layout_
)
);
}
if (fps_text_layout_)
{
2018-10-03 18:04:04 +08:00
render_target_->SetTransform(D2D1::Matrix3x2F::Identity());
solid_brush_->SetOpacity(1.0f);
text_renderer_->SetTextStyle(
2018-09-30 14:54:43 +08:00
D2D1::ColorF(D2D1::ColorF::White),
TRUE,
D2D1::ColorF(D2D1::ColorF::Black, 0.4f),
1.5f,
D2D1_LINE_JOIN_ROUND
);
2018-10-03 18:04:04 +08:00
fps_text_layout_->Draw(
nullptr,
text_renderer_,
10,
0
2018-09-04 22:42:34 +08:00
);
}
}
2018-10-16 14:13:15 +08:00
ID2D1HwndRenderTarget * easy2d::Graphics::GetRenderTarget() const
2018-09-04 22:42:34 +08:00
{
return render_target_;
}
2018-10-16 14:13:15 +08:00
ID2D1SolidColorBrush * easy2d::Graphics::GetSolidBrush() const
2018-09-04 22:42:34 +08:00
{
return solid_brush_;
}
2018-10-16 14:13:15 +08:00
easy2d::E2DTextRenderer * easy2d::Graphics::GetTextRender() const
2018-08-19 15:11:20 +08:00
{
2018-10-03 18:04:04 +08:00
return text_renderer_;
2018-08-19 15:11:20 +08:00
}
2018-10-16 14:13:15 +08:00
ID2D1Factory * easy2d::Graphics::GetFactory() const
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-10-16 14:13:15 +08:00
IWICImagingFactory * easy2d::Graphics::GetImagingFactory() const
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-10-16 14:13:15 +08:00
IDWriteFactory * easy2d::Graphics::GetWriteFactory() const
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-10-16 14:13:15 +08:00
ID2D1StrokeStyle * easy2d::Graphics::GetMiterStrokeStyle()
{
2018-09-04 22:42:34 +08:00
if (!miter_stroke_style_)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
2018-10-03 18:04:04 +08:00
factory_->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-10-16 14:13:15 +08:00
ID2D1StrokeStyle * easy2d::Graphics::GetBevelStrokeStyle()
{
2018-09-04 22:42:34 +08:00
if (!bevel_stroke_style_)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
2018-10-03 18:04:04 +08:00
factory_->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-10-16 14:13:15 +08:00
ID2D1StrokeStyle * easy2d::Graphics::GetRoundStrokeStyle()
{
2018-09-04 22:42:34 +08:00
if (!round_stroke_style_)
{
2018-08-15 00:06:03 +08:00
ThrowIfFailed(
2018-10-03 18:04:04 +08:00
factory_->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_;
}
2018-10-06 11:15:32 +08:00
2018-10-16 14:13:15 +08:00
float easy2d::Graphics::GetDpi()
2018-10-06 11:15:32 +08:00
{
HDC hdc = ::GetDC(0);
int dpi = ::GetDeviceCaps(hdc, LOGPIXELSX);
::ReleaseDC(0, hdc);
return static_cast<float>(dpi);
}