From 074af87127925ee425f6c10ba4b0c1392188d447 Mon Sep 17 00:00:00 2001
From: Nomango <569629550@qq.com>
Date: Sun, 10 Mar 2019 13:44:02 +0800
Subject: [PATCH] d3d11 support
---
project/Box2DSample/main.cpp | 2 +-
project/Easy2D/Easy2D.vcxproj | 16 +-
project/Easy2D/Easy2D.vcxproj.filters | 51 +-
project/HelloWorld/main.cpp | 8 +-
project/Samples/Demo1.h | 2 +
project/Samples/Demo2.h | 2 +
project/Samples/Demo3.h | 9 +-
project/Samples/main.cpp | 2 +-
src/core/Application.cpp | 109 ++--
src/core/Application.h | 21 +-
src/core/Canvas.cpp | 88 ++-
src/core/Canvas.h | 27 +-
src/core/DebugNode.cpp | 32 +-
src/core/DebugNode.h | 10 +-
src/core/Factory.h | 125 -----
src/core/Geometry.cpp | 71 ++-
src/core/Geometry.h | 6 +-
src/core/GeometryNode.cpp | 8 +-
src/core/GeometryNode.h | 2 +-
src/core/Image.cpp | 14 +-
src/core/Image.h | 8 +-
src/core/Input.cpp | 4 +-
src/core/Input.h | 2 +-
src/core/Node.cpp | 33 +-
src/core/Node.h | 19 +-
src/core/Scene.h | 2 +-
src/core/Sprite.cpp | 2 +-
src/core/Sprite.h | 2 +-
src/core/Text.cpp | 60 +-
src/core/Text.h | 20 +-
src/core/Transform.hpp | 4 +-
src/core/Transition.cpp | 22 +-
src/core/Transition.h | 4 +-
src/core/audio.cpp | 6 +-
src/core/audio.h | 2 +-
src/core/d2dhelper.hpp | 101 ----
src/core/helper.hpp | 4 +-
src/core/include-forwards.h | 2 +-
src/core/logs.h | 5 +-
src/core/modules.cpp | 11 +
src/core/modules.h | 3 +
src/core/render.cpp | 519 ++++++------------
src/core/render.h | 186 +++----
src/core/window.cpp | 8 +-
src/core/window.h | 3 +-
.../Factory.cpp => dx/D2DDeviceResources.cpp} | 378 ++++++-------
src/dx/D2DDeviceResources.h | 117 ++++
src/dx/D3D11DeviceResources.cpp | 387 +++++++++++++
src/dx/D3D11DeviceResources.h | 82 +++
src/dx/DeviceResources.h | 27 +
src/{core => dx}/TextRenderer.cpp | 145 +++--
src/{core => dx}/TextRenderer.h | 9 +-
src/dx/helper.hpp | 90 +++
src/easy2d.h | 3 +-
src/math/Matrix.cpp | 152 -----
src/math/{Matrix.h => Matrix.hpp} | 137 +++--
src/math/Rect.hpp | 2 +-
src/math/scalar.hpp | 1 -
src/math/vector.hpp | 7 +-
src/utils/Transcoder.cpp | 18 +-
60 files changed, 1741 insertions(+), 1451 deletions(-)
delete mode 100644 src/core/Factory.h
delete mode 100644 src/core/d2dhelper.hpp
rename src/{core/Factory.cpp => dx/D2DDeviceResources.cpp} (53%)
create mode 100644 src/dx/D2DDeviceResources.h
create mode 100644 src/dx/D3D11DeviceResources.cpp
create mode 100644 src/dx/D3D11DeviceResources.h
create mode 100644 src/dx/DeviceResources.h
rename src/{core => dx}/TextRenderer.cpp (77%)
rename src/{core => dx}/TextRenderer.h (88%)
create mode 100644 src/dx/helper.hpp
delete mode 100644 src/math/Matrix.cpp
rename src/math/{Matrix.h => Matrix.hpp} (57%)
diff --git a/project/Box2DSample/main.cpp b/project/Box2DSample/main.cpp
index 6f8f51cc..d51a78e1 100644
--- a/project/Box2DSample/main.cpp
+++ b/project/Box2DSample/main.cpp
@@ -216,7 +216,7 @@ int main()
}
catch (std::exception& e)
{
- ::MessageBoxA(nullptr, e.what(), "An exception has occurred!", MB_ICONERROR | MB_TASKMODAL);
+ ::MessageBoxA(nullptr, e.what(), "An exception has occurred!", MB_ICONERROR | MB_OK);
}
return 0;
}
diff --git a/project/Easy2D/Easy2D.vcxproj b/project/Easy2D/Easy2D.vcxproj
index 442d4eea..a5a99d20 100644
--- a/project/Easy2D/Easy2D.vcxproj
+++ b/project/Easy2D/Easy2D.vcxproj
@@ -13,12 +13,10 @@
-
-
@@ -47,16 +45,20 @@
-
+
+
+
+
+
-
+
@@ -83,7 +85,6 @@
-
@@ -102,11 +103,12 @@
-
-
+
+
+
diff --git a/project/Easy2D/Easy2D.vcxproj.filters b/project/Easy2D/Easy2D.vcxproj.filters
index b46a74cb..4ca70313 100644
--- a/project/Easy2D/Easy2D.vcxproj.filters
+++ b/project/Easy2D/Easy2D.vcxproj.filters
@@ -13,6 +13,9 @@
{07b6d541-4a1b-472a-aae0-daf9d082fe84}
+
+ {a9793a75-3212-4e31-a443-b23f18a1e136}
+
@@ -64,9 +67,6 @@
core
-
- core
-
core
@@ -154,9 +154,6 @@
math
-
- core
-
core
@@ -169,9 +166,6 @@
core
-
- core
-
core
@@ -208,9 +202,6 @@
core
-
- math
-
core
@@ -220,6 +211,24 @@
core
+
+ math
+
+
+ dx
+
+
+ dx
+
+
+ dx
+
+
+ dx
+
+
+ dx
+
@@ -264,9 +273,6 @@
core
-
- core
-
core
@@ -327,9 +333,6 @@
core
-
- core
-
core
@@ -351,11 +354,17 @@
core
-
- math
-
core
+
+ dx
+
+
+ dx
+
+
+ dx
+
\ No newline at end of file
diff --git a/project/HelloWorld/main.cpp b/project/HelloWorld/main.cpp
index ed22128b..998c131c 100644
--- a/project/HelloWorld/main.cpp
+++ b/project/HelloWorld/main.cpp
@@ -13,9 +13,11 @@ public:
{
// 创建文字节点
easy2d::TextPtr text = new easy2d::Text(L"Hello Easy2D!");
- // 修改节点位置
+ // 设置节点大小为文字布局大小
+ text->SetSize(text->GetLayoutSize());
+ // 修改节点位置, 使节点在屏幕上居中
text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
- // 修改节点锚点, 使节点在屏幕上居中
+ // 修改节点锚点, 使文字中心对齐屏幕中心
text->SetAnchor(0.5, 0.5);
// 添加到场景中
this->AddChild(text);
@@ -46,7 +48,7 @@ int main()
}
catch (std::exception& e)
{
- ::MessageBoxA(nullptr, e.what(), "An exception has occurred!", MB_ICONERROR | MB_TASKMODAL);
+ ::MessageBoxA(nullptr, e.what(), "An exception has occurred!", MB_ICONERROR | MB_OK);
}
return 0;
}
diff --git a/project/Samples/Demo1.h b/project/Samples/Demo1.h
index 1d49fc76..d9ee5c82 100644
--- a/project/Samples/Demo1.h
+++ b/project/Samples/Demo1.h
@@ -16,6 +16,8 @@ public:
{
// 创建文本
TextPtr text = new Text(L"Hello Easy2D!");
+ // 设置节点大小为文字布局大小
+ text->SetSize(text->GetLayoutSize());
// 让文本显示在屏幕中央
text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
text->SetAnchor(0.5, 0.5);
diff --git a/project/Samples/Demo2.h b/project/Samples/Demo2.h
index 97b49211..25471345 100644
--- a/project/Samples/Demo2.h
+++ b/project/Samples/Demo2.h
@@ -80,6 +80,8 @@ public:
// 创建说明文字
TextPtr text = new Text(L"按上下左右键移动\n按鼠标左键旋转\n点击鼠标右键隐藏");
+ // 设置节点大小为文字布局大小
+ text->SetSize(text->GetLayoutSize());
// 设置文字位置
text->SetAnchor(0.5f, 0.5f);
text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT - 50);
diff --git a/project/Samples/Demo3.h b/project/Samples/Demo3.h
index d5f3b6ea..0b798212 100644
--- a/project/Samples/Demo3.h
+++ b/project/Samples/Demo3.h
@@ -33,18 +33,15 @@ public:
// 创建说明文字
TextPtr intro_text = new Text(L"按上下键调整音量\n按空格键暂停或继续");
- intro_text->SetAnchor(0.5f, 0.5f);
- intro_text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 - 50);
+ intro_text->SetPosition(WINDOW_WIDTH / 2 - 80, WINDOW_HEIGHT / 2 - 50);
// 创建音量文字
volume_text = new Text(L"当前音量:");
- volume_text->SetAnchor(0.5f, 0.5f);
- volume_text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 + 30);
+ volume_text->SetPosition(WINDOW_WIDTH / 2 - 80, WINDOW_HEIGHT / 2 + 30);
// 创建状态文字
state_text = new Text(L"当前状态:");
- state_text->SetAnchor(0.5f, 0.5f);
- state_text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 + 60);
+ state_text->SetPosition(WINDOW_WIDTH / 2 - 80, WINDOW_HEIGHT / 2 + 60);
// 添加到场景
this->AddChild(intro_text);
diff --git a/project/Samples/main.cpp b/project/Samples/main.cpp
index 39a7e608..1fcbbbb9 100644
--- a/project/Samples/main.cpp
+++ b/project/Samples/main.cpp
@@ -76,7 +76,7 @@ int main()
}
catch (std::exception& e)
{
- ::MessageBoxA(nullptr, e.what(), "An exception has occurred!", MB_ICONERROR | MB_TASKMODAL);
+ ::MessageBoxA(nullptr, e.what(), "An exception has occurred!", MB_ICONERROR | MB_OK);
}
return 0;
}
diff --git a/src/core/Application.cpp b/src/core/Application.cpp
index b22d2742..21987cb1 100644
--- a/src/core/Application.cpp
+++ b/src/core/Application.cpp
@@ -21,7 +21,7 @@
#include "Application.h"
#include "logs.h"
#include "modules.h"
-#include "Factory.h"
+#include "render.h"
#include "Event.hpp"
#include "Scene.h"
#include "DebugNode.h"
@@ -37,7 +37,6 @@ namespace easy2d
Application::Application(String const& app_name)
: end_(true)
, inited_(false)
- , debug_(false)
, curr_scene_(nullptr)
, next_scene_(nullptr)
, transition_(nullptr)
@@ -56,44 +55,6 @@ namespace easy2d
void Application::Init(const Options& options)
{
- debug_ = options.debug;
-
- // show console if debug mode enabled
- if (debug_ && !::GetConsoleWindow())
- {
- if (!::AllocConsole())
- {
- E2D_WARNING_LOG(L"AllocConsole failed");
- }
- else
- {
- HWND console = ::GetConsoleWindow();
- FILE * dummy;
- freopen_s(&dummy, "CONOUT$", "w+t", stdout);
- freopen_s(&dummy, "CONIN$", "r+t", stdin);
- freopen_s(&dummy, "CONOUT$", "w+t", stderr);
- (void)dummy;
-
- std::cout.clear();
- std::wcout.clear();
- std::cin.clear();
- std::wcin.clear();
- std::cerr.clear();
- std::wcerr.clear();
-
- // disable the close button of console
- if (console)
- {
- HMENU hmenu = ::GetSystemMenu(console, FALSE);
- ::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
- }
- }
- }
-
- ThrowIfFailed(
- Factory::Instance().Init(debug_)
- );
-
ThrowIfFailed(
Window::Instance().Init(
options.title,
@@ -101,30 +62,26 @@ namespace easy2d
options.height,
options.icon,
options.fullscreen,
- Application::WndProc,
- debug_
+ Application::WndProc
)
);
HWND hwnd = Window::Instance().GetHandle();
ThrowIfFailed(
- RenderSystem::Instance().Init(
- hwnd,
- options.vsync,
- debug_
- )
+ Renderer::Instance().Init(hwnd)
);
+ Renderer::Instance().SetClearColor(options.clear_color);
+
ThrowIfFailed(
Input::Instance().Init(
- hwnd,
- debug_
+ hwnd
)
);
ThrowIfFailed(
- Audio::Instance().Init(debug_)
+ Audio::Instance().Init()
);
OnStart();
@@ -173,9 +130,8 @@ namespace easy2d
curr_scene_.Reset();
Audio::Instance().Destroy();
- RenderSystem::Instance().Destroy();
+ Renderer::Instance().Destroy();
Window::Instance().Destroy();
- Factory::Instance().Destroy();
}
}
@@ -251,16 +207,14 @@ namespace easy2d
if (next_scene_)
next_scene_->Update(dt);
- if (debug_)
+ if (DebugNode::IsShown())
DebugNode::Instance().Update(dt);
}
void Application::Render(HWND hwnd)
{
- auto& rt = RenderSystem::Instance();
-
ThrowIfFailed(
- rt.BeginDraw(hwnd)
+ Renderer::Instance().BeginDraw()
);
if (transition_)
@@ -272,16 +226,50 @@ namespace easy2d
curr_scene_->Render();
}
- if (debug_)
+ if (DebugNode::IsShown())
DebugNode::Instance().Render();
ThrowIfFailed(
- rt.EndDraw()
+ Renderer::Instance().EndDraw()
);
::InvalidateRect(hwnd, NULL, FALSE);
}
+ void Application::AllocConsole()
+ {
+ if (!::GetConsoleWindow())
+ {
+ if (!::AllocConsole())
+ {
+ E2D_WARNING_LOG(L"AllocConsole failed");
+ }
+ else
+ {
+ HWND console = ::GetConsoleWindow();
+ FILE * dummy;
+ freopen_s(&dummy, "CONOUT$", "w+t", stdout);
+ freopen_s(&dummy, "CONIN$", "r+t", stdin);
+ freopen_s(&dummy, "CONOUT$", "w+t", stderr);
+ (void)dummy;
+
+ std::cout.clear();
+ std::wcout.clear();
+ std::cin.clear();
+ std::wcin.clear();
+ std::cerr.clear();
+ std::wcerr.clear();
+
+ // disable the close button of console
+ if (console)
+ {
+ HMENU hmenu = ::GetSystemMenu(console, FALSE);
+ ::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
+ }
+ }
+ }
+ }
+
LRESULT CALLBACK Application::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
Application * app = reinterpret_cast(
@@ -363,10 +351,7 @@ namespace easy2d
UINT width = LOWORD(lparam);
UINT height = HIWORD(lparam);
- // 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染
- // 目标的大小。它可能会调用失败,但是这里可以忽略有可能的
- // 错误,因为这个错误将在下一次调用 EndDraw 时产生
- RenderSystem::Instance().Resize(width, height);
+ Renderer::Instance().GetDeviceResources()->SetLogicalSize(Size{ (float)width, (float)height });
if (SIZE_MAXHIDE == wparam || SIZE_MINIMIZED == wparam)
{
diff --git a/src/core/Application.h b/src/core/Application.h
index 9ee1e04a..1567a309 100644
--- a/src/core/Application.h
+++ b/src/core/Application.h
@@ -22,7 +22,6 @@
#include "include-forwards.h"
#include "time.h"
#include "window.h"
-#include "render.h"
#include "input.h"
#include "audio.h"
@@ -30,22 +29,22 @@ namespace easy2d
{
struct Options
{
- String title; // 标题
- int width; // 宽度
- int height; // 高度
- LPCWSTR icon; // 图标
- bool vsync; // 垂直同步
- bool fullscreen; // 全屏模式
- bool debug; // 调试模式
+ String title; // 标题
+ int width; // 宽度
+ int height; // 高度
+ LPCWSTR icon; // 图标
+ Color clear_color; // 清屏颜色
+ bool vsync; // 垂直同步
+ bool fullscreen; // 全屏模式
Options()
: title(L"Easy2D Game")
, width(640)
, height(480)
, icon(nullptr)
+ , clear_color(Color::Black)
, vsync(true)
, fullscreen(false)
- , debug(false)
{}
};
@@ -105,6 +104,9 @@ namespace easy2d
float scale_factor
);
+ // 分配控制台
+ static void AllocConsole();
+
private:
void Render(HWND);
@@ -115,7 +117,6 @@ namespace easy2d
private:
bool end_;
bool inited_;
- bool debug_;
float time_scale_;
String app_name_;
ScenePtr curr_scene_;
diff --git a/src/core/Canvas.cpp b/src/core/Canvas.cpp
index 1bfc4573..4ef31229 100644
--- a/src/core/Canvas.cpp
+++ b/src/core/Canvas.cpp
@@ -23,7 +23,7 @@
#include "logs.h"
#include "Image.h"
#include "Geometry.h"
-#include "Factory.h"
+#include "render.h"
namespace easy2d
{
@@ -31,37 +31,30 @@ namespace easy2d
: cache_expired_(false)
, stroke_width_(1.0f)
{
+ auto ctx = Renderer::Instance().GetDeviceResources()->GetD2DDeviceContext();
+
ThrowIfFailed(
- RenderSystem::Instance().CreateBitmapRenderTarget(render_target_)
+ ctx->CreateCompatibleRenderTarget(&render_target_)
);
- auto properties = D2D1::BrushProperties();
ThrowIfFailed(
render_target_->CreateSolidColorBrush(
D2D1::ColorF(0, 0, 0, 0),
- properties,
+ D2D1::BrushProperties(),
&fill_brush_)
);
ThrowIfFailed(
render_target_->CreateSolidColorBrush(
D2D1::ColorF(Color::White),
- properties,
+ D2D1::BrushProperties(),
&stroke_brush_)
);
ThrowIfFailed(
- render_target_->CreateSolidColorBrush(
- D2D1::ColorF(Color::White),
- properties,
- &text_brush_)
- );
-
- ThrowIfFailed(
- Factory::Instance().CreateTextRenderer(
- text_renderer_,
- render_target_,
- text_brush_
+ ITextRenderer::Create(
+ &text_renderer_,
+ render_target_.Get()
)
);
@@ -105,18 +98,18 @@ namespace easy2d
if (bitmap_cached_)
{
- RenderSystem::Instance().DrawBitmap(bitmap_cached_);
+ Renderer::Instance().DrawBitmap(bitmap_cached_);
}
}
void Canvas::SetStrokeColor(Color const& color)
{
- stroke_brush_->SetColor(ToD2dColorF(color));
+ stroke_brush_->SetColor(DX::ConvertToColorF(color));
}
void Canvas::SetFillColor(Color const& color)
{
- fill_brush_->SetColor(ToD2dColorF(color));
+ fill_brush_->SetColor(DX::ConvertToColorF(color));
}
void Canvas::SetStrokeWidth(float width)
@@ -126,7 +119,7 @@ namespace easy2d
void Canvas::SetOutlineJoinStyle(StrokeStyle outline_join)
{
- outline_join_style_ = Factory::Instance().GetStrokeStyle(outline_join);
+ outline_join_style_ = Renderer::Instance().GetDeviceResources()->GetStrokeStyle(outline_join);
}
void Canvas::SetTextStyle(Font const& font, TextStyle const & text_style)
@@ -135,12 +128,15 @@ namespace easy2d
text_style_ = text_style;
text_renderer_->SetTextStyle(
- ToD2dColorF(text_style_.color),
+ DX::ConvertToColorF(text_style_.color),
text_style_.outline,
- ToD2dColorF(text_style_.outline_color),
+ DX::ConvertToColorF(text_style_.outline_color),
text_style_.outline_width,
- Factory::Instance().GetStrokeStyle(text_style_.outline_stroke).Get()
+ Renderer::Instance().GetDeviceResources()->GetStrokeStyle(text_style_.outline_stroke)
);
+
+ // clear text format
+ text_format_ = nullptr;
}
Color Canvas::GetStrokeColor() const
@@ -162,7 +158,7 @@ namespace easy2d
void Canvas::SetBrushTransform(Matrix const & transform)
{
- render_target_->SetTransform(transform);
+ render_target_->SetTransform(DX::ConvertToMatrix3x2F(transform));
}
void Canvas::DrawLine(const Point & begin, const Point & end)
@@ -258,7 +254,7 @@ namespace easy2d
D2D1::RectF(0, 0, image->GetWidth(), image->GetHeight()),
opacity,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
- ToD2dRectF(image->GetCropRect())
+ DX::ConvertToRectF(image->GetCropRect())
);
cache_expired_ = true;
}
@@ -269,23 +265,25 @@ namespace easy2d
if (text.empty())
return;
- D2DTextFormatPtr text_format;
- ThrowIfFailed(
- Factory::Instance().CreateTextFormat(
- text_format,
- text_font_,
- text_style_
- )
- );
+ if (!text_format_)
+ {
+ ThrowIfFailed(
+ Renderer::Instance().GetDeviceResources()->CreateTextFormat(
+ text_format_,
+ text_font_,
+ text_style_
+ )
+ );
+ }
- D2DTextLayoutPtr text_layout;
+ ComPtr text_layout;
Size layout_size;
ThrowIfFailed(
- Factory::Instance().CreateTextLayout(
+ Renderer::Instance().GetDeviceResources()->CreateTextLayout(
text_layout,
layout_size,
text,
- text_format,
+ text_format_,
text_style_
)
);
@@ -390,14 +388,14 @@ namespace easy2d
current_geometry_ = nullptr;
ThrowIfFailed(
- Factory::Instance().CreatePathGeometry(current_geometry_)
+ Renderer::Instance().GetDeviceResources()->GetD2DFactory()->CreatePathGeometry(¤t_geometry_)
);
ThrowIfFailed(
current_geometry_->Open(¤t_sink_)
);
- current_sink_->BeginFigure(ToD2dPoint2F(begin_pos), D2D1_FIGURE_BEGIN_FILLED);
+ current_sink_->BeginFigure(DX::ConvertToPoint2F(begin_pos), D2D1_FIGURE_BEGIN_FILLED);
}
void Canvas::EndPath(bool closed)
@@ -415,7 +413,7 @@ namespace easy2d
void Canvas::AddLine(Point const & point)
{
if (current_sink_)
- current_sink_->AddLine(ToD2dPoint2F(point));
+ current_sink_->AddLine(DX::ConvertToPoint2F(point));
}
void Canvas::AddLines(Array const& points)
@@ -435,9 +433,9 @@ namespace easy2d
{
current_sink_->AddBezier(
D2D1::BezierSegment(
- ToD2dPoint2F(point1),
- ToD2dPoint2F(point2),
- ToD2dPoint2F(point3)
+ DX::ConvertToPoint2F(point1),
+ DX::ConvertToPoint2F(point2),
+ DX::ConvertToPoint2F(point3)
)
);
}
@@ -449,8 +447,8 @@ namespace easy2d
{
current_sink_->AddArc(
D2D1::ArcSegment(
- ToD2dPoint2F(point),
- ToD2dSizeF(radius),
+ DX::ConvertToPoint2F(point),
+ DX::ConvertToSizeF(radius),
rotation,
clockwise ? D2D1_SWEEP_DIRECTION_CLOCKWISE : D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE,
is_small ? D2D1_ARC_SIZE_SMALL : D2D1_ARC_SIZE_LARGE
@@ -492,7 +490,7 @@ namespace easy2d
return image;
}
- D2DBitmapPtr const& easy2d::Canvas::GetBitmap() const
+ ComPtr const& easy2d::Canvas::GetBitmap() const
{
if (cache_expired_)
{
diff --git a/src/core/Canvas.h b/src/core/Canvas.h
index 8ada5b0e..4cb2dbc1 100644
--- a/src/core/Canvas.h
+++ b/src/core/Canvas.h
@@ -22,7 +22,7 @@
#include "Node.h"
#include "Font.hpp"
#include "TextStyle.hpp"
-#include "TextRenderer.h"
+#include "../DX/TextRenderer.h"
#undef DrawText
@@ -30,7 +30,7 @@ namespace easy2d
{
// 画布
class E2D_API Canvas
- : public Node
+ : public VisualNode
{
public:
Canvas();
@@ -221,22 +221,23 @@ namespace easy2d
void OnRender() override;
protected:
- D2DBitmapPtr const& GetBitmap() const;
+ ComPtr const& GetBitmap() const;
protected:
float stroke_width_;
Font text_font_;
TextStyle text_style_;
- D2DPathGeometryPtr current_geometry_;
- D2DGeometrySinkPtr current_sink_;
- D2DStrokeStylePtr outline_join_style_;
- D2DSolidColorBrushPtr fill_brush_;
- D2DSolidColorBrushPtr stroke_brush_;
- D2DSolidColorBrushPtr text_brush_;
- D2DTextRendererPtr text_renderer_;
- D2DBitmapRenderTargetPtr render_target_;
- mutable bool cache_expired_;
- mutable D2DBitmapPtr bitmap_cached_;
+ ComPtr current_geometry_;
+ ComPtr current_sink_;
+ ComPtr outline_join_style_;
+ ComPtr fill_brush_;
+ ComPtr stroke_brush_;
+ ComPtr text_format_;
+ ComPtr text_renderer_;
+ ComPtr render_target_;
+
+ mutable bool cache_expired_;
+ mutable ComPtr bitmap_cached_;
};
}
\ No newline at end of file
diff --git a/src/core/DebugNode.cpp b/src/core/DebugNode.cpp
index 31a54911..ab214ac9 100644
--- a/src/core/DebugNode.cpp
+++ b/src/core/DebugNode.cpp
@@ -28,6 +28,11 @@
namespace easy2d
{
+ namespace
+ {
+ bool show = false;
+ }
+
DebugNode::DebugNode()
{
debug_text_ = new Text();
@@ -67,16 +72,14 @@ namespace easy2d
void DebugNode::OnRender()
{
- auto& rt = RenderSystem::Instance();
+ Renderer::Instance().GetSolidColorBrush()->SetColor(D2D1::ColorF(0.0f, 0.0f, 0.0f, 0.5f));
- rt.GetSolidBrush()->SetColor(D2D1::ColorF(0.0f, 0.0f, 0.0f, 0.5f));
-
- rt.GetRenderTarget()->FillRoundedRectangle(
+ Renderer::Instance().GetDeviceResources()->GetD2DDeviceContext()->FillRoundedRectangle(
D2D1::RoundedRect(
D2D1_RECT_F{ 10, 10, 200, 120 },
6.f,
6.f),
- rt.GetSolidBrush().Get()
+ Renderer::Instance().GetSolidColorBrush()
);
}
@@ -105,9 +108,9 @@ namespace easy2d
ss << "Objects: " << Object::__GetTracingObjects().size() << std::endl;
#endif
- ss << "Render: " << RenderSystem::Instance().GetStatus().duration.Milliseconds() << "ms" << std::endl;
+ ss << "Render: " << Renderer::Instance().GetStatus().duration.Milliseconds() << "ms" << std::endl;
- ss << "Primitives / sec: " << RenderSystem::Instance().GetStatus().primitives * frame_time_.size() << std::endl;
+ ss << "Primitives / sec: " << Renderer::Instance().GetStatus().primitives * frame_time_.size() << std::endl;
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
@@ -119,4 +122,19 @@ namespace easy2d
debug_text_->SetText(ss.str());
}
+ void DebugNode::Show()
+ {
+ show = true;
+ }
+
+ void DebugNode::Hide()
+ {
+ show = false;
+ }
+
+ bool DebugNode::IsShown()
+ {
+ return show;
+ }
+
}
diff --git a/src/core/DebugNode.h b/src/core/DebugNode.h
index 0cdd04e4..f6c926c6 100644
--- a/src/core/DebugNode.h
+++ b/src/core/DebugNode.h
@@ -26,7 +26,7 @@
namespace easy2d
{
class E2D_API DebugNode
- : public Node
+ : public VisualNode
, public Singleton
{
E2D_DECLARE_SINGLETON(DebugNode);
@@ -36,6 +36,12 @@ namespace easy2d
virtual ~DebugNode();
+ // 显示调试信息
+ void Show();
+
+ // 隐藏调试信息
+ void Hide();
+
void AddDebugText(String const& text);
void ClearDebugText();
@@ -44,6 +50,8 @@ namespace easy2d
void OnUpdate(Duration dt) override;
+ static bool IsShown();
+
protected:
TextPtr debug_text_;
Array frame_time_;
diff --git a/src/core/Factory.h b/src/core/Factory.h
deleted file mode 100644
index 9c47f483..00000000
--- a/src/core/Factory.h
+++ /dev/null
@@ -1,125 +0,0 @@
-// 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.
-
-#pragma once
-#include "include-forwards.h"
-#include "Singleton.hpp"
-#include "Font.hpp"
-#include "Resource.h"
-#include "TextRenderer.h"
-#include "TextStyle.hpp"
-
-namespace easy2d
-{
- class E2D_API Factory
- : public Singleton
- {
- E2D_DECLARE_SINGLETON(Factory);
-
- public:
- HRESULT Init(bool debug);
-
- void Destroy();
-
- HRESULT CreateHwndRenderTarget(
- _Out_ D2DHwndRenderTargetPtr& hwnd_render_target,
- D2D1_RENDER_TARGET_PROPERTIES const& properties,
- D2D1_HWND_RENDER_TARGET_PROPERTIES const& hwnd_rt_properties
- ) const;
-
- HRESULT CreateTextRenderer(
- _Out_ D2DTextRendererPtr& text_renderer,
- D2DRenderTargetPtr const& render_target,
- D2DSolidColorBrushPtr const& brush
- );
-
- HRESULT CreateBitmapFromFile(
- _Out_ D2DBitmapPtr& bitmap,
- D2DRenderTargetPtr const& rt,
- String const& file_path
- );
-
- HRESULT CreateBitmapFromResource(
- _Out_ D2DBitmapPtr& bitmap,
- D2DRenderTargetPtr const& rt,
- Resource const& res
- );
-
- HRESULT CreateRectangleGeometry(
- _Out_ D2DRectangleGeometryPtr& geo,
- Rect const& rect
- ) const;
-
- HRESULT CreateRoundedRectangleGeometry(
- _Out_ D2DRoundedRectangleGeometryPtr& geo,
- Rect const& rect,
- float radius_x,
- float radius_y
- ) const;
-
- HRESULT CreateEllipseGeometry(
- _Out_ D2DEllipseGeometryPtr& geo,
- Point const& center,
- float radius_x,
- float radius_y
- ) const;
-
- HRESULT CreateTransformedGeometry(
- _Out_ D2DTransformedGeometryPtr& transformed,
- Matrix const& matrix,
- D2DGeometryPtr const& geo
- ) const;
-
- HRESULT CreatePathGeometry(
- _Out_ D2DPathGeometryPtr& geometry
- ) const;
-
- HRESULT CreateTextFormat(
- _Out_ D2DTextFormatPtr& text_format,
- Font const& font,
- TextStyle const& text_style
- ) const;
-
- HRESULT CreateTextLayout(
- _Out_ D2DTextLayoutPtr& text_layout,
- _Out_ Size& layout_size,
- String const& text,
- D2DTextFormatPtr const& text_format,
- TextStyle const& text_style
- ) const;
-
- D2DStrokeStylePtr const& GetStrokeStyle(
- StrokeStyle stroke
- ) const;
-
- protected:
- Factory();
-
- ~Factory();
-
- protected:
- D2DFactoryPtr factory_;
- D2DImagingFactoryPtr imaging_factory_;
- D2DWriteFactoryPtr write_factory_;
- D2DStrokeStylePtr miter_stroke_style_;
- D2DStrokeStylePtr bevel_stroke_style_;
- D2DStrokeStylePtr round_stroke_style_;
- };
-}
diff --git a/src/core/Geometry.cpp b/src/core/Geometry.cpp
index 5e6dd997..46c0fdf9 100644
--- a/src/core/Geometry.cpp
+++ b/src/core/Geometry.cpp
@@ -19,7 +19,6 @@
// THE SOFTWARE.
#include "Geometry.h"
-#include "Factory.h"
#include "render.h"
#include "logs.h"
@@ -99,7 +98,7 @@ namespace easy2d
BOOL ret = 0;
// no matter it failed or not
geo_->FillContainsPoint(
- ToD2dPoint2F(point),
+ DX::ConvertToPoint2F(point),
D2D1::Matrix3x2F::Identity(),
&ret
);
@@ -126,10 +125,10 @@ namespace easy2d
void LineGeometry::SetLine(Point const & begin, Point const & end)
{
- D2DPathGeometryPtr path_geo;
- D2DGeometrySinkPtr path_sink;
+ ComPtr path_geo;
+ ComPtr path_sink;
- HRESULT hr = Factory::Instance().CreatePathGeometry(path_geo);
+ HRESULT hr = Renderer::Instance().GetDeviceResources()->GetD2DFactory()->CreatePathGeometry(&path_geo);
if (SUCCEEDED(hr))
{
@@ -138,8 +137,8 @@ namespace easy2d
if (SUCCEEDED(hr))
{
- path_sink->BeginFigure(ToD2dPoint2F(begin), D2D1_FIGURE_BEGIN_FILLED);
- path_sink->AddLine(ToD2dPoint2F(end));
+ path_sink->BeginFigure(DX::ConvertToPoint2F(begin), D2D1_FIGURE_BEGIN_FILLED);
+ path_sink->AddLine(DX::ConvertToPoint2F(end));
path_sink->EndFigure(D2D1_FIGURE_END_OPEN);
hr = path_sink->Close();
}
@@ -185,8 +184,10 @@ namespace easy2d
void RectangleGeometry::SetRect(Rect const & rect)
{
- D2DRectangleGeometryPtr geo;
- if (SUCCEEDED(Factory::Instance().CreateRectangleGeometry(geo, rect)))
+ ComPtr geo;
+ auto factory = Renderer::Instance().GetDeviceResources()->GetD2DFactory();
+
+ if (SUCCEEDED(factory->CreateRectangleGeometry(DX::ConvertToRectF(rect), &geo)))
{
geo_ = geo;
rect_ = rect;
@@ -224,8 +225,15 @@ namespace easy2d
void CircleGeometry::SetCircle(Point const & center, float radius)
{
- D2DEllipseGeometryPtr geo;
- if (SUCCEEDED(Factory::Instance().CreateEllipseGeometry(geo, center, radius, radius)))
+ ComPtr geo;
+ auto factory = Renderer::Instance().GetDeviceResources()->GetD2DFactory();
+
+ if (SUCCEEDED(factory->CreateEllipseGeometry(
+ D2D1::Ellipse(
+ DX::ConvertToPoint2F(center),
+ radius,
+ radius),
+ &geo)))
{
geo_ = geo;
center_ = center;
@@ -265,8 +273,15 @@ namespace easy2d
void EllipseGeometry::SetEllipse(Point const & center, float radius_x, float radius_y)
{
- D2DEllipseGeometryPtr geo;
- if (SUCCEEDED(Factory::Instance().CreateEllipseGeometry(geo, center, radius_x, radius_y)))
+ ComPtr geo;
+ auto factory = Renderer::Instance().GetDeviceResources()->GetD2DFactory();
+
+ if (SUCCEEDED(factory->CreateEllipseGeometry(
+ D2D1::Ellipse(
+ DX::ConvertToPoint2F(center),
+ radius_x,
+ radius_y),
+ &geo)))
{
geo_ = geo;
radius_x_ = radius_x;
@@ -291,15 +306,17 @@ namespace easy2d
{
current_geometry_ = nullptr;
+ auto factory = Renderer::Instance().GetDeviceResources()->GetD2DFactory();
+
ThrowIfFailed(
- Factory::Instance().CreatePathGeometry(current_geometry_)
+ factory->CreatePathGeometry(¤t_geometry_)
);
ThrowIfFailed(
current_geometry_->Open(¤t_sink_)
);
- current_sink_->BeginFigure(ToD2dPoint2F(begin_pos), D2D1_FIGURE_BEGIN_FILLED);
+ current_sink_->BeginFigure(DX::ConvertToPoint2F(begin_pos), D2D1_FIGURE_BEGIN_FILLED);
}
void PathGeometry::EndPath(bool closed)
@@ -321,7 +338,7 @@ namespace easy2d
void PathGeometry::AddLine(Point const & point)
{
if (current_sink_)
- current_sink_->AddLine(ToD2dPoint2F(point));
+ current_sink_->AddLine(DX::ConvertToPoint2F(point));
}
void PathGeometry::AddLines(Array const& points)
@@ -341,9 +358,9 @@ namespace easy2d
{
current_sink_->AddBezier(
D2D1::BezierSegment(
- ToD2dPoint2F(point1),
- ToD2dPoint2F(point2),
- ToD2dPoint2F(point3)
+ DX::ConvertToPoint2F(point1),
+ DX::ConvertToPoint2F(point2),
+ DX::ConvertToPoint2F(point3)
)
);
}
@@ -355,8 +372,8 @@ namespace easy2d
{
current_sink_->AddArc(
D2D1::ArcSegment(
- ToD2dPoint2F(point),
- ToD2dSizeF(radius),
+ DX::ConvertToPoint2F(point),
+ DX::ConvertToSizeF(radius),
rotation,
clockwise ? D2D1_SWEEP_DIRECTION_CLOCKWISE : D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE,
is_small ? D2D1_ARC_SIZE_SMALL : D2D1_ARC_SIZE_LARGE
@@ -404,8 +421,16 @@ namespace easy2d
void RoundedRectGeometry::SetRoundedRect(Rect const & rect, float radius_x, float radius_y)
{
- D2DRoundedRectangleGeometryPtr geo;
- if (SUCCEEDED(Factory::Instance().CreateRoundedRectangleGeometry(geo, rect, radius_x, radius_y)))
+ ComPtr geo;
+ auto factory = Renderer::Instance().GetDeviceResources()->GetD2DFactory();
+
+ if (SUCCEEDED(factory->CreateRoundedRectangleGeometry(
+ D2D1::RoundedRect(
+ DX::ConvertToRectF(rect),
+ radius_x,
+ radius_y
+ ),
+ &geo)))
{
geo_ = geo;
rect_ = rect;
diff --git a/src/core/Geometry.h b/src/core/Geometry.h
index 917113d9..aed3637f 100644
--- a/src/core/Geometry.h
+++ b/src/core/Geometry.h
@@ -57,7 +57,7 @@ namespace easy2d
float ComputeArea();
protected:
- D2DGeometryPtr geo_;
+ ComPtr geo_;
};
@@ -254,8 +254,8 @@ namespace easy2d
void ClearPath();
protected:
- D2DPathGeometryPtr current_geometry_;
- D2DGeometrySinkPtr current_sink_;
+ ComPtr current_geometry_;
+ ComPtr current_sink_;
};
diff --git a/src/core/GeometryNode.cpp b/src/core/GeometryNode.cpp
index 54cbcc69..3a3ca899 100644
--- a/src/core/GeometryNode.cpp
+++ b/src/core/GeometryNode.cpp
@@ -70,14 +70,12 @@ namespace easy2d
{
if (geometry_ && geometry_->geo_)
{
- auto& rt = RenderSystem::Instance();
-
- rt.FillGeometry(
- geometry_->geo_,
+ Renderer::Instance().FillGeometry(
+ geometry_->geo_.Get(),
fill_color_
);
- rt.DrawGeometry(
+ Renderer::Instance().DrawGeometry(
geometry_->geo_,
stroke_color_,
stroke_width_,
diff --git a/src/core/GeometryNode.h b/src/core/GeometryNode.h
index 00a1b9f1..6296a20b 100644
--- a/src/core/GeometryNode.h
+++ b/src/core/GeometryNode.h
@@ -26,7 +26,7 @@ namespace easy2d
{
// 几何图形
class E2D_API GeometryNode
- : public Node
+ : public VisualNode
{
public:
GeometryNode();
diff --git a/src/core/Image.cpp b/src/core/Image.cpp
index e4041442..87cb14e4 100644
--- a/src/core/Image.cpp
+++ b/src/core/Image.cpp
@@ -19,8 +19,8 @@
// THE SOFTWARE.
#include "Image.h"
-#include "render.h"
#include "logs.h"
+#include "render.h"
#include "../utils/File.h"
#include "../utils/string.h"
@@ -45,7 +45,7 @@ namespace easy2d
this->Crop(crop_rect);
}
- Image::Image(D2DBitmapPtr const & bitmap)
+ Image::Image(ComPtr const & bitmap)
: Image()
{
SetBitmap(bitmap);
@@ -58,7 +58,7 @@ namespace easy2d
bool Image::Load(Resource const& res)
{
HRESULT hr = S_OK;
- D2DBitmapPtr bitmap;
+ ComPtr bitmap;
if (res.IsFileType())
{
@@ -67,11 +67,11 @@ namespace easy2d
E2D_WARNING_LOG(L"Image file '%s' not found!", res.GetFileName().c_str());
return false;
}
- hr = RenderSystem::Instance().CreateBitmapFromFile(bitmap, res.GetFileName());
+ hr = Renderer::Instance().GetDeviceResources()->CreateBitmapFromFile(bitmap, res.GetFileName());
}
else
{
- hr = RenderSystem::Instance().CreateBitmapFromResource(bitmap, res);
+ hr = Renderer::Instance().GetDeviceResources()->CreateBitmapFromResource(bitmap, res);
}
if (FAILED(hr))
@@ -164,12 +164,12 @@ namespace easy2d
return crop_rect_;
}
- D2DBitmapPtr const& Image::GetBitmap() const
+ ComPtr const& Image::GetBitmap() const
{
return bitmap_;
}
- void Image::SetBitmap(D2DBitmapPtr const & bitmap)
+ void Image::SetBitmap(ComPtr const & bitmap)
{
if (bitmap)
{
diff --git a/src/core/Image.h b/src/core/Image.h
index ed525586..a335a77a 100644
--- a/src/core/Image.h
+++ b/src/core/Image.h
@@ -41,7 +41,7 @@ namespace easy2d
);
explicit Image(
- D2DBitmapPtr const& bitmap
+ ComPtr const& bitmap
);
virtual ~Image();
@@ -89,15 +89,15 @@ namespace easy2d
// 获取裁剪矩形
Rect const& GetCropRect() const;
- D2DBitmapPtr const& GetBitmap() const;
+ ComPtr const& GetBitmap() const;
protected:
void SetBitmap(
- D2DBitmapPtr const& bitmap
+ ComPtr const& bitmap
);
protected:
Rect crop_rect_;
- D2DBitmapPtr bitmap_;
+ ComPtr bitmap_;
};
}
diff --git a/src/core/Input.cpp b/src/core/Input.cpp
index e1d2b4ca..10d15285 100644
--- a/src/core/Input.cpp
+++ b/src/core/Input.cpp
@@ -37,10 +37,8 @@ namespace easy2d
{
}
- HRESULT Input::Init(HWND hwnd, bool debug)
+ HRESULT Input::Init(HWND hwnd)
{
- E2D_NOT_USED(debug);
-
hwnd_ = hwnd;
return S_OK;
diff --git a/src/core/Input.h b/src/core/Input.h
index 97ff6ace..dff99897 100644
--- a/src/core/Input.h
+++ b/src/core/Input.h
@@ -55,7 +55,7 @@ namespace easy2d
// 获得鼠标坐标
Point GetMousePos();
- HRESULT Init(HWND hwnd, bool debug);
+ HRESULT Init(HWND hwnd);
void Update();
diff --git a/src/core/Node.cpp b/src/core/Node.cpp
index 32e0ac25..1799e613 100644
--- a/src/core/Node.cpp
+++ b/src/core/Node.cpp
@@ -20,7 +20,6 @@
#include "Node.h"
#include "Action.h"
-#include "Factory.h"
#include "Scene.h"
#include "Task.h"
#include "render.h"
@@ -42,7 +41,7 @@ namespace easy2d
Node::Node()
: visible_(true)
- , pause_(false)
+ , update_pausing_(false)
, hover_(false)
, pressed_(false)
, responsible_(false)
@@ -59,7 +58,7 @@ namespace easy2d
void Node::Update(Duration dt)
{
- if (pause_)
+ if (update_pausing_)
return;
UpdateActions(this, dt);
@@ -88,13 +87,9 @@ namespace easy2d
UpdateTransform();
- auto& rt = RenderSystem::Instance();
-
if (children_.IsEmpty())
{
- rt.SetTransform(transform_matrix_);
- rt.SetOpacity(display_opacity_);
-
+ PrepareRender();
OnRender();
}
else
@@ -110,9 +105,7 @@ namespace easy2d
child = child->NextItem().Get();
}
- rt.SetTransform(transform_matrix_);
- rt.SetOpacity(display_opacity_);
-
+ PrepareRender();
OnRender();
while (child)
@@ -184,16 +177,6 @@ namespace easy2d
EventDispatcher::Dispatch(evt);
}
- void Node::PauseUpdating()
- {
- pause_ = true;
- }
-
- void Node::ResumeUpdating()
- {
- pause_ = false;
- }
-
Matrix const & Node::GetTransformMatrix() const
{
UpdateTransform();
@@ -607,4 +590,12 @@ namespace easy2d
Point local = GetTransformInverseMatrix().Transform(point);
return GetBounds().ContainsPoint(local);
}
+
+
+ void VisualNode::PrepareRender()
+ {
+ Renderer::Instance().SetTransform(transform_matrix_);
+ Renderer::Instance().SetOpacity(display_opacity_);
+ }
+
}
diff --git a/src/core/Node.h b/src/core/Node.h
index b38b9901..6c6f6dce 100644
--- a/src/core/Node.h
+++ b/src/core/Node.h
@@ -344,13 +344,13 @@ namespace easy2d
void RemoveFromParent();
// 暂停节点更新
- void PauseUpdating();
+ inline void PauseUpdating() { update_pausing_ = true; }
// 继续节点更新
- void ResumeUpdating();
+ inline void ResumeUpdating() { update_pausing_ = false; }
// 节点更新是否暂停
- inline bool IsUpdatePausing() const { return pause_; }
+ inline bool IsUpdatePausing() const { return update_pausing_; }
// 设置更新时的回调函数
inline void SetCallbackOnUpdate(UpdateCallback const& cb) { cb_update_ = cb; }
@@ -365,6 +365,8 @@ namespace easy2d
);
protected:
+ virtual void PrepareRender() {}
+
void Update(Duration dt);
void Render();
@@ -380,7 +382,7 @@ namespace easy2d
bool hover_;
bool pressed_;
bool responsible_;
- bool pause_;
+ bool update_pausing_;
int z_order_;
float opacity_;
float display_opacity_;
@@ -398,4 +400,13 @@ namespace easy2d
mutable Matrix transform_matrix_;
mutable Matrix transform_matrix_inverse_;
};
+
+
+ class E2D_API VisualNode
+ : public Node
+ {
+ public:
+ virtual void PrepareRender() override;
+ };
+
}
diff --git a/src/core/Scene.h b/src/core/Scene.h
index 3e40a66d..11e24dc1 100644
--- a/src/core/Scene.h
+++ b/src/core/Scene.h
@@ -25,7 +25,7 @@ namespace easy2d
{
// 场景
class E2D_API Scene
- : public Node
+ : public VisualNode
{
public:
Scene();
diff --git a/src/core/Sprite.cpp b/src/core/Sprite.cpp
index 5ac2e9d3..c0539b9c 100644
--- a/src/core/Sprite.cpp
+++ b/src/core/Sprite.cpp
@@ -99,7 +99,7 @@ namespace easy2d
{
if (image_)
{
- RenderSystem::Instance().DrawImage(image_, GetBounds());
+ Renderer::Instance().DrawImage(image_, GetBounds());
}
}
}
\ No newline at end of file
diff --git a/src/core/Sprite.h b/src/core/Sprite.h
index ed03aea6..9e2ac4db 100644
--- a/src/core/Sprite.h
+++ b/src/core/Sprite.h
@@ -26,7 +26,7 @@ namespace easy2d
{
// 精灵
class E2D_API Sprite
- : public Node
+ : public VisualNode
{
public:
Sprite();
diff --git a/src/core/Text.cpp b/src/core/Text.cpp
index 9039ade5..84054bef 100644
--- a/src/core/Text.cpp
+++ b/src/core/Text.cpp
@@ -19,9 +19,7 @@
// THE SOFTWARE.
#include "Text.h"
-#include "Factory.h"
#include "render.h"
-#include "include-forwards.h"
#include "logs.h"
namespace easy2d
@@ -45,6 +43,7 @@ namespace easy2d
Text::Text()
: font_(text_default_font)
, style_(text_default_style)
+ , layout_dirty_(false)
{
}
@@ -67,8 +66,8 @@ namespace easy2d
: font_(font)
, style_(style)
, text_(text)
+ , layout_dirty_(true)
{
- UpdateLayout();
}
Text::~Text()
@@ -127,6 +126,7 @@ namespace easy2d
int Text::GetLineCount()
{
+ UpdateLayout();
if (text_layout_)
{
DWRITE_TEXT_METRICS metrics;
@@ -138,6 +138,12 @@ namespace easy2d
return 0;
}
+ Size const& Text::GetLayoutSize() const
+ {
+ UpdateLayout();
+ return layout_size_;
+ }
+
bool Text::IsItalic() const
{
return font_.italic;
@@ -161,19 +167,19 @@ namespace easy2d
void Text::SetText(String const& text)
{
text_ = text;
- UpdateLayout();
+ layout_dirty_ = true;
}
void Text::SetStyle(const TextStyle& style)
{
style_ = style;
- UpdateLayout();
+ layout_dirty_ = true;
}
void Text::SetFont(const Font & font)
{
font_ = font;
- UpdateLayout();
+ layout_dirty_ = true;
}
void Text::SetFontFamily(String const& family)
@@ -181,7 +187,7 @@ namespace easy2d
if (font_.family != family)
{
font_.family = family;
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -190,7 +196,7 @@ namespace easy2d
if (font_.size != size)
{
font_.size = size;
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -199,7 +205,7 @@ namespace easy2d
if (font_.weight != weight)
{
font_.weight = weight;
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -213,7 +219,7 @@ namespace easy2d
if (font_.italic != val)
{
font_.italic = val;
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -222,7 +228,7 @@ namespace easy2d
if (style_.wrap != wrap)
{
style_.wrap = wrap;
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -231,7 +237,7 @@ namespace easy2d
if (style_.wrap_width != wrap_width)
{
style_.wrap_width = std::max(wrap_width, 0.f);
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -240,7 +246,7 @@ namespace easy2d
if (style_.line_spacing != line_spacing)
{
style_.line_spacing = line_spacing;
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -249,7 +255,7 @@ namespace easy2d
if (style_.alignment != align)
{
style_.alignment = align;
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -258,7 +264,7 @@ namespace easy2d
if (style_.underline != underline)
{
style_.underline = underline;
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -267,7 +273,7 @@ namespace easy2d
if (style_.strikethrough != strikethrough)
{
style_.strikethrough = strikethrough;
- UpdateLayout();
+ layout_dirty_ = true;
}
}
@@ -293,22 +299,27 @@ namespace easy2d
void Text::OnRender()
{
+ UpdateLayout();
+
if (text_layout_)
{
- auto& rt = RenderSystem::Instance();
- rt.SetTextStyle(
+ Renderer::Instance().SetTextStyle(
style_.color,
style_.outline,
style_.outline_color,
style_.outline_width,
style_.outline_stroke
);
- rt.DrawTextLayout(text_layout_);
+ Renderer::Instance().DrawTextLayout(text_layout_);
}
}
- void Text::UpdateLayout()
+ void Text::UpdateLayout() const
{
+ if (!layout_dirty_)
+ return;
+
+ layout_dirty_ = false;
text_format_ = nullptr;
text_layout_ = nullptr;
@@ -316,24 +327,21 @@ namespace easy2d
return;
ThrowIfFailed(
- Factory::Instance().CreateTextFormat(
+ Renderer::Instance().GetDeviceResources()->CreateTextFormat(
text_format_,
font_,
style_
)
);
- Size layout_size;
ThrowIfFailed(
- Factory::Instance().CreateTextLayout(
+ Renderer::Instance().GetDeviceResources()->CreateTextLayout(
text_layout_,
- layout_size,
+ layout_size_,
text_,
text_format_,
style_
)
);
-
- this->SetSize(layout_size);
}
}
diff --git a/src/core/Text.h b/src/core/Text.h
index d33c0bf1..1459d174 100644
--- a/src/core/Text.h
+++ b/src/core/Text.h
@@ -27,7 +27,7 @@ namespace easy2d
{
// 文本
class E2D_API Text
- : public Node
+ : public VisualNode
{
public:
Text();
@@ -87,6 +87,9 @@ namespace easy2d
// 获取文本显示行数
int GetLineCount();
+ // 获取文字布局大小
+ Size const& GetLayoutSize() const;
+
// 是否是斜体
bool IsItalic() const;
@@ -202,13 +205,16 @@ namespace easy2d
void OnRender() override;
protected:
- void UpdateLayout();
+ void UpdateLayout() const;
protected:
- String text_;
- Font font_;
- TextStyle style_;
- D2DTextFormatPtr text_format_;
- D2DTextLayoutPtr text_layout_;
+ String text_;
+ Font font_;
+ TextStyle style_;
+
+ mutable bool layout_dirty_;
+ mutable Size layout_size_;
+ mutable ComPtr text_format_;
+ mutable ComPtr text_layout_;
};
}
\ No newline at end of file
diff --git a/src/core/Transform.hpp b/src/core/Transform.hpp
index 8c2cbefe..5da5816a 100644
--- a/src/core/Transform.hpp
+++ b/src/core/Transform.hpp
@@ -19,11 +19,11 @@
// THE SOFTWARE.
#pragma once
-#include "helper.hpp"
+#include "../math/Matrix.hpp"
namespace easy2d
{
- class E2D_API Transform
+ class Transform
{
public:
float rotation; // 旋转
diff --git a/src/core/Transition.cpp b/src/core/Transition.cpp
index 447c8917..e33b7acf 100644
--- a/src/core/Transition.cpp
+++ b/src/core/Transition.cpp
@@ -66,14 +66,14 @@ namespace easy2d
if (in_scene_)
{
ThrowIfFailed(
- RenderSystem::Instance().CreateLayer(in_layer_)
+ Renderer::Instance().CreateLayer(in_layer_)
);
}
if (out_scene_)
{
ThrowIfFailed(
- RenderSystem::Instance().CreateLayer(out_layer_)
+ Renderer::Instance().CreateLayer(out_layer_)
);
}
@@ -101,34 +101,34 @@ namespace easy2d
void Transition::Render()
{
- auto& rt = RenderSystem::Instance();
+ auto& renderer = Renderer::Instance();
if (out_scene_)
{
- rt.PushClip(
+ renderer.PushClip(
out_scene_->GetTransformMatrix(),
window_size_
);
- rt.PushLayer(out_layer_, out_layer_prop_);
+ renderer.PushLayer(out_layer_, out_layer_prop_);
out_scene_->Render();
- rt.PopLayer();
- rt.PopClip();
+ renderer.PopLayer();
+ renderer.PopClip();
}
if (in_scene_)
{
- rt.PushClip(
+ renderer.PushClip(
in_scene_->GetTransformMatrix(),
window_size_
);
- rt.PushLayer(in_layer_, in_layer_prop_);
+ renderer.PushLayer(in_layer_, in_layer_prop_);
in_scene_->Render();
- rt.PopLayer();
- rt.PopClip();
+ renderer.PopLayer();
+ renderer.PopClip();
}
}
diff --git a/src/core/Transition.h b/src/core/Transition.h
index 6e9143bb..e21f34a8 100644
--- a/src/core/Transition.h
+++ b/src/core/Transition.h
@@ -63,8 +63,8 @@ namespace easy2d
Size window_size_;
ScenePtr out_scene_;
ScenePtr in_scene_;
- D2DLayerPtr out_layer_;
- D2DLayerPtr in_layer_;
+ ComPtr out_layer_;
+ ComPtr in_layer_;
LayerProperties out_layer_prop_;
LayerProperties in_layer_prop_;
};
diff --git a/src/core/audio.cpp b/src/core/audio.cpp
index cdbd92da..7ec893af 100644
--- a/src/core/audio.cpp
+++ b/src/core/audio.cpp
@@ -169,10 +169,8 @@ namespace easy2d
{
}
- HRESULT Audio::Init(bool debug)
+ HRESULT Audio::Init()
{
- E2D_NOT_USED(debug);
-
E2D_LOG(L"Initing audio resources");
HRESULT hr = modules::MediaFoundation::Get().MFStartup(MF_VERSION, MFSTARTUP_FULL);
@@ -202,7 +200,7 @@ namespace easy2d
mastering_voice_ = nullptr;
}
- SafeRelease(x_audio2_);
+ DX::SafeRelease(x_audio2_);
modules::MediaFoundation::Get().MFShutdown();
}
diff --git a/src/core/audio.h b/src/core/audio.h
index bd7660e9..355dfa09 100644
--- a/src/core/audio.h
+++ b/src/core/audio.h
@@ -82,7 +82,7 @@ namespace easy2d
using VoiceMap = UnorderedSet;
public:
- HRESULT Init(bool debug);
+ HRESULT Init();
void Destroy();
diff --git a/src/core/d2dhelper.hpp b/src/core/d2dhelper.hpp
deleted file mode 100644
index a8798e07..00000000
--- a/src/core/d2dhelper.hpp
+++ /dev/null
@@ -1,101 +0,0 @@
-// 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.
-
-#pragma once
-#include "IntrusivePtr.hpp"
-#include "../math/vector.hpp"
-#include "../math/Rect.hpp"
-#include "Color.h"
-#include
-#include
-
-#ifndef E2D_DECLARE_D2D_SMART_PTR
-#define E2D_DECLARE_D2D_SMART_PTR(class_name, sp_name)\
- using sp_name = ::easy2d::IntrusivePtr< class_name >
-#endif
-
-
-namespace easy2d
-{
- // "D2DPtr" is a shorthand for "COM Pointer"
-
- E2D_DECLARE_D2D_SMART_PTR(ID2D1Factory, D2DFactoryPtr);
- E2D_DECLARE_D2D_SMART_PTR(IWICImagingFactory, D2DImagingFactoryPtr);
- E2D_DECLARE_D2D_SMART_PTR(IDWriteFactory, D2DWriteFactoryPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1SolidColorBrush, D2DSolidColorBrushPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1RenderTarget, D2DRenderTargetPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1HwndRenderTarget, D2DHwndRenderTargetPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1BitmapRenderTarget, D2DBitmapRenderTargetPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1StrokeStyle, D2DStrokeStylePtr);
-
- E2D_DECLARE_D2D_SMART_PTR(ID2D1Geometry, D2DGeometryPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1RectangleGeometry, D2DRectangleGeometryPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1RoundedRectangleGeometry, D2DRoundedRectangleGeometryPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1EllipseGeometry, D2DEllipseGeometryPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1GeometryGroup, D2DGeometryGroupPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1PathGeometry, D2DPathGeometryPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1TransformedGeometry, D2DTransformedGeometryPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1GeometrySink, D2DGeometrySinkPtr);
-
- E2D_DECLARE_D2D_SMART_PTR(ID2D1Layer, D2DLayerPtr);
- E2D_DECLARE_D2D_SMART_PTR(ID2D1Bitmap, D2DBitmapPtr);
- E2D_DECLARE_D2D_SMART_PTR(IDWriteTextFormat, D2DTextFormatPtr);
- E2D_DECLARE_D2D_SMART_PTR(IDWriteTextLayout, D2DTextLayoutPtr);
-
- inline void IntrusivePtrAddRef(IUnknown* ptr)
- {
- if (ptr) { ptr->AddRef(); }
- }
-
- inline void IntrusivePtrRelease(IUnknown* ptr)
- {
- if (ptr) { ptr->Release(); }
- }
-
- template
- inline void SafeRelease(T*& ptr)
- {
- if (ptr != nullptr)
- {
- ptr->Release();
- ptr = nullptr;
- }
- }
-
- inline D2D1_POINT_2F const& ToD2dPoint2F(math::Vec2 const& point)
- {
- return reinterpret_cast(point);
- }
-
- inline D2D1_SIZE_F const& ToD2dSizeF(math::Vec2 const& size)
- {
- return reinterpret_cast(size);
- }
-
- inline D2D1_RECT_F ToD2dRectF(math::Rect const& rect)
- {
- return D2D1_RECT_F{ rect.origin.x, rect.origin.y, rect.origin.x + rect.size.x, rect.origin.y + rect.size.y };
- }
-
- inline D2D1_COLOR_F const& ToD2dColorF(Color const& color)
- {
- return reinterpret_cast(color);
- }
-}
diff --git a/src/core/helper.hpp b/src/core/helper.hpp
index 5343148d..db90dd91 100644
--- a/src/core/helper.hpp
+++ b/src/core/helper.hpp
@@ -25,7 +25,7 @@
#include "closure.hpp"
#include "../math/vector.hpp"
#include "../math/Rect.hpp"
-#include "../math/Matrix.h"
+#include "../math/Matrix.hpp"
#include
#include