From 2b411989cfbb6e148dd201d916ebaa2d44a98594 Mon Sep 17 00:00:00 2001 From: Haibo Date: Mon, 19 Nov 2018 19:32:12 +0800 Subject: [PATCH] add: debug node --- core/base/Debuger.cpp | 100 ++++++++++++++++++++++++++ core/base/Debuger.h | 51 +++++++++++++ core/base/Font.hpp | 31 ++++---- core/base/Game.cpp | 6 +- core/base/Text.h | 4 +- core/base/render.cpp | 63 ---------------- core/base/render.h | 3 - core/utils/string.cpp | 75 +++++++++++++++++++ core/utils/string.h | 29 ++++++++ project/vs2013/Easy2D.vcxproj | 4 ++ project/vs2013/Easy2D.vcxproj.filters | 12 ++++ project/vs2015/Easy2D.vcxproj | 4 ++ project/vs2015/Easy2D.vcxproj.filters | 12 ++++ project/vs2017/Easy2D.vcxproj | 4 ++ project/vs2017/Easy2D.vcxproj.filters | 12 ++++ 15 files changed, 325 insertions(+), 85 deletions(-) create mode 100644 core/base/Debuger.cpp create mode 100644 core/base/Debuger.h create mode 100644 core/utils/string.cpp create mode 100644 core/utils/string.h diff --git a/core/base/Debuger.cpp b/core/base/Debuger.cpp new file mode 100644 index 00000000..f5dc8088 --- /dev/null +++ b/core/base/Debuger.cpp @@ -0,0 +1,100 @@ +// 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. + +#include "Debuger.h" +#include "../utils/string.h" +#include +#include + +#pragma comment(lib, "psapi.lib") + +namespace easy2d +{ + + DebugerNode::DebugerNode() + { + debug_text_ = new Text(); + debug_text_->SetPosition(10, 10); + this->AddChild(debug_text_); + + Font font; + font.size = 16.f; + font.weight = FontWeight::Normal; + debug_text_->SetFont(font); + + TextStyle style; + style.wrap = false; + style.line_spacing = 18.f; + debug_text_->SetStyle(style); + } + + DebugerNode::~DebugerNode() + { + } + + void DebugerNode::AddDebugText(String const & text) + { + try + { + texts_.push_back(text); + } + catch (...) + { + } + } + + void DebugerNode::ClearDebugText() + { + texts_.clear(); + } + + void DebugerNode::OnUpdate(Duration const & dt) + { + try + { + frame_time_.push_back(time::Now()); + while (frame_time_.back() - frame_time_.front() >= time::Second) + { + frame_time_.erase(frame_time_.begin()); + } + } + catch (std::exception& e) + { + debug_text_->SetText(StringMultiByteToWideChar(e.what())); + return; + } + + std::wstringstream ss; + ss << "fps=" << frame_time_.size() << std::endl; + + PROCESS_MEMORY_COUNTERS_EX pmc; + GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc)); + if (pmc.PrivateUsage > 1024 * 1024) + ss << "memory=" << pmc.PrivateUsage / 1024 / 1024 << "Mb " << (pmc.PrivateUsage / 1024) % 1024 << "Kb"; + else + ss << "memory=" << pmc.PrivateUsage / 1024 << "Kb"; + + for (const auto& text : texts_) + ss << text << std::endl; + + debug_text_->SetText(ss.str()); + } + +} diff --git a/core/base/Debuger.h b/core/base/Debuger.h new file mode 100644 index 00000000..13cc967e --- /dev/null +++ b/core/base/Debuger.h @@ -0,0 +1,51 @@ +// 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 "Text.h" +#include "Singleton.hpp" +#include "time.h" + +namespace easy2d +{ + class DebugerNode + : public Node + { + E2D_DECLARE_SINGLETON(DebugerNode); + + public: + DebugerNode(); + + virtual ~DebugerNode(); + + void AddDebugText(String const& text); + + void ClearDebugText(); + + void OnUpdate(Duration const& dt) override; + + protected: + spText debug_text_; + std::vector frame_time_; + std::vector texts_; + }; + + E2D_DECLARE_SINGLETON_TYPE(DebugerNode, Debuger); +} diff --git a/core/base/Font.hpp b/core/base/Font.hpp index 49035798..a6a43cc7 100644 --- a/core/base/Font.hpp +++ b/core/base/Font.hpp @@ -23,6 +23,20 @@ namespace easy2d { + // 字体粗细值 + enum FontWeight : unsigned int + { + Thin = 100, + ExtraLight = 200, + Light = 300, + Normal = 400, + Medium = 500, + Bold = 700, + ExtraBold = 800, + Black = 900, + ExtraBlack = 950 + }; + // 字体 class Font { @@ -32,26 +46,11 @@ namespace easy2d unsigned int weight; // 粗细值 bool italic; // 是否斜体 - public: - // 字体粗细值 - enum Weight : unsigned int - { - Thin = 100, - ExtraLight = 200, - Light = 300, - Normal = 400, - Medium = 500, - Bold = 700, - ExtraBold = 800, - Black = 900, - ExtraBlack = 950 - }; - public: Font( const std::wstring& family = L"", float size = 22, - unsigned int weight = Font::Weight::Normal, + unsigned int weight = FontWeight::Normal, bool italic = false ) : family(family) diff --git a/core/base/Game.cpp b/core/base/Game.cpp index b6f20209..fdad9574 100644 --- a/core/base/Game.cpp +++ b/core/base/Game.cpp @@ -26,6 +26,7 @@ #include "modules.h" #include "Scene.h" #include "Transition.h" +#include "Debuger.h" #include "../math/Matrix.hpp" #include #include @@ -196,6 +197,9 @@ namespace easy2d if (next_scene_) next_scene_->Update(dt); + if (debug_enabled_) + Debuger::Instance()->Update(dt); + if (transition_) { transition_->Update(dt); @@ -249,7 +253,7 @@ namespace easy2d next_scene_->DrawBorder(); } - graphics->DrawDebugInfo(); + Debuger::Instance()->Visit(); } graphics->EndDraw(); diff --git a/core/base/Text.h b/core/base/Text.h index 95fdce17..0b7d1762 100644 --- a/core/base/Text.h +++ b/core/base/Text.h @@ -124,12 +124,12 @@ namespace easy2d float size ); - // 设置字体粗细值(默认值为 Text::Font::Weight::Normal) + // 设置字体粗细值(默认值为 FontWeight::Normal) void SetFontWeight( unsigned int weight ); - // 设置文字颜色(默认值为 Color::WHITE) + // 设置文字颜色(默认值为 Color::White) void SetColor( Color const& color ); diff --git a/core/base/render.cpp b/core/base/render.cpp index 442fdc4d..7b06751b 100644 --- a/core/base/render.cpp +++ b/core/base/render.cpp @@ -785,69 +785,6 @@ namespace easy2d clear_color_ = color; } - void GraphicsDevice::DrawDebugInfo() - { - static int render_times_ = 0; - static auto last_render_time_ = time::Now(); - - int64_t duration = (time::Now() - last_render_time_).Milliseconds(); - - if (!fps_text_format_) - { - ThrowIfFailed( - d2d.write_factory->CreateTextFormat( - L"", - nullptr, - DWRITE_FONT_WEIGHT::DWRITE_FONT_WEIGHT_NORMAL, - DWRITE_FONT_STYLE_NORMAL, - DWRITE_FONT_STRETCH_NORMAL, - 20, - L"", - &fps_text_format_ - ) - ); - - fps_text_format_->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); - } - - ++render_times_; - if (duration >= 100LL) - { - wchar_t fps_text[12] = {}; - int len = swprintf_s(fps_text, L"FPS: %.1f", 1000.f / static_cast(duration) * render_times_); - - last_render_time_ = time::Now(); - render_times_ = 0; - - fps_text_layout_ = nullptr; - ThrowIfFailed( - d2d.write_factory->CreateTextLayout( - fps_text, - len, - fps_text_format_.Get(), - 0, - 0, - &fps_text_layout_ - ) - ); - } - - if (fps_text_layout_) - { - d2d.render_target->SetTransform(D2D1::Matrix3x2F::Identity()); - d2d.solid_brush->SetOpacity(1.0f); - d2d.text_renderer->SetTextStyle( - D2D1::ColorF(D2D1::ColorF::White), - TRUE, - D2D1::ColorF(D2D1::ColorF::Black, 0.4f), - 1.5f, - d2d.round_stroke_style.Get() - ); - - fps_text_layout_->Draw(nullptr, d2d.text_renderer.Get(), 10, 0); - } - } - void GraphicsDevice::CreateDeviceResources(HWND hwnd) { if (!d2d.render_target) diff --git a/core/base/render.h b/core/base/render.h index 66af2b7f..e57ff1c2 100644 --- a/core/base/render.h +++ b/core/base/render.h @@ -66,9 +66,6 @@ namespace easy2d const Color& color ); - // 渲染调试信息 - void DrawDebugInfo(); - void CreateDeviceResources(HWND hwnd); HRESULT CreateRectGeometry( diff --git a/core/utils/string.cpp b/core/utils/string.cpp new file mode 100644 index 00000000..1c62b025 --- /dev/null +++ b/core/utils/string.cpp @@ -0,0 +1,75 @@ +// 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. + +#include "string.h" +#include "../base/macros.h" +#include "../base/logs.h" + +namespace easy2d +{ + std::wstring StringMultiByteToWideChar(const std::string& str) + { + std::wstring ret; + if (!str.empty()) + { + int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0); + if (len) + { + WCHAR* wstr_tmp = new WCHAR[len + 1]; + wstr_tmp[0] = 0; + + len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wstr_tmp, len + 1); + + ret = wstr_tmp; + delete[] wstr_tmp; + } + else + { + logs::Errorln(HRESULT_FROM_WIN32(GetLastError()), "Wrong convert to WideChar code"); + } + } + return ret; + } + + std::string StringWideCharToMultiByte(const std::wstring& wstr) + { + std::string ret; + if (!wstr.empty()) + { + int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, nullptr, 0, nullptr, FALSE); + if (len) + { + char* str_tmp = new char[len + 1]; + str_tmp[0] = 0; + + len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, str_tmp, len + 1, nullptr, FALSE); + + ret = str_tmp; + delete[] str_tmp; + } + else + { + logs::Errorln(HRESULT_FROM_WIN32(GetLastError()), ("Wrong convert to MultiByte code")); + } + } + + return ret; + } +} diff --git a/core/utils/string.h b/core/utils/string.h new file mode 100644 index 00000000..68a0b96d --- /dev/null +++ b/core/utils/string.h @@ -0,0 +1,29 @@ +// 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 + +namespace easy2d +{ + std::wstring StringMultiByteToWideChar(const std::string& str); + + std::string StringWideCharToMultiByte(const std::wstring& wstr); +} diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index 0efedc1a..533f1680 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -31,6 +31,7 @@ + @@ -74,6 +75,7 @@ + @@ -85,6 +87,7 @@ + @@ -112,6 +115,7 @@ + diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 178bd26f..7b6f223d 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -167,6 +167,12 @@ base + + base + + + utils + @@ -294,5 +300,11 @@ base + + base + + + utils + \ No newline at end of file diff --git a/project/vs2015/Easy2D.vcxproj b/project/vs2015/Easy2D.vcxproj index 5b781318..fef9c7fc 100644 --- a/project/vs2015/Easy2D.vcxproj +++ b/project/vs2015/Easy2D.vcxproj @@ -31,6 +31,7 @@ + @@ -74,6 +75,7 @@ + @@ -85,6 +87,7 @@ + @@ -112,6 +115,7 @@ + diff --git a/project/vs2015/Easy2D.vcxproj.filters b/project/vs2015/Easy2D.vcxproj.filters index 178bd26f..7b6f223d 100644 --- a/project/vs2015/Easy2D.vcxproj.filters +++ b/project/vs2015/Easy2D.vcxproj.filters @@ -167,6 +167,12 @@ base + + base + + + utils + @@ -294,5 +300,11 @@ base + + base + + + utils + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 8ccca401..4575fa88 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -31,6 +31,7 @@ + @@ -74,6 +75,7 @@ + @@ -85,6 +87,7 @@ + @@ -112,6 +115,7 @@ + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 178bd26f..7b6f223d 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -167,6 +167,12 @@ base + + base + + + utils + @@ -294,5 +300,11 @@ base + + base + + + utils + \ No newline at end of file