add: debug node

This commit is contained in:
Haibo 2018-11-19 19:32:12 +08:00 committed by Nomango
parent 91508fd7cf
commit 2b411989cf
15 changed files with 325 additions and 85 deletions

100
core/base/Debuger.cpp Normal file
View File

@ -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 <sstream>
#include <psapi.h>
#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());
}
}

51
core/base/Debuger.h Normal file
View File

@ -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<TimePoint> frame_time_;
std::vector<String> texts_;
};
E2D_DECLARE_SINGLETON_TYPE(DebugerNode, Debuger);
}

View File

@ -23,18 +23,8 @@
namespace easy2d
{
// 字体
class Font
{
public:
std::wstring family; // 字体族
float size; // 字号
unsigned int weight; // 粗细值
bool italic; // 是否斜体
public:
// ×ÖÌå´Öϸֵ
enum Weight : unsigned int
enum FontWeight : unsigned int
{
Thin = 100,
ExtraLight = 200,
@ -47,11 +37,20 @@ namespace easy2d
ExtraBlack = 950
};
// 字体
class Font
{
public:
std::wstring family; // 字体族
float size; // 字号
unsigned int weight; // 粗细值
bool italic; // 是否斜体
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)

View File

@ -26,6 +26,7 @@
#include "modules.h"
#include "Scene.h"
#include "Transition.h"
#include "Debuger.h"
#include "../math/Matrix.hpp"
#include <thread>
#include <imm.h>
@ -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();

View File

@ -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
);

View File

@ -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<float>(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)

View File

@ -66,9 +66,6 @@ namespace easy2d
const Color& color
);
// 渲染调试信息
void DrawDebugInfo();
void CreateDeviceResources(HWND hwnd);
HRESULT CreateRectGeometry(

75
core/utils/string.cpp Normal file
View File

@ -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;
}
}

29
core/utils/string.h Normal file
View File

@ -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 <string>
namespace easy2d
{
std::wstring StringMultiByteToWideChar(const std::string& str);
std::string StringWideCharToMultiByte(const std::wstring& wstr);
}

View File

@ -31,6 +31,7 @@
<ClInclude Include="..\..\core\base\Canvas.h" />
<ClInclude Include="..\..\core\base\Color.h" />
<ClInclude Include="..\..\core\base\d2dres.hpp" />
<ClInclude Include="..\..\core\base\Debuger.h" />
<ClInclude Include="..\..\core\base\Font.hpp" />
<ClInclude Include="..\..\core\base\Game.h" />
<ClInclude Include="..\..\core\base\Image.h" />
@ -74,6 +75,7 @@
<ClInclude Include="..\..\core\utils\File.h" />
<ClInclude Include="..\..\core\utils\Path.h" />
<ClInclude Include="..\..\core\utils\Player.h" />
<ClInclude Include="..\..\core\utils\string.h" />
<ClInclude Include="..\..\core\utils\Transcoder.h" />
</ItemGroup>
<ItemGroup>
@ -85,6 +87,7 @@
<ClCompile Include="..\..\core\base\CallFunc.cpp" />
<ClCompile Include="..\..\core\base\Canvas.cpp" />
<ClCompile Include="..\..\core\base\Color.cpp" />
<ClCompile Include="..\..\core\base\Debuger.cpp" />
<ClCompile Include="..\..\core\base\Game.cpp" />
<ClCompile Include="..\..\core\base\Image.cpp" />
<ClCompile Include="..\..\core\base\Input.cpp" />
@ -112,6 +115,7 @@
<ClCompile Include="..\..\core\utils\File.cpp" />
<ClCompile Include="..\..\core\utils\Path.cpp" />
<ClCompile Include="..\..\core\utils\Player.cpp" />
<ClCompile Include="..\..\core\utils\string.cpp" />
<ClCompile Include="..\..\core\utils\Transcoder.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">

View File

@ -167,6 +167,12 @@
<ClInclude Include="..\..\core\base\noncopyable.hpp">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\..\core\base\Debuger.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\..\core\utils\string.h">
<Filter>utils</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="base">
@ -294,5 +300,11 @@
<ClCompile Include="..\..\core\base\logs.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\base\Debuger.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\utils\string.cpp">
<Filter>utils</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -31,6 +31,7 @@
<ClInclude Include="..\..\core\base\Canvas.h" />
<ClInclude Include="..\..\core\base\Color.h" />
<ClInclude Include="..\..\core\base\d2dres.hpp" />
<ClInclude Include="..\..\core\base\Debuger.h" />
<ClInclude Include="..\..\core\base\Font.hpp" />
<ClInclude Include="..\..\core\base\Game.h" />
<ClInclude Include="..\..\core\base\Image.h" />
@ -74,6 +75,7 @@
<ClInclude Include="..\..\core\utils\File.h" />
<ClInclude Include="..\..\core\utils\Path.h" />
<ClInclude Include="..\..\core\utils\Player.h" />
<ClInclude Include="..\..\core\utils\string.h" />
<ClInclude Include="..\..\core\utils\Transcoder.h" />
</ItemGroup>
<ItemGroup>
@ -85,6 +87,7 @@
<ClCompile Include="..\..\core\base\CallFunc.cpp" />
<ClCompile Include="..\..\core\base\Canvas.cpp" />
<ClCompile Include="..\..\core\base\Color.cpp" />
<ClCompile Include="..\..\core\base\Debuger.cpp" />
<ClCompile Include="..\..\core\base\Game.cpp" />
<ClCompile Include="..\..\core\base\Image.cpp" />
<ClCompile Include="..\..\core\base\Input.cpp" />
@ -112,6 +115,7 @@
<ClCompile Include="..\..\core\utils\File.cpp" />
<ClCompile Include="..\..\core\utils\Path.cpp" />
<ClCompile Include="..\..\core\utils\Player.cpp" />
<ClCompile Include="..\..\core\utils\string.cpp" />
<ClCompile Include="..\..\core\utils\Transcoder.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">

View File

@ -167,6 +167,12 @@
<ClInclude Include="..\..\core\base\noncopyable.hpp">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\..\core\base\Debuger.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\..\core\utils\string.h">
<Filter>utils</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="base">
@ -294,5 +300,11 @@
<ClCompile Include="..\..\core\base\logs.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\base\Debuger.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\utils\string.cpp">
<Filter>utils</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -31,6 +31,7 @@
<ClInclude Include="..\..\core\base\Canvas.h" />
<ClInclude Include="..\..\core\base\Color.h" />
<ClInclude Include="..\..\core\base\d2dres.hpp" />
<ClInclude Include="..\..\core\base\Debuger.h" />
<ClInclude Include="..\..\core\base\Font.hpp" />
<ClInclude Include="..\..\core\base\Game.h" />
<ClInclude Include="..\..\core\base\Image.h" />
@ -74,6 +75,7 @@
<ClInclude Include="..\..\core\utils\File.h" />
<ClInclude Include="..\..\core\utils\Path.h" />
<ClInclude Include="..\..\core\utils\Player.h" />
<ClInclude Include="..\..\core\utils\string.h" />
<ClInclude Include="..\..\core\utils\Transcoder.h" />
</ItemGroup>
<ItemGroup>
@ -85,6 +87,7 @@
<ClCompile Include="..\..\core\base\CallFunc.cpp" />
<ClCompile Include="..\..\core\base\Canvas.cpp" />
<ClCompile Include="..\..\core\base\Color.cpp" />
<ClCompile Include="..\..\core\base\Debuger.cpp" />
<ClCompile Include="..\..\core\base\Game.cpp" />
<ClCompile Include="..\..\core\base\Image.cpp" />
<ClCompile Include="..\..\core\base\Input.cpp" />
@ -112,6 +115,7 @@
<ClCompile Include="..\..\core\utils\File.cpp" />
<ClCompile Include="..\..\core\utils\Path.cpp" />
<ClCompile Include="..\..\core\utils\Player.cpp" />
<ClCompile Include="..\..\core\utils\string.cpp" />
<ClCompile Include="..\..\core\utils\Transcoder.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">

View File

@ -167,6 +167,12 @@
<ClInclude Include="..\..\core\base\noncopyable.hpp">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\..\core\base\Debuger.h">
<Filter>base</Filter>
</ClInclude>
<ClInclude Include="..\..\core\utils\string.h">
<Filter>utils</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="base">
@ -294,5 +300,11 @@
<ClCompile Include="..\..\core\base\logs.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\base\Debuger.cpp">
<Filter>base</Filter>
</ClCompile>
<ClCompile Include="..\..\core\utils\string.cpp">
<Filter>utils</Filter>
</ClCompile>
</ItemGroup>
</Project>